src/HOL/Limits.thy
author paulson <lp15@cam.ac.uk>
Mon May 23 15:33:24 2016 +0100 (2016-05-23)
changeset 63114 27afe7af7379
parent 63104 9505a883403c
child 63263 c6c95d64607a
permissions -rw-r--r--
Lots of new material for multivariate analysis
     1 (*  Title:      HOL/Limits.thy
     2     Author:     Brian Huffman
     3     Author:     Jacques D. Fleuriot, University of Cambridge
     4     Author:     Lawrence C Paulson
     5     Author:     Jeremy Avigad
     6 *)
     7 
     8 section \<open>Limits on Real Vector Spaces\<close>
     9 
    10 theory Limits
    11 imports Real_Vector_Spaces
    12 begin
    13 
    14 subsection \<open>Filter going to infinity norm\<close>
    15 
    16 definition at_infinity :: "'a::real_normed_vector filter" where
    17   "at_infinity = (INF r. principal {x. r \<le> norm x})"
    18 
    19 lemma eventually_at_infinity: "eventually P at_infinity \<longleftrightarrow> (\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> P x)"
    20   unfolding at_infinity_def
    21   by (subst eventually_INF_base)
    22      (auto simp: subset_eq eventually_principal intro!: exI[of _ "max a b" for a b])
    23 
    24 corollary eventually_at_infinity_pos:
    25    "eventually p at_infinity \<longleftrightarrow> (\<exists>b. 0 < b \<and> (\<forall>x. norm x \<ge> b \<longrightarrow> p x))"
    26 apply (simp add: eventually_at_infinity, auto)
    27 apply (case_tac "b \<le> 0")
    28 using norm_ge_zero order_trans zero_less_one apply blast
    29 apply (force simp:)
    30 done
    31 
    32 lemma at_infinity_eq_at_top_bot:
    33   "(at_infinity :: real filter) = sup at_top at_bot"
    34   apply (simp add: filter_eq_iff eventually_sup eventually_at_infinity
    35                    eventually_at_top_linorder eventually_at_bot_linorder)
    36   apply safe
    37   apply (rule_tac x="b" in exI, simp)
    38   apply (rule_tac x="- b" in exI, simp)
    39   apply (rule_tac x="max (- Na) N" in exI, auto simp: abs_real_def)
    40   done
    41 
    42 lemma at_top_le_at_infinity: "at_top \<le> (at_infinity :: real filter)"
    43   unfolding at_infinity_eq_at_top_bot by simp
    44 
    45 lemma at_bot_le_at_infinity: "at_bot \<le> (at_infinity :: real filter)"
    46   unfolding at_infinity_eq_at_top_bot by simp
    47 
    48 lemma filterlim_at_top_imp_at_infinity:
    49   fixes f :: "_ \<Rightarrow> real"
    50   shows "filterlim f at_top F \<Longrightarrow> filterlim f at_infinity F"
    51   by (rule filterlim_mono[OF _ at_top_le_at_infinity order_refl])
    52 
    53 lemma lim_infinity_imp_sequentially:
    54   "(f \<longlongrightarrow> l) at_infinity \<Longrightarrow> ((\<lambda>n. f(n)) \<longlongrightarrow> l) sequentially"
    55 by (simp add: filterlim_at_top_imp_at_infinity filterlim_compose filterlim_real_sequentially)
    56 
    57 
    58 subsubsection \<open>Boundedness\<close>
    59 
    60 definition Bfun :: "('a \<Rightarrow> 'b::metric_space) \<Rightarrow> 'a filter \<Rightarrow> bool" where
    61   Bfun_metric_def: "Bfun f F = (\<exists>y. \<exists>K>0. eventually (\<lambda>x. dist (f x) y \<le> K) F)"
    62 
    63 abbreviation Bseq :: "(nat \<Rightarrow> 'a::metric_space) \<Rightarrow> bool" where
    64   "Bseq X \<equiv> Bfun X sequentially"
    65 
    66 lemma Bseq_conv_Bfun: "Bseq X \<longleftrightarrow> Bfun X sequentially" ..
    67 
    68 lemma Bseq_ignore_initial_segment: "Bseq X \<Longrightarrow> Bseq (\<lambda>n. X (n + k))"
    69   unfolding Bfun_metric_def by (subst eventually_sequentially_seg)
    70 
    71 lemma Bseq_offset: "Bseq (\<lambda>n. X (n + k)) \<Longrightarrow> Bseq X"
    72   unfolding Bfun_metric_def by (subst (asm) eventually_sequentially_seg)
    73 
    74 lemma Bfun_def:
    75   "Bfun f F \<longleftrightarrow> (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) F)"
    76   unfolding Bfun_metric_def norm_conv_dist
    77 proof safe
    78   fix y K assume K: "0 < K" and *: "eventually (\<lambda>x. dist (f x) y \<le> K) F"
    79   moreover have "eventually (\<lambda>x. dist (f x) 0 \<le> dist (f x) y + dist 0 y) F"
    80     by (intro always_eventually) (metis dist_commute dist_triangle)
    81   with * have "eventually (\<lambda>x. dist (f x) 0 \<le> K + dist 0 y) F"
    82     by eventually_elim auto
    83   with \<open>0 < K\<close> show "\<exists>K>0. eventually (\<lambda>x. dist (f x) 0 \<le> K) F"
    84     by (intro exI[of _ "K + dist 0 y"] add_pos_nonneg conjI zero_le_dist) auto
    85 qed (force simp del: norm_conv_dist [symmetric])
    86 
    87 lemma BfunI:
    88   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) F" shows "Bfun f F"
    89 unfolding Bfun_def
    90 proof (intro exI conjI allI)
    91   show "0 < max K 1" by simp
    92 next
    93   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) F"
    94     using K by (rule eventually_mono, simp)
    95 qed
    96 
    97 lemma BfunE:
    98   assumes "Bfun f F"
    99   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) F"
   100 using assms unfolding Bfun_def by blast
   101 
   102 lemma Cauchy_Bseq: "Cauchy X \<Longrightarrow> Bseq X"
   103   unfolding Cauchy_def Bfun_metric_def eventually_sequentially
   104   apply (erule_tac x=1 in allE)
   105   apply simp
   106   apply safe
   107   apply (rule_tac x="X M" in exI)
   108   apply (rule_tac x=1 in exI)
   109   apply (erule_tac x=M in allE)
   110   apply simp
   111   apply (rule_tac x=M in exI)
   112   apply (auto simp: dist_commute)
   113   done
   114 
   115 
   116 subsubsection \<open>Bounded Sequences\<close>
   117 
   118 lemma BseqI': "(\<And>n. norm (X n) \<le> K) \<Longrightarrow> Bseq X"
   119   by (intro BfunI) (auto simp: eventually_sequentially)
   120 
   121 lemma BseqI2': "\<forall>n\<ge>N. norm (X n) \<le> K \<Longrightarrow> Bseq X"
   122   by (intro BfunI) (auto simp: eventually_sequentially)
   123 
   124 lemma Bseq_def: "Bseq X \<longleftrightarrow> (\<exists>K>0. \<forall>n. norm (X n) \<le> K)"
   125   unfolding Bfun_def eventually_sequentially
   126 proof safe
   127   fix N K assume "0 < K" "\<forall>n\<ge>N. norm (X n) \<le> K"
   128   then show "\<exists>K>0. \<forall>n. norm (X n) \<le> K"
   129     by (intro exI[of _ "max (Max (norm ` X ` {..N})) K"] max.strict_coboundedI2)
   130        (auto intro!: imageI not_less[where 'a=nat, THEN iffD1] Max_ge simp: le_max_iff_disj)
   131 qed auto
   132 
   133 lemma BseqE: "\<lbrakk>Bseq X; \<And>K. \<lbrakk>0 < K; \<forall>n. norm (X n) \<le> K\<rbrakk> \<Longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
   134 unfolding Bseq_def by auto
   135 
   136 lemma BseqD: "Bseq X ==> \<exists>K. 0 < K & (\<forall>n. norm (X n) \<le> K)"
   137 by (simp add: Bseq_def)
   138 
   139 lemma BseqI: "[| 0 < K; \<forall>n. norm (X n) \<le> K |] ==> Bseq X"
   140 by (auto simp add: Bseq_def)
   141 
   142 lemma Bseq_bdd_above: "Bseq (X::nat \<Rightarrow> real) \<Longrightarrow> bdd_above (range X)"
   143 proof (elim BseqE, intro bdd_aboveI2)
   144   fix K n assume "0 < K" "\<forall>n. norm (X n) \<le> K" then show "X n \<le> K"
   145     by (auto elim!: allE[of _ n])
   146 qed
   147 
   148 lemma Bseq_bdd_above':
   149   "Bseq (X::nat \<Rightarrow> 'a :: real_normed_vector) \<Longrightarrow> bdd_above (range (\<lambda>n. norm (X n)))"
   150 proof (elim BseqE, intro bdd_aboveI2)
   151   fix K n assume "0 < K" "\<forall>n. norm (X n) \<le> K" then show "norm (X n) \<le> K"
   152     by (auto elim!: allE[of _ n])
   153 qed
   154 
   155 lemma Bseq_bdd_below: "Bseq (X::nat \<Rightarrow> real) \<Longrightarrow> bdd_below (range X)"
   156 proof (elim BseqE, intro bdd_belowI2)
   157   fix K n assume "0 < K" "\<forall>n. norm (X n) \<le> K" then show "- K \<le> X n"
   158     by (auto elim!: allE[of _ n])
   159 qed
   160 
   161 lemma Bseq_eventually_mono:
   162   assumes "eventually (\<lambda>n. norm (f n) \<le> norm (g n)) sequentially" "Bseq g"
   163   shows   "Bseq f"
   164 proof -
   165   from assms(1) obtain N where N: "\<And>n. n \<ge> N \<Longrightarrow> norm (f n) \<le> norm (g n)"
   166     by (auto simp: eventually_at_top_linorder)
   167   moreover from assms(2) obtain K where K: "\<And>n. norm (g n) \<le> K" by (blast elim!: BseqE)
   168   ultimately have "norm (f n) \<le> max K (Max {norm (f n) |n. n < N})" for n
   169     apply (cases "n < N")
   170     apply (rule max.coboundedI2, rule Max.coboundedI, auto) []
   171     apply (rule max.coboundedI1, force intro: order.trans[OF N K])
   172     done
   173   thus ?thesis by (blast intro: BseqI')
   174 qed
   175 
   176 lemma lemma_NBseq_def:
   177   "(\<exists>K > 0. \<forall>n. norm (X n) \<le> K) = (\<exists>N. \<forall>n. norm (X n) \<le> real(Suc N))"
   178 proof safe
   179   fix K :: real
   180   from reals_Archimedean2 obtain n :: nat where "K < real n" ..
   181   then have "K \<le> real (Suc n)" by auto
   182   moreover assume "\<forall>m. norm (X m) \<le> K"
   183   ultimately have "\<forall>m. norm (X m) \<le> real (Suc n)"
   184     by (blast intro: order_trans)
   185   then show "\<exists>N. \<forall>n. norm (X n) \<le> real (Suc N)" ..
   186 next
   187   show "\<And>N. \<forall>n. norm (X n) \<le> real (Suc N) \<Longrightarrow> \<exists>K>0. \<forall>n. norm (X n) \<le> K"
   188     using of_nat_0_less_iff by blast
   189 qed
   190 
   191 text\<open>alternative definition for Bseq\<close>
   192 lemma Bseq_iff: "Bseq X = (\<exists>N. \<forall>n. norm (X n) \<le> real(Suc N))"
   193 apply (simp add: Bseq_def)
   194 apply (simp (no_asm) add: lemma_NBseq_def)
   195 done
   196 
   197 lemma lemma_NBseq_def2:
   198      "(\<exists>K > 0. \<forall>n. norm (X n) \<le> K) = (\<exists>N. \<forall>n. norm (X n) < real(Suc N))"
   199 apply (subst lemma_NBseq_def, auto)
   200 apply (rule_tac x = "Suc N" in exI)
   201 apply (rule_tac [2] x = N in exI)
   202 apply (auto simp add: of_nat_Suc)
   203  prefer 2 apply (blast intro: order_less_imp_le)
   204 apply (drule_tac x = n in spec, simp)
   205 done
   206 
   207 (* yet another definition for Bseq *)
   208 lemma Bseq_iff1a: "Bseq X = (\<exists>N. \<forall>n. norm (X n) < real(Suc N))"
   209 by (simp add: Bseq_def lemma_NBseq_def2)
   210 
   211 subsubsection\<open>A Few More Equivalence Theorems for Boundedness\<close>
   212 
   213 text\<open>alternative formulation for boundedness\<close>
   214 lemma Bseq_iff2: "Bseq X = (\<exists>k > 0. \<exists>x. \<forall>n. norm (X(n) + -x) \<le> k)"
   215 apply (unfold Bseq_def, safe)
   216 apply (rule_tac [2] x = "k + norm x" in exI)
   217 apply (rule_tac x = K in exI, simp)
   218 apply (rule exI [where x = 0], auto)
   219 apply (erule order_less_le_trans, simp)
   220 apply (drule_tac x=n in spec)
   221 apply (drule order_trans [OF norm_triangle_ineq2])
   222 apply simp
   223 done
   224 
   225 text\<open>alternative formulation for boundedness\<close>
   226 lemma Bseq_iff3:
   227   "Bseq X \<longleftrightarrow> (\<exists>k>0. \<exists>N. \<forall>n. norm (X n + - X N) \<le> k)" (is "?P \<longleftrightarrow> ?Q")
   228 proof
   229   assume ?P
   230   then obtain K
   231     where *: "0 < K" and **: "\<And>n. norm (X n) \<le> K" by (auto simp add: Bseq_def)
   232   from * have "0 < K + norm (X 0)" by (rule order_less_le_trans) simp
   233   from ** have "\<forall>n. norm (X n - X 0) \<le> K + norm (X 0)"
   234     by (auto intro: order_trans norm_triangle_ineq4)
   235   then have "\<forall>n. norm (X n + - X 0) \<le> K + norm (X 0)"
   236     by simp
   237   with \<open>0 < K + norm (X 0)\<close> show ?Q by blast
   238 next
   239   assume ?Q then show ?P by (auto simp add: Bseq_iff2)
   240 qed
   241 
   242 lemma BseqI2: "(\<forall>n. k \<le> f n & f n \<le> (K::real)) ==> Bseq f"
   243 apply (simp add: Bseq_def)
   244 apply (rule_tac x = " (\<bar>k\<bar> + \<bar>K\<bar>) + 1" in exI, auto)
   245 apply (drule_tac x = n in spec, arith)
   246 done
   247 
   248 
   249 subsubsection\<open>Upper Bounds and Lubs of Bounded Sequences\<close>
   250 
   251 lemma Bseq_minus_iff: "Bseq (%n. -(X n) :: 'a :: real_normed_vector) = Bseq X"
   252   by (simp add: Bseq_def)
   253 
   254 lemma Bseq_add:
   255   assumes "Bseq (f :: nat \<Rightarrow> 'a :: real_normed_vector)"
   256   shows   "Bseq (\<lambda>x. f x + c)"
   257 proof -
   258   from assms obtain K where K: "\<And>x. norm (f x) \<le> K" unfolding Bseq_def by blast
   259   {
   260     fix x :: nat
   261     have "norm (f x + c) \<le> norm (f x) + norm c" by (rule norm_triangle_ineq)
   262     also have "norm (f x) \<le> K" by (rule K)
   263     finally have "norm (f x + c) \<le> K + norm c" by simp
   264   }
   265   thus ?thesis by (rule BseqI')
   266 qed
   267 
   268 lemma Bseq_add_iff: "Bseq (\<lambda>x. f x + c) \<longleftrightarrow> Bseq (f :: nat \<Rightarrow> 'a :: real_normed_vector)"
   269   using Bseq_add[of f c] Bseq_add[of "\<lambda>x. f x + c" "-c"] by auto
   270 
   271 lemma Bseq_mult:
   272   assumes "Bseq (f :: nat \<Rightarrow> 'a :: real_normed_field)"
   273   assumes "Bseq (g :: nat \<Rightarrow> 'a :: real_normed_field)"
   274   shows   "Bseq (\<lambda>x. f x * g x)"
   275 proof -
   276   from assms obtain K1 K2 where K: "\<And>x. norm (f x) \<le> K1" "K1 > 0" "\<And>x. norm (g x) \<le> K2" "K2 > 0"
   277     unfolding Bseq_def by blast
   278   hence "\<And>x. norm (f x * g x) \<le> K1 * K2" by (auto simp: norm_mult intro!: mult_mono)
   279   thus ?thesis by (rule BseqI')
   280 qed
   281 
   282 lemma Bfun_const [simp]: "Bfun (\<lambda>_. c) F"
   283   unfolding Bfun_metric_def by (auto intro!: exI[of _ c] exI[of _ "1::real"])
   284 
   285 lemma Bseq_cmult_iff: "(c :: 'a :: real_normed_field) \<noteq> 0 \<Longrightarrow> Bseq (\<lambda>x. c * f x) \<longleftrightarrow> Bseq f"
   286 proof
   287   assume "c \<noteq> 0" "Bseq (\<lambda>x. c * f x)"
   288   find_theorems "Bfun (\<lambda>_. ?c) _"
   289   from Bfun_const this(2) have "Bseq (\<lambda>x. inverse c * (c * f x))" by (rule Bseq_mult)
   290   with \<open>c \<noteq> 0\<close> show "Bseq f" by (simp add: divide_simps)
   291 qed (intro Bseq_mult Bfun_const)
   292 
   293 lemma Bseq_subseq: "Bseq (f :: nat \<Rightarrow> 'a :: real_normed_vector) \<Longrightarrow> Bseq (\<lambda>x. f (g x))"
   294   unfolding Bseq_def by auto
   295 
   296 lemma Bseq_Suc_iff: "Bseq (\<lambda>n. f (Suc n)) \<longleftrightarrow> Bseq (f :: nat \<Rightarrow> 'a :: real_normed_vector)"
   297   using Bseq_offset[of f 1] by (auto intro: Bseq_subseq)
   298 
   299 lemma increasing_Bseq_subseq_iff:
   300   assumes "\<And>x y. x \<le> y \<Longrightarrow> norm (f x :: 'a :: real_normed_vector) \<le> norm (f y)" "subseq g"
   301   shows   "Bseq (\<lambda>x. f (g x)) \<longleftrightarrow> Bseq f"
   302 proof
   303   assume "Bseq (\<lambda>x. f (g x))"
   304   then obtain K where K: "\<And>x. norm (f (g x)) \<le> K" unfolding Bseq_def by auto
   305   {
   306     fix x :: nat
   307     from filterlim_subseq[OF assms(2)] obtain y where "g y \<ge> x"
   308       by (auto simp: filterlim_at_top eventually_at_top_linorder)
   309     hence "norm (f x) \<le> norm (f (g y))" using assms(1) by blast
   310     also have "norm (f (g y)) \<le> K" by (rule K)
   311     finally have "norm (f x) \<le> K" .
   312   }
   313   thus "Bseq f" by (rule BseqI')
   314 qed (insert Bseq_subseq[of f g], simp_all)
   315 
   316 lemma nonneg_incseq_Bseq_subseq_iff:
   317   assumes "\<And>x. f x \<ge> 0" "incseq (f :: nat \<Rightarrow> real)" "subseq g"
   318   shows   "Bseq (\<lambda>x. f (g x)) \<longleftrightarrow> Bseq f"
   319   using assms by (intro increasing_Bseq_subseq_iff) (auto simp: incseq_def)
   320 
   321 lemma Bseq_eq_bounded: "range f \<subseteq> {a .. b::real} \<Longrightarrow> Bseq f"
   322   apply (simp add: subset_eq)
   323   apply (rule BseqI'[where K="max (norm a) (norm b)"])
   324   apply (erule_tac x=n in allE)
   325   apply auto
   326   done
   327 
   328 lemma incseq_bounded: "incseq X \<Longrightarrow> \<forall>i. X i \<le> (B::real) \<Longrightarrow> Bseq X"
   329   by (intro Bseq_eq_bounded[of X "X 0" B]) (auto simp: incseq_def)
   330 
   331 lemma decseq_bounded: "decseq X \<Longrightarrow> \<forall>i. (B::real) \<le> X i \<Longrightarrow> Bseq X"
   332   by (intro Bseq_eq_bounded[of X B "X 0"]) (auto simp: decseq_def)
   333 
   334 subsection \<open>Bounded Monotonic Sequences\<close>
   335 
   336 subsubsection\<open>A Bounded and Monotonic Sequence Converges\<close>
   337 
   338 (* TODO: delete *)
   339 (* FIXME: one use in NSA/HSEQ.thy *)
   340 lemma Bmonoseq_LIMSEQ: "\<forall>n. m \<le> n --> X n = X m ==> \<exists>L. (X \<longlonglongrightarrow> L)"
   341   apply (rule_tac x="X m" in exI)
   342   apply (rule filterlim_cong[THEN iffD2, OF refl refl _ tendsto_const])
   343   unfolding eventually_sequentially
   344   apply blast
   345   done
   346 
   347 subsection \<open>Convergence to Zero\<close>
   348 
   349 definition Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   350   where "Zfun f F = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) F)"
   351 
   352 lemma ZfunI:
   353   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F) \<Longrightarrow> Zfun f F"
   354   unfolding Zfun_def by simp
   355 
   356 lemma ZfunD:
   357   "\<lbrakk>Zfun f F; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F"
   358   unfolding Zfun_def by simp
   359 
   360 lemma Zfun_ssubst:
   361   "eventually (\<lambda>x. f x = g x) F \<Longrightarrow> Zfun g F \<Longrightarrow> Zfun f F"
   362   unfolding Zfun_def by (auto elim!: eventually_rev_mp)
   363 
   364 lemma Zfun_zero: "Zfun (\<lambda>x. 0) F"
   365   unfolding Zfun_def by simp
   366 
   367 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) F = Zfun (\<lambda>x. f x) F"
   368   unfolding Zfun_def by simp
   369 
   370 lemma Zfun_imp_Zfun:
   371   assumes f: "Zfun f F"
   372   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) F"
   373   shows "Zfun (\<lambda>x. g x) F"
   374 proof (cases)
   375   assume K: "0 < K"
   376   show ?thesis
   377   proof (rule ZfunI)
   378     fix r::real assume "0 < r"
   379     hence "0 < r / K" using K by simp
   380     then have "eventually (\<lambda>x. norm (f x) < r / K) F"
   381       using ZfunD [OF f] by blast
   382     with g show "eventually (\<lambda>x. norm (g x) < r) F"
   383     proof eventually_elim
   384       case (elim x)
   385       hence "norm (f x) * K < r"
   386         by (simp add: pos_less_divide_eq K)
   387       thus ?case
   388         by (simp add: order_le_less_trans [OF elim(1)])
   389     qed
   390   qed
   391 next
   392   assume "\<not> 0 < K"
   393   hence K: "K \<le> 0" by (simp only: not_less)
   394   show ?thesis
   395   proof (rule ZfunI)
   396     fix r :: real
   397     assume "0 < r"
   398     from g show "eventually (\<lambda>x. norm (g x) < r) F"
   399     proof eventually_elim
   400       case (elim x)
   401       also have "norm (f x) * K \<le> norm (f x) * 0"
   402         using K norm_ge_zero by (rule mult_left_mono)
   403       finally show ?case
   404         using \<open>0 < r\<close> by simp
   405     qed
   406   qed
   407 qed
   408 
   409 lemma Zfun_le: "\<lbrakk>Zfun g F; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f F"
   410   by (erule_tac K="1" in Zfun_imp_Zfun, simp)
   411 
   412 lemma Zfun_add:
   413   assumes f: "Zfun f F" and g: "Zfun g F"
   414   shows "Zfun (\<lambda>x. f x + g x) F"
   415 proof (rule ZfunI)
   416   fix r::real assume "0 < r"
   417   hence r: "0 < r / 2" by simp
   418   have "eventually (\<lambda>x. norm (f x) < r/2) F"
   419     using f r by (rule ZfunD)
   420   moreover
   421   have "eventually (\<lambda>x. norm (g x) < r/2) F"
   422     using g r by (rule ZfunD)
   423   ultimately
   424   show "eventually (\<lambda>x. norm (f x + g x) < r) F"
   425   proof eventually_elim
   426     case (elim x)
   427     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
   428       by (rule norm_triangle_ineq)
   429     also have "\<dots> < r/2 + r/2"
   430       using elim by (rule add_strict_mono)
   431     finally show ?case
   432       by simp
   433   qed
   434 qed
   435 
   436 lemma Zfun_minus: "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. - f x) F"
   437   unfolding Zfun_def by simp
   438 
   439 lemma Zfun_diff: "\<lbrakk>Zfun f F; Zfun g F\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) F"
   440   using Zfun_add [of f F "\<lambda>x. - g x"] by (simp add: Zfun_minus)
   441 
   442 lemma (in bounded_linear) Zfun:
   443   assumes g: "Zfun g F"
   444   shows "Zfun (\<lambda>x. f (g x)) F"
   445 proof -
   446   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
   447     using bounded by blast
   448   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) F"
   449     by simp
   450   with g show ?thesis
   451     by (rule Zfun_imp_Zfun)
   452 qed
   453 
   454 lemma (in bounded_bilinear) Zfun:
   455   assumes f: "Zfun f F"
   456   assumes g: "Zfun g F"
   457   shows "Zfun (\<lambda>x. f x ** g x) F"
   458 proof (rule ZfunI)
   459   fix r::real assume r: "0 < r"
   460   obtain K where K: "0 < K"
   461     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   462     using pos_bounded by blast
   463   from K have K': "0 < inverse K"
   464     by (rule positive_imp_inverse_positive)
   465   have "eventually (\<lambda>x. norm (f x) < r) F"
   466     using f r by (rule ZfunD)
   467   moreover
   468   have "eventually (\<lambda>x. norm (g x) < inverse K) F"
   469     using g K' by (rule ZfunD)
   470   ultimately
   471   show "eventually (\<lambda>x. norm (f x ** g x) < r) F"
   472   proof eventually_elim
   473     case (elim x)
   474     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   475       by (rule norm_le)
   476     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
   477       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero elim K)
   478     also from K have "r * inverse K * K = r"
   479       by simp
   480     finally show ?case .
   481   qed
   482 qed
   483 
   484 lemma (in bounded_bilinear) Zfun_left:
   485   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. f x ** a) F"
   486   by (rule bounded_linear_left [THEN bounded_linear.Zfun])
   487 
   488 lemma (in bounded_bilinear) Zfun_right:
   489   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. a ** f x) F"
   490   by (rule bounded_linear_right [THEN bounded_linear.Zfun])
   491 
   492 lemmas Zfun_mult = bounded_bilinear.Zfun [OF bounded_bilinear_mult]
   493 lemmas Zfun_mult_right = bounded_bilinear.Zfun_right [OF bounded_bilinear_mult]
   494 lemmas Zfun_mult_left = bounded_bilinear.Zfun_left [OF bounded_bilinear_mult]
   495 
   496 lemma tendsto_Zfun_iff: "(f \<longlongrightarrow> a) F = Zfun (\<lambda>x. f x - a) F"
   497   by (simp only: tendsto_iff Zfun_def dist_norm)
   498 
   499 lemma tendsto_0_le: "\<lbrakk>(f \<longlongrightarrow> 0) F; eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) F\<rbrakk>
   500                      \<Longrightarrow> (g \<longlongrightarrow> 0) F"
   501   by (simp add: Zfun_imp_Zfun tendsto_Zfun_iff)
   502 
   503 subsubsection \<open>Distance and norms\<close>
   504 
   505 lemma tendsto_dist [tendsto_intros]:
   506   fixes l m :: "'a :: metric_space"
   507   assumes f: "(f \<longlongrightarrow> l) F" and g: "(g \<longlongrightarrow> m) F"
   508   shows "((\<lambda>x. dist (f x) (g x)) \<longlongrightarrow> dist l m) F"
   509 proof (rule tendstoI)
   510   fix e :: real assume "0 < e"
   511   hence e2: "0 < e/2" by simp
   512   from tendstoD [OF f e2] tendstoD [OF g e2]
   513   show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
   514   proof (eventually_elim)
   515     case (elim x)
   516     then show "dist (dist (f x) (g x)) (dist l m) < e"
   517       unfolding dist_real_def
   518       using dist_triangle2 [of "f x" "g x" "l"]
   519       using dist_triangle2 [of "g x" "l" "m"]
   520       using dist_triangle3 [of "l" "m" "f x"]
   521       using dist_triangle [of "f x" "m" "g x"]
   522       by arith
   523   qed
   524 qed
   525 
   526 lemma continuous_dist[continuous_intros]:
   527   fixes f g :: "_ \<Rightarrow> 'a :: metric_space"
   528   shows "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. dist (f x) (g x))"
   529   unfolding continuous_def by (rule tendsto_dist)
   530 
   531 lemma continuous_on_dist[continuous_intros]:
   532   fixes f g :: "_ \<Rightarrow> 'a :: metric_space"
   533   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. dist (f x) (g x))"
   534   unfolding continuous_on_def by (auto intro: tendsto_dist)
   535 
   536 lemma tendsto_norm [tendsto_intros]:
   537   "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. norm (f x)) \<longlongrightarrow> norm a) F"
   538   unfolding norm_conv_dist by (intro tendsto_intros)
   539 
   540 lemma continuous_norm [continuous_intros]:
   541   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. norm (f x))"
   542   unfolding continuous_def by (rule tendsto_norm)
   543 
   544 lemma continuous_on_norm [continuous_intros]:
   545   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. norm (f x))"
   546   unfolding continuous_on_def by (auto intro: tendsto_norm)
   547 
   548 lemma tendsto_norm_zero:
   549   "(f \<longlongrightarrow> 0) F \<Longrightarrow> ((\<lambda>x. norm (f x)) \<longlongrightarrow> 0) F"
   550   by (drule tendsto_norm, simp)
   551 
   552 lemma tendsto_norm_zero_cancel:
   553   "((\<lambda>x. norm (f x)) \<longlongrightarrow> 0) F \<Longrightarrow> (f \<longlongrightarrow> 0) F"
   554   unfolding tendsto_iff dist_norm by simp
   555 
   556 lemma tendsto_norm_zero_iff:
   557   "((\<lambda>x. norm (f x)) \<longlongrightarrow> 0) F \<longleftrightarrow> (f \<longlongrightarrow> 0) F"
   558   unfolding tendsto_iff dist_norm by simp
   559 
   560 lemma tendsto_rabs [tendsto_intros]:
   561   "(f \<longlongrightarrow> (l::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) \<longlongrightarrow> \<bar>l\<bar>) F"
   562   by (fold real_norm_def, rule tendsto_norm)
   563 
   564 lemma continuous_rabs [continuous_intros]:
   565   "continuous F f \<Longrightarrow> continuous F (\<lambda>x. \<bar>f x :: real\<bar>)"
   566   unfolding real_norm_def[symmetric] by (rule continuous_norm)
   567 
   568 lemma continuous_on_rabs [continuous_intros]:
   569   "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. \<bar>f x :: real\<bar>)"
   570   unfolding real_norm_def[symmetric] by (rule continuous_on_norm)
   571 
   572 lemma tendsto_rabs_zero:
   573   "(f \<longlongrightarrow> (0::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) \<longlongrightarrow> 0) F"
   574   by (fold real_norm_def, rule tendsto_norm_zero)
   575 
   576 lemma tendsto_rabs_zero_cancel:
   577   "((\<lambda>x. \<bar>f x\<bar>) \<longlongrightarrow> (0::real)) F \<Longrightarrow> (f \<longlongrightarrow> 0) F"
   578   by (fold real_norm_def, rule tendsto_norm_zero_cancel)
   579 
   580 lemma tendsto_rabs_zero_iff:
   581   "((\<lambda>x. \<bar>f x\<bar>) \<longlongrightarrow> (0::real)) F \<longleftrightarrow> (f \<longlongrightarrow> 0) F"
   582   by (fold real_norm_def, rule tendsto_norm_zero_iff)
   583 
   584 subsection \<open>Topological Monoid\<close>
   585 
   586 class topological_monoid_add = topological_space + monoid_add +
   587   assumes tendsto_add_Pair: "LIM x (nhds a \<times>\<^sub>F nhds b). fst x + snd x :> nhds (a + b)"
   588 
   589 class topological_comm_monoid_add = topological_monoid_add + comm_monoid_add
   590 
   591 lemma tendsto_add [tendsto_intros]:
   592   fixes a b :: "'a::topological_monoid_add"
   593   shows "(f \<longlongrightarrow> a) F \<Longrightarrow> (g \<longlongrightarrow> b) F \<Longrightarrow> ((\<lambda>x. f x + g x) \<longlongrightarrow> a + b) F"
   594   using filterlim_compose[OF tendsto_add_Pair, of "\<lambda>x. (f x, g x)" a b F]
   595   by (simp add: nhds_prod[symmetric] tendsto_Pair)
   596 
   597 lemma continuous_add [continuous_intros]:
   598   fixes f g :: "_ \<Rightarrow> 'b::topological_monoid_add"
   599   shows "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. f x + g x)"
   600   unfolding continuous_def by (rule tendsto_add)
   601 
   602 lemma continuous_on_add [continuous_intros]:
   603   fixes f g :: "_ \<Rightarrow> 'b::topological_monoid_add"
   604   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f x + g x)"
   605   unfolding continuous_on_def by (auto intro: tendsto_add)
   606 
   607 lemma tendsto_add_zero:
   608   fixes f g :: "_ \<Rightarrow> 'b::topological_monoid_add"
   609   shows "\<lbrakk>(f \<longlongrightarrow> 0) F; (g \<longlongrightarrow> 0) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) \<longlongrightarrow> 0) F"
   610   by (drule (1) tendsto_add, simp)
   611 
   612 lemma tendsto_setsum [tendsto_intros]:
   613   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::topological_comm_monoid_add"
   614   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i \<longlongrightarrow> a i) F"
   615   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) \<longlongrightarrow> (\<Sum>i\<in>S. a i)) F"
   616 proof (cases "finite S")
   617   assume "finite S" thus ?thesis using assms
   618     by (induct, simp, simp add: tendsto_add)
   619 qed simp
   620 
   621 lemma continuous_setsum [continuous_intros]:
   622   fixes f :: "'a \<Rightarrow> 'b::t2_space \<Rightarrow> 'c::topological_comm_monoid_add"
   623   shows "(\<And>i. i \<in> S \<Longrightarrow> continuous F (f i)) \<Longrightarrow> continuous F (\<lambda>x. \<Sum>i\<in>S. f i x)"
   624   unfolding continuous_def by (rule tendsto_setsum)
   625 
   626 lemma continuous_on_setsum [continuous_intros]:
   627   fixes f :: "'a \<Rightarrow> 'b::topological_space \<Rightarrow> 'c::topological_comm_monoid_add"
   628   shows "(\<And>i. i \<in> S \<Longrightarrow> continuous_on s (f i)) \<Longrightarrow> continuous_on s (\<lambda>x. \<Sum>i\<in>S. f i x)"
   629   unfolding continuous_on_def by (auto intro: tendsto_setsum)
   630 
   631 instance nat :: topological_comm_monoid_add
   632   proof qed (simp add: nhds_discrete principal_prod_principal filterlim_principal eventually_principal)
   633 
   634 instance int :: topological_comm_monoid_add
   635   proof qed (simp add: nhds_discrete principal_prod_principal filterlim_principal eventually_principal)
   636 
   637 subsubsection \<open>Topological group\<close>
   638 
   639 class topological_group_add = topological_monoid_add + group_add +
   640   assumes tendsto_uminus_nhds: "(uminus \<longlongrightarrow> - a) (nhds a)"
   641 begin
   642 
   643 lemma tendsto_minus [tendsto_intros]: "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. - f x) \<longlongrightarrow> -a) F"
   644   by (rule filterlim_compose[OF tendsto_uminus_nhds])
   645 
   646 end
   647 
   648 class topological_ab_group_add = topological_group_add + ab_group_add
   649 
   650 instance topological_ab_group_add < topological_comm_monoid_add ..
   651 
   652 lemma continuous_minus [continuous_intros]:
   653   fixes f :: "'a::t2_space \<Rightarrow> 'b::topological_group_add"
   654   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. - f x)"
   655   unfolding continuous_def by (rule tendsto_minus)
   656 
   657 lemma continuous_on_minus [continuous_intros]:
   658   fixes f :: "_ \<Rightarrow> 'b::topological_group_add"
   659   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. - f x)"
   660   unfolding continuous_on_def by (auto intro: tendsto_minus)
   661 
   662 lemma tendsto_minus_cancel:
   663   fixes a :: "'a::topological_group_add"
   664   shows "((\<lambda>x. - f x) \<longlongrightarrow> - a) F \<Longrightarrow> (f \<longlongrightarrow> a) F"
   665   by (drule tendsto_minus, simp)
   666 
   667 lemma tendsto_minus_cancel_left:
   668     "(f \<longlongrightarrow> - (y::_::topological_group_add)) F \<longleftrightarrow> ((\<lambda>x. - f x) \<longlongrightarrow> y) F"
   669   using tendsto_minus_cancel[of f "- y" F]  tendsto_minus[of f "- y" F]
   670   by auto
   671 
   672 lemma tendsto_diff [tendsto_intros]:
   673   fixes a b :: "'a::topological_group_add"
   674   shows "\<lbrakk>(f \<longlongrightarrow> a) F; (g \<longlongrightarrow> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) \<longlongrightarrow> a - b) F"
   675   using tendsto_add [of f a F "\<lambda>x. - g x" "- b"] by (simp add: tendsto_minus)
   676 
   677 lemma continuous_diff [continuous_intros]:
   678   fixes f g :: "'a::t2_space \<Rightarrow> 'b::topological_group_add"
   679   shows "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. f x - g x)"
   680   unfolding continuous_def by (rule tendsto_diff)
   681 
   682 lemma continuous_on_diff [continuous_intros]:
   683   fixes f g :: "_ \<Rightarrow> 'b::topological_group_add"
   684   shows "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f x - g x)"
   685   unfolding continuous_on_def by (auto intro: tendsto_diff)
   686 
   687 lemma continuous_on_op_minus: "continuous_on (s::'a::topological_group_add set) (op - x)"
   688   by (rule continuous_intros | simp)+
   689 
   690 instance real_normed_vector < topological_ab_group_add
   691 proof
   692   fix a b :: 'a show "((\<lambda>x. fst x + snd x) \<longlongrightarrow> a + b) (nhds a \<times>\<^sub>F nhds b)"
   693     unfolding tendsto_Zfun_iff add_diff_add
   694     using tendsto_fst[OF filterlim_ident, of "(a,b)"] tendsto_snd[OF filterlim_ident, of "(a,b)"]
   695     by (intro Zfun_add)
   696        (auto simp add: tendsto_Zfun_iff[symmetric] nhds_prod[symmetric] intro!: tendsto_fst)
   697   show "(uminus \<longlongrightarrow> - a) (nhds a)"
   698     unfolding tendsto_Zfun_iff minus_diff_minus
   699     using filterlim_ident[of "nhds a"]
   700     by (intro Zfun_minus) (simp add: tendsto_Zfun_iff)
   701 qed
   702 
   703 lemmas real_tendsto_sandwich = tendsto_sandwich[where 'b=real]
   704 
   705 subsubsection \<open>Linear operators and multiplication\<close>
   706 
   707 lemma linear_times:
   708   fixes c::"'a::real_algebra" shows "linear (\<lambda>x. c * x)"
   709   by (auto simp: linearI distrib_left)
   710 
   711 lemma (in bounded_linear) tendsto:
   712   "(g \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. f (g x)) \<longlongrightarrow> f a) F"
   713   by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
   714 
   715 lemma (in bounded_linear) continuous:
   716   "continuous F g \<Longrightarrow> continuous F (\<lambda>x. f (g x))"
   717   using tendsto[of g _ F] by (auto simp: continuous_def)
   718 
   719 lemma (in bounded_linear) continuous_on:
   720   "continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f (g x))"
   721   using tendsto[of g] by (auto simp: continuous_on_def)
   722 
   723 lemma (in bounded_linear) tendsto_zero:
   724   "(g \<longlongrightarrow> 0) F \<Longrightarrow> ((\<lambda>x. f (g x)) \<longlongrightarrow> 0) F"
   725   by (drule tendsto, simp only: zero)
   726 
   727 lemma (in bounded_bilinear) tendsto:
   728   "\<lbrakk>(f \<longlongrightarrow> a) F; (g \<longlongrightarrow> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) \<longlongrightarrow> a ** b) F"
   729   by (simp only: tendsto_Zfun_iff prod_diff_prod
   730                  Zfun_add Zfun Zfun_left Zfun_right)
   731 
   732 lemma (in bounded_bilinear) continuous:
   733   "continuous F f \<Longrightarrow> continuous F g \<Longrightarrow> continuous F (\<lambda>x. f x ** g x)"
   734   using tendsto[of f _ F g] by (auto simp: continuous_def)
   735 
   736 lemma (in bounded_bilinear) continuous_on:
   737   "continuous_on s f \<Longrightarrow> continuous_on s g \<Longrightarrow> continuous_on s (\<lambda>x. f x ** g x)"
   738   using tendsto[of f _ _ g] by (auto simp: continuous_on_def)
   739 
   740 lemma (in bounded_bilinear) tendsto_zero:
   741   assumes f: "(f \<longlongrightarrow> 0) F"
   742   assumes g: "(g \<longlongrightarrow> 0) F"
   743   shows "((\<lambda>x. f x ** g x) \<longlongrightarrow> 0) F"
   744   using tendsto [OF f g] by (simp add: zero_left)
   745 
   746 lemma (in bounded_bilinear) tendsto_left_zero:
   747   "(f \<longlongrightarrow> 0) F \<Longrightarrow> ((\<lambda>x. f x ** c) \<longlongrightarrow> 0) F"
   748   by (rule bounded_linear.tendsto_zero [OF bounded_linear_left])
   749 
   750 lemma (in bounded_bilinear) tendsto_right_zero:
   751   "(f \<longlongrightarrow> 0) F \<Longrightarrow> ((\<lambda>x. c ** f x) \<longlongrightarrow> 0) F"
   752   by (rule bounded_linear.tendsto_zero [OF bounded_linear_right])
   753 
   754 lemmas tendsto_of_real [tendsto_intros] =
   755   bounded_linear.tendsto [OF bounded_linear_of_real]
   756 
   757 lemmas tendsto_scaleR [tendsto_intros] =
   758   bounded_bilinear.tendsto [OF bounded_bilinear_scaleR]
   759 
   760 lemmas tendsto_mult [tendsto_intros] =
   761   bounded_bilinear.tendsto [OF bounded_bilinear_mult]
   762 
   763 lemma tendsto_mult_left:
   764   fixes c::"'a::real_normed_algebra"
   765   shows "(f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. c * (f x)) \<longlongrightarrow> c * l) F"
   766 by (rule tendsto_mult [OF tendsto_const])
   767 
   768 lemma tendsto_mult_right:
   769   fixes c::"'a::real_normed_algebra"
   770   shows "(f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. (f x) * c) \<longlongrightarrow> l * c) F"
   771 by (rule tendsto_mult [OF _ tendsto_const])
   772 
   773 lemmas continuous_of_real [continuous_intros] =
   774   bounded_linear.continuous [OF bounded_linear_of_real]
   775 
   776 lemmas continuous_scaleR [continuous_intros] =
   777   bounded_bilinear.continuous [OF bounded_bilinear_scaleR]
   778 
   779 lemmas continuous_mult [continuous_intros] =
   780   bounded_bilinear.continuous [OF bounded_bilinear_mult]
   781 
   782 lemmas continuous_on_of_real [continuous_intros] =
   783   bounded_linear.continuous_on [OF bounded_linear_of_real]
   784 
   785 lemmas continuous_on_scaleR [continuous_intros] =
   786   bounded_bilinear.continuous_on [OF bounded_bilinear_scaleR]
   787 
   788 lemmas continuous_on_mult [continuous_intros] =
   789   bounded_bilinear.continuous_on [OF bounded_bilinear_mult]
   790 
   791 lemmas tendsto_mult_zero =
   792   bounded_bilinear.tendsto_zero [OF bounded_bilinear_mult]
   793 
   794 lemmas tendsto_mult_left_zero =
   795   bounded_bilinear.tendsto_left_zero [OF bounded_bilinear_mult]
   796 
   797 lemmas tendsto_mult_right_zero =
   798   bounded_bilinear.tendsto_right_zero [OF bounded_bilinear_mult]
   799 
   800 lemma tendsto_power [tendsto_intros]:
   801   fixes f :: "'a \<Rightarrow> 'b::{power,real_normed_algebra}"
   802   shows "(f \<longlongrightarrow> a) F \<Longrightarrow> ((\<lambda>x. f x ^ n) \<longlongrightarrow> a ^ n) F"
   803   by (induct n) (simp_all add: tendsto_mult)
   804 
   805 lemma continuous_power [continuous_intros]:
   806   fixes f :: "'a::t2_space \<Rightarrow> 'b::{power,real_normed_algebra}"
   807   shows "continuous F f \<Longrightarrow> continuous F (\<lambda>x. (f x)^n)"
   808   unfolding continuous_def by (rule tendsto_power)
   809 
   810 lemma continuous_on_power [continuous_intros]:
   811   fixes f :: "_ \<Rightarrow> 'b::{power,real_normed_algebra}"
   812   shows "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. (f x)^n)"
   813   unfolding continuous_on_def by (auto intro: tendsto_power)
   814 
   815 lemma tendsto_setprod [tendsto_intros]:
   816   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
   817   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i \<longlongrightarrow> L i) F"
   818   shows "((\<lambda>x. \<Prod>i\<in>S. f i x) \<longlongrightarrow> (\<Prod>i\<in>S. L i)) F"
   819 proof (cases "finite S")
   820   assume "finite S" thus ?thesis using assms
   821     by (induct, simp, simp add: tendsto_mult)
   822 qed simp
   823 
   824 lemma continuous_setprod [continuous_intros]:
   825   fixes f :: "'a \<Rightarrow> 'b::t2_space \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
   826   shows "(\<And>i. i \<in> S \<Longrightarrow> continuous F (f i)) \<Longrightarrow> continuous F (\<lambda>x. \<Prod>i\<in>S. f i x)"
   827   unfolding continuous_def by (rule tendsto_setprod)
   828 
   829 lemma continuous_on_setprod [continuous_intros]:
   830   fixes f :: "'a \<Rightarrow> _ \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
   831   shows "(\<And>i. i \<in> S \<Longrightarrow> continuous_on s (f i)) \<Longrightarrow> continuous_on s (\<lambda>x. \<Prod>i\<in>S. f i x)"
   832   unfolding continuous_on_def by (auto intro: tendsto_setprod)
   833 
   834 lemma tendsto_of_real_iff:
   835   "((\<lambda>x. of_real (f x) :: 'a :: real_normed_div_algebra) \<longlongrightarrow> of_real c) F \<longleftrightarrow> (f \<longlongrightarrow> c) F"
   836   unfolding tendsto_iff by simp
   837 
   838 lemma tendsto_add_const_iff:
   839   "((\<lambda>x. c + f x :: 'a :: real_normed_vector) \<longlongrightarrow> c + d) F \<longleftrightarrow> (f \<longlongrightarrow> d) F"
   840   using tendsto_add[OF tendsto_const[of c], of f d]
   841         tendsto_add[OF tendsto_const[of "-c"], of "\<lambda>x. c + f x" "c + d"] by auto
   842 
   843 
   844 subsubsection \<open>Inverse and division\<close>
   845 
   846 lemma (in bounded_bilinear) Zfun_prod_Bfun:
   847   assumes f: "Zfun f F"
   848   assumes g: "Bfun g F"
   849   shows "Zfun (\<lambda>x. f x ** g x) F"
   850 proof -
   851   obtain K where K: "0 \<le> K"
   852     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   853     using nonneg_bounded by blast
   854   obtain B where B: "0 < B"
   855     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) F"
   856     using g by (rule BfunE)
   857   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) F"
   858   using norm_g proof eventually_elim
   859     case (elim x)
   860     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   861       by (rule norm_le)
   862     also have "\<dots> \<le> norm (f x) * B * K"
   863       by (intro mult_mono' order_refl norm_g norm_ge_zero
   864                 mult_nonneg_nonneg K elim)
   865     also have "\<dots> = norm (f x) * (B * K)"
   866       by (rule mult.assoc)
   867     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
   868   qed
   869   with f show ?thesis
   870     by (rule Zfun_imp_Zfun)
   871 qed
   872 
   873 lemma (in bounded_bilinear) Bfun_prod_Zfun:
   874   assumes f: "Bfun f F"
   875   assumes g: "Zfun g F"
   876   shows "Zfun (\<lambda>x. f x ** g x) F"
   877   using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
   878 
   879 lemma Bfun_inverse_lemma:
   880   fixes x :: "'a::real_normed_div_algebra"
   881   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
   882   apply (subst nonzero_norm_inverse, clarsimp)
   883   apply (erule (1) le_imp_inverse_le)
   884   done
   885 
   886 lemma Bfun_inverse:
   887   fixes a :: "'a::real_normed_div_algebra"
   888   assumes f: "(f \<longlongrightarrow> a) F"
   889   assumes a: "a \<noteq> 0"
   890   shows "Bfun (\<lambda>x. inverse (f x)) F"
   891 proof -
   892   from a have "0 < norm a" by simp
   893   hence "\<exists>r>0. r < norm a" by (rule dense)
   894   then obtain r where r1: "0 < r" and r2: "r < norm a" by blast
   895   have "eventually (\<lambda>x. dist (f x) a < r) F"
   896     using tendstoD [OF f r1] by blast
   897   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) F"
   898   proof eventually_elim
   899     case (elim x)
   900     hence 1: "norm (f x - a) < r"
   901       by (simp add: dist_norm)
   902     hence 2: "f x \<noteq> 0" using r2 by auto
   903     hence "norm (inverse (f x)) = inverse (norm (f x))"
   904       by (rule nonzero_norm_inverse)
   905     also have "\<dots> \<le> inverse (norm a - r)"
   906     proof (rule le_imp_inverse_le)
   907       show "0 < norm a - r" using r2 by simp
   908     next
   909       have "norm a - norm (f x) \<le> norm (a - f x)"
   910         by (rule norm_triangle_ineq2)
   911       also have "\<dots> = norm (f x - a)"
   912         by (rule norm_minus_commute)
   913       also have "\<dots> < r" using 1 .
   914       finally show "norm a - r \<le> norm (f x)" by simp
   915     qed
   916     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
   917   qed
   918   thus ?thesis by (rule BfunI)
   919 qed
   920 
   921 lemma tendsto_inverse [tendsto_intros]:
   922   fixes a :: "'a::real_normed_div_algebra"
   923   assumes f: "(f \<longlongrightarrow> a) F"
   924   assumes a: "a \<noteq> 0"
   925   shows "((\<lambda>x. inverse (f x)) \<longlongrightarrow> inverse a) F"
   926 proof -
   927   from a have "0 < norm a" by simp
   928   with f have "eventually (\<lambda>x. dist (f x) a < norm a) F"
   929     by (rule tendstoD)
   930   then have "eventually (\<lambda>x. f x \<noteq> 0) F"
   931     unfolding dist_norm by (auto elim!: eventually_mono)
   932   with a have "eventually (\<lambda>x. inverse (f x) - inverse a =
   933     - (inverse (f x) * (f x - a) * inverse a)) F"
   934     by (auto elim!: eventually_mono simp: inverse_diff_inverse)
   935   moreover have "Zfun (\<lambda>x. - (inverse (f x) * (f x - a) * inverse a)) F"
   936     by (intro Zfun_minus Zfun_mult_left
   937       bounded_bilinear.Bfun_prod_Zfun [OF bounded_bilinear_mult]
   938       Bfun_inverse [OF f a] f [unfolded tendsto_Zfun_iff])
   939   ultimately show ?thesis
   940     unfolding tendsto_Zfun_iff by (rule Zfun_ssubst)
   941 qed
   942 
   943 lemma continuous_inverse:
   944   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"
   945   assumes "continuous F f" and "f (Lim F (\<lambda>x. x)) \<noteq> 0"
   946   shows "continuous F (\<lambda>x. inverse (f x))"
   947   using assms unfolding continuous_def by (rule tendsto_inverse)
   948 
   949 lemma continuous_at_within_inverse[continuous_intros]:
   950   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"
   951   assumes "continuous (at a within s) f" and "f a \<noteq> 0"
   952   shows "continuous (at a within s) (\<lambda>x. inverse (f x))"
   953   using assms unfolding continuous_within by (rule tendsto_inverse)
   954 
   955 lemma isCont_inverse[continuous_intros, simp]:
   956   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_div_algebra"
   957   assumes "isCont f a" and "f a \<noteq> 0"
   958   shows "isCont (\<lambda>x. inverse (f x)) a"
   959   using assms unfolding continuous_at by (rule tendsto_inverse)
   960 
   961 lemma continuous_on_inverse[continuous_intros]:
   962   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_div_algebra"
   963   assumes "continuous_on s f" and "\<forall>x\<in>s. f x \<noteq> 0"
   964   shows "continuous_on s (\<lambda>x. inverse (f x))"
   965   using assms unfolding continuous_on_def by (blast intro: tendsto_inverse)
   966 
   967 lemma tendsto_divide [tendsto_intros]:
   968   fixes a b :: "'a::real_normed_field"
   969   shows "\<lbrakk>(f \<longlongrightarrow> a) F; (g \<longlongrightarrow> b) F; b \<noteq> 0\<rbrakk>
   970     \<Longrightarrow> ((\<lambda>x. f x / g x) \<longlongrightarrow> a / b) F"
   971   by (simp add: tendsto_mult tendsto_inverse divide_inverse)
   972 
   973 lemma continuous_divide:
   974   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_field"
   975   assumes "continuous F f" and "continuous F g" and "g (Lim F (\<lambda>x. x)) \<noteq> 0"
   976   shows "continuous F (\<lambda>x. (f x) / (g x))"
   977   using assms unfolding continuous_def by (rule tendsto_divide)
   978 
   979 lemma continuous_at_within_divide[continuous_intros]:
   980   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_field"
   981   assumes "continuous (at a within s) f" "continuous (at a within s) g" and "g a \<noteq> 0"
   982   shows "continuous (at a within s) (\<lambda>x. (f x) / (g x))"
   983   using assms unfolding continuous_within by (rule tendsto_divide)
   984 
   985 lemma isCont_divide[continuous_intros, simp]:
   986   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_field"
   987   assumes "isCont f a" "isCont g a" "g a \<noteq> 0"
   988   shows "isCont (\<lambda>x. (f x) / g x) a"
   989   using assms unfolding continuous_at by (rule tendsto_divide)
   990 
   991 lemma continuous_on_divide[continuous_intros]:
   992   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_field"
   993   assumes "continuous_on s f" "continuous_on s g" and "\<forall>x\<in>s. g x \<noteq> 0"
   994   shows "continuous_on s (\<lambda>x. (f x) / (g x))"
   995   using assms unfolding continuous_on_def by (blast intro: tendsto_divide)
   996 
   997 lemma tendsto_sgn [tendsto_intros]:
   998   fixes l :: "'a::real_normed_vector"
   999   shows "\<lbrakk>(f \<longlongrightarrow> l) F; l \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. sgn (f x)) \<longlongrightarrow> sgn l) F"
  1000   unfolding sgn_div_norm by (simp add: tendsto_intros)
  1001 
  1002 lemma continuous_sgn:
  1003   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  1004   assumes "continuous F f" and "f (Lim F (\<lambda>x. x)) \<noteq> 0"
  1005   shows "continuous F (\<lambda>x. sgn (f x))"
  1006   using assms unfolding continuous_def by (rule tendsto_sgn)
  1007 
  1008 lemma continuous_at_within_sgn[continuous_intros]:
  1009   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  1010   assumes "continuous (at a within s) f" and "f a \<noteq> 0"
  1011   shows "continuous (at a within s) (\<lambda>x. sgn (f x))"
  1012   using assms unfolding continuous_within by (rule tendsto_sgn)
  1013 
  1014 lemma isCont_sgn[continuous_intros]:
  1015   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  1016   assumes "isCont f a" and "f a \<noteq> 0"
  1017   shows "isCont (\<lambda>x. sgn (f x)) a"
  1018   using assms unfolding continuous_at by (rule tendsto_sgn)
  1019 
  1020 lemma continuous_on_sgn[continuous_intros]:
  1021   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  1022   assumes "continuous_on s f" and "\<forall>x\<in>s. f x \<noteq> 0"
  1023   shows "continuous_on s (\<lambda>x. sgn (f x))"
  1024   using assms unfolding continuous_on_def by (blast intro: tendsto_sgn)
  1025 
  1026 lemma filterlim_at_infinity:
  1027   fixes f :: "_ \<Rightarrow> 'a::real_normed_vector"
  1028   assumes "0 \<le> c"
  1029   shows "(LIM x F. f x :> at_infinity) \<longleftrightarrow> (\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F)"
  1030   unfolding filterlim_iff eventually_at_infinity
  1031 proof safe
  1032   fix P :: "'a \<Rightarrow> bool" and b
  1033   assume *: "\<forall>r>c. eventually (\<lambda>x. r \<le> norm (f x)) F"
  1034     and P: "\<forall>x. b \<le> norm x \<longrightarrow> P x"
  1035   have "max b (c + 1) > c" by auto
  1036   with * have "eventually (\<lambda>x. max b (c + 1) \<le> norm (f x)) F"
  1037     by auto
  1038   then show "eventually (\<lambda>x. P (f x)) F"
  1039   proof eventually_elim
  1040     fix x assume "max b (c + 1) \<le> norm (f x)"
  1041     with P show "P (f x)" by auto
  1042   qed
  1043 qed force
  1044 
  1045 lemma not_tendsto_and_filterlim_at_infinity:
  1046   assumes "F \<noteq> bot"
  1047   assumes "(f \<longlongrightarrow> (c :: 'a :: real_normed_vector)) F"
  1048   assumes "filterlim f at_infinity F"
  1049   shows   False
  1050 proof -
  1051   from tendstoD[OF assms(2), of "1/2"]
  1052     have "eventually (\<lambda>x. dist (f x) c < 1/2) F" by simp
  1053   moreover from filterlim_at_infinity[of "norm c" f F] assms(3)
  1054     have "eventually (\<lambda>x. norm (f x) \<ge> norm c + 1) F" by simp
  1055   ultimately have "eventually (\<lambda>x. False) F"
  1056   proof eventually_elim
  1057     fix x assume A: "dist (f x) c < 1/2" and B: "norm (f x) \<ge> norm c + 1"
  1058     note B
  1059     also have "norm (f x) = dist (f x) 0" by simp
  1060     also have "... \<le> dist (f x) c + dist c 0" by (rule dist_triangle)
  1061     finally show False using A by simp
  1062   qed
  1063   with assms show False by simp
  1064 qed
  1065 
  1066 lemma filterlim_at_infinity_imp_not_convergent:
  1067   assumes "filterlim f at_infinity sequentially"
  1068   shows   "\<not>convergent f"
  1069   by (rule notI, rule not_tendsto_and_filterlim_at_infinity[OF _ _ assms])
  1070      (simp_all add: convergent_LIMSEQ_iff)
  1071 
  1072 lemma filterlim_at_infinity_imp_eventually_ne:
  1073   assumes "filterlim f at_infinity F"
  1074   shows   "eventually (\<lambda>z. f z \<noteq> c) F"
  1075 proof -
  1076   have "norm c + 1 > 0" by (intro add_nonneg_pos) simp_all
  1077   with filterlim_at_infinity[OF order.refl, of f F] assms
  1078     have "eventually (\<lambda>z. norm (f z) \<ge> norm c + 1) F" by blast
  1079   thus ?thesis by eventually_elim auto
  1080 qed
  1081 
  1082 lemma tendsto_of_nat [tendsto_intros]:
  1083   "filterlim (of_nat :: nat \<Rightarrow> 'a :: real_normed_algebra_1) at_infinity sequentially"
  1084 proof (subst filterlim_at_infinity[OF order.refl], intro allI impI)
  1085   fix r :: real
  1086   assume r: "r > 0"
  1087   define n where "n = nat \<lceil>r\<rceil>"
  1088   from r have n: "\<forall>m\<ge>n. of_nat m \<ge> r" unfolding n_def by linarith
  1089   from eventually_ge_at_top[of n] show "eventually (\<lambda>m. norm (of_nat m :: 'a) \<ge> r) sequentially"
  1090     by eventually_elim (insert n, simp_all)
  1091 qed
  1092 
  1093 
  1094 subsection \<open>Relate @{const at}, @{const at_left} and @{const at_right}\<close>
  1095 
  1096 text \<open>
  1097 
  1098 This lemmas are useful for conversion between @{term "at x"} to @{term "at_left x"} and
  1099 @{term "at_right x"} and also @{term "at_right 0"}.
  1100 
  1101 \<close>
  1102 
  1103 lemmas filterlim_split_at_real = filterlim_split_at[where 'a=real]
  1104 
  1105 lemma filtermap_nhds_shift: "filtermap (\<lambda>x. x - d) (nhds a) = nhds (a - d::'a::real_normed_vector)"
  1106   by (rule filtermap_fun_inverse[where g="\<lambda>x. x + d"])
  1107      (auto intro!: tendsto_eq_intros filterlim_ident)
  1108 
  1109 lemma filtermap_nhds_minus: "filtermap (\<lambda>x. - x) (nhds a) = nhds (- a::'a::real_normed_vector)"
  1110   by (rule filtermap_fun_inverse[where g=uminus])
  1111      (auto intro!: tendsto_eq_intros filterlim_ident)
  1112 
  1113 lemma filtermap_at_shift: "filtermap (\<lambda>x. x - d) (at a) = at (a - d::'a::real_normed_vector)"
  1114   by (simp add: filter_eq_iff eventually_filtermap eventually_at_filter filtermap_nhds_shift[symmetric])
  1115 
  1116 lemma filtermap_at_right_shift: "filtermap (\<lambda>x. x - d) (at_right a) = at_right (a - d::real)"
  1117   by (simp add: filter_eq_iff eventually_filtermap eventually_at_filter filtermap_nhds_shift[symmetric])
  1118 
  1119 lemma at_right_to_0: "at_right (a::real) = filtermap (\<lambda>x. x + a) (at_right 0)"
  1120   using filtermap_at_right_shift[of "-a" 0] by simp
  1121 
  1122 lemma filterlim_at_right_to_0:
  1123   "filterlim f F (at_right (a::real)) \<longleftrightarrow> filterlim (\<lambda>x. f (x + a)) F (at_right 0)"
  1124   unfolding filterlim_def filtermap_filtermap at_right_to_0[of a] ..
  1125 
  1126 lemma eventually_at_right_to_0:
  1127   "eventually P (at_right (a::real)) \<longleftrightarrow> eventually (\<lambda>x. P (x + a)) (at_right 0)"
  1128   unfolding at_right_to_0[of a] by (simp add: eventually_filtermap)
  1129 
  1130 lemma filtermap_at_minus: "filtermap (\<lambda>x. - x) (at a) = at (- a::'a::real_normed_vector)"
  1131   by (simp add: filter_eq_iff eventually_filtermap eventually_at_filter filtermap_nhds_minus[symmetric])
  1132 
  1133 lemma at_left_minus: "at_left (a::real) = filtermap (\<lambda>x. - x) (at_right (- a))"
  1134   by (simp add: filter_eq_iff eventually_filtermap eventually_at_filter filtermap_nhds_minus[symmetric])
  1135 
  1136 lemma at_right_minus: "at_right (a::real) = filtermap (\<lambda>x. - x) (at_left (- a))"
  1137   by (simp add: filter_eq_iff eventually_filtermap eventually_at_filter filtermap_nhds_minus[symmetric])
  1138 
  1139 lemma filterlim_at_left_to_right:
  1140   "filterlim f F (at_left (a::real)) \<longleftrightarrow> filterlim (\<lambda>x. f (- x)) F (at_right (-a))"
  1141   unfolding filterlim_def filtermap_filtermap at_left_minus[of a] ..
  1142 
  1143 lemma eventually_at_left_to_right:
  1144   "eventually P (at_left (a::real)) \<longleftrightarrow> eventually (\<lambda>x. P (- x)) (at_right (-a))"
  1145   unfolding at_left_minus[of a] by (simp add: eventually_filtermap)
  1146 
  1147 lemma filterlim_uminus_at_top_at_bot: "LIM x at_bot. - x :: real :> at_top"
  1148   unfolding filterlim_at_top eventually_at_bot_dense
  1149   by (metis leI minus_less_iff order_less_asym)
  1150 
  1151 lemma filterlim_uminus_at_bot_at_top: "LIM x at_top. - x :: real :> at_bot"
  1152   unfolding filterlim_at_bot eventually_at_top_dense
  1153   by (metis leI less_minus_iff order_less_asym)
  1154 
  1155 lemma at_top_mirror: "at_top = filtermap uminus (at_bot :: real filter)"
  1156   by (rule filtermap_fun_inverse[symmetric, of uminus])
  1157      (auto intro: filterlim_uminus_at_bot_at_top filterlim_uminus_at_top_at_bot)
  1158 
  1159 lemma at_bot_mirror: "at_bot = filtermap uminus (at_top :: real filter)"
  1160   unfolding at_top_mirror filtermap_filtermap by (simp add: filtermap_ident)
  1161 
  1162 lemma filterlim_at_top_mirror: "(LIM x at_top. f x :> F) \<longleftrightarrow> (LIM x at_bot. f (-x::real) :> F)"
  1163   unfolding filterlim_def at_top_mirror filtermap_filtermap ..
  1164 
  1165 lemma filterlim_at_bot_mirror: "(LIM x at_bot. f x :> F) \<longleftrightarrow> (LIM x at_top. f (-x::real) :> F)"
  1166   unfolding filterlim_def at_bot_mirror filtermap_filtermap ..
  1167 
  1168 lemma filterlim_uminus_at_top: "(LIM x F. f x :> at_top) \<longleftrightarrow> (LIM x F. - (f x) :: real :> at_bot)"
  1169   using filterlim_compose[OF filterlim_uminus_at_bot_at_top, of f F]
  1170   using filterlim_compose[OF filterlim_uminus_at_top_at_bot, of "\<lambda>x. - f x" F]
  1171   by auto
  1172 
  1173 lemma filterlim_uminus_at_bot: "(LIM x F. f x :> at_bot) \<longleftrightarrow> (LIM x F. - (f x) :: real :> at_top)"
  1174   unfolding filterlim_uminus_at_top by simp
  1175 
  1176 lemma filterlim_inverse_at_top_right: "LIM x at_right (0::real). inverse x :> at_top"
  1177   unfolding filterlim_at_top_gt[where c=0] eventually_at_filter
  1178 proof safe
  1179   fix Z :: real assume [arith]: "0 < Z"
  1180   then have "eventually (\<lambda>x. x < inverse Z) (nhds 0)"
  1181     by (auto simp add: eventually_nhds_metric dist_real_def intro!: exI[of _ "\<bar>inverse Z\<bar>"])
  1182   then show "eventually (\<lambda>x. x \<noteq> 0 \<longrightarrow> x \<in> {0<..} \<longrightarrow> Z \<le> inverse x) (nhds 0)"
  1183     by (auto elim!: eventually_mono simp: inverse_eq_divide field_simps)
  1184 qed
  1185 
  1186 lemma tendsto_inverse_0:
  1187   fixes x :: "_ \<Rightarrow> 'a::real_normed_div_algebra"
  1188   shows "(inverse \<longlongrightarrow> (0::'a)) at_infinity"
  1189   unfolding tendsto_Zfun_iff diff_0_right Zfun_def eventually_at_infinity
  1190 proof safe
  1191   fix r :: real assume "0 < r"
  1192   show "\<exists>b. \<forall>x. b \<le> norm x \<longrightarrow> norm (inverse x :: 'a) < r"
  1193   proof (intro exI[of _ "inverse (r / 2)"] allI impI)
  1194     fix x :: 'a
  1195     from \<open>0 < r\<close> have "0 < inverse (r / 2)" by simp
  1196     also assume *: "inverse (r / 2) \<le> norm x"
  1197     finally show "norm (inverse x) < r"
  1198       using * \<open>0 < r\<close> by (subst nonzero_norm_inverse) (simp_all add: inverse_eq_divide field_simps)
  1199   qed
  1200 qed
  1201 
  1202 lemma tendsto_add_filterlim_at_infinity:
  1203   assumes "(f \<longlongrightarrow> (c :: 'b :: real_normed_vector)) (F :: 'a filter)"
  1204   assumes "filterlim g at_infinity F"
  1205   shows   "filterlim (\<lambda>x. f x + g x) at_infinity F"
  1206 proof (subst filterlim_at_infinity[OF order_refl], safe)
  1207   fix r :: real assume r: "r > 0"
  1208   from assms(1) have "((\<lambda>x. norm (f x)) \<longlongrightarrow> norm c) F" by (rule tendsto_norm)
  1209   hence "eventually (\<lambda>x. norm (f x) < norm c + 1) F" by (rule order_tendstoD) simp_all
  1210   moreover from r have "r + norm c + 1 > 0" by (intro add_pos_nonneg) simp_all
  1211   with assms(2) have "eventually (\<lambda>x. norm (g x) \<ge> r + norm c + 1) F"
  1212     unfolding filterlim_at_infinity[OF order_refl] by (elim allE[of _ "r + norm c + 1"]) simp_all
  1213   ultimately show "eventually (\<lambda>x. norm (f x + g x) \<ge> r) F"
  1214   proof eventually_elim
  1215     fix x :: 'a assume A: "norm (f x) < norm c + 1" and B: "r + norm c + 1 \<le> norm (g x)"
  1216     from A B have "r \<le> norm (g x) - norm (f x)" by simp
  1217     also have "norm (g x) - norm (f x) \<le> norm (g x + f x)" by (rule norm_diff_ineq)
  1218     finally show "r \<le> norm (f x + g x)" by (simp add: add_ac)
  1219   qed
  1220 qed
  1221 
  1222 lemma tendsto_add_filterlim_at_infinity':
  1223   assumes "filterlim f at_infinity F"
  1224   assumes "(g \<longlongrightarrow> (c :: 'b :: real_normed_vector)) (F :: 'a filter)"
  1225   shows   "filterlim (\<lambda>x. f x + g x) at_infinity F"
  1226   by (subst add.commute) (rule tendsto_add_filterlim_at_infinity assms)+
  1227 
  1228 lemma filterlim_inverse_at_right_top: "LIM x at_top. inverse x :> at_right (0::real)"
  1229   unfolding filterlim_at
  1230   by (auto simp: eventually_at_top_dense)
  1231      (metis tendsto_inverse_0 filterlim_mono at_top_le_at_infinity order_refl)
  1232 
  1233 lemma filterlim_inverse_at_top:
  1234   "(f \<longlongrightarrow> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. 0 < f x) F \<Longrightarrow> LIM x F. inverse (f x) :> at_top"
  1235   by (intro filterlim_compose[OF filterlim_inverse_at_top_right])
  1236      (simp add: filterlim_def eventually_filtermap eventually_mono at_within_def le_principal)
  1237 
  1238 lemma filterlim_inverse_at_bot_neg:
  1239   "LIM x (at_left (0::real)). inverse x :> at_bot"
  1240   by (simp add: filterlim_inverse_at_top_right filterlim_uminus_at_bot filterlim_at_left_to_right)
  1241 
  1242 lemma filterlim_inverse_at_bot:
  1243   "(f \<longlongrightarrow> (0 :: real)) F \<Longrightarrow> eventually (\<lambda>x. f x < 0) F \<Longrightarrow> LIM x F. inverse (f x) :> at_bot"
  1244   unfolding filterlim_uminus_at_bot inverse_minus_eq[symmetric]
  1245   by (rule filterlim_inverse_at_top) (simp_all add: tendsto_minus_cancel_left[symmetric])
  1246 
  1247 lemma at_right_to_top: "(at_right (0::real)) = filtermap inverse at_top"
  1248   by (intro filtermap_fun_inverse[symmetric, where g=inverse])
  1249      (auto intro: filterlim_inverse_at_top_right filterlim_inverse_at_right_top)
  1250 
  1251 lemma eventually_at_right_to_top:
  1252   "eventually P (at_right (0::real)) \<longleftrightarrow> eventually (\<lambda>x. P (inverse x)) at_top"
  1253   unfolding at_right_to_top eventually_filtermap ..
  1254 
  1255 lemma filterlim_at_right_to_top:
  1256   "filterlim f F (at_right (0::real)) \<longleftrightarrow> (LIM x at_top. f (inverse x) :> F)"
  1257   unfolding filterlim_def at_right_to_top filtermap_filtermap ..
  1258 
  1259 lemma at_top_to_right: "at_top = filtermap inverse (at_right (0::real))"
  1260   unfolding at_right_to_top filtermap_filtermap inverse_inverse_eq filtermap_ident ..
  1261 
  1262 lemma eventually_at_top_to_right:
  1263   "eventually P at_top \<longleftrightarrow> eventually (\<lambda>x. P (inverse x)) (at_right (0::real))"
  1264   unfolding at_top_to_right eventually_filtermap ..
  1265 
  1266 lemma filterlim_at_top_to_right:
  1267   "filterlim f F at_top \<longleftrightarrow> (LIM x (at_right (0::real)). f (inverse x) :> F)"
  1268   unfolding filterlim_def at_top_to_right filtermap_filtermap ..
  1269 
  1270 lemma filterlim_inverse_at_infinity:
  1271   fixes x :: "_ \<Rightarrow> 'a::{real_normed_div_algebra, division_ring}"
  1272   shows "filterlim inverse at_infinity (at (0::'a))"
  1273   unfolding filterlim_at_infinity[OF order_refl]
  1274 proof safe
  1275   fix r :: real assume "0 < r"
  1276   then show "eventually (\<lambda>x::'a. r \<le> norm (inverse x)) (at 0)"
  1277     unfolding eventually_at norm_inverse
  1278     by (intro exI[of _ "inverse r"])
  1279        (auto simp: norm_conv_dist[symmetric] field_simps inverse_eq_divide)
  1280 qed
  1281 
  1282 lemma filterlim_inverse_at_iff:
  1283   fixes g :: "'a \<Rightarrow> 'b::{real_normed_div_algebra, division_ring}"
  1284   shows "(LIM x F. inverse (g x) :> at 0) \<longleftrightarrow> (LIM x F. g x :> at_infinity)"
  1285   unfolding filterlim_def filtermap_filtermap[symmetric]
  1286 proof
  1287   assume "filtermap g F \<le> at_infinity"
  1288   then have "filtermap inverse (filtermap g F) \<le> filtermap inverse at_infinity"
  1289     by (rule filtermap_mono)
  1290   also have "\<dots> \<le> at 0"
  1291     using tendsto_inverse_0[where 'a='b]
  1292     by (auto intro!: exI[of _ 1]
  1293              simp: le_principal eventually_filtermap filterlim_def at_within_def eventually_at_infinity)
  1294   finally show "filtermap inverse (filtermap g F) \<le> at 0" .
  1295 next
  1296   assume "filtermap inverse (filtermap g F) \<le> at 0"
  1297   then have "filtermap inverse (filtermap inverse (filtermap g F)) \<le> filtermap inverse (at 0)"
  1298     by (rule filtermap_mono)
  1299   with filterlim_inverse_at_infinity show "filtermap g F \<le> at_infinity"
  1300     by (auto intro: order_trans simp: filterlim_def filtermap_filtermap)
  1301 qed
  1302 
  1303 lemma tendsto_mult_filterlim_at_infinity:
  1304   assumes "F \<noteq> bot" "(f \<longlongrightarrow> (c :: 'a :: real_normed_field)) F" "c \<noteq> 0"
  1305   assumes "filterlim g at_infinity F"
  1306   shows   "filterlim (\<lambda>x. f x * g x) at_infinity F"
  1307 proof -
  1308   have "((\<lambda>x. inverse (f x) * inverse (g x)) \<longlongrightarrow> inverse c * 0) F"
  1309     by (intro tendsto_mult tendsto_inverse assms filterlim_compose[OF tendsto_inverse_0])
  1310   hence "filterlim (\<lambda>x. inverse (f x) * inverse (g x)) (at (inverse c * 0)) F"
  1311     unfolding filterlim_at using assms
  1312     by (auto intro: filterlim_at_infinity_imp_eventually_ne tendsto_imp_eventually_ne eventually_conj)
  1313   thus ?thesis by (subst filterlim_inverse_at_iff[symmetric]) simp_all
  1314 qed
  1315 
  1316 lemma tendsto_inverse_0_at_top: "LIM x F. f x :> at_top \<Longrightarrow> ((\<lambda>x. inverse (f x) :: real) \<longlongrightarrow> 0) F"
  1317  by (metis filterlim_at filterlim_mono[OF _ at_top_le_at_infinity order_refl] filterlim_inverse_at_iff)
  1318 
  1319 lemma mult_nat_left_at_top: "c > 0 \<Longrightarrow> filterlim (\<lambda>x. c * x :: nat) at_top sequentially"
  1320   by (rule filterlim_subseq) (auto simp: subseq_def)
  1321 
  1322 lemma mult_nat_right_at_top: "c > 0 \<Longrightarrow> filterlim (\<lambda>x. x * c :: nat) at_top sequentially"
  1323   by (rule filterlim_subseq) (auto simp: subseq_def)
  1324 
  1325 lemma at_to_infinity:
  1326   fixes x :: "'a :: {real_normed_field,field}"
  1327   shows "(at (0::'a)) = filtermap inverse at_infinity"
  1328 proof (rule antisym)
  1329   have "(inverse \<longlongrightarrow> (0::'a)) at_infinity"
  1330     by (fact tendsto_inverse_0)
  1331   then show "filtermap inverse at_infinity \<le> at (0::'a)"
  1332     apply (simp add: le_principal eventually_filtermap eventually_at_infinity filterlim_def at_within_def)
  1333     apply (rule_tac x="1" in exI, auto)
  1334     done
  1335 next
  1336   have "filtermap inverse (filtermap inverse (at (0::'a))) \<le> filtermap inverse at_infinity"
  1337     using filterlim_inverse_at_infinity unfolding filterlim_def
  1338     by (rule filtermap_mono)
  1339   then show "at (0::'a) \<le> filtermap inverse at_infinity"
  1340     by (simp add: filtermap_ident filtermap_filtermap)
  1341 qed
  1342 
  1343 lemma lim_at_infinity_0:
  1344   fixes l :: "'a :: {real_normed_field,field}"
  1345   shows "(f \<longlongrightarrow> l) at_infinity \<longleftrightarrow> ((f o inverse) \<longlongrightarrow> l) (at (0::'a))"
  1346 by (simp add: tendsto_compose_filtermap at_to_infinity filtermap_filtermap)
  1347 
  1348 lemma lim_zero_infinity:
  1349   fixes l :: "'a :: {real_normed_field,field}"
  1350   shows "((\<lambda>x. f(1 / x)) \<longlongrightarrow> l) (at (0::'a)) \<Longrightarrow> (f \<longlongrightarrow> l) at_infinity"
  1351 by (simp add: inverse_eq_divide lim_at_infinity_0 comp_def)
  1352 
  1353 
  1354 text \<open>
  1355 
  1356 We only show rules for multiplication and addition when the functions are either against a real
  1357 value or against infinity. Further rules are easy to derive by using @{thm filterlim_uminus_at_top}.
  1358 
  1359 \<close>
  1360 
  1361 lemma filterlim_tendsto_pos_mult_at_top:
  1362   assumes f: "(f \<longlongrightarrow> c) F" and c: "0 < c"
  1363   assumes g: "LIM x F. g x :> at_top"
  1364   shows "LIM x F. (f x * g x :: real) :> at_top"
  1365   unfolding filterlim_at_top_gt[where c=0]
  1366 proof safe
  1367   fix Z :: real assume "0 < Z"
  1368   from f \<open>0 < c\<close> have "eventually (\<lambda>x. c / 2 < f x) F"
  1369     by (auto dest!: tendstoD[where e="c / 2"] elim!: eventually_mono
  1370              simp: dist_real_def abs_real_def split: if_split_asm)
  1371   moreover from g have "eventually (\<lambda>x. (Z / c * 2) \<le> g x) F"
  1372     unfolding filterlim_at_top by auto
  1373   ultimately show "eventually (\<lambda>x. Z \<le> f x * g x) F"
  1374   proof eventually_elim
  1375     fix x assume "c / 2 < f x" "Z / c * 2 \<le> g x"
  1376     with \<open>0 < Z\<close> \<open>0 < c\<close> have "c / 2 * (Z / c * 2) \<le> f x * g x"
  1377       by (intro mult_mono) (auto simp: zero_le_divide_iff)
  1378     with \<open>0 < c\<close> show "Z \<le> f x * g x"
  1379        by simp
  1380   qed
  1381 qed
  1382 
  1383 lemma filterlim_at_top_mult_at_top:
  1384   assumes f: "LIM x F. f x :> at_top"
  1385   assumes g: "LIM x F. g x :> at_top"
  1386   shows "LIM x F. (f x * g x :: real) :> at_top"
  1387   unfolding filterlim_at_top_gt[where c=0]
  1388 proof safe
  1389   fix Z :: real assume "0 < Z"
  1390   from f have "eventually (\<lambda>x. 1 \<le> f x) F"
  1391     unfolding filterlim_at_top by auto
  1392   moreover from g have "eventually (\<lambda>x. Z \<le> g x) F"
  1393     unfolding filterlim_at_top by auto
  1394   ultimately show "eventually (\<lambda>x. Z \<le> f x * g x) F"
  1395   proof eventually_elim
  1396     fix x assume "1 \<le> f x" "Z \<le> g x"
  1397     with \<open>0 < Z\<close> have "1 * Z \<le> f x * g x"
  1398       by (intro mult_mono) (auto simp: zero_le_divide_iff)
  1399     then show "Z \<le> f x * g x"
  1400        by simp
  1401   qed
  1402 qed
  1403 
  1404 lemma filterlim_tendsto_pos_mult_at_bot:
  1405   assumes "(f \<longlongrightarrow> c) F" "0 < (c::real)" "filterlim g at_bot F"
  1406   shows "LIM x F. f x * g x :> at_bot"
  1407   using filterlim_tendsto_pos_mult_at_top[OF assms(1,2), of "\<lambda>x. - g x"] assms(3)
  1408   unfolding filterlim_uminus_at_bot by simp
  1409 
  1410 lemma filterlim_tendsto_neg_mult_at_bot:
  1411   assumes c: "(f \<longlongrightarrow> c) F" "(c::real) < 0" and g: "filterlim g at_top F"
  1412   shows "LIM x F. f x * g x :> at_bot"
  1413   using c filterlim_tendsto_pos_mult_at_top[of "\<lambda>x. - f x" "- c" F, OF _ _ g]
  1414   unfolding filterlim_uminus_at_bot tendsto_minus_cancel_left by simp
  1415 
  1416 lemma filterlim_pow_at_top:
  1417   fixes f :: "real \<Rightarrow> real"
  1418   assumes "0 < n" and f: "LIM x F. f x :> at_top"
  1419   shows "LIM x F. (f x)^n :: real :> at_top"
  1420 using \<open>0 < n\<close> proof (induct n)
  1421   case (Suc n) with f show ?case
  1422     by (cases "n = 0") (auto intro!: filterlim_at_top_mult_at_top)
  1423 qed simp
  1424 
  1425 lemma filterlim_pow_at_bot_even:
  1426   fixes f :: "real \<Rightarrow> real"
  1427   shows "0 < n \<Longrightarrow> LIM x F. f x :> at_bot \<Longrightarrow> even n \<Longrightarrow> LIM x F. (f x)^n :> at_top"
  1428   using filterlim_pow_at_top[of n "\<lambda>x. - f x" F] by (simp add: filterlim_uminus_at_top)
  1429 
  1430 lemma filterlim_pow_at_bot_odd:
  1431   fixes f :: "real \<Rightarrow> real"
  1432   shows "0 < n \<Longrightarrow> LIM x F. f x :> at_bot \<Longrightarrow> odd n \<Longrightarrow> LIM x F. (f x)^n :> at_bot"
  1433   using filterlim_pow_at_top[of n "\<lambda>x. - f x" F] by (simp add: filterlim_uminus_at_bot)
  1434 
  1435 lemma filterlim_tendsto_add_at_top:
  1436   assumes f: "(f \<longlongrightarrow> c) F"
  1437   assumes g: "LIM x F. g x :> at_top"
  1438   shows "LIM x F. (f x + g x :: real) :> at_top"
  1439   unfolding filterlim_at_top_gt[where c=0]
  1440 proof safe
  1441   fix Z :: real assume "0 < Z"
  1442   from f have "eventually (\<lambda>x. c - 1 < f x) F"
  1443     by (auto dest!: tendstoD[where e=1] elim!: eventually_mono simp: dist_real_def)
  1444   moreover from g have "eventually (\<lambda>x. Z - (c - 1) \<le> g x) F"
  1445     unfolding filterlim_at_top by auto
  1446   ultimately show "eventually (\<lambda>x. Z \<le> f x + g x) F"
  1447     by eventually_elim simp
  1448 qed
  1449 
  1450 lemma LIM_at_top_divide:
  1451   fixes f g :: "'a \<Rightarrow> real"
  1452   assumes f: "(f \<longlongrightarrow> a) F" "0 < a"
  1453   assumes g: "(g \<longlongrightarrow> 0) F" "eventually (\<lambda>x. 0 < g x) F"
  1454   shows "LIM x F. f x / g x :> at_top"
  1455   unfolding divide_inverse
  1456   by (rule filterlim_tendsto_pos_mult_at_top[OF f]) (rule filterlim_inverse_at_top[OF g])
  1457 
  1458 lemma filterlim_at_top_add_at_top:
  1459   assumes f: "LIM x F. f x :> at_top"
  1460   assumes g: "LIM x F. g x :> at_top"
  1461   shows "LIM x F. (f x + g x :: real) :> at_top"
  1462   unfolding filterlim_at_top_gt[where c=0]
  1463 proof safe
  1464   fix Z :: real assume "0 < Z"
  1465   from f have "eventually (\<lambda>x. 0 \<le> f x) F"
  1466     unfolding filterlim_at_top by auto
  1467   moreover from g have "eventually (\<lambda>x. Z \<le> g x) F"
  1468     unfolding filterlim_at_top by auto
  1469   ultimately show "eventually (\<lambda>x. Z \<le> f x + g x) F"
  1470     by eventually_elim simp
  1471 qed
  1472 
  1473 lemma tendsto_divide_0:
  1474   fixes f :: "_ \<Rightarrow> 'a::{real_normed_div_algebra, division_ring}"
  1475   assumes f: "(f \<longlongrightarrow> c) F"
  1476   assumes g: "LIM x F. g x :> at_infinity"
  1477   shows "((\<lambda>x. f x / g x) \<longlongrightarrow> 0) F"
  1478   using tendsto_mult[OF f filterlim_compose[OF tendsto_inverse_0 g]] by (simp add: divide_inverse)
  1479 
  1480 lemma linear_plus_1_le_power:
  1481   fixes x :: real
  1482   assumes x: "0 \<le> x"
  1483   shows "real n * x + 1 \<le> (x + 1) ^ n"
  1484 proof (induct n)
  1485   case (Suc n)
  1486   have "real (Suc n) * x + 1 \<le> (x + 1) * (real n * x + 1)"
  1487     by (simp add: field_simps of_nat_Suc x)
  1488   also have "\<dots> \<le> (x + 1)^Suc n"
  1489     using Suc x by (simp add: mult_left_mono)
  1490   finally show ?case .
  1491 qed simp
  1492 
  1493 lemma filterlim_realpow_sequentially_gt1:
  1494   fixes x :: "'a :: real_normed_div_algebra"
  1495   assumes x[arith]: "1 < norm x"
  1496   shows "LIM n sequentially. x ^ n :> at_infinity"
  1497 proof (intro filterlim_at_infinity[THEN iffD2] allI impI)
  1498   fix y :: real assume "0 < y"
  1499   have "0 < norm x - 1" by simp
  1500   then obtain N::nat where "y < real N * (norm x - 1)" by (blast dest: reals_Archimedean3)
  1501   also have "\<dots> \<le> real N * (norm x - 1) + 1" by simp
  1502   also have "\<dots> \<le> (norm x - 1 + 1) ^ N" by (rule linear_plus_1_le_power) simp
  1503   also have "\<dots> = norm x ^ N" by simp
  1504   finally have "\<forall>n\<ge>N. y \<le> norm x ^ n"
  1505     by (metis order_less_le_trans power_increasing order_less_imp_le x)
  1506   then show "eventually (\<lambda>n. y \<le> norm (x ^ n)) sequentially"
  1507     unfolding eventually_sequentially
  1508     by (auto simp: norm_power)
  1509 qed simp
  1510 
  1511 
  1512 subsection \<open>Limits of Sequences\<close>
  1513 
  1514 lemma [trans]: "X = Y \<Longrightarrow> Y \<longlonglongrightarrow> z \<Longrightarrow> X \<longlonglongrightarrow> z"
  1515   by simp
  1516 
  1517 lemma LIMSEQ_iff:
  1518   fixes L :: "'a::real_normed_vector"
  1519   shows "(X \<longlonglongrightarrow> L) = (\<forall>r>0. \<exists>no. \<forall>n \<ge> no. norm (X n - L) < r)"
  1520 unfolding lim_sequentially dist_norm ..
  1521 
  1522 lemma LIMSEQ_I:
  1523   fixes L :: "'a::real_normed_vector"
  1524   shows "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. norm (X n - L) < r) \<Longrightarrow> X \<longlonglongrightarrow> L"
  1525 by (simp add: LIMSEQ_iff)
  1526 
  1527 lemma LIMSEQ_D:
  1528   fixes L :: "'a::real_normed_vector"
  1529   shows "\<lbrakk>X \<longlonglongrightarrow> L; 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. norm (X n - L) < r"
  1530 by (simp add: LIMSEQ_iff)
  1531 
  1532 lemma LIMSEQ_linear: "\<lbrakk> X \<longlonglongrightarrow> x ; l > 0 \<rbrakk> \<Longrightarrow> (\<lambda> n. X (n * l)) \<longlonglongrightarrow> x"
  1533   unfolding tendsto_def eventually_sequentially
  1534   by (metis div_le_dividend div_mult_self1_is_m le_trans mult.commute)
  1535 
  1536 lemma Bseq_inverse_lemma:
  1537   fixes x :: "'a::real_normed_div_algebra"
  1538   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
  1539 apply (subst nonzero_norm_inverse, clarsimp)
  1540 apply (erule (1) le_imp_inverse_le)
  1541 done
  1542 
  1543 lemma Bseq_inverse:
  1544   fixes a :: "'a::real_normed_div_algebra"
  1545   shows "\<lbrakk>X \<longlonglongrightarrow> a; a \<noteq> 0\<rbrakk> \<Longrightarrow> Bseq (\<lambda>n. inverse (X n))"
  1546   by (rule Bfun_inverse)
  1547 
  1548 text\<open>Transformation of limit.\<close>
  1549 
  1550 lemma Lim_transform:
  1551   fixes a b :: "'a::real_normed_vector"
  1552   shows "\<lbrakk>(g \<longlongrightarrow> a) F; ((\<lambda>x. f x - g x) \<longlongrightarrow> 0) F\<rbrakk> \<Longrightarrow> (f \<longlongrightarrow> a) F"
  1553   using tendsto_add [of g a F "\<lambda>x. f x - g x" 0] by simp
  1554 
  1555 lemma Lim_transform2:
  1556   fixes a b :: "'a::real_normed_vector"
  1557   shows "\<lbrakk>(f \<longlongrightarrow> a) F; ((\<lambda>x. f x - g x) \<longlongrightarrow> 0) F\<rbrakk> \<Longrightarrow> (g \<longlongrightarrow> a) F"
  1558   by (erule Lim_transform) (simp add: tendsto_minus_cancel)
  1559 
  1560 proposition Lim_transform_eq:
  1561   fixes a :: "'a::real_normed_vector"
  1562   shows "((\<lambda>x. f x - g x) \<longlongrightarrow> 0) F \<Longrightarrow> (f \<longlongrightarrow> a) F \<longleftrightarrow> (g \<longlongrightarrow> a) F"
  1563 using Lim_transform Lim_transform2 by blast
  1564 
  1565 lemma Lim_transform_eventually:
  1566   "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> (f \<longlongrightarrow> l) net \<Longrightarrow> (g \<longlongrightarrow> l) net"
  1567   apply (rule topological_tendstoI)
  1568   apply (drule (2) topological_tendstoD)
  1569   apply (erule (1) eventually_elim2, simp)
  1570   done
  1571 
  1572 lemma Lim_transform_within:
  1573   assumes "(f \<longlongrightarrow> l) (at x within S)"
  1574     and "0 < d"
  1575     and "\<And>x'. \<lbrakk>x'\<in>S; 0 < dist x' x; dist x' x < d\<rbrakk> \<Longrightarrow> f x' = g x'"
  1576   shows "(g \<longlongrightarrow> l) (at x within S)"
  1577 proof (rule Lim_transform_eventually)
  1578   show "eventually (\<lambda>x. f x = g x) (at x within S)"
  1579     using assms by (auto simp: eventually_at)
  1580   show "(f \<longlongrightarrow> l) (at x within S)" by fact
  1581 qed
  1582 
  1583 text\<open>Common case assuming being away from some crucial point like 0.\<close>
  1584 
  1585 lemma Lim_transform_away_within:
  1586   fixes a b :: "'a::t1_space"
  1587   assumes "a \<noteq> b"
  1588     and "\<forall>x\<in>S. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
  1589     and "(f \<longlongrightarrow> l) (at a within S)"
  1590   shows "(g \<longlongrightarrow> l) (at a within S)"
  1591 proof (rule Lim_transform_eventually)
  1592   show "(f \<longlongrightarrow> l) (at a within S)" by fact
  1593   show "eventually (\<lambda>x. f x = g x) (at a within S)"
  1594     unfolding eventually_at_topological
  1595     by (rule exI [where x="- {b}"], simp add: open_Compl assms)
  1596 qed
  1597 
  1598 lemma Lim_transform_away_at:
  1599   fixes a b :: "'a::t1_space"
  1600   assumes ab: "a\<noteq>b"
  1601     and fg: "\<forall>x. x \<noteq> a \<and> x \<noteq> b \<longrightarrow> f x = g x"
  1602     and fl: "(f \<longlongrightarrow> l) (at a)"
  1603   shows "(g \<longlongrightarrow> l) (at a)"
  1604   using Lim_transform_away_within[OF ab, of UNIV f g l] fg fl by simp
  1605 
  1606 text\<open>Alternatively, within an open set.\<close>
  1607 
  1608 lemma Lim_transform_within_open:
  1609   assumes "(f \<longlongrightarrow> l) (at a within T)"
  1610     and "open s" and "a \<in> s"
  1611     and "\<And>x. \<lbrakk>x\<in>s; x \<noteq> a\<rbrakk> \<Longrightarrow> f x = g x"
  1612   shows "(g \<longlongrightarrow> l) (at a within T)"
  1613 proof (rule Lim_transform_eventually)
  1614   show "eventually (\<lambda>x. f x = g x) (at a within T)"
  1615     unfolding eventually_at_topological
  1616     using assms by auto
  1617   show "(f \<longlongrightarrow> l) (at a within T)" by fact
  1618 qed
  1619 
  1620 text\<open>A congruence rule allowing us to transform limits assuming not at point.\<close>
  1621 
  1622 (* FIXME: Only one congruence rule for tendsto can be used at a time! *)
  1623 
  1624 lemma Lim_cong_within(*[cong add]*):
  1625   assumes "a = b"
  1626     and "x = y"
  1627     and "S = T"
  1628     and "\<And>x. x \<noteq> b \<Longrightarrow> x \<in> T \<Longrightarrow> f x = g x"
  1629   shows "(f \<longlongrightarrow> x) (at a within S) \<longleftrightarrow> (g \<longlongrightarrow> y) (at b within T)"
  1630   unfolding tendsto_def eventually_at_topological
  1631   using assms by simp
  1632 
  1633 lemma Lim_cong_at(*[cong add]*):
  1634   assumes "a = b" "x = y"
  1635     and "\<And>x. x \<noteq> a \<Longrightarrow> f x = g x"
  1636   shows "((\<lambda>x. f x) \<longlongrightarrow> x) (at a) \<longleftrightarrow> ((g \<longlongrightarrow> y) (at a))"
  1637   unfolding tendsto_def eventually_at_topological
  1638   using assms by simp
  1639 text\<open>An unbounded sequence's inverse tends to 0\<close>
  1640 
  1641 lemma LIMSEQ_inverse_zero:
  1642   "\<forall>r::real. \<exists>N. \<forall>n\<ge>N. r < X n \<Longrightarrow> (\<lambda>n. inverse (X n)) \<longlonglongrightarrow> 0"
  1643   apply (rule filterlim_compose[OF tendsto_inverse_0])
  1644   apply (simp add: filterlim_at_infinity[OF order_refl] eventually_sequentially)
  1645   apply (metis abs_le_D1 linorder_le_cases linorder_not_le)
  1646   done
  1647 
  1648 text\<open>The sequence @{term "1/n"} tends to 0 as @{term n} tends to infinity\<close>
  1649 
  1650 lemma LIMSEQ_inverse_real_of_nat: "(%n. inverse(real(Suc n))) \<longlonglongrightarrow> 0"
  1651   by (metis filterlim_compose tendsto_inverse_0 filterlim_mono order_refl filterlim_Suc
  1652             filterlim_compose[OF filterlim_real_sequentially] at_top_le_at_infinity)
  1653 
  1654 text\<open>The sequence @{term "r + 1/n"} tends to @{term r} as @{term n} tends to
  1655 infinity is now easily proved\<close>
  1656 
  1657 lemma LIMSEQ_inverse_real_of_nat_add:
  1658      "(%n. r + inverse(real(Suc n))) \<longlonglongrightarrow> r"
  1659   using tendsto_add [OF tendsto_const LIMSEQ_inverse_real_of_nat] by auto
  1660 
  1661 lemma LIMSEQ_inverse_real_of_nat_add_minus:
  1662      "(%n. r + -inverse(real(Suc n))) \<longlonglongrightarrow> r"
  1663   using tendsto_add [OF tendsto_const tendsto_minus [OF LIMSEQ_inverse_real_of_nat]]
  1664   by auto
  1665 
  1666 lemma LIMSEQ_inverse_real_of_nat_add_minus_mult:
  1667      "(%n. r*( 1 + -inverse(real(Suc n)))) \<longlonglongrightarrow> r"
  1668   using tendsto_mult [OF tendsto_const LIMSEQ_inverse_real_of_nat_add_minus [of 1]]
  1669   by auto
  1670 
  1671 lemma lim_inverse_n: "((\<lambda>n. inverse(of_nat n)) \<longlongrightarrow> (0::'a::real_normed_field)) sequentially"
  1672   using lim_1_over_n by (simp add: inverse_eq_divide)
  1673 
  1674 lemma LIMSEQ_Suc_n_over_n: "(\<lambda>n. of_nat (Suc n) / of_nat n :: 'a :: real_normed_field) \<longlonglongrightarrow> 1"
  1675 proof (rule Lim_transform_eventually)
  1676   show "eventually (\<lambda>n. 1 + inverse (of_nat n :: 'a) = of_nat (Suc n) / of_nat n) sequentially"
  1677     using eventually_gt_at_top[of "0::nat"] by eventually_elim (simp add: field_simps)
  1678   have "(\<lambda>n. 1 + inverse (of_nat n) :: 'a) \<longlonglongrightarrow> 1 + 0"
  1679     by (intro tendsto_add tendsto_const lim_inverse_n)
  1680   thus "(\<lambda>n. 1 + inverse (of_nat n) :: 'a) \<longlonglongrightarrow> 1" by simp
  1681 qed
  1682 
  1683 lemma LIMSEQ_n_over_Suc_n: "(\<lambda>n. of_nat n / of_nat (Suc n) :: 'a :: real_normed_field) \<longlonglongrightarrow> 1"
  1684 proof (rule Lim_transform_eventually)
  1685   show "eventually (\<lambda>n. inverse (of_nat (Suc n) / of_nat n :: 'a) =
  1686                         of_nat n / of_nat (Suc n)) sequentially"
  1687     using eventually_gt_at_top[of "0::nat"]
  1688     by eventually_elim (simp add: field_simps del: of_nat_Suc)
  1689   have "(\<lambda>n. inverse (of_nat (Suc n) / of_nat n :: 'a)) \<longlonglongrightarrow> inverse 1"
  1690     by (intro tendsto_inverse LIMSEQ_Suc_n_over_n) simp_all
  1691   thus "(\<lambda>n. inverse (of_nat (Suc n) / of_nat n :: 'a)) \<longlonglongrightarrow> 1" by simp
  1692 qed
  1693 
  1694 subsection \<open>Convergence on sequences\<close>
  1695 
  1696 lemma convergent_cong:
  1697   assumes "eventually (\<lambda>x. f x = g x) sequentially"
  1698   shows   "convergent f \<longleftrightarrow> convergent g"
  1699   unfolding convergent_def by (subst filterlim_cong[OF refl refl assms]) (rule refl)
  1700 
  1701 lemma convergent_Suc_iff: "convergent (\<lambda>n. f (Suc n)) \<longleftrightarrow> convergent f"
  1702   by (auto simp: convergent_def LIMSEQ_Suc_iff)
  1703 
  1704 lemma convergent_ignore_initial_segment: "convergent (\<lambda>n. f (n + m)) = convergent f"
  1705 proof (induction m arbitrary: f)
  1706   case (Suc m)
  1707   have "convergent (\<lambda>n. f (n + Suc m)) \<longleftrightarrow> convergent (\<lambda>n. f (Suc n + m))" by simp
  1708   also have "\<dots> \<longleftrightarrow> convergent (\<lambda>n. f (n + m))" by (rule convergent_Suc_iff)
  1709   also have "\<dots> \<longleftrightarrow> convergent f" by (rule Suc)
  1710   finally show ?case .
  1711 qed simp_all
  1712 
  1713 lemma convergent_add:
  1714   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_vector"
  1715   assumes "convergent (\<lambda>n. X n)"
  1716   assumes "convergent (\<lambda>n. Y n)"
  1717   shows "convergent (\<lambda>n. X n + Y n)"
  1718   using assms unfolding convergent_def by (blast intro: tendsto_add)
  1719 
  1720 lemma convergent_setsum:
  1721   fixes X :: "'a \<Rightarrow> nat \<Rightarrow> 'b::real_normed_vector"
  1722   assumes "\<And>i. i \<in> A \<Longrightarrow> convergent (\<lambda>n. X i n)"
  1723   shows "convergent (\<lambda>n. \<Sum>i\<in>A. X i n)"
  1724 proof (cases "finite A")
  1725   case True from this and assms show ?thesis
  1726     by (induct A set: finite) (simp_all add: convergent_const convergent_add)
  1727 qed (simp add: convergent_const)
  1728 
  1729 lemma (in bounded_linear) convergent:
  1730   assumes "convergent (\<lambda>n. X n)"
  1731   shows "convergent (\<lambda>n. f (X n))"
  1732   using assms unfolding convergent_def by (blast intro: tendsto)
  1733 
  1734 lemma (in bounded_bilinear) convergent:
  1735   assumes "convergent (\<lambda>n. X n)" and "convergent (\<lambda>n. Y n)"
  1736   shows "convergent (\<lambda>n. X n ** Y n)"
  1737   using assms unfolding convergent_def by (blast intro: tendsto)
  1738 
  1739 lemma convergent_minus_iff:
  1740   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
  1741   shows "convergent X \<longleftrightarrow> convergent (\<lambda>n. - X n)"
  1742 apply (simp add: convergent_def)
  1743 apply (auto dest: tendsto_minus)
  1744 apply (drule tendsto_minus, auto)
  1745 done
  1746 
  1747 lemma convergent_diff:
  1748   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_vector"
  1749   assumes "convergent (\<lambda>n. X n)"
  1750   assumes "convergent (\<lambda>n. Y n)"
  1751   shows "convergent (\<lambda>n. X n - Y n)"
  1752   using assms unfolding convergent_def by (blast intro: tendsto_diff)
  1753 
  1754 lemma convergent_norm:
  1755   assumes "convergent f"
  1756   shows   "convergent (\<lambda>n. norm (f n))"
  1757 proof -
  1758   from assms have "f \<longlonglongrightarrow> lim f" by (simp add: convergent_LIMSEQ_iff)
  1759   hence "(\<lambda>n. norm (f n)) \<longlonglongrightarrow> norm (lim f)" by (rule tendsto_norm)
  1760   thus ?thesis by (auto simp: convergent_def)
  1761 qed
  1762 
  1763 lemma convergent_of_real:
  1764   "convergent f \<Longrightarrow> convergent (\<lambda>n. of_real (f n) :: 'a :: real_normed_algebra_1)"
  1765   unfolding convergent_def by (blast intro!: tendsto_of_real)
  1766 
  1767 lemma convergent_add_const_iff:
  1768   "convergent (\<lambda>n. c + f n :: 'a :: real_normed_vector) \<longleftrightarrow> convergent f"
  1769 proof
  1770   assume "convergent (\<lambda>n. c + f n)"
  1771   from convergent_diff[OF this convergent_const[of c]] show "convergent f" by simp
  1772 next
  1773   assume "convergent f"
  1774   from convergent_add[OF convergent_const[of c] this] show "convergent (\<lambda>n. c + f n)" by simp
  1775 qed
  1776 
  1777 lemma convergent_add_const_right_iff:
  1778   "convergent (\<lambda>n. f n + c :: 'a :: real_normed_vector) \<longleftrightarrow> convergent f"
  1779   using convergent_add_const_iff[of c f] by (simp add: add_ac)
  1780 
  1781 lemma convergent_diff_const_right_iff:
  1782   "convergent (\<lambda>n. f n - c :: 'a :: real_normed_vector) \<longleftrightarrow> convergent f"
  1783   using convergent_add_const_right_iff[of f "-c"] by (simp add: add_ac)
  1784 
  1785 lemma convergent_mult:
  1786   fixes X Y :: "nat \<Rightarrow> 'a::real_normed_field"
  1787   assumes "convergent (\<lambda>n. X n)"
  1788   assumes "convergent (\<lambda>n. Y n)"
  1789   shows "convergent (\<lambda>n. X n * Y n)"
  1790   using assms unfolding convergent_def by (blast intro: tendsto_mult)
  1791 
  1792 lemma convergent_mult_const_iff:
  1793   assumes "c \<noteq> 0"
  1794   shows   "convergent (\<lambda>n. c * f n :: 'a :: real_normed_field) \<longleftrightarrow> convergent f"
  1795 proof
  1796   assume "convergent (\<lambda>n. c * f n)"
  1797   from assms convergent_mult[OF this convergent_const[of "inverse c"]]
  1798     show "convergent f" by (simp add: field_simps)
  1799 next
  1800   assume "convergent f"
  1801   from convergent_mult[OF convergent_const[of c] this] show "convergent (\<lambda>n. c * f n)" by simp
  1802 qed
  1803 
  1804 lemma convergent_mult_const_right_iff:
  1805   assumes "c \<noteq> 0"
  1806   shows   "convergent (\<lambda>n. (f n :: 'a :: real_normed_field) * c) \<longleftrightarrow> convergent f"
  1807   using convergent_mult_const_iff[OF assms, of f] by (simp add: mult_ac)
  1808 
  1809 lemma convergent_imp_Bseq: "convergent f \<Longrightarrow> Bseq f"
  1810   by (simp add: Cauchy_Bseq convergent_Cauchy)
  1811 
  1812 
  1813 text \<open>A monotone sequence converges to its least upper bound.\<close>
  1814 
  1815 lemma LIMSEQ_incseq_SUP:
  1816   fixes X :: "nat \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology}"
  1817   assumes u: "bdd_above (range X)"
  1818   assumes X: "incseq X"
  1819   shows "X \<longlonglongrightarrow> (SUP i. X i)"
  1820   by (rule order_tendstoI)
  1821      (auto simp: eventually_sequentially u less_cSUP_iff intro: X[THEN incseqD] less_le_trans cSUP_lessD[OF u])
  1822 
  1823 lemma LIMSEQ_decseq_INF:
  1824   fixes X :: "nat \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology}"
  1825   assumes u: "bdd_below (range X)"
  1826   assumes X: "decseq X"
  1827   shows "X \<longlonglongrightarrow> (INF i. X i)"
  1828   by (rule order_tendstoI)
  1829      (auto simp: eventually_sequentially u cINF_less_iff intro: X[THEN decseqD] le_less_trans less_cINF_D[OF u])
  1830 
  1831 text\<open>Main monotonicity theorem\<close>
  1832 
  1833 lemma Bseq_monoseq_convergent: "Bseq X \<Longrightarrow> monoseq X \<Longrightarrow> convergent (X::nat\<Rightarrow>real)"
  1834   by (auto simp: monoseq_iff convergent_def intro: LIMSEQ_decseq_INF LIMSEQ_incseq_SUP dest: Bseq_bdd_above Bseq_bdd_below)
  1835 
  1836 lemma Bseq_mono_convergent: "Bseq X \<Longrightarrow> (\<forall>m n. m \<le> n \<longrightarrow> X m \<le> X n) \<Longrightarrow> convergent (X::nat\<Rightarrow>real)"
  1837   by (auto intro!: Bseq_monoseq_convergent incseq_imp_monoseq simp: incseq_def)
  1838 
  1839 lemma monoseq_imp_convergent_iff_Bseq: "monoseq (f :: nat \<Rightarrow> real) \<Longrightarrow> convergent f \<longleftrightarrow> Bseq f"
  1840   using Bseq_monoseq_convergent[of f] convergent_imp_Bseq[of f] by blast
  1841 
  1842 lemma Bseq_monoseq_convergent'_inc:
  1843   "Bseq (\<lambda>n. f (n + M) :: real) \<Longrightarrow> (\<And>m n. M \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> f m \<le> f n) \<Longrightarrow> convergent f"
  1844   by (subst convergent_ignore_initial_segment [symmetric, of _ M])
  1845      (auto intro!: Bseq_monoseq_convergent simp: monoseq_def)
  1846 
  1847 lemma Bseq_monoseq_convergent'_dec:
  1848   "Bseq (\<lambda>n. f (n + M) :: real) \<Longrightarrow> (\<And>m n. M \<le> m \<Longrightarrow> m \<le> n \<Longrightarrow> f m \<ge> f n) \<Longrightarrow> convergent f"
  1849   by (subst convergent_ignore_initial_segment [symmetric, of _ M])
  1850      (auto intro!: Bseq_monoseq_convergent simp: monoseq_def)
  1851 
  1852 lemma Cauchy_iff:
  1853   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
  1854   shows "Cauchy X \<longleftrightarrow> (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (X m - X n) < e)"
  1855   unfolding Cauchy_def dist_norm ..
  1856 
  1857 lemma CauchyI:
  1858   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
  1859   shows "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (X m - X n) < e) \<Longrightarrow> Cauchy X"
  1860 by (simp add: Cauchy_iff)
  1861 
  1862 lemma CauchyD:
  1863   fixes X :: "nat \<Rightarrow> 'a::real_normed_vector"
  1864   shows "\<lbrakk>Cauchy X; 0 < e\<rbrakk> \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. norm (X m - X n) < e"
  1865 by (simp add: Cauchy_iff)
  1866 
  1867 lemma incseq_convergent:
  1868   fixes X :: "nat \<Rightarrow> real"
  1869   assumes "incseq X" and "\<forall>i. X i \<le> B"
  1870   obtains L where "X \<longlonglongrightarrow> L" "\<forall>i. X i \<le> L"
  1871 proof atomize_elim
  1872   from incseq_bounded[OF assms] \<open>incseq X\<close> Bseq_monoseq_convergent[of X]
  1873   obtain L where "X \<longlonglongrightarrow> L"
  1874     by (auto simp: convergent_def monoseq_def incseq_def)
  1875   with \<open>incseq X\<close> show "\<exists>L. X \<longlonglongrightarrow> L \<and> (\<forall>i. X i \<le> L)"
  1876     by (auto intro!: exI[of _ L] incseq_le)
  1877 qed
  1878 
  1879 lemma decseq_convergent:
  1880   fixes X :: "nat \<Rightarrow> real"
  1881   assumes "decseq X" and "\<forall>i. B \<le> X i"
  1882   obtains L where "X \<longlonglongrightarrow> L" "\<forall>i. L \<le> X i"
  1883 proof atomize_elim
  1884   from decseq_bounded[OF assms] \<open>decseq X\<close> Bseq_monoseq_convergent[of X]
  1885   obtain L where "X \<longlonglongrightarrow> L"
  1886     by (auto simp: convergent_def monoseq_def decseq_def)
  1887   with \<open>decseq X\<close> show "\<exists>L. X \<longlonglongrightarrow> L \<and> (\<forall>i. L \<le> X i)"
  1888     by (auto intro!: exI[of _ L] decseq_le)
  1889 qed
  1890 
  1891 subsubsection \<open>Cauchy Sequences are Bounded\<close>
  1892 
  1893 text\<open>A Cauchy sequence is bounded -- this is the standard
  1894   proof mechanization rather than the nonstandard proof\<close>
  1895 
  1896 lemma lemmaCauchy: "\<forall>n \<ge> M. norm (X M - X n) < (1::real)
  1897           ==>  \<forall>n \<ge> M. norm (X n :: 'a::real_normed_vector) < 1 + norm (X M)"
  1898 apply (clarify, drule spec, drule (1) mp)
  1899 apply (simp only: norm_minus_commute)
  1900 apply (drule order_le_less_trans [OF norm_triangle_ineq2])
  1901 apply simp
  1902 done
  1903 
  1904 subsection \<open>Power Sequences\<close>
  1905 
  1906 text\<open>The sequence @{term "x^n"} tends to 0 if @{term "0\<le>x"} and @{term
  1907 "x<1"}.  Proof will use (NS) Cauchy equivalence for convergence and
  1908   also fact that bounded and monotonic sequence converges.\<close>
  1909 
  1910 lemma Bseq_realpow: "[| 0 \<le> (x::real); x \<le> 1 |] ==> Bseq (%n. x ^ n)"
  1911 apply (simp add: Bseq_def)
  1912 apply (rule_tac x = 1 in exI)
  1913 apply (simp add: power_abs)
  1914 apply (auto dest: power_mono)
  1915 done
  1916 
  1917 lemma monoseq_realpow: fixes x :: real shows "[| 0 \<le> x; x \<le> 1 |] ==> monoseq (%n. x ^ n)"
  1918 apply (clarify intro!: mono_SucI2)
  1919 apply (cut_tac n = n and N = "Suc n" and a = x in power_decreasing, auto)
  1920 done
  1921 
  1922 lemma convergent_realpow:
  1923   "[| 0 \<le> (x::real); x \<le> 1 |] ==> convergent (%n. x ^ n)"
  1924 by (blast intro!: Bseq_monoseq_convergent Bseq_realpow monoseq_realpow)
  1925 
  1926 lemma LIMSEQ_inverse_realpow_zero: "1 < (x::real) \<Longrightarrow> (\<lambda>n. inverse (x ^ n)) \<longlonglongrightarrow> 0"
  1927   by (rule filterlim_compose[OF tendsto_inverse_0 filterlim_realpow_sequentially_gt1]) simp
  1928 
  1929 lemma LIMSEQ_realpow_zero:
  1930   "\<lbrakk>0 \<le> (x::real); x < 1\<rbrakk> \<Longrightarrow> (\<lambda>n. x ^ n) \<longlonglongrightarrow> 0"
  1931 proof cases
  1932   assume "0 \<le> x" and "x \<noteq> 0"
  1933   hence x0: "0 < x" by simp
  1934   assume x1: "x < 1"
  1935   from x0 x1 have "1 < inverse x"
  1936     by (rule one_less_inverse)
  1937   hence "(\<lambda>n. inverse (inverse x ^ n)) \<longlonglongrightarrow> 0"
  1938     by (rule LIMSEQ_inverse_realpow_zero)
  1939   thus ?thesis by (simp add: power_inverse)
  1940 qed (rule LIMSEQ_imp_Suc, simp)
  1941 
  1942 lemma LIMSEQ_power_zero:
  1943   fixes x :: "'a::{real_normed_algebra_1}"
  1944   shows "norm x < 1 \<Longrightarrow> (\<lambda>n. x ^ n) \<longlonglongrightarrow> 0"
  1945 apply (drule LIMSEQ_realpow_zero [OF norm_ge_zero])
  1946 apply (simp only: tendsto_Zfun_iff, erule Zfun_le)
  1947 apply (simp add: power_abs norm_power_ineq)
  1948 done
  1949 
  1950 lemma LIMSEQ_divide_realpow_zero: "1 < x \<Longrightarrow> (\<lambda>n. a / (x ^ n) :: real) \<longlonglongrightarrow> 0"
  1951   by (rule tendsto_divide_0 [OF tendsto_const filterlim_realpow_sequentially_gt1]) simp
  1952 
  1953 text\<open>Limit of @{term "c^n"} for @{term"\<bar>c\<bar> < 1"}\<close>
  1954 
  1955 lemma LIMSEQ_rabs_realpow_zero: "\<bar>c\<bar> < 1 \<Longrightarrow> (\<lambda>n. \<bar>c\<bar> ^ n :: real) \<longlonglongrightarrow> 0"
  1956   by (rule LIMSEQ_realpow_zero [OF abs_ge_zero])
  1957 
  1958 lemma LIMSEQ_rabs_realpow_zero2: "\<bar>c\<bar> < 1 \<Longrightarrow> (\<lambda>n. c ^ n :: real) \<longlonglongrightarrow> 0"
  1959   by (rule LIMSEQ_power_zero) simp
  1960 
  1961 
  1962 subsection \<open>Limits of Functions\<close>
  1963 
  1964 lemma LIM_eq:
  1965   fixes a :: "'a::real_normed_vector" and L :: "'b::real_normed_vector"
  1966   shows "f \<midarrow>a\<rightarrow> L =
  1967      (\<forall>r>0.\<exists>s>0.\<forall>x. x \<noteq> a & norm (x-a) < s --> norm (f x - L) < r)"
  1968 by (simp add: LIM_def dist_norm)
  1969 
  1970 lemma LIM_I:
  1971   fixes a :: "'a::real_normed_vector" and L :: "'b::real_normed_vector"
  1972   shows "(!!r. 0<r ==> \<exists>s>0.\<forall>x. x \<noteq> a & norm (x-a) < s --> norm (f x - L) < r)
  1973       ==> f \<midarrow>a\<rightarrow> L"
  1974 by (simp add: LIM_eq)
  1975 
  1976 lemma LIM_D:
  1977   fixes a :: "'a::real_normed_vector" and L :: "'b::real_normed_vector"
  1978   shows "[| f \<midarrow>a\<rightarrow> L; 0<r |]
  1979       ==> \<exists>s>0.\<forall>x. x \<noteq> a & norm (x-a) < s --> norm (f x - L) < r"
  1980 by (simp add: LIM_eq)
  1981 
  1982 lemma LIM_offset:
  1983   fixes a :: "'a::real_normed_vector"
  1984   shows "f \<midarrow>a\<rightarrow> L \<Longrightarrow> (\<lambda>x. f (x + k)) \<midarrow>(a - k)\<rightarrow> L"
  1985   unfolding filtermap_at_shift[symmetric, of a k] filterlim_def filtermap_filtermap by simp
  1986 
  1987 lemma LIM_offset_zero:
  1988   fixes a :: "'a::real_normed_vector"
  1989   shows "f \<midarrow>a\<rightarrow> L \<Longrightarrow> (\<lambda>h. f (a + h)) \<midarrow>0\<rightarrow> L"
  1990 by (drule_tac k="a" in LIM_offset, simp add: add.commute)
  1991 
  1992 lemma LIM_offset_zero_cancel:
  1993   fixes a :: "'a::real_normed_vector"
  1994   shows "(\<lambda>h. f (a + h)) \<midarrow>0\<rightarrow> L \<Longrightarrow> f \<midarrow>a\<rightarrow> L"
  1995 by (drule_tac k="- a" in LIM_offset, simp)
  1996 
  1997 lemma LIM_offset_zero_iff:
  1998   fixes f :: "'a :: real_normed_vector \<Rightarrow> _"
  1999   shows  "f \<midarrow>a\<rightarrow> L \<longleftrightarrow> (\<lambda>h. f (a + h)) \<midarrow>0\<rightarrow> L"
  2000   using LIM_offset_zero_cancel[of f a L] LIM_offset_zero[of f L a] by auto
  2001 
  2002 lemma LIM_zero:
  2003   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  2004   shows "(f \<longlongrightarrow> l) F \<Longrightarrow> ((\<lambda>x. f x - l) \<longlongrightarrow> 0) F"
  2005 unfolding tendsto_iff dist_norm by simp
  2006 
  2007 lemma LIM_zero_cancel:
  2008   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  2009   shows "((\<lambda>x. f x - l) \<longlongrightarrow> 0) F \<Longrightarrow> (f \<longlongrightarrow> l) F"
  2010 unfolding tendsto_iff dist_norm by simp
  2011 
  2012 lemma LIM_zero_iff:
  2013   fixes f :: "'a::metric_space \<Rightarrow> 'b::real_normed_vector"
  2014   shows "((\<lambda>x. f x - l) \<longlongrightarrow> 0) F = (f \<longlongrightarrow> l) F"
  2015 unfolding tendsto_iff dist_norm by simp
  2016 
  2017 lemma LIM_imp_LIM:
  2018   fixes f :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
  2019   fixes g :: "'a::topological_space \<Rightarrow> 'c::real_normed_vector"
  2020   assumes f: "f \<midarrow>a\<rightarrow> l"
  2021   assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> norm (g x - m) \<le> norm (f x - l)"
  2022   shows "g \<midarrow>a\<rightarrow> m"
  2023   by (rule metric_LIM_imp_LIM [OF f],
  2024     simp add: dist_norm le)
  2025 
  2026 lemma LIM_equal2:
  2027   fixes f g :: "'a::real_normed_vector \<Rightarrow> 'b::topological_space"
  2028   assumes 1: "0 < R"
  2029   assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; norm (x - a) < R\<rbrakk> \<Longrightarrow> f x = g x"
  2030   shows "g \<midarrow>a\<rightarrow> l \<Longrightarrow> f \<midarrow>a\<rightarrow> l"
  2031 by (rule metric_LIM_equal2 [OF 1 2], simp_all add: dist_norm)
  2032 
  2033 lemma LIM_compose2:
  2034   fixes a :: "'a::real_normed_vector"
  2035   assumes f: "f \<midarrow>a\<rightarrow> b"
  2036   assumes g: "g \<midarrow>b\<rightarrow> c"
  2037   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> norm (x - a) < d \<longrightarrow> f x \<noteq> b"
  2038   shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> c"
  2039 by (rule metric_LIM_compose2 [OF f g inj [folded dist_norm]])
  2040 
  2041 lemma real_LIM_sandwich_zero:
  2042   fixes f g :: "'a::topological_space \<Rightarrow> real"
  2043   assumes f: "f \<midarrow>a\<rightarrow> 0"
  2044   assumes 1: "\<And>x. x \<noteq> a \<Longrightarrow> 0 \<le> g x"
  2045   assumes 2: "\<And>x. x \<noteq> a \<Longrightarrow> g x \<le> f x"
  2046   shows "g \<midarrow>a\<rightarrow> 0"
  2047 proof (rule LIM_imp_LIM [OF f]) (* FIXME: use tendsto_sandwich *)
  2048   fix x assume x: "x \<noteq> a"
  2049   have "norm (g x - 0) = g x" by (simp add: 1 x)
  2050   also have "g x \<le> f x" by (rule 2 [OF x])
  2051   also have "f x \<le> \<bar>f x\<bar>" by (rule abs_ge_self)
  2052   also have "\<bar>f x\<bar> = norm (f x - 0)" by simp
  2053   finally show "norm (g x - 0) \<le> norm (f x - 0)" .
  2054 qed
  2055 
  2056 
  2057 subsection \<open>Continuity\<close>
  2058 
  2059 lemma LIM_isCont_iff:
  2060   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::topological_space"
  2061   shows "(f \<midarrow>a\<rightarrow> f a) = ((\<lambda>h. f (a + h)) \<midarrow>0\<rightarrow> f a)"
  2062 by (rule iffI [OF LIM_offset_zero LIM_offset_zero_cancel])
  2063 
  2064 lemma isCont_iff:
  2065   fixes f :: "'a::real_normed_vector \<Rightarrow> 'b::topological_space"
  2066   shows "isCont f x = (\<lambda>h. f (x + h)) \<midarrow>0\<rightarrow> f x"
  2067 by (simp add: isCont_def LIM_isCont_iff)
  2068 
  2069 lemma isCont_LIM_compose2:
  2070   fixes a :: "'a::real_normed_vector"
  2071   assumes f [unfolded isCont_def]: "isCont f a"
  2072   assumes g: "g \<midarrow>f a\<rightarrow> l"
  2073   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> norm (x - a) < d \<longrightarrow> f x \<noteq> f a"
  2074   shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> l"
  2075 by (rule LIM_compose2 [OF f g inj])
  2076 
  2077 
  2078 lemma isCont_norm [simp]:
  2079   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  2080   shows "isCont f a \<Longrightarrow> isCont (\<lambda>x. norm (f x)) a"
  2081   by (fact continuous_norm)
  2082 
  2083 lemma isCont_rabs [simp]:
  2084   fixes f :: "'a::t2_space \<Rightarrow> real"
  2085   shows "isCont f a \<Longrightarrow> isCont (\<lambda>x. \<bar>f x\<bar>) a"
  2086   by (fact continuous_rabs)
  2087 
  2088 lemma isCont_add [simp]:
  2089   fixes f :: "'a::t2_space \<Rightarrow> 'b::topological_monoid_add"
  2090   shows "\<lbrakk>isCont f a; isCont g a\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. f x + g x) a"
  2091   by (fact continuous_add)
  2092 
  2093 lemma isCont_minus [simp]:
  2094   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  2095   shows "isCont f a \<Longrightarrow> isCont (\<lambda>x. - f x) a"
  2096   by (fact continuous_minus)
  2097 
  2098 lemma isCont_diff [simp]:
  2099   fixes f :: "'a::t2_space \<Rightarrow> 'b::real_normed_vector"
  2100   shows "\<lbrakk>isCont f a; isCont g a\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. f x - g x) a"
  2101   by (fact continuous_diff)
  2102 
  2103 lemma isCont_mult [simp]:
  2104   fixes f g :: "'a::t2_space \<Rightarrow> 'b::real_normed_algebra"
  2105   shows "\<lbrakk>isCont f a; isCont g a\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. f x * g x) a"
  2106   by (fact continuous_mult)
  2107 
  2108 lemma (in bounded_linear) isCont:
  2109   "isCont g a \<Longrightarrow> isCont (\<lambda>x. f (g x)) a"
  2110   by (fact continuous)
  2111 
  2112 lemma (in bounded_bilinear) isCont:
  2113   "\<lbrakk>isCont f a; isCont g a\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. f x ** g x) a"
  2114   by (fact continuous)
  2115 
  2116 lemmas isCont_scaleR [simp] =
  2117   bounded_bilinear.isCont [OF bounded_bilinear_scaleR]
  2118 
  2119 lemmas isCont_of_real [simp] =
  2120   bounded_linear.isCont [OF bounded_linear_of_real]
  2121 
  2122 lemma isCont_power [simp]:
  2123   fixes f :: "'a::t2_space \<Rightarrow> 'b::{power,real_normed_algebra}"
  2124   shows "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x ^ n) a"
  2125   by (fact continuous_power)
  2126 
  2127 lemma isCont_setsum [simp]:
  2128   fixes f :: "'a \<Rightarrow> 'b::t2_space \<Rightarrow> 'c::topological_comm_monoid_add"
  2129   shows "\<forall>i\<in>A. isCont (f i) a \<Longrightarrow> isCont (\<lambda>x. \<Sum>i\<in>A. f i x) a"
  2130   by (auto intro: continuous_setsum)
  2131 
  2132 subsection \<open>Uniform Continuity\<close>
  2133 
  2134 lemma uniformly_continuous_on_def:
  2135   fixes f :: "'a::metric_space \<Rightarrow> 'b::metric_space"
  2136   shows "uniformly_continuous_on s f \<longleftrightarrow>
  2137     (\<forall>e>0. \<exists>d>0. \<forall>x\<in>s. \<forall>x'\<in>s. dist x' x < d \<longrightarrow> dist (f x') (f x) < e)"
  2138   unfolding uniformly_continuous_on_uniformity
  2139     uniformity_dist filterlim_INF filterlim_principal eventually_inf_principal
  2140   by (force simp: Ball_def uniformity_dist[symmetric] eventually_uniformity_metric)
  2141 
  2142 abbreviation isUCont :: "['a::metric_space \<Rightarrow> 'b::metric_space] \<Rightarrow> bool" where
  2143   "isUCont f \<equiv> uniformly_continuous_on UNIV f"
  2144 
  2145 lemma isUCont_def: "isUCont f = (\<forall>r>0. \<exists>s>0. \<forall>x y. dist x y < s \<longrightarrow> dist (f x) (f y) < r)"
  2146   by (auto simp: uniformly_continuous_on_def dist_commute)
  2147 
  2148 lemma isUCont_isCont: "isUCont f ==> isCont f x"
  2149   by (drule uniformly_continuous_imp_continuous) (simp add: continuous_on_eq_continuous_at)
  2150 
  2151 lemma uniformly_continuous_on_Cauchy:
  2152   fixes f::"'a::metric_space \<Rightarrow> 'b::metric_space"
  2153   assumes "uniformly_continuous_on S f" "Cauchy X" "\<And>n. X n \<in> S"
  2154   shows "Cauchy (\<lambda>n. f (X n))"
  2155   using assms
  2156   unfolding uniformly_continuous_on_def
  2157   apply -
  2158   apply (rule metric_CauchyI)
  2159   apply (drule_tac x=e in spec, safe)
  2160   apply (drule_tac e=d in metric_CauchyD, safe)
  2161   apply (rule_tac x=M in exI, simp)
  2162   done
  2163 
  2164 lemma isUCont_Cauchy:
  2165   "\<lbrakk>isUCont f; Cauchy X\<rbrakk> \<Longrightarrow> Cauchy (\<lambda>n. f (X n))"
  2166   by (rule uniformly_continuous_on_Cauchy[where S=UNIV and f=f]) simp_all
  2167 
  2168 lemma (in bounded_linear) isUCont: "isUCont f"
  2169 unfolding isUCont_def dist_norm
  2170 proof (intro allI impI)
  2171   fix r::real assume r: "0 < r"
  2172   obtain K where K: "0 < K" and norm_le: "\<And>x. norm (f x) \<le> norm x * K"
  2173     using pos_bounded by blast
  2174   show "\<exists>s>0. \<forall>x y. norm (x - y) < s \<longrightarrow> norm (f x - f y) < r"
  2175   proof (rule exI, safe)
  2176     from r K show "0 < r / K" by simp
  2177   next
  2178     fix x y :: 'a
  2179     assume xy: "norm (x - y) < r / K"
  2180     have "norm (f x - f y) = norm (f (x - y))" by (simp only: diff)
  2181     also have "\<dots> \<le> norm (x - y) * K" by (rule norm_le)
  2182     also from K xy have "\<dots> < r" by (simp only: pos_less_divide_eq)
  2183     finally show "norm (f x - f y) < r" .
  2184   qed
  2185 qed
  2186 
  2187 lemma (in bounded_linear) Cauchy: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. f (X n))"
  2188 by (rule isUCont [THEN isUCont_Cauchy])
  2189 
  2190 lemma LIM_less_bound:
  2191   fixes f :: "real \<Rightarrow> real"
  2192   assumes ev: "b < x" "\<forall> x' \<in> { b <..< x}. 0 \<le> f x'" and "isCont f x"
  2193   shows "0 \<le> f x"
  2194 proof (rule tendsto_le_const)
  2195   show "(f \<longlongrightarrow> f x) (at_left x)"
  2196     using \<open>isCont f x\<close> by (simp add: filterlim_at_split isCont_def)
  2197   show "eventually (\<lambda>x. 0 \<le> f x) (at_left x)"
  2198     using ev by (auto simp: eventually_at dist_real_def intro!: exI[of _ "x - b"])
  2199 qed simp
  2200 
  2201 
  2202 subsection \<open>Nested Intervals and Bisection -- Needed for Compactness\<close>
  2203 
  2204 lemma nested_sequence_unique:
  2205   assumes "\<forall>n. f n \<le> f (Suc n)" "\<forall>n. g (Suc n) \<le> g n" "\<forall>n. f n \<le> g n" "(\<lambda>n. f n - g n) \<longlonglongrightarrow> 0"
  2206   shows "\<exists>l::real. ((\<forall>n. f n \<le> l) \<and> f \<longlonglongrightarrow> l) \<and> ((\<forall>n. l \<le> g n) \<and> g \<longlonglongrightarrow> l)"
  2207 proof -
  2208   have "incseq f" unfolding incseq_Suc_iff by fact
  2209   have "decseq g" unfolding decseq_Suc_iff by fact
  2210 
  2211   { fix n
  2212     from \<open>decseq g\<close> have "g n \<le> g 0" by (rule decseqD) simp
  2213     with \<open>\<forall>n. f n \<le> g n\<close>[THEN spec, of n] have "f n \<le> g 0" by auto }
  2214   then obtain u where "f \<longlonglongrightarrow> u" "\<forall>i. f i \<le> u"
  2215     using incseq_convergent[OF \<open>incseq f\<close>] by auto
  2216   moreover
  2217   { fix n
  2218     from \<open>incseq f\<close> have "f 0 \<le> f n" by (rule incseqD) simp
  2219     with \<open>\<forall>n. f n \<le> g n\<close>[THEN spec, of n] have "f 0 \<le> g n" by simp }
  2220   then obtain l where "g \<longlonglongrightarrow> l" "\<forall>i. l \<le> g i"
  2221     using decseq_convergent[OF \<open>decseq g\<close>] by auto
  2222   moreover note LIMSEQ_unique[OF assms(4) tendsto_diff[OF \<open>f \<longlonglongrightarrow> u\<close> \<open>g \<longlonglongrightarrow> l\<close>]]
  2223   ultimately show ?thesis by auto
  2224 qed
  2225 
  2226 lemma Bolzano[consumes 1, case_names trans local]:
  2227   fixes P :: "real \<Rightarrow> real \<Rightarrow> bool"
  2228   assumes [arith]: "a \<le> b"
  2229   assumes trans: "\<And>a b c. \<lbrakk>P a b; P b c; a \<le> b; b \<le> c\<rbrakk> \<Longrightarrow> P a c"
  2230   assumes local: "\<And>x. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> \<exists>d>0. \<forall>a b. a \<le> x \<and> x \<le> b \<and> b - a < d \<longrightarrow> P a b"
  2231   shows "P a b"
  2232 proof -
  2233   define bisect where "bisect =
  2234     rec_nat (a, b) (\<lambda>n (x, y). if P x ((x+y) / 2) then ((x+y)/2, y) else (x, (x+y)/2))"
  2235   define l u where "l n = fst (bisect n)" and "u n = snd (bisect n)" for n
  2236   have l[simp]: "l 0 = a" "\<And>n. l (Suc n) = (if P (l n) ((l n + u n) / 2) then (l n + u n) / 2 else l n)"
  2237     and u[simp]: "u 0 = b" "\<And>n. u (Suc n) = (if P (l n) ((l n + u n) / 2) then u n else (l n + u n) / 2)"
  2238     by (simp_all add: l_def u_def bisect_def split: prod.split)
  2239 
  2240   { fix n have "l n \<le> u n" by (induct n) auto } note this[simp]
  2241 
  2242   have "\<exists>x. ((\<forall>n. l n \<le> x) \<and> l \<longlonglongrightarrow> x) \<and> ((\<forall>n. x \<le> u n) \<and> u \<longlonglongrightarrow> x)"
  2243   proof (safe intro!: nested_sequence_unique)
  2244     fix n show "l n \<le> l (Suc n)" "u (Suc n) \<le> u n" by (induct n) auto
  2245   next
  2246     { fix n have "l n - u n = (a - b) / 2^n" by (induct n) (auto simp: field_simps) }
  2247     then show "(\<lambda>n. l n - u n) \<longlonglongrightarrow> 0" by (simp add: LIMSEQ_divide_realpow_zero)
  2248   qed fact
  2249   then obtain x where x: "\<And>n. l n \<le> x" "\<And>n. x \<le> u n" and "l \<longlonglongrightarrow> x" "u \<longlonglongrightarrow> x" by auto
  2250   obtain d where "0 < d" and d: "\<And>a b. a \<le> x \<Longrightarrow> x \<le> b \<Longrightarrow> b - a < d \<Longrightarrow> P a b"
  2251     using \<open>l 0 \<le> x\<close> \<open>x \<le> u 0\<close> local[of x] by auto
  2252 
  2253   show "P a b"
  2254   proof (rule ccontr)
  2255     assume "\<not> P a b"
  2256     { fix n have "\<not> P (l n) (u n)"
  2257       proof (induct n)
  2258         case (Suc n) with trans[of "l n" "(l n + u n) / 2" "u n"] show ?case by auto
  2259       qed (simp add: \<open>\<not> P a b\<close>) }
  2260     moreover
  2261     { have "eventually (\<lambda>n. x - d / 2 < l n) sequentially"
  2262         using \<open>0 < d\<close> \<open>l \<longlonglongrightarrow> x\<close> by (intro order_tendstoD[of _ x]) auto
  2263       moreover have "eventually (\<lambda>n. u n < x + d / 2) sequentially"
  2264         using \<open>0 < d\<close> \<open>u \<longlonglongrightarrow> x\<close> by (intro order_tendstoD[of _ x]) auto
  2265       ultimately have "eventually (\<lambda>n. P (l n) (u n)) sequentially"
  2266       proof eventually_elim
  2267         fix n assume "x - d / 2 < l n" "u n < x + d / 2"
  2268         from add_strict_mono[OF this] have "u n - l n < d" by simp
  2269         with x show "P (l n) (u n)" by (rule d)
  2270       qed }
  2271     ultimately show False by simp
  2272   qed
  2273 qed
  2274 
  2275 lemma compact_Icc[simp, intro]: "compact {a .. b::real}"
  2276 proof (cases "a \<le> b", rule compactI)
  2277   fix C assume C: "a \<le> b" "\<forall>t\<in>C. open t" "{a..b} \<subseteq> \<Union>C"
  2278   define T where "T = {a .. b}"
  2279   from C(1,3) show "\<exists>C'\<subseteq>C. finite C' \<and> {a..b} \<subseteq> \<Union>C'"
  2280   proof (induct rule: Bolzano)
  2281     case (trans a b c)
  2282     then have *: "{a .. c} = {a .. b} \<union> {b .. c}" by auto
  2283     from trans obtain C1 C2 where "C1\<subseteq>C \<and> finite C1 \<and> {a..b} \<subseteq> \<Union>C1" "C2\<subseteq>C \<and> finite C2 \<and> {b..c} \<subseteq> \<Union>C2"
  2284       by (auto simp: *)
  2285     with trans show ?case
  2286       unfolding * by (intro exI[of _ "C1 \<union> C2"]) auto
  2287   next
  2288     case (local x)
  2289     then have "x \<in> \<Union>C" using C by auto
  2290     with C(2) obtain c where "x \<in> c" "open c" "c \<in> C" by auto
  2291     then obtain e where "0 < e" "{x - e <..< x + e} \<subseteq> c"
  2292       by (auto simp: open_dist dist_real_def subset_eq Ball_def abs_less_iff)
  2293     with \<open>c \<in> C\<close> show ?case
  2294       by (safe intro!: exI[of _ "e/2"] exI[of _ "{c}"]) auto
  2295   qed
  2296 qed simp
  2297 
  2298 
  2299 lemma continuous_image_closed_interval:
  2300   fixes a b and f :: "real \<Rightarrow> real"
  2301   defines "S \<equiv> {a..b}"
  2302   assumes "a \<le> b" and f: "continuous_on S f"
  2303   shows "\<exists>c d. f`S = {c..d} \<and> c \<le> d"
  2304 proof -
  2305   have S: "compact S" "S \<noteq> {}"
  2306     using \<open>a \<le> b\<close> by (auto simp: S_def)
  2307   obtain c where "c \<in> S" "\<forall>d\<in>S. f d \<le> f c"
  2308     using continuous_attains_sup[OF S f] by auto
  2309   moreover obtain d where "d \<in> S" "\<forall>c\<in>S. f d \<le> f c"
  2310     using continuous_attains_inf[OF S f] by auto
  2311   moreover have "connected (f`S)"
  2312     using connected_continuous_image[OF f] connected_Icc by (auto simp: S_def)
  2313   ultimately have "f ` S = {f d .. f c} \<and> f d \<le> f c"
  2314     by (auto simp: connected_iff_interval)
  2315   then show ?thesis
  2316     by auto
  2317 qed
  2318 
  2319 lemma open_Collect_positive:
  2320  fixes f :: "'a::t2_space \<Rightarrow> real"
  2321  assumes f: "continuous_on s f"
  2322  shows "\<exists>A. open A \<and> A \<inter> s = {x\<in>s. 0 < f x}"
  2323  using continuous_on_open_invariant[THEN iffD1, OF f, rule_format, of "{0 <..}"]
  2324  by (auto simp: Int_def field_simps)
  2325 
  2326 lemma open_Collect_less_Int:
  2327  fixes f g :: "'a::t2_space \<Rightarrow> real"
  2328  assumes f: "continuous_on s f" and g: "continuous_on s g"
  2329  shows "\<exists>A. open A \<and> A \<inter> s = {x\<in>s. f x < g x}"
  2330  using open_Collect_positive[OF continuous_on_diff[OF g f]] by (simp add: field_simps)
  2331 
  2332 
  2333 subsection \<open>Boundedness of continuous functions\<close>
  2334 
  2335 text\<open>By bisection, function continuous on closed interval is bounded above\<close>
  2336 
  2337 lemma isCont_eq_Ub:
  2338   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
  2339   shows "a \<le> b \<Longrightarrow> \<forall>x::real. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow>
  2340     \<exists>M. (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M) \<and> (\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M)"
  2341   using continuous_attains_sup[of "{a .. b}" f]
  2342   by (auto simp add: continuous_at_imp_continuous_on Ball_def Bex_def)
  2343 
  2344 lemma isCont_eq_Lb:
  2345   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
  2346   shows "a \<le> b \<Longrightarrow> \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow>
  2347     \<exists>M. (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> M \<le> f x) \<and> (\<exists>x. a \<le> x \<and> x \<le> b \<and> f x = M)"
  2348   using continuous_attains_inf[of "{a .. b}" f]
  2349   by (auto simp add: continuous_at_imp_continuous_on Ball_def Bex_def)
  2350 
  2351 lemma isCont_bounded:
  2352   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
  2353   shows "a \<le> b \<Longrightarrow> \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow> \<exists>M. \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M"
  2354   using isCont_eq_Ub[of a b f] by auto
  2355 
  2356 lemma isCont_has_Ub:
  2357   fixes f :: "real \<Rightarrow> 'a::linorder_topology"
  2358   shows "a \<le> b \<Longrightarrow> \<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x \<Longrightarrow>
  2359     \<exists>M. (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> M) \<and> (\<forall>N. N < M \<longrightarrow> (\<exists>x. a \<le> x \<and> x \<le> b \<and> N < f x))"
  2360   using isCont_eq_Ub[of a b f] by auto
  2361 
  2362 (*HOL style here: object-level formulations*)
  2363 lemma IVT_objl: "(f(a::real) \<le> (y::real) & y \<le> f(b) & a \<le> b &
  2364       (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
  2365       --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
  2366   by (blast intro: IVT)
  2367 
  2368 lemma IVT2_objl: "(f(b::real) \<le> (y::real) & y \<le> f(a) & a \<le> b &
  2369       (\<forall>x. a \<le> x & x \<le> b --> isCont f x))
  2370       --> (\<exists>x. a \<le> x & x \<le> b & f(x) = y)"
  2371   by (blast intro: IVT2)
  2372 
  2373 lemma isCont_Lb_Ub:
  2374   fixes f :: "real \<Rightarrow> real"
  2375   assumes "a \<le> b" "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> isCont f x"
  2376   shows "\<exists>L M. (\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> L \<le> f x \<and> f x \<le> M) \<and>
  2377                (\<forall>y. L \<le> y \<and> y \<le> M \<longrightarrow> (\<exists>x. a \<le> x \<and> x \<le> b \<and> (f x = y)))"
  2378 proof -
  2379   obtain M where M: "a \<le> M" "M \<le> b" "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f x \<le> f M"
  2380     using isCont_eq_Ub[OF assms] by auto
  2381   obtain L where L: "a \<le> L" "L \<le> b" "\<forall>x. a \<le> x \<and> x \<le> b \<longrightarrow> f L \<le> f x"
  2382     using isCont_eq_Lb[OF assms] by auto
  2383   show ?thesis
  2384     using IVT[of f L _ M] IVT2[of f L _ M] M L assms
  2385     apply (rule_tac x="f L" in exI)
  2386     apply (rule_tac x="f M" in exI)
  2387     apply (cases "L \<le> M")
  2388     apply (simp, metis order_trans)
  2389     apply (simp, metis order_trans)
  2390     done
  2391 qed
  2392 
  2393 
  2394 text\<open>Continuity of inverse function\<close>
  2395 
  2396 lemma isCont_inverse_function:
  2397   fixes f g :: "real \<Rightarrow> real"
  2398   assumes d: "0 < d"
  2399       and inj: "\<forall>z. \<bar>z-x\<bar> \<le> d \<longrightarrow> g (f z) = z"
  2400       and cont: "\<forall>z. \<bar>z-x\<bar> \<le> d \<longrightarrow> isCont f z"
  2401   shows "isCont g (f x)"
  2402 proof -
  2403   let ?A = "f (x - d)" and ?B = "f (x + d)" and ?D = "{x - d..x + d}"
  2404 
  2405   have f: "continuous_on ?D f"
  2406     using cont by (intro continuous_at_imp_continuous_on ballI) auto
  2407   then have g: "continuous_on (f`?D) g"
  2408     using inj by (intro continuous_on_inv) auto
  2409 
  2410   from d f have "{min ?A ?B <..< max ?A ?B} \<subseteq> f ` ?D"
  2411     by (intro connected_contains_Ioo connected_continuous_image) (auto split: split_min split_max)
  2412   with g have "continuous_on {min ?A ?B <..< max ?A ?B} g"
  2413     by (rule continuous_on_subset)
  2414   moreover
  2415   have "(?A < f x \<and> f x < ?B) \<or> (?B < f x \<and> f x < ?A)"
  2416     using d inj by (intro continuous_inj_imp_mono[OF _ _ f] inj_on_imageI2[of g, OF inj_onI]) auto
  2417   then have "f x \<in> {min ?A ?B <..< max ?A ?B}"
  2418     by auto
  2419   ultimately
  2420   show ?thesis
  2421     by (simp add: continuous_on_eq_continuous_at)
  2422 qed
  2423 
  2424 lemma isCont_inverse_function2:
  2425   fixes f g :: "real \<Rightarrow> real" shows
  2426   "\<lbrakk>a < x; x < b;
  2427     \<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> g (f z) = z;
  2428     \<forall>z. a \<le> z \<and> z \<le> b \<longrightarrow> isCont f z\<rbrakk>
  2429    \<Longrightarrow> isCont g (f x)"
  2430 apply (rule isCont_inverse_function
  2431        [where f=f and d="min (x - a) (b - x)"])
  2432 apply (simp_all add: abs_le_iff)
  2433 done
  2434 
  2435 (* need to rename second isCont_inverse *)
  2436 
  2437 lemma isCont_inv_fun:
  2438   fixes f g :: "real \<Rightarrow> real"
  2439   shows "[| 0 < d; \<forall>z. \<bar>z - x\<bar> \<le> d --> g(f(z)) = z;
  2440          \<forall>z. \<bar>z - x\<bar> \<le> d --> isCont f z |]
  2441       ==> isCont g (f x)"
  2442 by (rule isCont_inverse_function)
  2443 
  2444 text\<open>Bartle/Sherbert: Introduction to Real Analysis, Theorem 4.2.9, p. 110\<close>
  2445 lemma LIM_fun_gt_zero:
  2446   fixes f :: "real \<Rightarrow> real"
  2447   shows "f \<midarrow>c\<rightarrow> l \<Longrightarrow> 0 < l \<Longrightarrow> \<exists>r. 0 < r \<and> (\<forall>x. x \<noteq> c \<and> \<bar>c - x\<bar> < r \<longrightarrow> 0 < f x)"
  2448 apply (drule (1) LIM_D, clarify)
  2449 apply (rule_tac x = s in exI)
  2450 apply (simp add: abs_less_iff)
  2451 done
  2452 
  2453 lemma LIM_fun_less_zero:
  2454   fixes f :: "real \<Rightarrow> real"
  2455   shows "f \<midarrow>c\<rightarrow> l \<Longrightarrow> l < 0 \<Longrightarrow> \<exists>r. 0 < r \<and> (\<forall>x. x \<noteq> c \<and> \<bar>c - x\<bar> < r \<longrightarrow> f x < 0)"
  2456 apply (drule LIM_D [where r="-l"], simp, clarify)
  2457 apply (rule_tac x = s in exI)
  2458 apply (simp add: abs_less_iff)
  2459 done
  2460 
  2461 lemma LIM_fun_not_zero:
  2462   fixes f :: "real \<Rightarrow> real"
  2463   shows "f \<midarrow>c\<rightarrow> l \<Longrightarrow> l \<noteq> 0 \<Longrightarrow> \<exists>r. 0 < r \<and> (\<forall>x. x \<noteq> c \<and> \<bar>c - x\<bar> < r \<longrightarrow> f x \<noteq> 0)"
  2464   using LIM_fun_gt_zero[of f l c] LIM_fun_less_zero[of f l c] by (auto simp add: neq_iff)
  2465 
  2466 end