Theory Limits

Up to index of Isabelle/HOL

theory Limits
imports RealVector
(*  Title       : Limits.thy
Author : Brian Huffman
*)


header {* Filters and Limits *}

theory Limits
imports RealVector
begin

subsection {* Filters *}

text {*
This definition also allows non-proper filters.
*}


locale is_filter =
fixes F :: "('a => bool) => bool"
assumes True: "F (λx. True)"
assumes conj: "F (λx. P x) ==> F (λx. Q x) ==> F (λx. P x ∧ Q x)"
assumes mono: "∀x. P x --> Q x ==> F (λx. P x) ==> F (λx. Q x)"

typedef 'a filter = "{F :: ('a => bool) => bool. is_filter F}"
proof
show "(λx. True) ∈ ?filter" by (auto intro: is_filter.intro)
qed

lemma is_filter_Rep_filter: "is_filter (Rep_filter F)"
using Rep_filter [of F] by simp

lemma Abs_filter_inverse':
assumes "is_filter F" shows "Rep_filter (Abs_filter F) = F"
using assms by (simp add: Abs_filter_inverse)


subsection {* Eventually *}

definition eventually :: "('a => bool) => 'a filter => bool"
where "eventually P F <-> Rep_filter F P"

lemma eventually_Abs_filter:
assumes "is_filter F" shows "eventually P (Abs_filter F) = F P"
unfolding eventually_def using assms by (simp add: Abs_filter_inverse)

lemma filter_eq_iff:
shows "F = F' <-> (∀P. eventually P F = eventually P F')"
unfolding Rep_filter_inject [symmetric] fun_eq_iff eventually_def ..

lemma eventually_True [simp]: "eventually (λx. True) F"
unfolding eventually_def
by (rule is_filter.True [OF is_filter_Rep_filter])

lemma always_eventually: "∀x. P x ==> eventually P F"
proof -
assume "∀x. P x" hence "P = (λx. True)" by (simp add: ext)
thus "eventually P F" by simp
qed

lemma eventually_mono:
"(∀x. P x --> Q x) ==> eventually P F ==> eventually Q F"
unfolding eventually_def
by (rule is_filter.mono [OF is_filter_Rep_filter])

lemma eventually_conj:
assumes P: "eventually (λx. P x) F"
assumes Q: "eventually (λx. Q x) F"
shows "eventually (λx. P x ∧ Q x) F"
using assms unfolding eventually_def
by (rule is_filter.conj [OF is_filter_Rep_filter])

lemma eventually_Ball_finite:
assumes "finite A" and "∀y∈A. eventually (λx. P x y) net"
shows "eventually (λx. ∀y∈A. P x y) net"
using assms by (induct set: finite, simp, simp add: eventually_conj)

lemma eventually_all_finite:
fixes P :: "'a => 'b::finite => bool"
assumes "!!y. eventually (λx. P x y) net"
shows "eventually (λx. ∀y. P x y) net"
using eventually_Ball_finite [of UNIV P] assms by simp

lemma eventually_mp:
assumes "eventually (λx. P x --> Q x) F"
assumes "eventually (λx. P x) F"
shows "eventually (λx. Q x) F"
proof (rule eventually_mono)
show "∀x. (P x --> Q x) ∧ P x --> Q x" by simp
show "eventually (λx. (P x --> Q x) ∧ P x) F"
using assms by (rule eventually_conj)
qed

lemma eventually_rev_mp:
assumes "eventually (λx. P x) F"
assumes "eventually (λx. P x --> Q x) F"
shows "eventually (λx. Q x) F"
using assms(2) assms(1) by (rule eventually_mp)

lemma eventually_conj_iff:
"eventually (λx. P x ∧ Q x) F <-> eventually P F ∧ eventually Q F"
by (auto intro: eventually_conj elim: eventually_rev_mp)

lemma eventually_elim1:
assumes "eventually (λi. P i) F"
assumes "!!i. P i ==> Q i"
shows "eventually (λi. Q i) F"
using assms by (auto elim!: eventually_rev_mp)

lemma eventually_elim2:
assumes "eventually (λi. P i) F"
assumes "eventually (λi. Q i) F"
assumes "!!i. P i ==> Q i ==> R i"
shows "eventually (λi. R i) F"
using assms by (auto elim!: eventually_rev_mp)

lemma eventually_subst:
assumes "eventually (λn. P n = Q n) F"
shows "eventually P F = eventually Q F" (is "?L = ?R")
proof -
from assms have "eventually (λx. P x --> Q x) F"
and "eventually (λx. Q x --> P x) F"
by (auto elim: eventually_elim1)
then show ?thesis by (auto elim: eventually_elim2)
qed

ML {*
fun eventually_elim_tac ctxt thms thm =
let
val thy = Proof_Context.theory_of ctxt
val mp_thms = thms RL [@{thm eventually_rev_mp}]
val raw_elim_thm =
(@{thm allI} RS @{thm always_eventually})
|> fold (fn thm1 => fn thm2 => thm2 RS thm1) mp_thms
|> fold (fn _ => fn thm => @{thm impI} RS thm) thms
val cases_prop = prop_of (raw_elim_thm RS thm)
val cases = (Rule_Cases.make_common (thy, cases_prop) [(("elim", []), [])])
in
CASES cases (rtac raw_elim_thm 1) thm
end
*}


method_setup eventually_elim = {*
Scan.succeed (fn ctxt => METHOD_CASES (eventually_elim_tac ctxt))
*}
"elimination of eventually quantifiers"


subsection {* Finer-than relation *}

text {* @{term "F ≤ F'"} means that filter @{term F} is finer than
filter @{term F'}. *}


instantiation filter :: (type) complete_lattice
begin

definition le_filter_def:
"F ≤ F' <-> (∀P. eventually P F' --> eventually P F)"

definition
"(F :: 'a filter) < F' <-> F ≤ F' ∧ ¬ F' ≤ F"

definition
"top = Abs_filter (λP. ∀x. P x)"

definition
"bot = Abs_filter (λP. True)"

definition
"sup F F' = Abs_filter (λP. eventually P F ∧ eventually P F')"

definition
"inf F F' = Abs_filter
(λP. ∃Q R. eventually Q F ∧ eventually R F' ∧ (∀x. Q x ∧ R x --> P x))"


definition
"Sup S = Abs_filter (λP. ∀F∈S. eventually P F)"

definition
"Inf S = Sup {F::'a filter. ∀F'∈S. F ≤ F'}"

lemma eventually_top [simp]: "eventually P top <-> (∀x. P x)"
unfolding top_filter_def
by (rule eventually_Abs_filter, rule is_filter.intro, auto)

lemma eventually_bot [simp]: "eventually P bot"
unfolding bot_filter_def
by (subst eventually_Abs_filter, rule is_filter.intro, auto)

lemma eventually_sup:
"eventually P (sup F F') <-> eventually P F ∧ eventually P F'"
unfolding sup_filter_def
by (rule eventually_Abs_filter, rule is_filter.intro)
(auto elim!: eventually_rev_mp)

lemma eventually_inf:
"eventually P (inf F F') <->
(∃Q R. eventually Q F ∧ eventually R F' ∧ (∀x. Q x ∧ R x --> P x))"

unfolding inf_filter_def
apply (rule eventually_Abs_filter, rule is_filter.intro)
apply (fast intro: eventually_True)
apply clarify
apply (intro exI conjI)
apply (erule (1) eventually_conj)
apply (erule (1) eventually_conj)
apply simp
apply auto
done

lemma eventually_Sup:
"eventually P (Sup S) <-> (∀F∈S. eventually P F)"
unfolding Sup_filter_def
apply (rule eventually_Abs_filter, rule is_filter.intro)
apply (auto intro: eventually_conj elim!: eventually_rev_mp)
done

instance proof
fix F F' F'' :: "'a filter" and S :: "'a filter set"
{ show "F < F' <-> F ≤ F' ∧ ¬ F' ≤ F"
by (rule less_filter_def) }
{ show "F ≤ F"
unfolding le_filter_def by simp }
{ assume "F ≤ F'" and "F' ≤ F''" thus "F ≤ F''"
unfolding le_filter_def by simp }
{ assume "F ≤ F'" and "F' ≤ F" thus "F = F'"
unfolding le_filter_def filter_eq_iff by fast }
{ show "F ≤ top"
unfolding le_filter_def eventually_top by (simp add: always_eventually) }
{ show "bot ≤ F"
unfolding le_filter_def by simp }
{ show "F ≤ sup F F'" and "F' ≤ sup F F'"
unfolding le_filter_def eventually_sup by simp_all }
{ assume "F ≤ F''" and "F' ≤ F''" thus "sup F F' ≤ F''"
unfolding le_filter_def eventually_sup by simp }
{ show "inf F F' ≤ F" and "inf F F' ≤ F'"
unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
{ assume "F ≤ F'" and "F ≤ F''" thus "F ≤ inf F' F''"
unfolding le_filter_def eventually_inf
by (auto elim!: eventually_mono intro: eventually_conj) }
{ assume "F ∈ S" thus "F ≤ Sup S"
unfolding le_filter_def eventually_Sup by simp }
{ assume "!!F. F ∈ S ==> F ≤ F'" thus "Sup S ≤ F'"
unfolding le_filter_def eventually_Sup by simp }
{ assume "F'' ∈ S" thus "Inf S ≤ F''"
unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
{ assume "!!F'. F' ∈ S ==> F ≤ F'" thus "F ≤ Inf S"
unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
qed

end

lemma filter_leD:
"F ≤ F' ==> eventually P F' ==> eventually P F"
unfolding le_filter_def by simp

lemma filter_leI:
"(!!P. eventually P F' ==> eventually P F) ==> F ≤ F'"
unfolding le_filter_def by simp

lemma eventually_False:
"eventually (λx. False) F <-> F = bot"
unfolding filter_eq_iff by (auto elim: eventually_rev_mp)

abbreviation (input) trivial_limit :: "'a filter => bool"
where "trivial_limit F ≡ F = bot"

lemma trivial_limit_def: "trivial_limit F <-> eventually (λx. False) F"
by (rule eventually_False [symmetric])


subsection {* Map function for filters *}

definition filtermap :: "('a => 'b) => 'a filter => 'b filter"
where "filtermap f F = Abs_filter (λP. eventually (λx. P (f x)) F)"

lemma eventually_filtermap:
"eventually P (filtermap f F) = eventually (λx. P (f x)) F"
unfolding filtermap_def
apply (rule eventually_Abs_filter)
apply (rule is_filter.intro)
apply (auto elim!: eventually_rev_mp)
done

lemma filtermap_ident: "filtermap (λx. x) F = F"
by (simp add: filter_eq_iff eventually_filtermap)

lemma filtermap_filtermap:
"filtermap f (filtermap g F) = filtermap (λx. f (g x)) F"
by (simp add: filter_eq_iff eventually_filtermap)

lemma filtermap_mono: "F ≤ F' ==> filtermap f F ≤ filtermap f F'"
unfolding le_filter_def eventually_filtermap by simp

lemma filtermap_bot [simp]: "filtermap f bot = bot"
by (simp add: filter_eq_iff eventually_filtermap)

lemma filtermap_sup: "filtermap f (sup F1 F2) = sup (filtermap f F1) (filtermap f F2)"
by (auto simp: filter_eq_iff eventually_filtermap eventually_sup)

subsection {* Order filters *}

definition at_top :: "('a::order) filter"
where "at_top = Abs_filter (λP. ∃k. ∀n≥k. P n)"

lemma eventually_at_top_linorder: "eventually P at_top <-> (∃N::'a::linorder. ∀n≥N. P n)"
unfolding at_top_def
proof (rule eventually_Abs_filter, rule is_filter.intro)
fix P Q :: "'a => bool"
assume "∃i. ∀n≥i. P n" and "∃j. ∀n≥j. Q n"
then obtain i j where "∀n≥i. P n" and "∀n≥j. Q n" by auto
then have "∀n≥max i j. P n ∧ Q n" by simp
then show "∃k. ∀n≥k. P n ∧ Q n" ..
qed auto

lemma eventually_ge_at_top:
"eventually (λx. (c::_::linorder) ≤ x) at_top"
unfolding eventually_at_top_linorder by auto

lemma eventually_at_top_dense: "eventually P at_top <-> (∃N::'a::dense_linorder. ∀n>N. P n)"
unfolding eventually_at_top_linorder
proof safe
fix N assume "∀n≥N. P n" then show "∃N. ∀n>N. P n" by (auto intro!: exI[of _ N])
next
fix N assume "∀n>N. P n"
moreover from gt_ex[of N] guess y ..
ultimately show "∃N. ∀n≥N. P n" by (auto intro!: exI[of _ y])
qed

lemma eventually_gt_at_top:
"eventually (λx. (c::_::dense_linorder) < x) at_top"
unfolding eventually_at_top_dense by auto

definition at_bot :: "('a::order) filter"
where "at_bot = Abs_filter (λP. ∃k. ∀n≤k. P n)"

lemma eventually_at_bot_linorder:
fixes P :: "'a::linorder => bool" shows "eventually P at_bot <-> (∃N. ∀n≤N. P n)"
unfolding at_bot_def
proof (rule eventually_Abs_filter, rule is_filter.intro)
fix P Q :: "'a => bool"
assume "∃i. ∀n≤i. P n" and "∃j. ∀n≤j. Q n"
then obtain i j where "∀n≤i. P n" and "∀n≤j. Q n" by auto
then have "∀n≤min i j. P n ∧ Q n" by simp
then show "∃k. ∀n≤k. P n ∧ Q n" ..
qed auto

lemma eventually_le_at_bot:
"eventually (λx. x ≤ (c::_::linorder)) at_bot"
unfolding eventually_at_bot_linorder by auto

lemma eventually_at_bot_dense:
fixes P :: "'a::dense_linorder => bool" shows "eventually P at_bot <-> (∃N. ∀n<N. P n)"
unfolding eventually_at_bot_linorder
proof safe
fix N assume "∀n≤N. P n" then show "∃N. ∀n<N. P n" by (auto intro!: exI[of _ N])
next
fix N assume "∀n<N. P n"
moreover from lt_ex[of N] guess y ..
ultimately show "∃N. ∀n≤N. P n" by (auto intro!: exI[of _ y])
qed

lemma eventually_gt_at_bot:
"eventually (λx. x < (c::_::dense_linorder)) at_bot"
unfolding eventually_at_bot_dense by auto

subsection {* Sequentially *}

abbreviation sequentially :: "nat filter"
where "sequentially == at_top"

lemma sequentially_def: "sequentially = Abs_filter (λP. ∃k. ∀n≥k. P n)"
unfolding at_top_def by simp

lemma eventually_sequentially:
"eventually P sequentially <-> (∃N. ∀n≥N. P n)"
by (rule eventually_at_top_linorder)

lemma sequentially_bot [simp, intro]: "sequentially ≠ bot"
unfolding filter_eq_iff eventually_sequentially by auto

lemmas trivial_limit_sequentially = sequentially_bot

lemma eventually_False_sequentially [simp]:
"¬ eventually (λn. False) sequentially"
by (simp add: eventually_False)

lemma le_sequentially:
"F ≤ sequentially <-> (∀N. eventually (λn. N ≤ n) F)"
unfolding le_filter_def eventually_sequentially
by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)

lemma eventually_sequentiallyI:
assumes "!!x. c ≤ x ==> P x"
shows "eventually P sequentially"
using assms by (auto simp: eventually_sequentially)


subsection {* Standard filters *}

definition within :: "'a filter => 'a set => 'a filter" (infixr "within" 70)
where "F within S = Abs_filter (λP. eventually (λx. x ∈ S --> P x) F)"

definition (in topological_space) nhds :: "'a => 'a filter"
where "nhds a = Abs_filter (λP. ∃S. open S ∧ a ∈ S ∧ (∀x∈S. P x))"

definition (in topological_space) at :: "'a => 'a filter"
where "at a = nhds a within - {a}"

abbreviation at_right :: "'a::{topological_space, order} => 'a filter" where
"at_right x ≡ at x within {x <..}"

abbreviation at_left :: "'a::{topological_space, order} => 'a filter" where
"at_left x ≡ at x within {..< x}"

definition at_infinity :: "'a::real_normed_vector filter" where
"at_infinity = Abs_filter (λP. ∃r. ∀x. r ≤ norm x --> P x)"

lemma eventually_within:
"eventually P (F within S) = eventually (λx. x ∈ S --> P x) F"
unfolding within_def
by (rule eventually_Abs_filter, rule is_filter.intro)
(auto elim!: eventually_rev_mp)

lemma within_UNIV [simp]: "F within UNIV = F"
unfolding filter_eq_iff eventually_within by simp

lemma within_empty [simp]: "F within {} = bot"
unfolding filter_eq_iff eventually_within by simp

lemma within_within_eq: "(F within S) within T = F within (S ∩ T)"
by (auto simp: filter_eq_iff eventually_within elim: eventually_elim1)

lemma at_within_eq: "at x within T = nhds x within (T - {x})"
unfolding at_def within_within_eq by (simp add: ac_simps Diff_eq)

lemma within_le: "F within S ≤ F"
unfolding le_filter_def eventually_within by (auto elim: eventually_elim1)

lemma le_withinI: "F ≤ F' ==> eventually (λx. x ∈ S) F ==> F ≤ F' within S"
unfolding le_filter_def eventually_within by (auto elim: eventually_elim2)

lemma le_within_iff: "eventually (λx. x ∈ S) F ==> F ≤ F' within S <-> F ≤ F'"
by (blast intro: within_le le_withinI order_trans)

lemma eventually_nhds:
"eventually P (nhds a) <-> (∃S. open S ∧ a ∈ S ∧ (∀x∈S. P x))"
unfolding nhds_def
proof (rule eventually_Abs_filter, rule is_filter.intro)
have "open UNIV ∧ a ∈ UNIV ∧ (∀x∈UNIV. True)" by simp
thus "∃S. open S ∧ a ∈ S ∧ (∀x∈S. True)" ..
next
fix P Q
assume "∃S. open S ∧ a ∈ S ∧ (∀x∈S. P x)"
and "∃T. open T ∧ a ∈ T ∧ (∀x∈T. Q x)"
then obtain S T where
"open S ∧ a ∈ S ∧ (∀x∈S. P x)"
"open T ∧ a ∈ T ∧ (∀x∈T. Q x)" by auto
hence "open (S ∩ T) ∧ a ∈ S ∩ T ∧ (∀x∈(S ∩ T). P x ∧ Q x)"
by (simp add: open_Int)
thus "∃S. open S ∧ a ∈ S ∧ (∀x∈S. P x ∧ Q x)" ..
qed auto

lemma eventually_nhds_metric:
"eventually P (nhds a) <-> (∃d>0. ∀x. dist x a < d --> P x)"
unfolding eventually_nhds open_dist
apply safe
apply fast
apply (rule_tac x="{x. dist x a < d}" in exI, simp)
apply clarsimp
apply (rule_tac x="d - dist x a" in exI, clarsimp)
apply (simp only: less_diff_eq)
apply (erule le_less_trans [OF dist_triangle])
done

lemma nhds_neq_bot [simp]: "nhds a ≠ bot"
unfolding trivial_limit_def eventually_nhds by simp

lemma eventually_at_topological:
"eventually P (at a) <-> (∃S. open S ∧ a ∈ S ∧ (∀x∈S. x ≠ a --> P x))"
unfolding at_def eventually_within eventually_nhds by simp

lemma eventually_at:
fixes a :: "'a::metric_space"
shows "eventually P (at a) <-> (∃d>0. ∀x. x ≠ a ∧ dist x a < d --> P x)"
unfolding at_def eventually_within eventually_nhds_metric by auto

lemma eventually_within_less: (* COPY FROM Topo/eventually_within *)
"eventually P (at a within S) <-> (∃d>0. ∀x∈S. 0 < dist x a ∧ dist x a < d --> P x)"
unfolding eventually_within eventually_at dist_nz by auto

lemma eventually_within_le: (* COPY FROM Topo/eventually_within_le *)
"eventually P (at a within S) <-> (∃d>0. ∀x∈S. 0 < dist x a ∧ dist x a <= d --> P x)"
unfolding eventually_within_less by auto (metis dense order_le_less_trans)

lemma at_eq_bot_iff: "at a = bot <-> open {a}"
unfolding trivial_limit_def eventually_at_topological
by (safe, case_tac "S = {a}", simp, fast, fast)

lemma at_neq_bot [simp]: "at (a::'a::perfect_space) ≠ bot"
by (simp add: at_eq_bot_iff not_open_singleton)

lemma trivial_limit_at_left_real [simp]: (* maybe generalize type *)
"¬ trivial_limit (at_left (x::real))"
unfolding trivial_limit_def eventually_within_le
apply clarsimp
apply (rule_tac x="x - d/2" in bexI)
apply (auto simp: dist_real_def)
done

lemma trivial_limit_at_right_real [simp]: (* maybe generalize type *)
"¬ trivial_limit (at_right (x::real))"
unfolding trivial_limit_def eventually_within_le
apply clarsimp
apply (rule_tac x="x + d/2" in bexI)
apply (auto simp: dist_real_def)
done

lemma eventually_at_infinity:
"eventually P at_infinity <-> (∃b. ∀x. b ≤ norm x --> P x)"
unfolding at_infinity_def
proof (rule eventually_Abs_filter, rule is_filter.intro)
fix P Q :: "'a => bool"
assume "∃r. ∀x. r ≤ norm x --> P x" and "∃s. ∀x. s ≤ norm x --> Q x"
then obtain r s where
"∀x. r ≤ norm x --> P x" and "∀x. s ≤ norm x --> Q x" by auto
then have "∀x. max r s ≤ norm x --> P x ∧ Q x" by simp
then show "∃r. ∀x. r ≤ norm x --> P x ∧ Q x" ..
qed auto

lemma at_infinity_eq_at_top_bot:
"(at_infinity :: real filter) = sup at_top at_bot"
unfolding sup_filter_def at_infinity_def eventually_at_top_linorder eventually_at_bot_linorder
proof (intro arg_cong[where f=Abs_filter] ext iffI)
fix P :: "real => bool" assume "∃r. ∀x. r ≤ norm x --> P x"
then guess r ..
then have "(∀x≥r. P x) ∧ (∀x≤-r. P x)" by auto
then show "(∃r. ∀x≥r. P x) ∧ (∃r. ∀x≤r. P x)" by auto
next
fix P :: "real => bool" assume "(∃r. ∀x≥r. P x) ∧ (∃r. ∀x≤r. P x)"
then obtain p q where "∀x≥p. P x" "∀x≤q. P x" by auto
then show "∃r. ∀x. r ≤ norm x --> P x"
by (intro exI[of _ "max p (-q)"])
(auto simp: abs_real_def)
qed

lemma at_top_le_at_infinity:
"at_top ≤ (at_infinity :: real filter)"
unfolding at_infinity_eq_at_top_bot by simp

lemma at_bot_le_at_infinity:
"at_bot ≤ (at_infinity :: real filter)"
unfolding at_infinity_eq_at_top_bot by simp

subsection {* Boundedness *}

definition Bfun :: "('a => 'b::real_normed_vector) => 'a filter => bool"
where "Bfun f F = (∃K>0. eventually (λx. norm (f x) ≤ K) F)"

lemma BfunI:
assumes K: "eventually (λx. norm (f x) ≤ K) F" shows "Bfun f F"
unfolding Bfun_def
proof (intro exI conjI allI)
show "0 < max K 1" by simp
next
show "eventually (λx. norm (f x) ≤ max K 1) F"
using K by (rule eventually_elim1, simp)
qed

lemma BfunE:
assumes "Bfun f F"
obtains B where "0 < B" and "eventually (λx. norm (f x) ≤ B) F"
using assms unfolding Bfun_def by fast


subsection {* Convergence to Zero *}

definition Zfun :: "('a => 'b::real_normed_vector) => 'a filter => bool"
where "Zfun f F = (∀r>0. eventually (λx. norm (f x) < r) F)"

lemma ZfunI:
"(!!r. 0 < r ==> eventually (λx. norm (f x) < r) F) ==> Zfun f F"
unfolding Zfun_def by simp

lemma ZfunD:
"[|Zfun f F; 0 < r|] ==> eventually (λx. norm (f x) < r) F"
unfolding Zfun_def by simp

lemma Zfun_ssubst:
"eventually (λx. f x = g x) F ==> Zfun g F ==> Zfun f F"
unfolding Zfun_def by (auto elim!: eventually_rev_mp)

lemma Zfun_zero: "Zfun (λx. 0) F"
unfolding Zfun_def by simp

lemma Zfun_norm_iff: "Zfun (λx. norm (f x)) F = Zfun (λx. f x) F"
unfolding Zfun_def by simp

lemma Zfun_imp_Zfun:
assumes f: "Zfun f F"
assumes g: "eventually (λx. norm (g x) ≤ norm (f x) * K) F"
shows "Zfun (λx. g x) F"
proof (cases)
assume K: "0 < K"
show ?thesis
proof (rule ZfunI)
fix r::real assume "0 < r"
hence "0 < r / K"
using K by (rule divide_pos_pos)
then have "eventually (λx. norm (f x) < r / K) F"
using ZfunD [OF f] by fast
with g show "eventually (λx. norm (g x) < r) F"
proof eventually_elim
case (elim x)
hence "norm (f x) * K < r"
by (simp add: pos_less_divide_eq K)
thus ?case
by (simp add: order_le_less_trans [OF elim(1)])
qed
qed
next
assume "¬ 0 < K"
hence K: "K ≤ 0" by (simp only: not_less)
show ?thesis
proof (rule ZfunI)
fix r :: real
assume "0 < r"
from g show "eventually (λx. norm (g x) < r) F"
proof eventually_elim
case (elim x)
also have "norm (f x) * K ≤ norm (f x) * 0"
using K norm_ge_zero by (rule mult_left_mono)
finally show ?case
using `0 < r` by simp
qed
qed
qed

lemma Zfun_le: "[|Zfun g F; ∀x. norm (f x) ≤ norm (g x)|] ==> Zfun f F"
by (erule_tac K="1" in Zfun_imp_Zfun, simp)

lemma Zfun_add:
assumes f: "Zfun f F" and g: "Zfun g F"
shows "Zfun (λx. f x + g x) F"
proof (rule ZfunI)
fix r::real assume "0 < r"
hence r: "0 < r / 2" by simp
have "eventually (λx. norm (f x) < r/2) F"
using f r by (rule ZfunD)
moreover
have "eventually (λx. norm (g x) < r/2) F"
using g r by (rule ZfunD)
ultimately
show "eventually (λx. norm (f x + g x) < r) F"
proof eventually_elim
case (elim x)
have "norm (f x + g x) ≤ norm (f x) + norm (g x)"
by (rule norm_triangle_ineq)
also have "… < r/2 + r/2"
using elim by (rule add_strict_mono)
finally show ?case
by simp
qed
qed

lemma Zfun_minus: "Zfun f F ==> Zfun (λx. - f x) F"
unfolding Zfun_def by simp

lemma Zfun_diff: "[|Zfun f F; Zfun g F|] ==> Zfun (λx. f x - g x) F"
by (simp only: diff_minus Zfun_add Zfun_minus)

lemma (in bounded_linear) Zfun:
assumes g: "Zfun g F"
shows "Zfun (λx. f (g x)) F"
proof -
obtain K where "!!x. norm (f x) ≤ norm x * K"
using bounded by fast
then have "eventually (λx. norm (f (g x)) ≤ norm (g x) * K) F"
by simp
with g show ?thesis
by (rule Zfun_imp_Zfun)
qed

lemma (in bounded_bilinear) Zfun:
assumes f: "Zfun f F"
assumes g: "Zfun g F"
shows "Zfun (λx. f x ** g x) F"
proof (rule ZfunI)
fix r::real assume r: "0 < r"
obtain K where K: "0 < K"
and norm_le: "!!x y. norm (x ** y) ≤ norm x * norm y * K"
using pos_bounded by fast
from K have K': "0 < inverse K"
by (rule positive_imp_inverse_positive)
have "eventually (λx. norm (f x) < r) F"
using f r by (rule ZfunD)
moreover
have "eventually (λx. norm (g x) < inverse K) F"
using g K' by (rule ZfunD)
ultimately
show "eventually (λx. norm (f x ** g x) < r) F"
proof eventually_elim
case (elim x)
have "norm (f x ** g x) ≤ norm (f x) * norm (g x) * K"
by (rule norm_le)
also have "norm (f x) * norm (g x) * K < r * inverse K * K"
by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero elim K)
also from K have "r * inverse K * K = r"
by simp
finally show ?case .
qed
qed

lemma (in bounded_bilinear) Zfun_left:
"Zfun f F ==> Zfun (λx. f x ** a) F"
by (rule bounded_linear_left [THEN bounded_linear.Zfun])

lemma (in bounded_bilinear) Zfun_right:
"Zfun f F ==> Zfun (λx. a ** f x) F"
by (rule bounded_linear_right [THEN bounded_linear.Zfun])

lemmas Zfun_mult = bounded_bilinear.Zfun [OF bounded_bilinear_mult]
lemmas Zfun_mult_right = bounded_bilinear.Zfun_right [OF bounded_bilinear_mult]
lemmas Zfun_mult_left = bounded_bilinear.Zfun_left [OF bounded_bilinear_mult]


subsection {* Limits *}

definition filterlim :: "('a => 'b) => 'b filter => 'a filter => bool" where
"filterlim f F2 F1 <-> filtermap f F1 ≤ F2"

syntax
"_LIM" :: "pttrns => 'a => 'b => 'a => bool" ("(3LIM (_)/ (_)./ (_) :> (_))" [1000, 10, 0, 10] 10)

translations
"LIM x F1. f :> F2" == "CONST filterlim (%x. f) F2 F1"

lemma filterlim_iff:
"(LIM x F1. f x :> F2) <-> (∀P. eventually P F2 --> eventually (λx. P (f x)) F1)"
unfolding filterlim_def le_filter_def eventually_filtermap ..

lemma filterlim_compose:
"filterlim g F3 F2 ==> filterlim f F2 F1 ==> filterlim (λx. g (f x)) F3 F1"
unfolding filterlim_def filtermap_filtermap[symmetric] by (metis filtermap_mono order_trans)

lemma filterlim_mono:
"filterlim f F2 F1 ==> F2 ≤ F2' ==> F1' ≤ F1 ==> filterlim f F2' F1'"
unfolding filterlim_def by (metis filtermap_mono order_trans)

lemma filterlim_ident: "LIM x F. x :> F"
by (simp add: filterlim_def filtermap_ident)

lemma filterlim_cong:
"F1 = F1' ==> F2 = F2' ==> eventually (λx. f x = g x) F2 ==> filterlim f F1 F2 = filterlim g F1' F2'"
by (auto simp: filterlim_def le_filter_def eventually_filtermap elim: eventually_elim2)

lemma filterlim_within:
"(LIM x F1. f x :> F2 within S) <-> (eventually (λx. f x ∈ S) F1 ∧ (LIM x F1. f x :> F2))"
unfolding filterlim_def
proof safe
assume "filtermap f F1 ≤ F2 within S" then show "eventually (λx. f x ∈ S) F1"
by (auto simp: le_filter_def eventually_filtermap eventually_within elim!: allE[of _ "λx. x ∈ S"])
qed (auto intro: within_le order_trans simp: le_within_iff eventually_filtermap)

lemma filterlim_filtermap: "filterlim f F1 (filtermap g F2) = filterlim (λx. f (g x)) F1 F2"
unfolding filterlim_def filtermap_filtermap ..

lemma filterlim_sup:
"filterlim f F F1 ==> filterlim f F F2 ==> filterlim f F (sup F1 F2)"
unfolding filterlim_def filtermap_sup by auto

lemma filterlim_Suc: "filterlim Suc sequentially sequentially"
by (simp add: filterlim_iff eventually_sequentially) (metis le_Suc_eq)

abbreviation (in topological_space)
tendsto :: "('b => 'a) => 'a => 'b filter => bool" (infixr "--->" 55) where
"(f ---> l) F ≡ filterlim f (nhds l) F"

ML {*
structure Tendsto_Intros = Named_Thms
(
val name = @{binding tendsto_intros}
val description = "introduction rules for tendsto"
)
*}


setup Tendsto_Intros.setup

lemma tendsto_def: "(f ---> l) F <-> (∀S. open S --> l ∈ S --> eventually (λx. f x ∈ S) F)"
unfolding filterlim_def
proof safe
fix S assume "open S" "l ∈ S" "filtermap f F ≤ nhds l"
then show "eventually (λx. f x ∈ S) F"
unfolding eventually_nhds eventually_filtermap le_filter_def
by (auto elim!: allE[of _ "λx. x ∈ S"] eventually_rev_mp)
qed (auto elim!: eventually_rev_mp simp: eventually_nhds eventually_filtermap le_filter_def)

lemma filterlim_at:
"(LIM x F. f x :> at b) <-> (eventually (λx. f x ≠ b) F ∧ (f ---> b) F)"
by (simp add: at_def filterlim_within)

lemma tendsto_mono: "F ≤ F' ==> (f ---> l) F' ==> (f ---> l) F"
unfolding tendsto_def le_filter_def by fast

lemma topological_tendstoI:
"(!!S. open S ==> l ∈ S ==> eventually (λx. f x ∈ S) F)
==> (f ---> l) F"

unfolding tendsto_def by auto

lemma topological_tendstoD:
"(f ---> l) F ==> open S ==> l ∈ S ==> eventually (λx. f x ∈ S) F"
unfolding tendsto_def by auto

lemma tendstoI:
assumes "!!e. 0 < e ==> eventually (λx. dist (f x) l < e) F"
shows "(f ---> l) F"
apply (rule topological_tendstoI)
apply (simp add: open_dist)
apply (drule (1) bspec, clarify)
apply (drule assms)
apply (erule eventually_elim1, simp)
done

lemma tendstoD:
"(f ---> l) F ==> 0 < e ==> eventually (λx. dist (f x) l < e) F"
apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
apply (clarsimp simp add: open_dist)
apply (rule_tac x="e - dist x l" in exI, clarsimp)
apply (simp only: less_diff_eq)
apply (erule le_less_trans [OF dist_triangle])
apply simp
apply simp
done

lemma tendsto_iff:
"(f ---> l) F <-> (∀e>0. eventually (λx. dist (f x) l < e) F)"
using tendstoI tendstoD by fast

lemma tendsto_Zfun_iff: "(f ---> a) F = Zfun (λx. f x - a) F"
by (simp only: tendsto_iff Zfun_def dist_norm)

lemma tendsto_bot [simp]: "(f ---> a) bot"
unfolding tendsto_def by simp

lemma tendsto_ident_at [tendsto_intros]: "((λx. x) ---> a) (at a)"
unfolding tendsto_def eventually_at_topological by auto

lemma tendsto_ident_at_within [tendsto_intros]:
"((λx. x) ---> a) (at a within S)"
unfolding tendsto_def eventually_within eventually_at_topological by auto

lemma tendsto_const [tendsto_intros]: "((λx. k) ---> k) F"
by (simp add: tendsto_def)

lemma tendsto_unique:
fixes f :: "'a => 'b::t2_space"
assumes "¬ trivial_limit F" and "(f ---> a) F" and "(f ---> b) F"
shows "a = b"
proof (rule ccontr)
assume "a ≠ b"
obtain U V where "open U" "open V" "a ∈ U" "b ∈ V" "U ∩ V = {}"
using hausdorff [OF `a ≠ b`] by fast
have "eventually (λx. f x ∈ U) F"
using `(f ---> a) F` `open U` `a ∈ U` by (rule topological_tendstoD)
moreover
have "eventually (λx. f x ∈ V) F"
using `(f ---> b) F` `open V` `b ∈ V` by (rule topological_tendstoD)
ultimately
have "eventually (λx. False) F"
proof eventually_elim
case (elim x)
hence "f x ∈ U ∩ V" by simp
with `U ∩ V = {}` show ?case by simp
qed
with `¬ trivial_limit F` show "False"
by (simp add: trivial_limit_def)
qed

lemma tendsto_const_iff:
fixes a b :: "'a::t2_space"
assumes "¬ trivial_limit F" shows "((λx. a) ---> b) F <-> a = b"
by (safe intro!: tendsto_const tendsto_unique [OF assms tendsto_const])

lemma tendsto_at_iff_tendsto_nhds:
"(g ---> g l) (at l) <-> (g ---> g l) (nhds l)"
unfolding tendsto_def at_def eventually_within
by (intro ext all_cong imp_cong) (auto elim!: eventually_elim1)

lemma tendsto_compose:
"(g ---> g l) (at l) ==> (f ---> l) F ==> ((λx. g (f x)) ---> g l) F"
unfolding tendsto_at_iff_tendsto_nhds by (rule filterlim_compose[of g])

lemma tendsto_compose_eventually:
"(g ---> m) (at l) ==> (f ---> l) F ==> eventually (λx. f x ≠ l) F ==> ((λx. g (f x)) ---> m) F"
by (rule filterlim_compose[of g _ "at l"]) (auto simp add: filterlim_at)

lemma metric_tendsto_imp_tendsto:
assumes f: "(f ---> a) F"
assumes le: "eventually (λx. dist (g x) b ≤ dist (f x) a) F"
shows "(g ---> b) F"
proof (rule tendstoI)
fix e :: real assume "0 < e"
with f have "eventually (λx. dist (f x) a < e) F" by (rule tendstoD)
with le show "eventually (λx. dist (g x) b < e) F"
using le_less_trans by (rule eventually_elim2)
qed

subsubsection {* Distance and norms *}

lemma tendsto_dist [tendsto_intros]:
assumes f: "(f ---> l) F" and g: "(g ---> m) F"
shows "((λx. dist (f x) (g x)) ---> dist l m) F"
proof (rule tendstoI)
fix e :: real assume "0 < e"
hence e2: "0 < e/2" by simp
from tendstoD [OF f e2] tendstoD [OF g e2]
show "eventually (λx. dist (dist (f x) (g x)) (dist l m) < e) F"
proof (eventually_elim)
case (elim x)
then show "dist (dist (f x) (g x)) (dist l m) < e"
unfolding dist_real_def
using dist_triangle2 [of "f x" "g x" "l"]
using dist_triangle2 [of "g x" "l" "m"]
using dist_triangle3 [of "l" "m" "f x"]
using dist_triangle [of "f x" "m" "g x"]
by arith
qed
qed

lemma norm_conv_dist: "norm x = dist x 0"
unfolding dist_norm by simp

lemma tendsto_norm [tendsto_intros]:
"(f ---> a) F ==> ((λx. norm (f x)) ---> norm a) F"
unfolding norm_conv_dist by (intro tendsto_intros)

lemma tendsto_norm_zero:
"(f ---> 0) F ==> ((λx. norm (f x)) ---> 0) F"
by (drule tendsto_norm, simp)

lemma tendsto_norm_zero_cancel:
"((λx. norm (f x)) ---> 0) F ==> (f ---> 0) F"
unfolding tendsto_iff dist_norm by simp

lemma tendsto_norm_zero_iff:
"((λx. norm (f x)) ---> 0) F <-> (f ---> 0) F"
unfolding tendsto_iff dist_norm by simp

lemma tendsto_rabs [tendsto_intros]:
"(f ---> (l::real)) F ==> ((λx. ¦f x¦) ---> ¦l¦) F"
by (fold real_norm_def, rule tendsto_norm)

lemma tendsto_rabs_zero:
"(f ---> (0::real)) F ==> ((λx. ¦f x¦) ---> 0) F"
by (fold real_norm_def, rule tendsto_norm_zero)

lemma tendsto_rabs_zero_cancel:
"((λx. ¦f x¦) ---> (0::real)) F ==> (f ---> 0) F"
by (fold real_norm_def, rule tendsto_norm_zero_cancel)

lemma tendsto_rabs_zero_iff:
"((λx. ¦f x¦) ---> (0::real)) F <-> (f ---> 0) F"
by (fold real_norm_def, rule tendsto_norm_zero_iff)

subsubsection {* Addition and subtraction *}

lemma tendsto_add [tendsto_intros]:
fixes a b :: "'a::real_normed_vector"
shows "[|(f ---> a) F; (g ---> b) F|] ==> ((λx. f x + g x) ---> a + b) F"
by (simp only: tendsto_Zfun_iff add_diff_add Zfun_add)

lemma tendsto_add_zero:
fixes f g :: "'a::type => 'b::real_normed_vector"
shows "[|(f ---> 0) F; (g ---> 0) F|] ==> ((λx. f x + g x) ---> 0) F"
by (drule (1) tendsto_add, simp)

lemma tendsto_minus [tendsto_intros]:
fixes a :: "'a::real_normed_vector"
shows "(f ---> a) F ==> ((λx. - f x) ---> - a) F"
by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)

lemma tendsto_minus_cancel:
fixes a :: "'a::real_normed_vector"
shows "((λx. - f x) ---> - a) F ==> (f ---> a) F"
by (drule tendsto_minus, simp)

lemma tendsto_minus_cancel_left:
"(f ---> - (y::_::real_normed_vector)) F <-> ((λx. - f x) ---> y) F"
using tendsto_minus_cancel[of f "- y" F] tendsto_minus[of f "- y" F]
by auto

lemma tendsto_diff [tendsto_intros]:
fixes a b :: "'a::real_normed_vector"
shows "[|(f ---> a) F; (g ---> b) F|] ==> ((λx. f x - g x) ---> a - b) F"
by (simp add: diff_minus tendsto_add tendsto_minus)

lemma tendsto_setsum [tendsto_intros]:
fixes f :: "'a => 'b => 'c::real_normed_vector"
assumes "!!i. i ∈ S ==> (f i ---> a i) F"
shows "((λx. ∑i∈S. f i x) ---> (∑i∈S. a i)) F"
proof (cases "finite S")
assume "finite S" thus ?thesis using assms
by (induct, simp add: tendsto_const, simp add: tendsto_add)
next
assume "¬ finite S" thus ?thesis
by (simp add: tendsto_const)
qed

lemma real_tendsto_sandwich:
fixes f g h :: "'a => real"
assumes ev: "eventually (λn. f n ≤ g n) net" "eventually (λn. g n ≤ h n) net"
assumes lim: "(f ---> c) net" "(h ---> c) net"
shows "(g ---> c) net"
proof -
have "((λn. g n - f n) ---> 0) net"
proof (rule metric_tendsto_imp_tendsto)
show "eventually (λn. dist (g n - f n) 0 ≤ dist (h n - f n) 0) net"
using ev by (rule eventually_elim2) (simp add: dist_real_def)
show "((λn. h n - f n) ---> 0) net"
using tendsto_diff[OF lim(2,1)] by simp
qed
from tendsto_add[OF this lim(1)] show ?thesis by simp
qed

subsubsection {* Linear operators and multiplication *}

lemma (in bounded_linear) tendsto:
"(g ---> a) F ==> ((λx. f (g x)) ---> f a) F"
by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)

lemma (in bounded_linear) tendsto_zero:
"(g ---> 0) F ==> ((λx. f (g x)) ---> 0) F"
by (drule tendsto, simp only: zero)

lemma (in bounded_bilinear) tendsto:
"[|(f ---> a) F; (g ---> b) F|] ==> ((λx. f x ** g x) ---> a ** b) F"
by (simp only: tendsto_Zfun_iff prod_diff_prod
Zfun_add Zfun Zfun_left Zfun_right)

lemma (in bounded_bilinear) tendsto_zero:
assumes f: "(f ---> 0) F"
assumes g: "(g ---> 0) F"
shows "((λx. f x ** g x) ---> 0) F"
using tendsto [OF f g] by (simp add: zero_left)

lemma (in bounded_bilinear) tendsto_left_zero:
"(f ---> 0) F ==> ((λx. f x ** c) ---> 0) F"
by (rule bounded_linear.tendsto_zero [OF bounded_linear_left])

lemma (in bounded_bilinear) tendsto_right_zero:
"(f ---> 0) F ==> ((λx. c ** f x) ---> 0) F"
by (rule bounded_linear.tendsto_zero [OF bounded_linear_right])

lemmas tendsto_of_real [tendsto_intros] =
bounded_linear.tendsto [OF bounded_linear_of_real]

lemmas tendsto_scaleR [tendsto_intros] =
bounded_bilinear.tendsto [OF bounded_bilinear_scaleR]

lemmas tendsto_mult [tendsto_intros] =
bounded_bilinear.tendsto [OF bounded_bilinear_mult]

lemmas tendsto_mult_zero =
bounded_bilinear.tendsto_zero [OF bounded_bilinear_mult]

lemmas tendsto_mult_left_zero =
bounded_bilinear.tendsto_left_zero [OF bounded_bilinear_mult]

lemmas tendsto_mult_right_zero =
bounded_bilinear.tendsto_right_zero [OF bounded_bilinear_mult]

lemma tendsto_power [tendsto_intros]:
fixes f :: "'a => 'b::{power,real_normed_algebra}"
shows "(f ---> a) F ==> ((λx. f x ^ n) ---> a ^ n) F"
by (induct n) (simp_all add: tendsto_const tendsto_mult)

lemma tendsto_setprod [tendsto_intros]:
fixes f :: "'a => 'b => 'c::{real_normed_algebra,comm_ring_1}"
assumes "!!i. i ∈ S ==> (f i ---> L i) F"
shows "((λx. ∏i∈S. f i x) ---> (∏i∈S. L i)) F"
proof (cases "finite S")
assume "finite S" thus ?thesis using assms
by (induct, simp add: tendsto_const, simp add: tendsto_mult)
next
assume "¬ finite S" thus ?thesis
by (simp add: tendsto_const)
qed

lemma tendsto_le_const:
fixes f :: "_ => real"
assumes F: "¬ trivial_limit F"
assumes x: "(f ---> x) F" and a: "eventually (λx. a ≤ f x) F"
shows "a ≤ x"
proof (rule ccontr)
assume "¬ a ≤ x"
with x have "eventually (λx. f x < a) F"
by (auto simp add: tendsto_def elim!: allE[of _ "{..< a}"])
with a have "eventually (λx. False) F"
by eventually_elim auto
with F show False
by (simp add: eventually_False)
qed

lemma tendsto_le:
fixes f g :: "_ => real"
assumes F: "¬ trivial_limit F"
assumes x: "(f ---> x) F" and y: "(g ---> y) F"
assumes ev: "eventually (λx. g x ≤ f x) F"
shows "y ≤ x"
using tendsto_le_const[OF F tendsto_diff[OF x y], of 0] ev
by (simp add: sign_simps)

subsubsection {* Inverse and division *}

lemma (in bounded_bilinear) Zfun_prod_Bfun:
assumes f: "Zfun f F"
assumes g: "Bfun g F"
shows "Zfun (λx. f x ** g x) F"
proof -
obtain K where K: "0 ≤ K"
and norm_le: "!!x y. norm (x ** y) ≤ norm x * norm y * K"
using nonneg_bounded by fast
obtain B where B: "0 < B"
and norm_g: "eventually (λx. norm (g x) ≤ B) F"
using g by (rule BfunE)
have "eventually (λx. norm (f x ** g x) ≤ norm (f x) * (B * K)) F"
using norm_g proof eventually_elim
case (elim x)
have "norm (f x ** g x) ≤ norm (f x) * norm (g x) * K"
by (rule norm_le)
also have "… ≤ norm (f x) * B * K"
by (intro mult_mono' order_refl norm_g norm_ge_zero
mult_nonneg_nonneg K elim)
also have "… = norm (f x) * (B * K)"
by (rule mult_assoc)
finally show "norm (f x ** g x) ≤ norm (f x) * (B * K)" .
qed
with f show ?thesis
by (rule Zfun_imp_Zfun)
qed

lemma (in bounded_bilinear) flip:
"bounded_bilinear (λx y. y ** x)"
apply default
apply (rule add_right)
apply (rule add_left)
apply (rule scaleR_right)
apply (rule scaleR_left)
apply (subst mult_commute)
using bounded by fast

lemma (in bounded_bilinear) Bfun_prod_Zfun:
assumes f: "Bfun f F"
assumes g: "Zfun g F"
shows "Zfun (λx. f x ** g x) F"
using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)

lemma Bfun_inverse_lemma:
fixes x :: "'a::real_normed_div_algebra"
shows "[|r ≤ norm x; 0 < r|] ==> norm (inverse x) ≤ inverse r"
apply (subst nonzero_norm_inverse, clarsimp)
apply (erule (1) le_imp_inverse_le)
done

lemma Bfun_inverse:
fixes a :: "'a::real_normed_div_algebra"
assumes f: "(f ---> a) F"
assumes a: "a ≠ 0"
shows "Bfun (λx. inverse (f x)) F"
proof -
from a have "0 < norm a" by simp
hence "∃r>0. r < norm a" by (rule dense)
then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
have "eventually (λx. dist (f x) a < r) F"
using tendstoD [OF f r1] by fast
hence "eventually (λx. norm (inverse (f x)) ≤ inverse (norm a - r)) F"
proof eventually_elim
case (elim x)
hence 1: "norm (f x - a) < r"
by (simp add: dist_norm)
hence 2: "f x ≠ 0" using r2 by auto
hence "norm (inverse (f x)) = inverse (norm (f x))"
by (rule nonzero_norm_inverse)
also have "… ≤ inverse (norm a - r)"
proof (rule le_imp_inverse_le)
show "0 < norm a - r" using r2 by simp
next
have "norm a - norm (f x) ≤ norm (a - f x)"
by (rule norm_triangle_ineq2)
also have "… = norm (f x - a)"
by (rule norm_minus_commute)
also have "… < r" using 1 .
finally show "norm a - r ≤ norm (f x)" by simp
qed
finally show "norm (inverse (f x)) ≤ inverse (norm a - r)" .
qed
thus ?thesis by (rule BfunI)
qed

lemma tendsto_inverse [tendsto_intros]:
fixes a :: "'a::real_normed_div_algebra"
assumes f: "(f ---> a) F"
assumes a: "a ≠ 0"
shows "((λx. inverse (f x)) ---> inverse a) F"
proof -
from a have "0 < norm a" by simp
with f have "eventually (λx. dist (f x) a < norm a) F"
by (rule tendstoD)
then have "eventually (λx. f x ≠ 0) F"
unfolding dist_norm by (auto elim!: eventually_elim1)
with a have "eventually (λx. inverse (f x) - inverse a =
- (inverse (f x) * (f x - a) * inverse a)) F"

by (auto elim!: eventually_elim1 simp: inverse_diff_inverse)
moreover have "Zfun (λx. - (inverse (f x) * (f x - a) * inverse a)) F"
by (intro Zfun_minus Zfun_mult_left
bounded_bilinear.Bfun_prod_Zfun [OF bounded_bilinear_mult]
Bfun_inverse [OF f a] f [unfolded tendsto_Zfun_iff])
ultimately show ?thesis
unfolding tendsto_Zfun_iff by (rule Zfun_ssubst)
qed

lemma tendsto_divide [tendsto_intros]:
fixes a b :: "'a::real_normed_field"
shows "[|(f ---> a) F; (g ---> b) F; b ≠ 0|]
==> ((λx. f x / g x) ---> a / b) F"

by (simp add: tendsto_mult tendsto_inverse divide_inverse)

lemma tendsto_sgn [tendsto_intros]:
fixes l :: "'a::real_normed_vector"
shows "[|(f ---> l) F; l ≠ 0|] ==> ((λx. sgn (f x)) ---> sgn l) F"
unfolding sgn_div_norm by (simp add: tendsto_intros)

subsection {* Limits to @{const at_top} and @{const at_bot} *}

lemma filterlim_at_top:
fixes f :: "'a => ('b::linorder)"
shows "(LIM x F. f x :> at_top) <-> (∀Z. eventually (λx. Z ≤ f x) F)"
by (auto simp: filterlim_iff eventually_at_top_linorder elim!: eventually_elim1)

lemma filterlim_at_top_dense:
fixes f :: "'a => ('b::dense_linorder)"
shows "(LIM x F. f x :> at_top) <-> (∀Z. eventually (λx. Z < f x) F)"
by (metis eventually_elim1[of _ F] eventually_gt_at_top order_less_imp_le
filterlim_at_top[of f F] filterlim_iff[of f at_top F])

lemma filterlim_at_top_ge:
fixes f :: "'a => ('b::linorder)" and c :: "'b"
shows "(LIM x F. f x :> at_top) <-> (∀Z≥c. eventually (λx. Z ≤ f x) F)"
unfolding filterlim_at_top
proof safe
fix Z assume *: "∀Z≥c. eventually (λx. Z ≤ f x) F"
with *[THEN spec, of "max Z c"] show "eventually (λx. Z ≤ f x) F"
by (auto elim!: eventually_elim1)
qed simp

lemma filterlim_at_top_at_top:
fixes f :: "'a::linorder => 'b::linorder"
assumes mono: "!!x y. Q x ==> Q y ==> x ≤ y ==> f x ≤ f y"
assumes bij: "!!x. P x ==> f (g x) = x" "!!x. P x ==> Q (g x)"
assumes Q: "eventually Q at_top"
assumes P: "eventually P at_top"
shows "filterlim f at_top at_top"
proof -
from P obtain x where x: "!!y. x ≤ y ==> P y"
unfolding eventually_at_top_linorder by auto
show ?thesis
proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
fix z assume "x ≤ z"
with x have "P z" by auto
have "eventually (λx. g z ≤ x) at_top"
by (rule eventually_ge_at_top)
with Q show "eventually (λx. z ≤ f x) at_top"
by eventually_elim (metis mono bij `P z`)
qed
qed

lemma filterlim_at_top_gt:
fixes f :: "'a => ('b::dense_linorder)" and c :: "'b"
shows "(LIM x F. f x :> at_top) <-> (∀Z>c. eventually (λx. Z ≤ f x) F)"
by (metis filterlim_at_top order_less_le_trans gt_ex filterlim_at_top_ge)

lemma filterlim_at_bot:
fixes f :: "'a => ('b::linorder)"
shows "(LIM x F. f x :> at_bot) <-> (∀Z. eventually (λx. f x ≤ Z) F)"
by (auto simp: filterlim_iff eventually_at_bot_linorder elim!: eventually_elim1)

lemma filterlim_at_bot_le:
fixes f :: "'a => ('b::linorder)" and c :: "'b"
shows "(LIM x F. f x :> at_bot) <-> (∀Z≤c. eventually (λx. Z ≥ f x) F)"
unfolding filterlim_at_bot
proof safe
fix Z assume *: "∀Z≤c. eventually (λx. Z ≥ f x) F"
with *[THEN spec, of "min Z c"] show "eventually (λx. Z ≥ f x) F"
by (auto elim!: eventually_elim1)
qed simp

lemma filterlim_at_bot_lt:
fixes f :: "'a => ('b::dense_linorder)" and c :: "'b"
shows "(LIM x F. f x :> at_bot) <-> (∀Z<c. eventually (λx. Z ≥ f x) F)"
by (metis filterlim_at_bot filterlim_at_bot_le lt_ex order_le_less_trans)

lemma filterlim_at_bot_at_right:
fixes f :: "real => 'b::linorder"
assumes mono: "!!x y. Q x ==> Q y ==> x ≤ y ==> f x ≤ f y"
assumes bij: "!!x. P x ==> f (g x) = x" "!!x. P x ==> Q (g x)"
assumes Q: "eventually Q (at_right a)" and bound: "!!b. Q b ==> a < b"
assumes P: "eventually P at_bot"
shows "filterlim f at_bot (at_right a)"
proof -
from P obtain x where x: "!!y. y ≤ x ==> P y"
unfolding eventually_at_bot_linorder by auto
show ?thesis
proof (intro filterlim_at_bot_le[THEN iffD2] allI impI)
fix z assume "z ≤ x"
with x have "P z" by auto
have "eventually (λx. x ≤ g z) (at_right a)"
using bound[OF bij(2)[OF `P z`]]
by (auto simp add: eventually_within_less dist_real_def intro!: exI[of _ "g z - a"])
with Q show "eventually (λx. f x ≤ z) (at_right a)"
by eventually_elim (metis bij `P z` mono)
qed
qed

lemma filterlim_at_top_at_left:
fixes f :: "real => 'b::linorder"
assumes mono: "!!x y. Q x ==> Q y ==> x ≤ y ==> f x ≤ f y"
assumes bij: "!!x. P x ==> f (g x) = x" "!!x. P x ==> Q (g x)"
assumes Q: "eventually Q (at_left a)" and bound: "!!b. Q b ==> b < a"
assumes P: "eventually P at_top"
shows "filterlim f at_top (at_left a)"
proof -
from P obtain x where x: "!!y. x ≤ y ==> P y"
unfolding eventually_at_top_linorder by auto
show ?thesis
proof (intro filterlim_at_top_ge[THEN iffD2] allI impI)
fix z assume "x ≤ z"
with x have "P z" by auto
have "eventually (λx. g z ≤ x) (at_left a)"
using bound[OF bij(2)[OF `P z`]]
by (auto simp add: eventually_within_less dist_real_def intro!: exI[of _ "a - g z"])
with Q show "eventually (λx. z ≤ f x) (at_left a)"
by eventually_elim (metis bij `P z` mono)
qed
qed

lemma filterlim_at_infinity:
fixes f :: "_ => 'a::real_normed_vector"
assumes "0 ≤ c"
shows "(LIM x F. f x :> at_infinity) <-> (∀r>c. eventually (λx. r ≤ norm (f x)) F)"
unfolding filterlim_iff eventually_at_infinity
proof safe
fix P :: "'a => bool" and b
assume *: "∀r>c. eventually (λx. r ≤ norm (f x)) F"
and P: "∀x. b ≤ norm x --> P x"
have "max b (c + 1) > c" by auto
with * have "eventually (λx. max b (c + 1) ≤ norm (f x)) F"
by auto
then show "eventually (λx. P (f x)) F"
proof eventually_elim
fix x assume "max b (c + 1) ≤ norm (f x)"
with P show "P (f x)" by auto
qed
qed force

lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
unfolding filterlim_at_top
apply (intro allI)
apply (rule_tac c="natceiling (Z + 1)" in eventually_sequentiallyI)
apply (auto simp: natceiling_le_eq)
done

subsection {* Relate @{const at}, @{const at_left} and @{const at_right} *}

text {*

This lemmas are useful for conversion between @{term "at x"} to @{term "at_left x"} and
@{term "at_right x"} and also @{term "at_right 0"}.

*}


lemma at_eq_sup_left_right: "at (x::real) = sup (at_left x) (at_right x)"
by (auto simp: eventually_within at_def filter_eq_iff eventually_sup
elim: eventually_elim2 eventually_elim1)

lemma filterlim_split_at_real:
"filterlim f F (at_left x) ==> filterlim f F (at_right x) ==> filterlim f F (at (x::real))"
by (subst at_eq_sup_left_right) (rule filterlim_sup)

lemma filtermap_nhds_shift: "filtermap (λx. x - d) (nhds a) = nhds (a - d::real)"
unfolding filter_eq_iff eventually_filtermap eventually_nhds_metric
by (intro allI ex_cong) (auto simp: dist_real_def field_simps)

lemma filtermap_nhds_minus: "filtermap (λx. - x) (nhds a) = nhds (- a::real)"
unfolding filter_eq_iff eventually_filtermap eventually_nhds_metric
apply (intro allI ex_cong)
apply (auto simp: dist_real_def field_simps)
apply (erule_tac x="-x" in allE)
apply simp
done

lemma filtermap_at_shift: "filtermap (λx. x - d) (at a) = at (a - d::real)"
unfolding at_def filtermap_nhds_shift[symmetric]
by (simp add: filter_eq_iff eventually_filtermap eventually_within)

lemma filtermap_at_right_shift: "filtermap (λx. x - d) (at_right a) = at_right (a - d::real)"
unfolding filtermap_at_shift[symmetric]
by (simp add: filter_eq_iff eventually_filtermap eventually_within)

lemma at_right_to_0: "at_right (a::real) = filtermap (λx. x + a) (at_right 0)"
using filtermap_at_right_shift[of "-a" 0] by simp

lemma filterlim_at_right_to_0:
"filterlim f F (at_right (a::real)) <-> filterlim (λx. f (x + a)) F (at_right 0)"
unfolding filterlim_def filtermap_filtermap at_right_to_0[of a] ..

lemma eventually_at_right_to_0:
"eventually P (at_right (a::real)) <-> eventually (λx. P (x + a)) (at_right 0)"
unfolding at_right_to_0[of a] by (simp add: eventually_filtermap)

lemma filtermap_at_minus: "filtermap (λx. - x) (at a) = at (- a::real)"
unfolding at_def filtermap_nhds_minus[symmetric]
by (simp add: filter_eq_iff eventually_filtermap eventually_within)

lemma at_left_minus: "at_left (a::real) = filtermap (λx. - x) (at_right (- a))"
by (simp add: filter_eq_iff eventually_filtermap eventually_within filtermap_at_minus[symmetric])

lemma at_right_minus: "at_right (a::real) = filtermap (λx. - x) (at_left (- a))"
by (simp add: filter_eq_iff eventually_filtermap eventually_within filtermap_at_minus[symmetric])

lemma filterlim_at_left_to_right:
"filterlim f F (at_left (a::real)) <-> filterlim (λx. f (- x)) F (at_right (-a))"
unfolding filterlim_def filtermap_filtermap at_left_minus[of a] ..

lemma eventually_at_left_to_right:
"eventually P (at_left (a::real)) <-> eventually (λx. P (- x)) (at_right (-a))"
unfolding at_left_minus[of a] by (simp add: eventually_filtermap)

lemma filterlim_at_split:
"filterlim f F (at (x::real)) <-> filterlim f F (at_left x) ∧ filterlim f F (at_right x)"
by (subst at_eq_sup_left_right) (simp add: filterlim_def filtermap_sup)

lemma eventually_at_split:
"eventually P (at (x::real)) <-> eventually P (at_left x) ∧ eventually P (at_right x)"
by (subst at_eq_sup_left_right) (simp add: eventually_sup)

lemma at_top_mirror: "at_top = filtermap uminus (at_bot :: real filter)"
unfolding filter_eq_iff eventually_filtermap eventually_at_top_linorder eventually_at_bot_linorder
by (metis le_minus_iff minus_minus)

lemma at_bot_mirror: "at_bot = filtermap uminus (at_top :: real filter)"
unfolding at_top_mirror filtermap_filtermap by (simp add: filtermap_ident)

lemma filterlim_at_top_mirror: "(LIM x at_top. f x :> F) <-> (LIM x at_bot. f (-x::real) :> F)"
unfolding filterlim_def at_top_mirror filtermap_filtermap ..

lemma filterlim_at_bot_mirror: "(LIM x at_bot. f x :> F) <-> (LIM x at_top. f (-x::real) :> F)"
unfolding filterlim_def at_bot_mirror filtermap_filtermap ..

lemma filterlim_uminus_at_top_at_bot: "LIM x at_bot. - x :: real :> at_top"
unfolding filterlim_at_top eventually_at_bot_dense
by (metis leI minus_less_iff order_less_asym)

lemma filterlim_uminus_at_bot_at_top: "LIM x at_top. - x :: real :> at_bot"
unfolding filterlim_at_bot eventually_at_top_dense
by (metis leI less_minus_iff order_less_asym)

lemma filterlim_uminus_at_top: "(LIM x F. f x :> at_top) <-> (LIM x F. - (f x) :: real :> at_bot)"
using filterlim_compose[OF filterlim_uminus_at_bot_at_top, of f F]
using filterlim_compose[OF filterlim_uminus_at_top_at_bot, of "λx. - f x" F]
by auto

lemma filterlim_uminus_at_bot: "(LIM x F. f x :> at_bot) <-> (LIM x F. - (f x) :: real :> at_top)"
unfolding filterlim_uminus_at_top by simp

lemma filterlim_inverse_at_top_right: "LIM x at_right (0::real). inverse x :> at_top"
unfolding filterlim_at_top_gt[where c=0] eventually_within at_def
proof safe
fix Z :: real assume [arith]: "0 < Z"
then have "eventually (λx. x < inverse Z) (nhds 0)"
by (auto simp add: eventually_nhds_metric dist_real_def intro!: exI[of _ "¦inverse Z¦"])
then show "eventually (λx. x ∈ - {0} --> x ∈ {0<..} --> Z ≤ inverse x) (nhds 0)"
by (auto elim!: eventually_elim1 simp: inverse_eq_divide field_simps)
qed

lemma filterlim_inverse_at_top:
"(f ---> (0 :: real)) F ==> eventually (λx. 0 < f x) F ==> LIM x F. inverse (f x) :> at_top"
by (intro filterlim_compose[OF filterlim_inverse_at_top_right])
(simp add: filterlim_def eventually_filtermap le_within_iff at_def eventually_elim1)

lemma filterlim_inverse_at_bot_neg:
"LIM x (at_left (0::real)). inverse x :> at_bot"
by (simp add: filterlim_inverse_at_top_right filterlim_uminus_at_bot filterlim_at_left_to_right)

lemma filterlim_inverse_at_bot:
"(f ---> (0 :: real)) F ==> eventually (λx. f x < 0) F ==> LIM x F. inverse (f x) :> at_bot"
unfolding filterlim_uminus_at_bot inverse_minus_eq[symmetric]
by (rule filterlim_inverse_at_top) (simp_all add: tendsto_minus_cancel_left[symmetric])

lemma tendsto_inverse_0:
fixes x :: "_ => 'a::real_normed_div_algebra"
shows "(inverse ---> (0::'a)) at_infinity"
unfolding tendsto_Zfun_iff diff_0_right Zfun_def eventually_at_infinity
proof safe
fix r :: real assume "0 < r"
show "∃b. ∀x. b ≤ norm x --> norm (inverse x :: 'a) < r"
proof (intro exI[of _ "inverse (r / 2)"] allI impI)
fix x :: 'a
from `0 < r` have "0 < inverse (r / 2)" by simp
also assume *: "inverse (r / 2) ≤ norm x"
finally show "norm (inverse x) < r"
using * `0 < r` by (subst nonzero_norm_inverse) (simp_all add: inverse_eq_divide field_simps)
qed
qed

lemma at_right_to_top: "(at_right (0::real)) = filtermap inverse at_top"
proof (rule antisym)
have "(inverse ---> (0::real)) at_top"
by (metis tendsto_inverse_0 filterlim_mono at_top_le_at_infinity order_refl)
then show "filtermap inverse at_top ≤ at_right (0::real)"
unfolding at_within_eq
by (intro le_withinI) (simp_all add: eventually_filtermap eventually_gt_at_top filterlim_def)
next
have "filtermap inverse (filtermap inverse (at_right (0::real))) ≤ filtermap inverse at_top"
using filterlim_inverse_at_top_right unfolding filterlim_def by (rule filtermap_mono)
then show "at_right (0::real) ≤ filtermap inverse at_top"
by (simp add: filtermap_ident filtermap_filtermap)
qed

lemma eventually_at_right_to_top:
"eventually P (at_right (0::real)) <-> eventually (λx. P (inverse x)) at_top"
unfolding at_right_to_top eventually_filtermap ..

lemma filterlim_at_right_to_top:
"filterlim f F (at_right (0::real)) <-> (LIM x at_top. f (inverse x) :> F)"
unfolding filterlim_def at_right_to_top filtermap_filtermap ..

lemma at_top_to_right: "at_top = filtermap inverse (at_right (0::real))"
unfolding at_right_to_top filtermap_filtermap inverse_inverse_eq filtermap_ident ..

lemma eventually_at_top_to_right:
"eventually P at_top <-> eventually (λx. P (inverse x)) (at_right (0::real))"
unfolding at_top_to_right eventually_filtermap ..

lemma filterlim_at_top_to_right:
"filterlim f F at_top <-> (LIM x (at_right (0::real)). f (inverse x) :> F)"
unfolding filterlim_def at_top_to_right filtermap_filtermap ..

lemma filterlim_inverse_at_infinity:
fixes x :: "_ => 'a::{real_normed_div_algebra, division_ring_inverse_zero}"
shows "filterlim inverse at_infinity (at (0::'a))"
unfolding filterlim_at_infinity[OF order_refl]
proof safe
fix r :: real assume "0 < r"
then show "eventually (λx::'a. r ≤ norm (inverse x)) (at 0)"
unfolding eventually_at norm_inverse
by (intro exI[of _ "inverse r"])
(auto simp: norm_conv_dist[symmetric] field_simps inverse_eq_divide)
qed

lemma filterlim_inverse_at_iff:
fixes g :: "'a => 'b::{real_normed_div_algebra, division_ring_inverse_zero}"
shows "(LIM x F. inverse (g x) :> at 0) <-> (LIM x F. g x :> at_infinity)"
unfolding filterlim_def filtermap_filtermap[symmetric]
proof
assume "filtermap g F ≤ at_infinity"
then have "filtermap inverse (filtermap g F) ≤ filtermap inverse at_infinity"
by (rule filtermap_mono)
also have "… ≤ at 0"
using tendsto_inverse_0
by (auto intro!: le_withinI exI[of _ 1]
simp: eventually_filtermap eventually_at_infinity filterlim_def at_def)
finally show "filtermap inverse (filtermap g F) ≤ at 0" .
next
assume "filtermap inverse (filtermap g F) ≤ at 0"
then have "filtermap inverse (filtermap inverse (filtermap g F)) ≤ filtermap inverse (at 0)"
by (rule filtermap_mono)
with filterlim_inverse_at_infinity show "filtermap g F ≤ at_infinity"
by (auto intro: order_trans simp: filterlim_def filtermap_filtermap)
qed

lemma tendsto_inverse_0_at_top:
"LIM x F. f x :> at_top ==> ((λx. inverse (f x) :: real) ---> 0) F"
by (metis at_top_le_at_infinity filterlim_at filterlim_inverse_at_iff filterlim_mono order_refl)

text {*

We only show rules for multiplication and addition when the functions are either against a real
value or against infinity. Further rules are easy to derive by using @{thm filterlim_uminus_at_top}.

*}


lemma filterlim_tendsto_pos_mult_at_top:
assumes f: "(f ---> c) F" and c: "0 < c"
assumes g: "LIM x F. g x :> at_top"
shows "LIM x F. (f x * g x :: real) :> at_top"
unfolding filterlim_at_top_gt[where c=0]
proof safe
fix Z :: real assume "0 < Z"
from f `0 < c` have "eventually (λx. c / 2 < f x) F"
by (auto dest!: tendstoD[where e="c / 2"] elim!: eventually_elim1
simp: dist_real_def abs_real_def split: split_if_asm)
moreover from g have "eventually (λx. (Z / c * 2) ≤ g x) F"
unfolding filterlim_at_top by auto
ultimately show "eventually (λx. Z ≤ f x * g x) F"
proof eventually_elim
fix x assume "c / 2 < f x" "Z / c * 2 ≤ g x"
with `0 < Z` `0 < c` have "c / 2 * (Z / c * 2) ≤ f x * g x"
by (intro mult_mono) (auto simp: zero_le_divide_iff)
with `0 < c` show "Z ≤ f x * g x"
by simp
qed
qed

lemma filterlim_at_top_mult_at_top:
assumes f: "LIM x F. f x :> at_top"
assumes g: "LIM x F. g x :> at_top"
shows "LIM x F. (f x * g x :: real) :> at_top"
unfolding filterlim_at_top_gt[where c=0]
proof safe
fix Z :: real assume "0 < Z"
from f have "eventually (λx. 1 ≤ f x) F"
unfolding filterlim_at_top by auto
moreover from g have "eventually (λx. Z ≤ g x) F"
unfolding filterlim_at_top by auto
ultimately show "eventually (λx. Z ≤ f x * g x) F"
proof eventually_elim
fix x assume "1 ≤ f x" "Z ≤ g x"
with `0 < Z` have "1 * Z ≤ f x * g x"
by (intro mult_mono) (auto simp: zero_le_divide_iff)
then show "Z ≤ f x * g x"
by simp
qed
qed

lemma filterlim_tendsto_pos_mult_at_bot:
assumes "(f ---> c) F" "0 < (c::real)" "filterlim g at_bot F"
shows "LIM x F. f x * g x :> at_bot"
using filterlim_tendsto_pos_mult_at_top[OF assms(1,2), of "λx. - g x"] assms(3)
unfolding filterlim_uminus_at_bot by simp

lemma filterlim_tendsto_add_at_top:
assumes f: "(f ---> c) F"
assumes g: "LIM x F. g x :> at_top"
shows "LIM x F. (f x + g x :: real) :> at_top"
unfolding filterlim_at_top_gt[where c=0]
proof safe
fix Z :: real assume "0 < Z"
from f have "eventually (λx. c - 1 < f x) F"
by (auto dest!: tendstoD[where e=1] elim!: eventually_elim1 simp: dist_real_def)
moreover from g have "eventually (λx. Z - (c - 1) ≤ g x) F"
unfolding filterlim_at_top by auto
ultimately show "eventually (λx. Z ≤ f x + g x) F"
by eventually_elim simp
qed

lemma LIM_at_top_divide:
fixes f g :: "'a => real"
assumes f: "(f ---> a) F" "0 < a"
assumes g: "(g ---> 0) F" "eventually (λx. 0 < g x) F"
shows "LIM x F. f x / g x :> at_top"
unfolding divide_inverse
by (rule filterlim_tendsto_pos_mult_at_top[OF f]) (rule filterlim_inverse_at_top[OF g])

lemma filterlim_at_top_add_at_top:
assumes f: "LIM x F. f x :> at_top"
assumes g: "LIM x F. g x :> at_top"
shows "LIM x F. (f x + g x :: real) :> at_top"
unfolding filterlim_at_top_gt[where c=0]
proof safe
fix Z :: real assume "0 < Z"
from f have "eventually (λx. 0 ≤ f x) F"
unfolding filterlim_at_top by auto
moreover from g have "eventually (λx. Z ≤ g x) F"
unfolding filterlim_at_top by auto
ultimately show "eventually (λx. Z ≤ f x + g x) F"
by eventually_elim simp
qed

lemma tendsto_divide_0:
fixes f :: "_ => 'a::{real_normed_div_algebra, division_ring_inverse_zero}"
assumes f: "(f ---> c) F"
assumes g: "LIM x F. g x :> at_infinity"
shows "((λx. f x / g x) ---> 0) F"
using tendsto_mult[OF f filterlim_compose[OF tendsto_inverse_0 g]] by (simp add: divide_inverse)

lemma linear_plus_1_le_power:
fixes x :: real
assumes x: "0 ≤ x"
shows "real n * x + 1 ≤ (x + 1) ^ n"
proof (induct n)
case (Suc n)
have "real (Suc n) * x + 1 ≤ (x + 1) * (real n * x + 1)"
by (simp add: field_simps real_of_nat_Suc mult_nonneg_nonneg x)
also have "… ≤ (x + 1)^Suc n"
using Suc x by (simp add: mult_left_mono)
finally show ?case .
qed simp

lemma filterlim_realpow_sequentially_gt1:
fixes x :: "'a :: real_normed_div_algebra"
assumes x[arith]: "1 < norm x"
shows "LIM n sequentially. x ^ n :> at_infinity"
proof (intro filterlim_at_infinity[THEN iffD2] allI impI)
fix y :: real assume "0 < y"
have "0 < norm x - 1" by simp
then obtain N::nat where "y < real N * (norm x - 1)" by (blast dest: reals_Archimedean3)
also have "… ≤ real N * (norm x - 1) + 1" by simp
also have "… ≤ (norm x - 1 + 1) ^ N" by (rule linear_plus_1_le_power) simp
also have "… = norm x ^ N" by simp
finally have "∀n≥N. y ≤ norm x ^ n"
by (metis order_less_le_trans power_increasing order_less_imp_le x)
then show "eventually (λn. y ≤ norm (x ^ n)) sequentially"
unfolding eventually_sequentially
by (auto simp: norm_power)
qed simp

end