src/HOL/Limits.thy
 author hoelzl Mon Jan 14 17:16:59 2013 +0100 (2013-01-14) changeset 50880 b22ecedde1c7 parent 50419 3177d0374701 child 50999 3de230ed0547 permissions -rw-r--r--
move eventually_Ball_finite to Limits
1 (*  Title       : Limits.thy
2     Author      : Brian Huffman
3 *)
5 header {* Filters and Limits *}
7 theory Limits
8 imports RealVector
9 begin
11 subsection {* Filters *}
13 text {*
14   This definition also allows non-proper filters.
15 *}
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)"
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
28 lemma is_filter_Rep_filter: "is_filter (Rep_filter F)"
29   using Rep_filter [of F] by simp
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)
36 subsection {* Eventually *}
38 definition eventually :: "('a \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> bool"
39   where "eventually P F \<longleftrightarrow> Rep_filter F P"
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)
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 ..
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])
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
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])
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])
71 lemma eventually_Ball_finite:
72   assumes "finite A" and "\<forall>y\<in>A. eventually (\<lambda>x. P x y) net"
73   shows "eventually (\<lambda>x. \<forall>y\<in>A. P x y) net"
74 using assms by (induct set: finite, simp, simp add: eventually_conj)
76 lemma eventually_all_finite:
77   fixes P :: "'a \<Rightarrow> 'b::finite \<Rightarrow> bool"
78   assumes "\<And>y. eventually (\<lambda>x. P x y) net"
79   shows "eventually (\<lambda>x. \<forall>y. P x y) net"
80 using eventually_Ball_finite [of UNIV P] assms by simp
82 lemma eventually_mp:
83   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
84   assumes "eventually (\<lambda>x. P x) F"
85   shows "eventually (\<lambda>x. Q x) F"
86 proof (rule eventually_mono)
87   show "\<forall>x. (P x \<longrightarrow> Q x) \<and> P x \<longrightarrow> Q x" by simp
88   show "eventually (\<lambda>x. (P x \<longrightarrow> Q x) \<and> P x) F"
89     using assms by (rule eventually_conj)
90 qed
92 lemma eventually_rev_mp:
93   assumes "eventually (\<lambda>x. P x) F"
94   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
95   shows "eventually (\<lambda>x. Q x) F"
96 using assms(2) assms(1) by (rule eventually_mp)
98 lemma eventually_conj_iff:
99   "eventually (\<lambda>x. P x \<and> Q x) F \<longleftrightarrow> eventually P F \<and> eventually Q F"
100   by (auto intro: eventually_conj elim: eventually_rev_mp)
102 lemma eventually_elim1:
103   assumes "eventually (\<lambda>i. P i) F"
104   assumes "\<And>i. P i \<Longrightarrow> Q i"
105   shows "eventually (\<lambda>i. Q i) F"
106   using assms by (auto elim!: eventually_rev_mp)
108 lemma eventually_elim2:
109   assumes "eventually (\<lambda>i. P i) F"
110   assumes "eventually (\<lambda>i. Q i) F"
111   assumes "\<And>i. P i \<Longrightarrow> Q i \<Longrightarrow> R i"
112   shows "eventually (\<lambda>i. R i) F"
113   using assms by (auto elim!: eventually_rev_mp)
115 lemma eventually_subst:
116   assumes "eventually (\<lambda>n. P n = Q n) F"
117   shows "eventually P F = eventually Q F" (is "?L = ?R")
118 proof -
119   from assms have "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
120       and "eventually (\<lambda>x. Q x \<longrightarrow> P x) F"
121     by (auto elim: eventually_elim1)
122   then show ?thesis by (auto elim: eventually_elim2)
123 qed
125 ML {*
126   fun eventually_elim_tac ctxt thms thm =
127     let
128       val thy = Proof_Context.theory_of ctxt
129       val mp_thms = thms RL [@{thm eventually_rev_mp}]
130       val raw_elim_thm =
131         (@{thm allI} RS @{thm always_eventually})
132         |> fold (fn thm1 => fn thm2 => thm2 RS thm1) mp_thms
133         |> fold (fn _ => fn thm => @{thm impI} RS thm) thms
134       val cases_prop = prop_of (raw_elim_thm RS thm)
135       val cases = (Rule_Cases.make_common (thy, cases_prop) [(("elim", []), [])])
136     in
137       CASES cases (rtac raw_elim_thm 1) thm
138     end
139 *}
141 method_setup eventually_elim = {*
142   Scan.succeed (fn ctxt => METHOD_CASES (eventually_elim_tac ctxt))
143 *} "elimination of eventually quantifiers"
146 subsection {* Finer-than relation *}
148 text {* @{term "F \<le> F'"} means that filter @{term F} is finer than
149 filter @{term F'}. *}
151 instantiation filter :: (type) complete_lattice
152 begin
154 definition le_filter_def:
155   "F \<le> F' \<longleftrightarrow> (\<forall>P. eventually P F' \<longrightarrow> eventually P F)"
157 definition
158   "(F :: 'a filter) < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
160 definition
161   "top = Abs_filter (\<lambda>P. \<forall>x. P x)"
163 definition
164   "bot = Abs_filter (\<lambda>P. True)"
166 definition
167   "sup F F' = Abs_filter (\<lambda>P. eventually P F \<and> eventually P F')"
169 definition
170   "inf F F' = Abs_filter
171       (\<lambda>P. \<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
173 definition
174   "Sup S = Abs_filter (\<lambda>P. \<forall>F\<in>S. eventually P F)"
176 definition
177   "Inf S = Sup {F::'a filter. \<forall>F'\<in>S. F \<le> F'}"
179 lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
180   unfolding top_filter_def
181   by (rule eventually_Abs_filter, rule is_filter.intro, auto)
183 lemma eventually_bot [simp]: "eventually P bot"
184   unfolding bot_filter_def
185   by (subst eventually_Abs_filter, rule is_filter.intro, auto)
187 lemma eventually_sup:
188   "eventually P (sup F F') \<longleftrightarrow> eventually P F \<and> eventually P F'"
189   unfolding sup_filter_def
190   by (rule eventually_Abs_filter, rule is_filter.intro)
191      (auto elim!: eventually_rev_mp)
193 lemma eventually_inf:
194   "eventually P (inf F F') \<longleftrightarrow>
195    (\<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
196   unfolding inf_filter_def
197   apply (rule eventually_Abs_filter, rule is_filter.intro)
198   apply (fast intro: eventually_True)
199   apply clarify
200   apply (intro exI conjI)
201   apply (erule (1) eventually_conj)
202   apply (erule (1) eventually_conj)
203   apply simp
204   apply auto
205   done
207 lemma eventually_Sup:
208   "eventually P (Sup S) \<longleftrightarrow> (\<forall>F\<in>S. eventually P F)"
209   unfolding Sup_filter_def
210   apply (rule eventually_Abs_filter, rule is_filter.intro)
211   apply (auto intro: eventually_conj elim!: eventually_rev_mp)
212   done
214 instance proof
215   fix F F' F'' :: "'a filter" and S :: "'a filter set"
216   { show "F < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
217     by (rule less_filter_def) }
218   { show "F \<le> F"
219     unfolding le_filter_def by simp }
220   { assume "F \<le> F'" and "F' \<le> F''" thus "F \<le> F''"
221     unfolding le_filter_def by simp }
222   { assume "F \<le> F'" and "F' \<le> F" thus "F = F'"
223     unfolding le_filter_def filter_eq_iff by fast }
224   { show "F \<le> top"
225     unfolding le_filter_def eventually_top by (simp add: always_eventually) }
226   { show "bot \<le> F"
227     unfolding le_filter_def by simp }
228   { show "F \<le> sup F F'" and "F' \<le> sup F F'"
229     unfolding le_filter_def eventually_sup by simp_all }
230   { assume "F \<le> F''" and "F' \<le> F''" thus "sup F F' \<le> F''"
231     unfolding le_filter_def eventually_sup by simp }
232   { show "inf F F' \<le> F" and "inf F F' \<le> F'"
233     unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
234   { assume "F \<le> F'" and "F \<le> F''" thus "F \<le> inf F' F''"
235     unfolding le_filter_def eventually_inf
236     by (auto elim!: eventually_mono intro: eventually_conj) }
237   { assume "F \<in> S" thus "F \<le> Sup S"
238     unfolding le_filter_def eventually_Sup by simp }
239   { assume "\<And>F. F \<in> S \<Longrightarrow> F \<le> F'" thus "Sup S \<le> F'"
240     unfolding le_filter_def eventually_Sup by simp }
241   { assume "F'' \<in> S" thus "Inf S \<le> F''"
242     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
243   { assume "\<And>F'. F' \<in> S \<Longrightarrow> F \<le> F'" thus "F \<le> Inf S"
244     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
245 qed
247 end
249 lemma filter_leD:
250   "F \<le> F' \<Longrightarrow> eventually P F' \<Longrightarrow> eventually P F"
251   unfolding le_filter_def by simp
253 lemma filter_leI:
254   "(\<And>P. eventually P F' \<Longrightarrow> eventually P F) \<Longrightarrow> F \<le> F'"
255   unfolding le_filter_def by simp
257 lemma eventually_False:
258   "eventually (\<lambda>x. False) F \<longleftrightarrow> F = bot"
259   unfolding filter_eq_iff by (auto elim: eventually_rev_mp)
261 abbreviation (input) trivial_limit :: "'a filter \<Rightarrow> bool"
262   where "trivial_limit F \<equiv> F = bot"
264 lemma trivial_limit_def: "trivial_limit F \<longleftrightarrow> eventually (\<lambda>x. False) F"
265   by (rule eventually_False [symmetric])
268 subsection {* Map function for filters *}
270 definition filtermap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow> 'b filter"
271   where "filtermap f F = Abs_filter (\<lambda>P. eventually (\<lambda>x. P (f x)) F)"
273 lemma eventually_filtermap:
274   "eventually P (filtermap f F) = eventually (\<lambda>x. P (f x)) F"
275   unfolding filtermap_def
276   apply (rule eventually_Abs_filter)
277   apply (rule is_filter.intro)
278   apply (auto elim!: eventually_rev_mp)
279   done
281 lemma filtermap_ident: "filtermap (\<lambda>x. x) F = F"
282   by (simp add: filter_eq_iff eventually_filtermap)
284 lemma filtermap_filtermap:
285   "filtermap f (filtermap g F) = filtermap (\<lambda>x. f (g x)) F"
286   by (simp add: filter_eq_iff eventually_filtermap)
288 lemma filtermap_mono: "F \<le> F' \<Longrightarrow> filtermap f F \<le> filtermap f F'"
289   unfolding le_filter_def eventually_filtermap by simp
291 lemma filtermap_bot [simp]: "filtermap f bot = bot"
292   by (simp add: filter_eq_iff eventually_filtermap)
294 lemma filtermap_sup: "filtermap f (sup F1 F2) = sup (filtermap f F1) (filtermap f F2)"
295   by (auto simp: filter_eq_iff eventually_filtermap eventually_sup)
297 subsection {* Order filters *}
299 definition at_top :: "('a::order) filter"
300   where "at_top = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
302 lemma eventually_at_top_linorder: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::linorder. \<forall>n\<ge>N. P n)"
303   unfolding at_top_def
304 proof (rule eventually_Abs_filter, rule is_filter.intro)
305   fix P Q :: "'a \<Rightarrow> bool"
306   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
307   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
308   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
309   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
310 qed auto
312 lemma eventually_ge_at_top:
313   "eventually (\<lambda>x. (c::_::linorder) \<le> x) at_top"
314   unfolding eventually_at_top_linorder by auto
316 lemma eventually_at_top_dense: "eventually P at_top \<longleftrightarrow> (\<exists>N::'a::dense_linorder. \<forall>n>N. P n)"
317   unfolding eventually_at_top_linorder
318 proof safe
319   fix N assume "\<forall>n\<ge>N. P n" then show "\<exists>N. \<forall>n>N. P n" by (auto intro!: exI[of _ N])
320 next
321   fix N assume "\<forall>n>N. P n"
322   moreover from gt_ex[of N] guess y ..
323   ultimately show "\<exists>N. \<forall>n\<ge>N. P n" by (auto intro!: exI[of _ y])
324 qed
326 lemma eventually_gt_at_top:
327   "eventually (\<lambda>x. (c::_::dense_linorder) < x) at_top"
328   unfolding eventually_at_top_dense by auto
330 definition at_bot :: "('a::order) filter"
331   where "at_bot = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<le>k. P n)"
333 lemma eventually_at_bot_linorder:
334   fixes P :: "'a::linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n\<le>N. P n)"
335   unfolding at_bot_def
336 proof (rule eventually_Abs_filter, rule is_filter.intro)
337   fix P Q :: "'a \<Rightarrow> bool"
338   assume "\<exists>i. \<forall>n\<le>i. P n" and "\<exists>j. \<forall>n\<le>j. Q n"
339   then obtain i j where "\<forall>n\<le>i. P n" and "\<forall>n\<le>j. Q n" by auto
340   then have "\<forall>n\<le>min i j. P n \<and> Q n" by simp
341   then show "\<exists>k. \<forall>n\<le>k. P n \<and> Q n" ..
342 qed auto
344 lemma eventually_le_at_bot:
345   "eventually (\<lambda>x. x \<le> (c::_::linorder)) at_bot"
346   unfolding eventually_at_bot_linorder by auto
348 lemma eventually_at_bot_dense:
349   fixes P :: "'a::dense_linorder \<Rightarrow> bool" shows "eventually P at_bot \<longleftrightarrow> (\<exists>N. \<forall>n<N. P n)"
350   unfolding eventually_at_bot_linorder
351 proof safe
352   fix N assume "\<forall>n\<le>N. P n" then show "\<exists>N. \<forall>n<N. P n" by (auto intro!: exI[of _ N])
353 next
354   fix N assume "\<forall>n<N. P n"
355   moreover from lt_ex[of N] guess y ..
356   ultimately show "\<exists>N. \<forall>n\<le>N. P n" by (auto intro!: exI[of _ y])
357 qed
359 lemma eventually_gt_at_bot:
360   "eventually (\<lambda>x. x < (c::_::dense_linorder)) at_bot"
361   unfolding eventually_at_bot_dense by auto
363 subsection {* Sequentially *}
365 abbreviation sequentially :: "nat filter"
366   where "sequentially == at_top"
368 lemma sequentially_def: "sequentially = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
369   unfolding at_top_def by simp
371 lemma eventually_sequentially:
372   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
373   by (rule eventually_at_top_linorder)
375 lemma sequentially_bot [simp, intro]: "sequentially \<noteq> bot"
376   unfolding filter_eq_iff eventually_sequentially by auto
378 lemmas trivial_limit_sequentially = sequentially_bot
380 lemma eventually_False_sequentially [simp]:
381   "\<not> eventually (\<lambda>n. False) sequentially"
382   by (simp add: eventually_False)
384 lemma le_sequentially:
385   "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
386   unfolding le_filter_def eventually_sequentially
387   by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
389 lemma eventually_sequentiallyI:
390   assumes "\<And>x. c \<le> x \<Longrightarrow> P x"
391   shows "eventually P sequentially"
392 using assms by (auto simp: eventually_sequentially)
395 subsection {* Standard filters *}
397 definition within :: "'a filter \<Rightarrow> 'a set \<Rightarrow> 'a filter" (infixr "within" 70)
398   where "F within S = Abs_filter (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F)"
400 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
401   where "nhds a = Abs_filter (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
403 definition (in topological_space) at :: "'a \<Rightarrow> 'a filter"
404   where "at a = nhds a within - {a}"
406 abbreviation at_right :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
407   "at_right x \<equiv> at x within {x <..}"
409 abbreviation at_left :: "'a\<Colon>{topological_space, order} \<Rightarrow> 'a filter" where
410   "at_left x \<equiv> at x within {..< x}"
412 definition at_infinity :: "'a::real_normed_vector filter" where
413   "at_infinity = Abs_filter (\<lambda>P. \<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x)"
415 lemma eventually_within:
416   "eventually P (F within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F"
417   unfolding within_def
418   by (rule eventually_Abs_filter, rule is_filter.intro)
419      (auto elim!: eventually_rev_mp)
421 lemma within_UNIV [simp]: "F within UNIV = F"
422   unfolding filter_eq_iff eventually_within by simp
424 lemma within_empty [simp]: "F within {} = bot"
425   unfolding filter_eq_iff eventually_within by simp
427 lemma within_within_eq: "(F within S) within T = F within (S \<inter> T)"
428   by (auto simp: filter_eq_iff eventually_within elim: eventually_elim1)
430 lemma at_within_eq: "at x within T = nhds x within (T - {x})"
431   unfolding at_def within_within_eq by (simp add: ac_simps Diff_eq)
433 lemma within_le: "F within S \<le> F"
434   unfolding le_filter_def eventually_within by (auto elim: eventually_elim1)
436 lemma le_withinI: "F \<le> F' \<Longrightarrow> eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S"
437   unfolding le_filter_def eventually_within by (auto elim: eventually_elim2)
439 lemma le_within_iff: "eventually (\<lambda>x. x \<in> S) F \<Longrightarrow> F \<le> F' within S \<longleftrightarrow> F \<le> F'"
440   by (blast intro: within_le le_withinI order_trans)
442 lemma eventually_nhds:
443   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
444 unfolding nhds_def
445 proof (rule eventually_Abs_filter, rule is_filter.intro)
446   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
447   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" ..
448 next
449   fix P Q
450   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
451      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
452   then obtain S T where
453     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
454     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
455   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
456     by (simp add: open_Int)
457   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" ..
458 qed auto
460 lemma eventually_nhds_metric:
461   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
462 unfolding eventually_nhds open_dist
463 apply safe
464 apply fast
465 apply (rule_tac x="{x. dist x a < d}" in exI, simp)
466 apply clarsimp
467 apply (rule_tac x="d - dist x a" in exI, clarsimp)
468 apply (simp only: less_diff_eq)
469 apply (erule le_less_trans [OF dist_triangle])
470 done
472 lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
473   unfolding trivial_limit_def eventually_nhds by simp
475 lemma eventually_at_topological:
476   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
477 unfolding at_def eventually_within eventually_nhds by simp
479 lemma eventually_at:
480   fixes a :: "'a::metric_space"
481   shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
482 unfolding at_def eventually_within eventually_nhds_metric by auto
484 lemma eventually_within_less: (* COPY FROM Topo/eventually_within *)
485   "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)"
486   unfolding eventually_within eventually_at dist_nz by auto
488 lemma eventually_within_le: (* COPY FROM Topo/eventually_within_le *)
489   "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)"
490   unfolding eventually_within_less by auto (metis dense order_le_less_trans)
492 lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
493   unfolding trivial_limit_def eventually_at_topological
494   by (safe, case_tac "S = {a}", simp, fast, fast)
496 lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
497   by (simp add: at_eq_bot_iff not_open_singleton)
499 lemma trivial_limit_at_left_real [simp]: (* maybe generalize type *)
500   "\<not> trivial_limit (at_left (x::real))"
501   unfolding trivial_limit_def eventually_within_le
502   apply clarsimp
503   apply (rule_tac x="x - d/2" in bexI)
504   apply (auto simp: dist_real_def)
505   done
507 lemma trivial_limit_at_right_real [simp]: (* maybe generalize type *)
508   "\<not> trivial_limit (at_right (x::real))"
509   unfolding trivial_limit_def eventually_within_le
510   apply clarsimp
511   apply (rule_tac x="x + d/2" in bexI)
512   apply (auto simp: dist_real_def)
513   done
515 lemma eventually_at_infinity:
516   "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> P x)"
517 unfolding at_infinity_def
518 proof (rule eventually_Abs_filter, rule is_filter.intro)
519   fix P Q :: "'a \<Rightarrow> bool"
520   assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<exists>s. \<forall>x. s \<le> norm x \<longrightarrow> Q x"
521   then obtain r s where
522     "\<forall>x. r \<le> norm x \<longrightarrow> P x" and "\<forall>x. s \<le> norm x \<longrightarrow> Q x" by auto
523   then have "\<forall>x. max r s \<le> norm x \<longrightarrow> P x \<and> Q x" by simp
524   then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x \<and> Q x" ..
525 qed auto
527 lemma at_infinity_eq_at_top_bot:
528   "(at_infinity \<Colon> real filter) = sup at_top at_bot"
529   unfolding sup_filter_def at_infinity_def eventually_at_top_linorder eventually_at_bot_linorder
530 proof (intro arg_cong[where f=Abs_filter] ext iffI)
531   fix P :: "real \<Rightarrow> bool" assume "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x"
532   then guess r ..
533   then have "(\<forall>x\<ge>r. P x) \<and> (\<forall>x\<le>-r. P x)" by auto
534   then show "(\<exists>r. \<forall>x\<ge>r. P x) \<and> (\<exists>r. \<forall>x\<le>r. P x)" by auto
535 next
536   fix P :: "real \<Rightarrow> bool" assume "(\<exists>r. \<forall>x\<ge>r. P x) \<and> (\<exists>r. \<forall>x\<le>r. P x)"
537   then obtain p q where "\<forall>x\<ge>p. P x" "\<forall>x\<le>q. P x" by auto
538   then show "\<exists>r. \<forall>x. r \<le> norm x \<longrightarrow> P x"
539     by (intro exI[of _ "max p (-q)"])
540        (auto simp: abs_real_def)
541 qed
543 lemma at_top_le_at_infinity:
544   "at_top \<le> (at_infinity :: real filter)"
545   unfolding at_infinity_eq_at_top_bot by simp
547 lemma at_bot_le_at_infinity:
548   "at_bot \<le> (at_infinity :: real filter)"
549   unfolding at_infinity_eq_at_top_bot by simp
551 subsection {* Boundedness *}
553 definition Bfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
554   where "Bfun f F = (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) F)"
556 lemma BfunI:
557   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) F" shows "Bfun f F"
558 unfolding Bfun_def
559 proof (intro exI conjI allI)
560   show "0 < max K 1" by simp
561 next
562   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) F"
563     using K by (rule eventually_elim1, simp)
564 qed
566 lemma BfunE:
567   assumes "Bfun f F"
568   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) F"
569 using assms unfolding Bfun_def by fast
572 subsection {* Convergence to Zero *}
574 definition Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
575   where "Zfun f F = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) F)"
577 lemma ZfunI:
578   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F) \<Longrightarrow> Zfun f F"
579   unfolding Zfun_def by simp
581 lemma ZfunD:
582   "\<lbrakk>Zfun f F; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F"
583   unfolding Zfun_def by simp
585 lemma Zfun_ssubst:
586   "eventually (\<lambda>x. f x = g x) F \<Longrightarrow> Zfun g F \<Longrightarrow> Zfun f F"
587   unfolding Zfun_def by (auto elim!: eventually_rev_mp)
589 lemma Zfun_zero: "Zfun (\<lambda>x. 0) F"
590   unfolding Zfun_def by simp
592 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) F = Zfun (\<lambda>x. f x) F"
593   unfolding Zfun_def by simp
595 lemma Zfun_imp_Zfun:
596   assumes f: "Zfun f F"
597   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) F"
598   shows "Zfun (\<lambda>x. g x) F"
599 proof (cases)
600   assume K: "0 < K"
601   show ?thesis
602   proof (rule ZfunI)
603     fix r::real assume "0 < r"
604     hence "0 < r / K"
605       using K by (rule divide_pos_pos)
606     then have "eventually (\<lambda>x. norm (f x) < r / K) F"
607       using ZfunD [OF f] by fast
608     with g show "eventually (\<lambda>x. norm (g x) < r) F"
609     proof eventually_elim
610       case (elim x)
611       hence "norm (f x) * K < r"
612         by (simp add: pos_less_divide_eq K)
613       thus ?case
614         by (simp add: order_le_less_trans [OF elim(1)])
615     qed
616   qed
617 next
618   assume "\<not> 0 < K"
619   hence K: "K \<le> 0" by (simp only: not_less)
620   show ?thesis
621   proof (rule ZfunI)
622     fix r :: real
623     assume "0 < r"
624     from g show "eventually (\<lambda>x. norm (g x) < r) F"
625     proof eventually_elim
626       case (elim x)
627       also have "norm (f x) * K \<le> norm (f x) * 0"
628         using K norm_ge_zero by (rule mult_left_mono)
629       finally show ?case
630         using `0 < r` by simp
631     qed
632   qed
633 qed
635 lemma Zfun_le: "\<lbrakk>Zfun g F; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f F"
636   by (erule_tac K="1" in Zfun_imp_Zfun, simp)
639   assumes f: "Zfun f F" and g: "Zfun g F"
640   shows "Zfun (\<lambda>x. f x + g x) F"
641 proof (rule ZfunI)
642   fix r::real assume "0 < r"
643   hence r: "0 < r / 2" by simp
644   have "eventually (\<lambda>x. norm (f x) < r/2) F"
645     using f r by (rule ZfunD)
646   moreover
647   have "eventually (\<lambda>x. norm (g x) < r/2) F"
648     using g r by (rule ZfunD)
649   ultimately
650   show "eventually (\<lambda>x. norm (f x + g x) < r) F"
651   proof eventually_elim
652     case (elim x)
653     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
654       by (rule norm_triangle_ineq)
655     also have "\<dots> < r/2 + r/2"
656       using elim by (rule add_strict_mono)
657     finally show ?case
658       by simp
659   qed
660 qed
662 lemma Zfun_minus: "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. - f x) F"
663   unfolding Zfun_def by simp
665 lemma Zfun_diff: "\<lbrakk>Zfun f F; Zfun g F\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) F"
666   by (simp only: diff_minus Zfun_add Zfun_minus)
668 lemma (in bounded_linear) Zfun:
669   assumes g: "Zfun g F"
670   shows "Zfun (\<lambda>x. f (g x)) F"
671 proof -
672   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
673     using bounded by fast
674   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) F"
675     by simp
676   with g show ?thesis
677     by (rule Zfun_imp_Zfun)
678 qed
680 lemma (in bounded_bilinear) Zfun:
681   assumes f: "Zfun f F"
682   assumes g: "Zfun g F"
683   shows "Zfun (\<lambda>x. f x ** g x) F"
684 proof (rule ZfunI)
685   fix r::real assume r: "0 < r"
686   obtain K where K: "0 < K"
687     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
688     using pos_bounded by fast
689   from K have K': "0 < inverse K"
690     by (rule positive_imp_inverse_positive)
691   have "eventually (\<lambda>x. norm (f x) < r) F"
692     using f r by (rule ZfunD)
693   moreover
694   have "eventually (\<lambda>x. norm (g x) < inverse K) F"
695     using g K' by (rule ZfunD)
696   ultimately
697   show "eventually (\<lambda>x. norm (f x ** g x) < r) F"
698   proof eventually_elim
699     case (elim x)
700     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
701       by (rule norm_le)
702     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
703       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero elim K)
704     also from K have "r * inverse K * K = r"
705       by simp
706     finally show ?case .
707   qed
708 qed
710 lemma (in bounded_bilinear) Zfun_left:
711   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. f x ** a) F"
712   by (rule bounded_linear_left [THEN bounded_linear.Zfun])
714 lemma (in bounded_bilinear) Zfun_right:
715   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. a ** f x) F"
716   by (rule bounded_linear_right [THEN bounded_linear.Zfun])
718 lemmas Zfun_mult = bounded_bilinear.Zfun [OF bounded_bilinear_mult]
719 lemmas Zfun_mult_right = bounded_bilinear.Zfun_right [OF bounded_bilinear_mult]
720 lemmas Zfun_mult_left = bounded_bilinear.Zfun_left [OF bounded_bilinear_mult]
723 subsection {* Limits *}
725 definition filterlim :: "('a \<Rightarrow> 'b) \<Rightarrow> 'b filter \<Rightarrow> 'a filter \<Rightarrow> bool" where
726   "filterlim f F2 F1 \<longleftrightarrow> filtermap f F1 \<le> F2"
728 syntax
729   "_LIM" :: "pttrns \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> bool" ("(3LIM (_)/ (_)./ (_) :> (_))" [1000, 10, 0, 10] 10)
731 translations
732   "LIM x F1. f :> F2"   == "CONST filterlim (%x. f) F2 F1"
734 lemma filterlim_iff:
735   "(LIM x F1. f x :> F2) \<longleftrightarrow> (\<forall>P. eventually P F2 \<longrightarrow> eventually (\<lambda>x. P (f x)) F1)"
736   unfolding filterlim_def le_filter_def eventually_filtermap ..
738 lemma filterlim_compose:
739   "filterlim g F3 F2 \<Longrightarrow> filterlim f F2 F1 \<Longrightarrow> filterlim (\<lambda>x. g (f x)) F3 F1"
740   unfolding filterlim_def filtermap_filtermap[symmetric] by (metis filtermap_mono order_trans)
742 lemma filterlim_mono:
743   "filterlim f F2 F1 \<Longrightarrow> F2 \<le> F2' \<Longrightarrow> F1' \<le> F1 \<Longrightarrow> filterlim f F2' F1'"
744   unfolding filterlim_def by (metis filtermap_mono order_trans)
746 lemma filterlim_ident: "LIM x F. x :> F"
747   by (simp add: filterlim_def filtermap_ident)
749 lemma filterlim_cong:
750   "F1 = F1' \<Longrightarrow> F2 = F2' \<Longrightarrow> eventually (\<lambda>x. f x = g x) F2 \<Longrightarrow> filterlim f F1 F2 = filterlim g F1' F2'"
751   by (auto simp: filterlim_def le_filter_def eventually_filtermap elim: eventually_elim2)
753 lemma filterlim_within:
754   "(LIM x F1. f x :> F2 within S) \<longleftrightarrow> (eventually (\<lambda>x. f x \<in> S) F1 \<and> (LIM x F1. f x :> F2))"
755   unfolding filterlim_def
756 proof safe
757   assume "filtermap f F1 \<le> F2 within S" then show "eventually (\<lambda>x. f x \<in> S) F1"
758     by (auto simp: le_filter_def eventually_filtermap eventually_within elim!: allE[of _ "\<lambda>x. x \<in> S"])
759 qed (auto intro: within_le order_trans simp: le_within_iff eventually_filtermap)
761 lemma filterlim_filtermap: "filterlim f F1 (filtermap g F2) = filterlim (\<lambda>x. f (g x)) F1 F2"
762   unfolding filterlim_def filtermap_filtermap ..
764 lemma filterlim_sup:
765   "filterlim f F F1 \<Longrightarrow> filterlim f F F2 \<Longrightarrow> filterlim f F (sup F1 F2)"
766   unfolding filterlim_def filtermap_sup by auto
768 lemma filterlim_Suc: "filterlim Suc sequentially sequentially"
769   by (simp add: filterlim_iff eventually_sequentially) (metis le_Suc_eq)
771 abbreviation (in topological_space)
772   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "--->" 55) where
773   "(f ---> l) F \<equiv> filterlim f (nhds l) F"
775 ML {*
776 structure Tendsto_Intros = Named_Thms
777 (
778   val name = @{binding tendsto_intros}
779   val description = "introduction rules for tendsto"
780 )
781 *}
783 setup Tendsto_Intros.setup
785 lemma tendsto_def: "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
786   unfolding filterlim_def
787 proof safe
788   fix S assume "open S" "l \<in> S" "filtermap f F \<le> nhds l"
789   then show "eventually (\<lambda>x. f x \<in> S) F"
790     unfolding eventually_nhds eventually_filtermap le_filter_def
791     by (auto elim!: allE[of _ "\<lambda>x. x \<in> S"] eventually_rev_mp)
792 qed (auto elim!: eventually_rev_mp simp: eventually_nhds eventually_filtermap le_filter_def)
794 lemma filterlim_at:
795   "(LIM x F. f x :> at b) \<longleftrightarrow> (eventually (\<lambda>x. f x \<noteq> b) F \<and> (f ---> b) F)"
796   by (simp add: at_def filterlim_within)
798 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
799   unfolding tendsto_def le_filter_def by fast
801 lemma topological_tendstoI:
802   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F)
803     \<Longrightarrow> (f ---> l) F"
804   unfolding tendsto_def by auto
806 lemma topological_tendstoD:
807   "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
808   unfolding tendsto_def by auto
810 lemma tendstoI:
811   assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
812   shows "(f ---> l) F"
813   apply (rule topological_tendstoI)
814   apply (simp add: open_dist)
815   apply (drule (1) bspec, clarify)
816   apply (drule assms)
817   apply (erule eventually_elim1, simp)
818   done
820 lemma tendstoD:
821   "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
822   apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
823   apply (clarsimp simp add: open_dist)
824   apply (rule_tac x="e - dist x l" in exI, clarsimp)
825   apply (simp only: less_diff_eq)
826   apply (erule le_less_trans [OF dist_triangle])
827   apply simp
828   apply simp
829   done
831 lemma tendsto_iff:
832   "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
833   using tendstoI tendstoD by fast
835 lemma tendsto_Zfun_iff: "(f ---> a) F = Zfun (\<lambda>x. f x - a) F"
836   by (simp only: tendsto_iff Zfun_def dist_norm)
838 lemma tendsto_bot [simp]: "(f ---> a) bot"
839   unfolding tendsto_def by simp
841 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
842   unfolding tendsto_def eventually_at_topological by auto
844 lemma tendsto_ident_at_within [tendsto_intros]:
845   "((\<lambda>x. x) ---> a) (at a within S)"
846   unfolding tendsto_def eventually_within eventually_at_topological by auto
848 lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) F"
849   by (simp add: tendsto_def)
851 lemma tendsto_unique:
852   fixes f :: "'a \<Rightarrow> 'b::t2_space"
853   assumes "\<not> trivial_limit F" and "(f ---> a) F" and "(f ---> b) F"
854   shows "a = b"
855 proof (rule ccontr)
856   assume "a \<noteq> b"
857   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
858     using hausdorff [OF `a \<noteq> b`] by fast
859   have "eventually (\<lambda>x. f x \<in> U) F"
860     using `(f ---> a) F` `open U` `a \<in> U` by (rule topological_tendstoD)
861   moreover
862   have "eventually (\<lambda>x. f x \<in> V) F"
863     using `(f ---> b) F` `open V` `b \<in> V` by (rule topological_tendstoD)
864   ultimately
865   have "eventually (\<lambda>x. False) F"
866   proof eventually_elim
867     case (elim x)
868     hence "f x \<in> U \<inter> V" by simp
869     with `U \<inter> V = {}` show ?case by simp
870   qed
871   with `\<not> trivial_limit F` show "False"
872     by (simp add: trivial_limit_def)
873 qed
875 lemma tendsto_const_iff:
876   fixes a b :: "'a::t2_space"
877   assumes "\<not> trivial_limit F" shows "((\<lambda>x. a) ---> b) F \<longleftrightarrow> a = b"
878   by (safe intro!: tendsto_const tendsto_unique [OF assms tendsto_const])
880 lemma tendsto_at_iff_tendsto_nhds:
881   "(g ---> g l) (at l) \<longleftrightarrow> (g ---> g l) (nhds l)"
882   unfolding tendsto_def at_def eventually_within
883   by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)
885 lemma tendsto_compose:
886   "(g ---> g l) (at l) \<Longrightarrow> (f ---> l) F \<Longrightarrow> ((\<lambda>x. g (f x)) ---> g l) F"
887   unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])
889 lemma tendsto_compose_eventually:
890   "(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"
891   by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)
893 lemma metric_tendsto_imp_tendsto:
894   assumes f: "(f ---> a) F"
895   assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
896   shows "(g ---> b) F"
897 proof (rule tendstoI)
898   fix e :: real assume "0 < e"
899   with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
900   with le show "eventually (\<lambda>x. dist (g x) b < e) F"
901     using le_less_trans by (rule eventually_elim2)
902 qed
904 subsubsection {* Distance and norms *}
906 lemma tendsto_dist [tendsto_intros]:
907   assumes f: "(f ---> l) F" and g: "(g ---> m) F"
908   shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) F"
909 proof (rule tendstoI)
910   fix e :: real assume "0 < e"
911   hence e2: "0 < e/2" by simp
912   from tendstoD [OF f e2] tendstoD [OF g e2]
913   show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
914   proof (eventually_elim)
915     case (elim x)
916     then show "dist (dist (f x) (g x)) (dist l m) < e"
917       unfolding dist_real_def
918       using dist_triangle2 [of "f x" "g x" "l"]
919       using dist_triangle2 [of "g x" "l" "m"]
920       using dist_triangle3 [of "l" "m" "f x"]
921       using dist_triangle [of "f x" "m" "g x"]
922       by arith
923   qed
924 qed
926 lemma norm_conv_dist: "norm x = dist x 0"
927   unfolding dist_norm by simp
929 lemma tendsto_norm [tendsto_intros]:
930   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> norm a) F"
931   unfolding norm_conv_dist by (intro tendsto_intros)
933 lemma tendsto_norm_zero:
934   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> 0) F"
935   by (drule tendsto_norm, simp)
937 lemma tendsto_norm_zero_cancel:
938   "((\<lambda>x. norm (f x)) ---> 0) F \<Longrightarrow> (f ---> 0) F"
939   unfolding tendsto_iff dist_norm by simp
941 lemma tendsto_norm_zero_iff:
942   "((\<lambda>x. norm (f x)) ---> 0) F \<longleftrightarrow> (f ---> 0) F"
943   unfolding tendsto_iff dist_norm by simp
945 lemma tendsto_rabs [tendsto_intros]:
946   "(f ---> (l::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> \<bar>l\<bar>) F"
947   by (fold real_norm_def, rule tendsto_norm)
949 lemma tendsto_rabs_zero:
950   "(f ---> (0::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> 0) F"
951   by (fold real_norm_def, rule tendsto_norm_zero)
953 lemma tendsto_rabs_zero_cancel:
954   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<Longrightarrow> (f ---> 0) F"
955   by (fold real_norm_def, rule tendsto_norm_zero_cancel)
957 lemma tendsto_rabs_zero_iff:
958   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<longleftrightarrow> (f ---> 0) F"
959   by (fold real_norm_def, rule tendsto_norm_zero_iff)
961 subsubsection {* Addition and subtraction *}
963 lemma tendsto_add [tendsto_intros]:
964   fixes a b :: "'a::real_normed_vector"
965   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> a + b) F"
969   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
970   shows "\<lbrakk>(f ---> 0) F; (g ---> 0) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> 0) F"
971   by (drule (1) tendsto_add, simp)
973 lemma tendsto_minus [tendsto_intros]:
974   fixes a :: "'a::real_normed_vector"
975   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. - f x) ---> - a) F"
976   by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)
978 lemma tendsto_minus_cancel:
979   fixes a :: "'a::real_normed_vector"
980   shows "((\<lambda>x. - f x) ---> - a) F \<Longrightarrow> (f ---> a) F"
981   by (drule tendsto_minus, simp)
983 lemma tendsto_minus_cancel_left:
984     "(f ---> - (y::_::real_normed_vector)) F \<longleftrightarrow> ((\<lambda>x. - f x) ---> y) F"
985   using tendsto_minus_cancel[of f "- y" F]  tendsto_minus[of f "- y" F]
986   by auto
988 lemma tendsto_diff [tendsto_intros]:
989   fixes a b :: "'a::real_normed_vector"
990   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) ---> a - b) F"
991   by (simp add: diff_minus tendsto_add tendsto_minus)
993 lemma tendsto_setsum [tendsto_intros]:
994   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
995   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> a i) F"
996   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) ---> (\<Sum>i\<in>S. a i)) F"
997 proof (cases "finite S")
998   assume "finite S" thus ?thesis using assms
1000 next
1001   assume "\<not> finite S" thus ?thesis
1002     by (simp add: tendsto_const)
1003 qed
1005 lemma real_tendsto_sandwich:
1006   fixes f g h :: "'a \<Rightarrow> real"
1007   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
1008   assumes lim: "(f ---> c) net" "(h ---> c) net"
1009   shows "(g ---> c) net"
1010 proof -
1011   have "((\<lambda>n. g n - f n) ---> 0) net"
1012   proof (rule metric_tendsto_imp_tendsto)
1013     show "eventually (\<lambda>n. dist (g n - f n) 0 \<le> dist (h n - f n) 0) net"
1014       using ev by (rule eventually_elim2) (simp add: dist_real_def)
1015     show "((\<lambda>n. h n - f n) ---> 0) net"
1016       using tendsto_diff[OF lim(2,1)] by simp
1017   qed
1018   from tendsto_add[OF this lim(1)] show ?thesis by simp
1019 qed
1021 subsubsection {* Linear operators and multiplication *}
1023 lemma (in bounded_linear) tendsto:
1024   "(g ---> a) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> f a) F"
1025   by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
1027 lemma (in bounded_linear) tendsto_zero:
1028   "(g ---> 0) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> 0) F"
1029   by (drule tendsto, simp only: zero)
1031 lemma (in bounded_bilinear) tendsto:
1032   "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) ---> a ** b) F"
1033   by (simp only: tendsto_Zfun_iff prod_diff_prod
1034                  Zfun_add Zfun Zfun_left Zfun_right)
1036 lemma (in bounded_bilinear) tendsto_zero:
1037   assumes f: "(f ---> 0) F"
1038   assumes g: "(g ---> 0) F"
1039   shows "((\<lambda>x. f x ** g x) ---> 0) F"
1040   using tendsto [OF f g] by (simp add: zero_left)
1042 lemma (in bounded_bilinear) tendsto_left_zero:
1043   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. f x ** c) ---> 0) F"
1044   by (rule bounded_linear.tendsto_zero [OF bounded_linear_left])
1046 lemma (in bounded_bilinear) tendsto_right_zero:
1047   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. c ** f x) ---> 0) F"
1048   by (rule bounded_linear.tendsto_zero [OF bounded_linear_right])
1050 lemmas tendsto_of_real [tendsto_intros] =
1051   bounded_linear.tendsto [OF bounded_linear_of_real]
1053 lemmas tendsto_scaleR [tendsto_intros] =
1054   bounded_bilinear.tendsto [OF bounded_bilinear_scaleR]
1056 lemmas tendsto_mult [tendsto_intros] =
1057   bounded_bilinear.tendsto [OF bounded_bilinear_mult]
1059 lemmas tendsto_mult_zero =
1060   bounded_bilinear.tendsto_zero [OF bounded_bilinear_mult]
1062 lemmas tendsto_mult_left_zero =
1063   bounded_bilinear.tendsto_left_zero [OF bounded_bilinear_mult]
1065 lemmas tendsto_mult_right_zero =
1066   bounded_bilinear.tendsto_right_zero [OF bounded_bilinear_mult]
1068 lemma tendsto_power [tendsto_intros]:
1069   fixes f :: "'a \<Rightarrow> 'b::{power,real_normed_algebra}"
1070   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. f x ^ n) ---> a ^ n) F"
1071   by (induct n) (simp_all add: tendsto_const tendsto_mult)
1073 lemma tendsto_setprod [tendsto_intros]:
1074   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
1075   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> L i) F"
1076   shows "((\<lambda>x. \<Prod>i\<in>S. f i x) ---> (\<Prod>i\<in>S. L i)) F"
1077 proof (cases "finite S")
1078   assume "finite S" thus ?thesis using assms
1079     by (induct, simp add: tendsto_const, simp add: tendsto_mult)
1080 next
1081   assume "\<not> finite S" thus ?thesis
1082     by (simp add: tendsto_const)
1083 qed
1085 lemma tendsto_le_const:
1086   fixes f :: "_ \<Rightarrow> real"
1087   assumes F: "\<not> trivial_limit F"
1088   assumes x: "(f ---> x) F" and a: "eventually (\<lambda>x. a \<le> f x) F"
1089   shows "a \<le> x"
1090 proof (rule ccontr)
1091   assume "\<not> a \<le> x"
1092   with x have "eventually (\<lambda>x. f x < a) F"
1093     by (auto simp add: tendsto_def elim!: allE[of _ "{..< a}"])
1094   with a have "eventually (\<lambda>x. False) F"
1095     by eventually_elim auto
1096   with F show False
1097     by (simp add: eventually_False)
1098 qed
1100 lemma tendsto_le:
1101   fixes f g :: "_ \<Rightarrow> real"
1102   assumes F: "\<not> trivial_limit F"
1103   assumes x: "(f ---> x) F" and y: "(g ---> y) F"
1104   assumes ev: "eventually (\<lambda>x. g x \<le> f x) F"
1105   shows "y \<le> x"
1106   using tendsto_le_const[OF F tendsto_diff[OF x y], of 0] ev
1107   by (simp add: sign_simps)
1109 subsubsection {* Inverse and division *}
1111 lemma (in bounded_bilinear) Zfun_prod_Bfun:
1112   assumes f: "Zfun f F"
1113   assumes g: "Bfun g F"
1114   shows "Zfun (\<lambda>x. f x ** g x) F"
1115 proof -
1116   obtain K where K: "0 \<le> K"
1117     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
1118     using nonneg_bounded by fast
1119   obtain B where B: "0 < B"
1120     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) F"
1121     using g by (rule BfunE)
1122   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) F"
1123   using norm_g proof eventually_elim
1124     case (elim x)
1125     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
1126       by (rule norm_le)
1127     also have "\<dots> \<le> norm (f x) * B * K"
1128       by (intro mult_mono' order_refl norm_g norm_ge_zero
1129                 mult_nonneg_nonneg K elim)
1130     also have "\<dots> = norm (f x) * (B * K)"
1131       by (rule mult_assoc)
1132     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
1133   qed
1134   with f show ?thesis
1135     by (rule Zfun_imp_Zfun)
1136 qed
1138 lemma (in bounded_bilinear) flip:
1139   "bounded_bilinear (\<lambda>x y. y ** x)"
1140   apply default
1141   apply (rule add_right)
1142   apply (rule add_left)
1143   apply (rule scaleR_right)
1144   apply (rule scaleR_left)
1145   apply (subst mult_commute)
1146   using bounded by fast
1148 lemma (in bounded_bilinear) Bfun_prod_Zfun:
1149   assumes f: "Bfun f F"
1150   assumes g: "Zfun g F"
1151   shows "Zfun (\<lambda>x. f x ** g x) F"
1152   using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
1154 lemma Bfun_inverse_lemma:
1155   fixes x :: "'a::real_normed_div_algebra"
1156   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
1157   apply (subst nonzero_norm_inverse, clarsimp)
1158   apply (erule (1) le_imp_inverse_le)
1159   done
1161 lemma Bfun_inverse:
1162   fixes a :: "'a::real_normed_div_algebra"
1163   assumes f: "(f ---> a) F"
1164   assumes a: "a \<noteq> 0"
1165   shows "Bfun (\<lambda>x. inverse (f x)) F"
1166 proof -
1167   from a have "0 < norm a" by simp
1168   hence "\<exists>r>0. r < norm a" by (rule dense)
1169   then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
1170   have "eventually (\<lambda>x. dist (f x) a < r) F"
1171     using tendstoD [OF f r1] by fast
1172   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) F"
1173   proof eventually_elim
1174     case (elim x)
1175     hence 1: "norm (f x - a) < r"
1176       by (simp add: dist_norm)
1177     hence 2: "f x \<noteq> 0" using r2 by auto
1178     hence "norm (inverse (f x)) = inverse (norm (f x))"
1179       by (rule nonzero_norm_inverse)
1180     also have "\<dots> \<le> inverse (norm a - r)"
1181     proof (rule le_imp_inverse_le)
1182       show "0 < norm a - r" using r2 by simp
1183     next
1184       have "norm a - norm (f x) \<le> norm (a - f x)"
1185         by (rule norm_triangle_ineq2)
1186       also have "\<dots> = norm (f x - a)"
1187         by (rule norm_minus_commute)
1188       also have "\<dots> < r" using 1 .
1189       finally show "norm a - r \<le> norm (f x)" by simp
1190     qed
1191     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
1192   qed
1193   thus ?thesis by (rule BfunI)
1194 qed
1196 lemma tendsto_inverse [tendsto_intros]:
1197   fixes a :: "'a::real_normed_div_algebra"
1198   assumes f: "(f ---> a) F"
1199   assumes a: "a \<noteq> 0"
1200   shows "((\<lambda>x. inverse (f x)) ---> inverse a) F"
1201 proof -
1202   from a have "0 < norm a" by simp
1203   with f have "eventually (\<lambda>x. dist (f x) a < norm a) F"
1204     by (rule tendstoD)
1205   then have "eventually (\<lambda>x. f x \<noteq> 0) F"
1206     unfolding dist_norm by (auto elim!: eventually_elim1)
1207   with a have "eventually (\<lambda>x. inverse (f x) - inverse a =
1208     - (inverse (f x) * (f x - a) * inverse a)) F"
1209     by (auto elim!: eventually_elim1 simp: inverse_diff_inverse)
1210   moreover have "Zfun (\<lambda>x. - (inverse (f x) * (f x - a) * inverse a)) F"
1211     by (intro Zfun_minus Zfun_mult_left
1212       bounded_bilinear.Bfun_prod_Zfun [OF bounded_bilinear_mult]
1213       Bfun_inverse [OF f a] f [unfolded tendsto_Zfun_iff])
1214   ultimately show ?thesis
1215     unfolding tendsto_Zfun_iff by (rule Zfun_ssubst)
1216 qed
1218 lemma tendsto_divide [tendsto_intros]:
1219   fixes a b :: "'a::real_normed_field"
1220   shows "\<lbrakk>(f ---> a) F; (g ---> b) F; b \<noteq> 0\<rbrakk>
1221     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) F"
1222   by (simp add: tendsto_mult tendsto_inverse divide_inverse)
1224 lemma tendsto_sgn [tendsto_intros]:
1225   fixes l :: "'a::real_normed_vector"
1226   shows "\<lbrakk>(f ---> l) F; l \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. sgn (f x)) ---> sgn l) F"
1227   unfolding sgn_div_norm by (simp add: tendsto_intros)
1229 subsection {* Limits to @{const at_top} and @{const at_bot} *}
1231 lemma filterlim_at_top:
1232   fixes f :: "'a \<Rightarrow> ('b::linorder)"
1233   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z \<le> f x) F)"
1234   by (auto simp: filterlim_iff eventually_at_top_linorder elim!: eventually_elim1)
1236 lemma filterlim_at_top_dense:
1237   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)"
1238   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. Z < f x) F)"
1239   by (metis eventually_elim1[of _ F] eventually_gt_at_top order_less_imp_le
1240             filterlim_at_top[of f F] filterlim_iff[of f at_top F])
1242 lemma filterlim_at_top_ge:
1243   fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
1244   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z\<ge>c. eventually (\<lambda>x. Z \<le> f x) F)"
1245   unfolding filterlim_at_top
1246 proof safe
1247   fix Z assume *: "\<forall>Z\<ge>c. eventually (\<lambda>x. Z \<le> f x) F"
1248   with *[THEN spec, of "max Z c"] show "eventually (\<lambda>x. Z \<le> f x) F"
1249     by (auto elim!: eventually_elim1)
1250 qed simp
1252 lemma filterlim_at_top_at_top:
1253   fixes f :: "'a::linorder \<Rightarrow> 'b::linorder"
1254   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
1255   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
1256   assumes Q: "eventually Q at_top"
1257   assumes P: "eventually P at_top"
1258   shows "filterlim f at_top at_top"
1259 proof -
1260   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
1261     unfolding eventually_at_top_linorder by auto
1262   show ?thesis
1263   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
1264     fix z assume "x \<le> z"
1265     with x have "P z" by auto
1266     have "eventually (\<lambda>x. g z \<le> x) at_top"
1267       by (rule eventually_ge_at_top)
1268     with Q show "eventually (\<lambda>x. z \<le> f x) at_top"
1269       by eventually_elim (metis mono bij `P z`)
1270   qed
1271 qed
1273 lemma filterlim_at_top_gt:
1274   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
1275   shows "(LIM x F. f x :> at_top) \<longleftrightarrow> (\<forall>Z>c. eventually (\<lambda>x. Z \<le> f x) F)"
1276   by (metis filterlim_at_top order_less_le_trans gt_ex filterlim_at_top_ge)
1278 lemma filterlim_at_bot:
1279   fixes f :: "'a \<Rightarrow> ('b::linorder)"
1280   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z. eventually (\<lambda>x. f x \<le> Z) F)"
1281   by (auto simp: filterlim_iff eventually_at_bot_linorder elim!: eventually_elim1)
1283 lemma filterlim_at_bot_le:
1284   fixes f :: "'a \<Rightarrow> ('b::linorder)" and c :: "'b"
1285   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F)"
1286   unfolding filterlim_at_bot
1287 proof safe
1288   fix Z assume *: "\<forall>Z\<le>c. eventually (\<lambda>x. Z \<ge> f x) F"
1289   with *[THEN spec, of "min Z c"] show "eventually (\<lambda>x. Z \<ge> f x) F"
1290     by (auto elim!: eventually_elim1)
1291 qed simp
1293 lemma filterlim_at_bot_lt:
1294   fixes f :: "'a \<Rightarrow> ('b::dense_linorder)" and c :: "'b"
1295   shows "(LIM x F. f x :> at_bot) \<longleftrightarrow> (\<forall>Z<c. eventually (\<lambda>x. Z \<ge> f x) F)"
1296   by (metis filterlim_at_bot filterlim_at_bot_le lt_ex order_le_less_trans)
1298 lemma filterlim_at_bot_at_right:
1299   fixes f :: "real \<Rightarrow> 'b::linorder"
1300   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
1301   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
1302   assumes Q: "eventually Q (at_right a)" and bound: "\<And>b. Q b \<Longrightarrow> a < b"
1303   assumes P: "eventually P at_bot"
1304   shows "filterlim f at_bot (at_right a)"
1305 proof -
1306   from P obtain x where x: "\<And>y. y \<le> x \<Longrightarrow> P y"
1307     unfolding eventually_at_bot_linorder by auto
1308   show ?thesis
1309   proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
1310     fix z assume "z \<le> x"
1311     with x have "P z" by auto
1312     have "eventually (\<lambda>x. x \<le> g z) (at_right a)"
1313       using bound[OF bij(2)[OF `P z`]]
1314       by (auto simp add: eventually_within_less dist_real_def intro!:  exI[of _ "g z - a"])
1315     with Q show "eventually (\<lambda>x. f x \<le> z) (at_right a)"
1316       by eventually_elim (metis bij `P z` mono)
1317   qed
1318 qed
1320 lemma filterlim_at_top_at_left:
1321   fixes f :: "real \<Rightarrow> 'b::linorder"
1322   assumes mono: "\<And>x y. Q x \<Longrightarrow> Q y \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
1323   assumes bij: "\<And>x. P x \<Longrightarrow> f (g x) = x" "\<And>x. P x \<Longrightarrow> Q (g x)"
1324   assumes Q: "eventually Q (at_left a)" and bound: "\<And>b. Q b \<Longrightarrow> b < a"
1325   assumes P: "eventually P at_top"
1326   shows "filterlim f at_top (at_left a)"
1327 proof -
1328   from P obtain x where x: "\<And>y. x \<le> y \<Longrightarrow> P y"
1329     unfolding eventually_at_top_linorder by auto
1330   show ?thesis
1331   proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
1332     fix z assume "x \<le> z"
1333     with x have "P z" by auto
1334     have "eventually (\<lambda>x. g z \<le> x) (at_left a)"
1335       using bound[OF bij(2)[OF `P z`]]
1336       by (auto simp add: eventually_within_less dist_real_def intro!:  exI[of _ "a - g z"])
1337     with Q show "eventually (\<lambda>x. z \<le> f x) (at_left a)"
1338       by eventually_elim (metis bij `P z` mono)
1339   qed
1340 qed
1342 lemma filterlim_at_infinity:
1343   fixes f :: "_ \<Rightarrow> 'a\<Colon>real_normed_vector"
1344   assumes "0 \<le> c"
1345   shows "(LIM x F. f x :> at_infinity) \<longleftrightarrow> (\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F)"
1346   unfolding filterlim_iff eventually_at_infinity
1347 proof safe
1348   fix P :: "'a \<Rightarrow> bool" and b
1349   assume *: "\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F"
1350     and P: "\<forall>x. b \<le> norm x \<longrightarrow> P x"
1351   have "max b (c + 1) > c" by auto
1352   with * have "eventually (\<lambda>x. max b (c + 1) \<le> norm (f x)) F"
1353     by auto
1354   then show "eventually (\<lambda>x. P (f x)) F"
1355   proof eventually_elim
1356     fix x assume "max b (c + 1) \<le> norm (f x)"
1357     with P show "P (f x)" by auto
1358   qed
1359 qed force
1361 lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
1362   unfolding filterlim_at_top
1363   apply (intro allI)
1364   apply (rule_tac c="natceiling (Z + 1)" in eventually_sequentiallyI)
1365   apply (auto simp: natceiling_le_eq)
1366   done
1368 subsection {* Relate @{const at}, @{const at_left} and @{const at_right} *}
1370 text {*
1372 This lemmas are useful for conversion between @{term "at x"} to @{term "at_left x"} and
1373 @{term "at_right x"} and also @{term "at_right 0"}.
1375 *}
1377 lemma at_eq_sup_left_right: "at (x::real) = sup (at_left x) (at_right x)"
1378   by (auto simp: eventually_within at_def filter_eq_iff eventually_sup
1379            elim: eventually_elim2 eventually_elim1)
1381 lemma filterlim_split_at_real:
1382   "filterlim f F (at_left x) \<Longrightarrow> filterlim f F (at_right x) \<Longrightarrow> filterlim f F (at (x::real))"
1383   by (subst at_eq_sup_left_right) (rule filterlim_sup)
1385 lemma filtermap_nhds_shift: "filtermap (\<lambda>x. x - d) (nhds a) = nhds (a - d::real)"
1386   unfolding filter_eq_iff eventually_filtermap eventually_nhds_metric
1387   by (intro allI ex_cong) (auto simp: dist_real_def field_simps)
1389 lemma filtermap_nhds_minus: "filtermap (\<lambda>x. - x) (nhds a) = nhds (- a::real)"
1390   unfolding filter_eq_iff eventually_filtermap eventually_nhds_metric
1391   apply (intro allI ex_cong)
1392   apply (auto simp: dist_real_def field_simps)
1393   apply (erule_tac x="-x" in allE)
1394   apply simp
1395   done
1397 lemma filtermap_at_shift: "filtermap (\<lambda>x. x - d) (at a) = at (a - d::real)"
1398   unfolding at_def filtermap_nhds_shift[symmetric]
1399   by (simp add: filter_eq_iff eventually_filtermap eventually_within)
1401 lemma filtermap_at_right_shift: "filtermap (\<lambda>x. x - d) (at_right a) = at_right (a - d::real)"
1402   unfolding filtermap_at_shift[symmetric]
1403   by (simp add: filter_eq_iff eventually_filtermap eventually_within)
1405 lemma at_right_to_0: "at_right (a::real) = filtermap (\<lambda>x. x + a) (at_right 0)"
1406   using filtermap_at_right_shift[of "-a" 0] by simp
1408 lemma filterlim_at_right_to_0:
1409   "filterlim f F (at_right (a::real)) \<longleftrightarrow> filterlim (\<lambda>x. f (x + a)) F (at_right 0)"
1410   unfolding filterlim_def filtermap_filtermap at_right_to_0[of a] ..
1412 lemma eventually_at_right_to_0:
1413   "eventually P (at_right (a::real)) \<longleftrightarrow> eventually (\<lambda>x. P (x + a)) (at_right 0)"
1414   unfolding at_right_to_0[of a] by (simp add: eventually_filtermap)
1416 lemma filtermap_at_minus: "filtermap (\<lambda>x. - x) (at a) = at (- a::real)"
1417   unfolding at_def filtermap_nhds_minus[symmetric]
1418   by (simp add: filter_eq_iff eventually_filtermap eventually_within)
1420 lemma at_left_minus: "at_left (a::real) = filtermap (\<lambda>x. - x) (at_right (- a))"
1421   by (simp add: filter_eq_iff eventually_filtermap eventually_within filtermap_at_minus[symmetric])
1423 lemma at_right_minus: "at_right (a::real) = filtermap (\<lambda>x. - x) (at_left (- a))"
1424   by (simp add: filter_eq_iff eventually_filtermap eventually_within filtermap_at_minus[symmetric])
1426 lemma filterlim_at_left_to_right:
1427   "filterlim f F (at_left (a::real)) \<longleftrightarrow> filterlim (\<lambda>x. f (- x)) F (at_right (-a))"
1428   unfolding filterlim_def filtermap_filtermap at_left_minus[of a] ..
1430 lemma eventually_at_left_to_right:
1431   "eventually P (at_left (a::real)) \<longleftrightarrow> eventually (\<lambda>x. P (- x)) (at_right (-a))"
1432   unfolding at_left_minus[of a] by (simp add: eventually_filtermap)
1434 lemma filterlim_at_split:
1435   "filterlim f F (at (x::real)) \<longleftrightarrow> filterlim f F (at_left x) \<and> filterlim f F (at_right x)"
1436   by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)
1438 lemma eventually_at_split:
1439   "eventually P (at (x::real)) \<longleftrightarrow> eventually P (at_left x) \<and> eventually P (at_right x)"
1440   by (subst at_eq_sup_left_right) (simp add: eventually_sup)
1442 lemma at_top_mirror: "at_top = filtermap uminus (at_bot :: real filter)"
1443   unfolding filter_eq_iff eventually_filtermap eventually_at_top_linorder eventually_at_bot_linorder
1444   by (metis le_minus_iff minus_minus)
1446 lemma at_bot_mirror: "at_bot = filtermap uminus (at_top :: real filter)"
1447   unfolding at_top_mirror filtermap_filtermap by (simp add: filtermap_ident)
1449 lemma filterlim_at_top_mirror: "(LIM x at_top. f x :> F) \<longleftrightarrow> (LIM x at_bot. f (-x::real) :> F)"
1450   unfolding filterlim_def at_top_mirror filtermap_filtermap ..
1452 lemma filterlim_at_bot_mirror: "(LIM x at_bot. f x :> F) \<longleftrightarrow> (LIM x at_top. f (-x::real) :> F)"
1453   unfolding filterlim_def at_bot_mirror filtermap_filtermap ..
1455 lemma filterlim_uminus_at_top_at_bot: "LIM x at_bot. - x :: real :> at_top"
1456   unfolding filterlim_at_top eventually_at_bot_dense
1457   by (metis leI minus_less_iff order_less_asym)
1459 lemma filterlim_uminus_at_bot_at_top: "LIM x at_top. - x :: real :> at_bot"
1460   unfolding filterlim_at_bot eventually_at_top_dense
1461   by (metis leI less_minus_iff order_less_asym)
1463 lemma filterlim_uminus_at_top: "(LIM x F. f x :> at_top) \<longleftrightarrow> (LIM x F. - (f x) :: real :> at_bot)"
1464   using filterlim_compose[OF filterlim_uminus_at_bot_at_top, of f F]
1465   using filterlim_compose[OF filterlim_uminus_at_top_at_bot, of "\<lambda>x. - f x" F]
1466   by auto
1468 lemma filterlim_uminus_at_bot: "(LIM x F. f x :> at_bot) \<longleftrightarrow> (LIM x F. - (f x) :: real :> at_top)"
1469   unfolding filterlim_uminus_at_top by simp
1471 lemma filterlim_inverse_at_top_right: "LIM x at_right (0::real). inverse x :> at_top"
1472   unfolding filterlim_at_top_gt[where c=0] eventually_within at_def
1473 proof safe
1474   fix Z :: real assume [arith]: "0 < Z"
1475   then have "eventually (\<lambda>x. x < inverse Z) (nhds 0)"
1476     by (auto simp add: eventually_nhds_metric dist_real_def intro!: exI[of _ "\<bar>inverse Z\<bar>"])
1477   then show "eventually (\<lambda>x. x \<in> - {0} \<longrightarrow> x \<in> {0<..} \<longrightarrow> Z \<le> inverse x) (nhds 0)"
1478     by (auto elim!: eventually_elim1 simp: inverse_eq_divide field_simps)
1479 qed
1481 lemma filterlim_inverse_at_top:
1482   "(f ---> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. 0 < f x) F \<Longrightarrow> LIM x F. inverse (f x) :> at_top"
1483   by (intro filterlim_compose[OF filterlim_inverse_at_top_right])
1484      (simp add: filterlim_def eventually_filtermap le_within_iff at_def eventually_elim1)
1486 lemma filterlim_inverse_at_bot_neg:
1487   "LIM x (at_left (0::real)). inverse x :> at_bot"
1488   by (simp add: filterlim_inverse_at_top_right filterlim_uminus_at_bot filterlim_at_left_to_right)
1490 lemma filterlim_inverse_at_bot:
1491   "(f ---> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. f x < 0) F \<Longrightarrow> LIM x F. inverse (f x) :> at_bot"
1492   unfolding filterlim_uminus_at_bot inverse_minus_eq[symmetric]
1493   by (rule filterlim_inverse_at_top) (simp_all add: tendsto_minus_cancel_left[symmetric])
1495 lemma tendsto_inverse_0:
1496   fixes x :: "_ \<Rightarrow> 'a\<Colon>real_normed_div_algebra"
1497   shows "(inverse ---> (0::'a)) at_infinity"
1498   unfolding tendsto_Zfun_iff diff_0_right Zfun_def eventually_at_infinity
1499 proof safe
1500   fix r :: real assume "0 < r"
1501   show "\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> norm (inverse x :: 'a) < r"
1502   proof (intro exI[of _ "inverse (r / 2)"] allI impI)
1503     fix x :: 'a
1504     from `0 < r` have "0 < inverse (r / 2)" by simp
1505     also assume *: "inverse (r / 2) \<le> norm x"
1506     finally show "norm (inverse x) < r"
1507       using * `0 < r` by (subst nonzero_norm_inverse) (simp_all add: inverse_eq_divide field_simps)
1508   qed
1509 qed
1511 lemma at_right_to_top: "(at_right (0::real)) = filtermap inverse at_top"
1512 proof (rule antisym)
1513   have "(inverse ---> (0::real)) at_top"
1514     by (metis tendsto_inverse_0 filterlim_mono at_top_le_at_infinity order_refl)
1515   then show "filtermap inverse at_top \<le> at_right (0::real)"
1516     unfolding at_within_eq
1517     by (intro le_withinI) (simp_all add: eventually_filtermap eventually_gt_at_top filterlim_def)
1518 next
1519   have "filtermap inverse (filtermap inverse (at_right (0::real))) \<le> filtermap inverse at_top"
1520     using filterlim_inverse_at_top_right unfolding filterlim_def by (rule filtermap_mono)
1521   then show "at_right (0::real) \<le> filtermap inverse at_top"
1522     by (simp add: filtermap_ident filtermap_filtermap)
1523 qed
1525 lemma eventually_at_right_to_top:
1526   "eventually P (at_right (0::real)) \<longleftrightarrow> eventually (\<lambda>x. P (inverse x)) at_top"
1527   unfolding at_right_to_top eventually_filtermap ..
1529 lemma filterlim_at_right_to_top:
1530   "filterlim f F (at_right (0::real)) \<longleftrightarrow> (LIM x at_top. f (inverse x) :> F)"
1531   unfolding filterlim_def at_right_to_top filtermap_filtermap ..
1533 lemma at_top_to_right: "at_top = filtermap inverse (at_right (0::real))"
1534   unfolding at_right_to_top filtermap_filtermap inverse_inverse_eq filtermap_ident ..
1536 lemma eventually_at_top_to_right:
1537   "eventually P at_top \<longleftrightarrow> eventually (\<lambda>x. P (inverse x)) (at_right (0::real))"
1538   unfolding at_top_to_right eventually_filtermap ..
1540 lemma filterlim_at_top_to_right:
1541   "filterlim f F at_top \<longleftrightarrow> (LIM x (at_right (0::real)). f (inverse x) :> F)"
1542   unfolding filterlim_def at_top_to_right filtermap_filtermap ..
1544 lemma filterlim_inverse_at_infinity:
1545   fixes x :: "_ \<Rightarrow> 'a\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
1546   shows "filterlim inverse at_infinity (at (0::'a))"
1547   unfolding filterlim_at_infinity[OF order_refl]
1548 proof safe
1549   fix r :: real assume "0 < r"
1550   then show "eventually (\<lambda>x::'a. r \<le> norm (inverse x)) (at 0)"
1551     unfolding eventually_at norm_inverse
1552     by (intro exI[of _ "inverse r"])
1553        (auto simp: norm_conv_dist[symmetric] field_simps inverse_eq_divide)
1554 qed
1556 lemma filterlim_inverse_at_iff:
1557   fixes g :: "'a \<Rightarrow> 'b\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
1558   shows "(LIM x F. inverse (g x) :> at 0) \<longleftrightarrow> (LIM x F. g x :> at_infinity)"
1559   unfolding filterlim_def filtermap_filtermap[symmetric]
1560 proof
1561   assume "filtermap g F \<le> at_infinity"
1562   then have "filtermap inverse (filtermap g F) \<le> filtermap inverse at_infinity"
1563     by (rule filtermap_mono)
1564   also have "\<dots> \<le> at 0"
1565     using tendsto_inverse_0
1566     by (auto intro!: le_withinI exI[of _ 1]
1567              simp: eventually_filtermap eventually_at_infinity filterlim_def at_def)
1568   finally show "filtermap inverse (filtermap g F) \<le> at 0" .
1569 next
1570   assume "filtermap inverse (filtermap g F) \<le> at 0"
1571   then have "filtermap inverse (filtermap inverse (filtermap g F)) \<le> filtermap inverse (at 0)"
1572     by (rule filtermap_mono)
1573   with filterlim_inverse_at_infinity show "filtermap g F \<le> at_infinity"
1574     by (auto intro: order_trans simp: filterlim_def filtermap_filtermap)
1575 qed
1577 lemma tendsto_inverse_0_at_top:
1578   "LIM x F. f x :> at_top \<Longrightarrow> ((\<lambda>x. inverse (f x) :: real) ---> 0) F"
1579  by (metis at_top_le_at_infinity filterlim_at filterlim_inverse_at_iff filterlim_mono order_refl)
1581 text {*
1583 We only show rules for multiplication and addition when the functions are either against a real
1584 value or against infinity. Further rules are easy to derive by using @{thm filterlim_uminus_at_top}.
1586 *}
1588 lemma filterlim_tendsto_pos_mult_at_top:
1589   assumes f: "(f ---> c) F" and c: "0 < c"
1590   assumes g: "LIM x F. g x :> at_top"
1591   shows "LIM x F. (f x * g x :: real) :> at_top"
1592   unfolding filterlim_at_top_gt[where c=0]
1593 proof safe
1594   fix Z :: real assume "0 < Z"
1595   from f `0 < c` have "eventually (\<lambda>x. c / 2 < f x) F"
1596     by (auto dest!: tendstoD[where e="c / 2"] elim!: eventually_elim1
1597              simp: dist_real_def abs_real_def split: split_if_asm)
1598   moreover from g have "eventually (\<lambda>x. (Z / c * 2) \<le> g x) F"
1599     unfolding filterlim_at_top by auto
1600   ultimately show "eventually (\<lambda>x. Z \<le> f x * g x) F"
1601   proof eventually_elim
1602     fix x assume "c / 2 < f x" "Z / c * 2 \<le> g x"
1603     with `0 < Z` `0 < c` have "c / 2 * (Z / c * 2) \<le> f x * g x"
1604       by (intro mult_mono) (auto simp: zero_le_divide_iff)
1605     with `0 < c` show "Z \<le> f x * g x"
1606        by simp
1607   qed
1608 qed
1610 lemma filterlim_at_top_mult_at_top:
1611   assumes f: "LIM x F. f x :> at_top"
1612   assumes g: "LIM x F. g x :> at_top"
1613   shows "LIM x F. (f x * g x :: real) :> at_top"
1614   unfolding filterlim_at_top_gt[where c=0]
1615 proof safe
1616   fix Z :: real assume "0 < Z"
1617   from f have "eventually (\<lambda>x. 1 \<le> f x) F"
1618     unfolding filterlim_at_top by auto
1619   moreover from g have "eventually (\<lambda>x. Z \<le> g x) F"
1620     unfolding filterlim_at_top by auto
1621   ultimately show "eventually (\<lambda>x. Z \<le> f x * g x) F"
1622   proof eventually_elim
1623     fix x assume "1 \<le> f x" "Z \<le> g x"
1624     with `0 < Z` have "1 * Z \<le> f x * g x"
1625       by (intro mult_mono) (auto simp: zero_le_divide_iff)
1626     then show "Z \<le> f x * g x"
1627        by simp
1628   qed
1629 qed
1631 lemma filterlim_tendsto_pos_mult_at_bot:
1632   assumes "(f ---> c) F" "0 < (c::real)" "filterlim g at_bot F"
1633   shows "LIM x F. f x * g x :> at_bot"
1634   using filterlim_tendsto_pos_mult_at_top[OF assms(1,2), of "\<lambda>x. - g x"] assms(3)
1635   unfolding filterlim_uminus_at_bot by simp
1638   assumes f: "(f ---> c) F"
1639   assumes g: "LIM x F. g x :> at_top"
1640   shows "LIM x F. (f x + g x :: real) :> at_top"
1641   unfolding filterlim_at_top_gt[where c=0]
1642 proof safe
1643   fix Z :: real assume "0 < Z"
1644   from f have "eventually (\<lambda>x. c - 1 < f x) F"
1645     by (auto dest!: tendstoD[where e=1] elim!: eventually_elim1 simp: dist_real_def)
1646   moreover from g have "eventually (\<lambda>x. Z - (c - 1) \<le> g x) F"
1647     unfolding filterlim_at_top by auto
1648   ultimately show "eventually (\<lambda>x. Z \<le> f x + g x) F"
1649     by eventually_elim simp
1650 qed
1652 lemma LIM_at_top_divide:
1653   fixes f g :: "'a \<Rightarrow> real"
1654   assumes f: "(f ---> a) F" "0 < a"
1655   assumes g: "(g ---> 0) F" "eventually (\<lambda>x. 0 < g x) F"
1656   shows "LIM x F. f x / g x :> at_top"
1657   unfolding divide_inverse
1658   by (rule filterlim_tendsto_pos_mult_at_top[OF f]) (rule filterlim_inverse_at_top[OF g])
1661   assumes f: "LIM x F. f x :> at_top"
1662   assumes g: "LIM x F. g x :> at_top"
1663   shows "LIM x F. (f x + g x :: real) :> at_top"
1664   unfolding filterlim_at_top_gt[where c=0]
1665 proof safe
1666   fix Z :: real assume "0 < Z"
1667   from f have "eventually (\<lambda>x. 0 \<le> f x) F"
1668     unfolding filterlim_at_top by auto
1669   moreover from g have "eventually (\<lambda>x. Z \<le> g x) F"
1670     unfolding filterlim_at_top by auto
1671   ultimately show "eventually (\<lambda>x. Z \<le> f x + g x) F"
1672     by eventually_elim simp
1673 qed
1675 lemma tendsto_divide_0:
1676   fixes f :: "_ \<Rightarrow> 'a\<Colon>{real_normed_div_algebra, division_ring_inverse_zero}"
1677   assumes f: "(f ---> c) F"
1678   assumes g: "LIM x F. g x :> at_infinity"
1679   shows "((\<lambda>x. f x / g x) ---> 0) F"
1680   using tendsto_mult[OF f filterlim_compose[OF tendsto_inverse_0 g]] by (simp add: divide_inverse)
1682 lemma linear_plus_1_le_power:
1683   fixes x :: real
1684   assumes x: "0 \<le> x"
1685   shows "real n * x + 1 \<le> (x + 1) ^ n"
1686 proof (induct n)
1687   case (Suc n)
1688   have "real (Suc n) * x + 1 \<le> (x + 1) * (real n * x + 1)"
1689     by (simp add: field_simps real_of_nat_Suc mult_nonneg_nonneg x)
1690   also have "\<dots> \<le> (x + 1)^Suc n"
1691     using Suc x by (simp add: mult_left_mono)
1692   finally show ?case .
1693 qed simp
1695 lemma filterlim_realpow_sequentially_gt1:
1696   fixes x :: "'a :: real_normed_div_algebra"
1697   assumes x[arith]: "1 < norm x"
1698   shows "LIM n sequentially. x ^ n :> at_infinity"
1699 proof (intro filterlim_at_infinity[THEN iffD2] allI impI)
1700   fix y :: real assume "0 < y"
1701   have "0 < norm x - 1" by simp
1702   then obtain N::nat where "y < real N * (norm x - 1)" by (blast dest: reals_Archimedean3)
1703   also have "\<dots> \<le> real N * (norm x - 1) + 1" by simp
1704   also have "\<dots> \<le> (norm x - 1 + 1) ^ N" by (rule linear_plus_1_le_power) simp
1705   also have "\<dots> = norm x ^ N" by simp
1706   finally have "\<forall>n\<ge>N. y \<le> norm x ^ n"
1707     by (metis order_less_le_trans power_increasing order_less_imp_le x)
1708   then show "eventually (\<lambda>n. y \<le> norm (x ^ n)) sequentially"
1709     unfolding eventually_sequentially
1710     by (auto simp: norm_power)
1711 qed simp
1713 end