src/HOL/Limits.thy
author noschinl
Mon Mar 12 21:34:43 2012 +0100 (2012-03-12)
changeset 46887 cb891d9a23c1
parent 46886 4cd29473c65d
child 47432 e1576d13e933
permissions -rw-r--r--
use eventually_elim method
     1 (*  Title       : Limits.thy
     2     Author      : Brian Huffman
     3 *)
     4 
     5 header {* Filters and Limits *}
     6 
     7 theory Limits
     8 imports RealVector
     9 begin
    10 
    11 subsection {* Filters *}
    12 
    13 text {*
    14   This definition also allows non-proper filters.
    15 *}
    16 
    17 locale is_filter =
    18   fixes F :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
    19   assumes True: "F (\<lambda>x. True)"
    20   assumes conj: "F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x) \<Longrightarrow> F (\<lambda>x. P x \<and> Q x)"
    21   assumes mono: "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> F (\<lambda>x. P x) \<Longrightarrow> F (\<lambda>x. Q x)"
    22 
    23 typedef (open) 'a filter = "{F :: ('a \<Rightarrow> bool) \<Rightarrow> bool. is_filter F}"
    24 proof
    25   show "(\<lambda>x. True) \<in> ?filter" by (auto intro: is_filter.intro)
    26 qed
    27 
    28 lemma is_filter_Rep_filter: "is_filter (Rep_filter F)"
    29   using Rep_filter [of F] by simp
    30 
    31 lemma Abs_filter_inverse':
    32   assumes "is_filter F" shows "Rep_filter (Abs_filter F) = F"
    33   using assms by (simp add: Abs_filter_inverse)
    34 
    35 
    36 subsection {* Eventually *}
    37 
    38 definition eventually :: "('a \<Rightarrow> bool) \<Rightarrow> 'a filter \<Rightarrow> bool"
    39   where "eventually P F \<longleftrightarrow> Rep_filter F P"
    40 
    41 lemma eventually_Abs_filter:
    42   assumes "is_filter F" shows "eventually P (Abs_filter F) = F P"
    43   unfolding eventually_def using assms by (simp add: Abs_filter_inverse)
    44 
    45 lemma filter_eq_iff:
    46   shows "F = F' \<longleftrightarrow> (\<forall>P. eventually P F = eventually P F')"
    47   unfolding Rep_filter_inject [symmetric] fun_eq_iff eventually_def ..
    48 
    49 lemma eventually_True [simp]: "eventually (\<lambda>x. True) F"
    50   unfolding eventually_def
    51   by (rule is_filter.True [OF is_filter_Rep_filter])
    52 
    53 lemma always_eventually: "\<forall>x. P x \<Longrightarrow> eventually P F"
    54 proof -
    55   assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
    56   thus "eventually P F" by simp
    57 qed
    58 
    59 lemma eventually_mono:
    60   "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually P F \<Longrightarrow> eventually Q F"
    61   unfolding eventually_def
    62   by (rule is_filter.mono [OF is_filter_Rep_filter])
    63 
    64 lemma eventually_conj:
    65   assumes P: "eventually (\<lambda>x. P x) F"
    66   assumes Q: "eventually (\<lambda>x. Q x) F"
    67   shows "eventually (\<lambda>x. P x \<and> Q x) F"
    68   using assms unfolding eventually_def
    69   by (rule is_filter.conj [OF is_filter_Rep_filter])
    70 
    71 lemma eventually_mp:
    72   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
    73   assumes "eventually (\<lambda>x. P x) F"
    74   shows "eventually (\<lambda>x. Q x) F"
    75 proof (rule eventually_mono)
    76   show "\<forall>x. (P x \<longrightarrow> Q x) \<and> P x \<longrightarrow> Q x" by simp
    77   show "eventually (\<lambda>x. (P x \<longrightarrow> Q x) \<and> P x) F"
    78     using assms by (rule eventually_conj)
    79 qed
    80 
    81 lemma eventually_rev_mp:
    82   assumes "eventually (\<lambda>x. P x) F"
    83   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
    84   shows "eventually (\<lambda>x. Q x) F"
    85 using assms(2) assms(1) by (rule eventually_mp)
    86 
    87 lemma eventually_conj_iff:
    88   "eventually (\<lambda>x. P x \<and> Q x) F \<longleftrightarrow> eventually P F \<and> eventually Q F"
    89   by (auto intro: eventually_conj elim: eventually_rev_mp)
    90 
    91 lemma eventually_elim1:
    92   assumes "eventually (\<lambda>i. P i) F"
    93   assumes "\<And>i. P i \<Longrightarrow> Q i"
    94   shows "eventually (\<lambda>i. Q i) F"
    95   using assms by (auto elim!: eventually_rev_mp)
    96 
    97 lemma eventually_elim2:
    98   assumes "eventually (\<lambda>i. P i) F"
    99   assumes "eventually (\<lambda>i. Q i) F"
   100   assumes "\<And>i. P i \<Longrightarrow> Q i \<Longrightarrow> R i"
   101   shows "eventually (\<lambda>i. R i) F"
   102   using assms by (auto elim!: eventually_rev_mp)
   103 
   104 lemma eventually_subst:
   105   assumes "eventually (\<lambda>n. P n = Q n) F"
   106   shows "eventually P F = eventually Q F" (is "?L = ?R")
   107 proof -
   108   from assms have "eventually (\<lambda>x. P x \<longrightarrow> Q x) F"
   109       and "eventually (\<lambda>x. Q x \<longrightarrow> P x) F"
   110     by (auto elim: eventually_elim1)
   111   then show ?thesis by (auto elim: eventually_elim2)
   112 qed
   113 
   114 ML {*
   115   fun ev_elim_tac ctxt thms thm = let
   116       val thy = Proof_Context.theory_of ctxt
   117       val mp_thms = thms RL [@{thm eventually_rev_mp}]
   118       val raw_elim_thm =
   119         (@{thm allI} RS @{thm always_eventually})
   120         |> fold (fn thm1 => fn thm2 => thm2 RS thm1) mp_thms
   121         |> fold (fn _ => fn thm => @{thm impI} RS thm) thms
   122       val cases_prop = prop_of (raw_elim_thm RS thm)
   123       val cases = (Rule_Cases.make_common (thy, cases_prop) [(("elim", []), [])])
   124     in
   125       CASES cases (rtac raw_elim_thm 1) thm
   126     end
   127 
   128   fun eventually_elim_setup name =
   129     Method.setup name (Scan.succeed (fn ctxt => METHOD_CASES (ev_elim_tac ctxt)))
   130       "elimination of eventually quantifiers"
   131 *}
   132 
   133 setup {* eventually_elim_setup @{binding "eventually_elim"} *}
   134 
   135 
   136 subsection {* Finer-than relation *}
   137 
   138 text {* @{term "F \<le> F'"} means that filter @{term F} is finer than
   139 filter @{term F'}. *}
   140 
   141 instantiation filter :: (type) complete_lattice
   142 begin
   143 
   144 definition le_filter_def:
   145   "F \<le> F' \<longleftrightarrow> (\<forall>P. eventually P F' \<longrightarrow> eventually P F)"
   146 
   147 definition
   148   "(F :: 'a filter) < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   149 
   150 definition
   151   "top = Abs_filter (\<lambda>P. \<forall>x. P x)"
   152 
   153 definition
   154   "bot = Abs_filter (\<lambda>P. True)"
   155 
   156 definition
   157   "sup F F' = Abs_filter (\<lambda>P. eventually P F \<and> eventually P F')"
   158 
   159 definition
   160   "inf F F' = Abs_filter
   161       (\<lambda>P. \<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   162 
   163 definition
   164   "Sup S = Abs_filter (\<lambda>P. \<forall>F\<in>S. eventually P F)"
   165 
   166 definition
   167   "Inf S = Sup {F::'a filter. \<forall>F'\<in>S. F \<le> F'}"
   168 
   169 lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
   170   unfolding top_filter_def
   171   by (rule eventually_Abs_filter, rule is_filter.intro, auto)
   172 
   173 lemma eventually_bot [simp]: "eventually P bot"
   174   unfolding bot_filter_def
   175   by (subst eventually_Abs_filter, rule is_filter.intro, auto)
   176 
   177 lemma eventually_sup:
   178   "eventually P (sup F F') \<longleftrightarrow> eventually P F \<and> eventually P F'"
   179   unfolding sup_filter_def
   180   by (rule eventually_Abs_filter, rule is_filter.intro)
   181      (auto elim!: eventually_rev_mp)
   182 
   183 lemma eventually_inf:
   184   "eventually P (inf F F') \<longleftrightarrow>
   185    (\<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   186   unfolding inf_filter_def
   187   apply (rule eventually_Abs_filter, rule is_filter.intro)
   188   apply (fast intro: eventually_True)
   189   apply clarify
   190   apply (intro exI conjI)
   191   apply (erule (1) eventually_conj)
   192   apply (erule (1) eventually_conj)
   193   apply simp
   194   apply auto
   195   done
   196 
   197 lemma eventually_Sup:
   198   "eventually P (Sup S) \<longleftrightarrow> (\<forall>F\<in>S. eventually P F)"
   199   unfolding Sup_filter_def
   200   apply (rule eventually_Abs_filter, rule is_filter.intro)
   201   apply (auto intro: eventually_conj elim!: eventually_rev_mp)
   202   done
   203 
   204 instance proof
   205   fix F F' F'' :: "'a filter" and S :: "'a filter set"
   206   { show "F < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   207     by (rule less_filter_def) }
   208   { show "F \<le> F"
   209     unfolding le_filter_def by simp }
   210   { assume "F \<le> F'" and "F' \<le> F''" thus "F \<le> F''"
   211     unfolding le_filter_def by simp }
   212   { assume "F \<le> F'" and "F' \<le> F" thus "F = F'"
   213     unfolding le_filter_def filter_eq_iff by fast }
   214   { show "F \<le> top"
   215     unfolding le_filter_def eventually_top by (simp add: always_eventually) }
   216   { show "bot \<le> F"
   217     unfolding le_filter_def by simp }
   218   { show "F \<le> sup F F'" and "F' \<le> sup F F'"
   219     unfolding le_filter_def eventually_sup by simp_all }
   220   { assume "F \<le> F''" and "F' \<le> F''" thus "sup F F' \<le> F''"
   221     unfolding le_filter_def eventually_sup by simp }
   222   { show "inf F F' \<le> F" and "inf F F' \<le> F'"
   223     unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
   224   { assume "F \<le> F'" and "F \<le> F''" thus "F \<le> inf F' F''"
   225     unfolding le_filter_def eventually_inf
   226     by (auto elim!: eventually_mono intro: eventually_conj) }
   227   { assume "F \<in> S" thus "F \<le> Sup S"
   228     unfolding le_filter_def eventually_Sup by simp }
   229   { assume "\<And>F. F \<in> S \<Longrightarrow> F \<le> F'" thus "Sup S \<le> F'"
   230     unfolding le_filter_def eventually_Sup by simp }
   231   { assume "F'' \<in> S" thus "Inf S \<le> F''"
   232     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   233   { assume "\<And>F'. F' \<in> S \<Longrightarrow> F \<le> F'" thus "F \<le> Inf S"
   234     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   235 qed
   236 
   237 end
   238 
   239 lemma filter_leD:
   240   "F \<le> F' \<Longrightarrow> eventually P F' \<Longrightarrow> eventually P F"
   241   unfolding le_filter_def by simp
   242 
   243 lemma filter_leI:
   244   "(\<And>P. eventually P F' \<Longrightarrow> eventually P F) \<Longrightarrow> F \<le> F'"
   245   unfolding le_filter_def by simp
   246 
   247 lemma eventually_False:
   248   "eventually (\<lambda>x. False) F \<longleftrightarrow> F = bot"
   249   unfolding filter_eq_iff by (auto elim: eventually_rev_mp)
   250 
   251 abbreviation (input) trivial_limit :: "'a filter \<Rightarrow> bool"
   252   where "trivial_limit F \<equiv> F = bot"
   253 
   254 lemma trivial_limit_def: "trivial_limit F \<longleftrightarrow> eventually (\<lambda>x. False) F"
   255   by (rule eventually_False [symmetric])
   256 
   257 
   258 subsection {* Map function for filters *}
   259 
   260 definition filtermap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow> 'b filter"
   261   where "filtermap f F = Abs_filter (\<lambda>P. eventually (\<lambda>x. P (f x)) F)"
   262 
   263 lemma eventually_filtermap:
   264   "eventually P (filtermap f F) = eventually (\<lambda>x. P (f x)) F"
   265   unfolding filtermap_def
   266   apply (rule eventually_Abs_filter)
   267   apply (rule is_filter.intro)
   268   apply (auto elim!: eventually_rev_mp)
   269   done
   270 
   271 lemma filtermap_ident: "filtermap (\<lambda>x. x) F = F"
   272   by (simp add: filter_eq_iff eventually_filtermap)
   273 
   274 lemma filtermap_filtermap:
   275   "filtermap f (filtermap g F) = filtermap (\<lambda>x. f (g x)) F"
   276   by (simp add: filter_eq_iff eventually_filtermap)
   277 
   278 lemma filtermap_mono: "F \<le> F' \<Longrightarrow> filtermap f F \<le> filtermap f F'"
   279   unfolding le_filter_def eventually_filtermap by simp
   280 
   281 lemma filtermap_bot [simp]: "filtermap f bot = bot"
   282   by (simp add: filter_eq_iff eventually_filtermap)
   283 
   284 
   285 subsection {* Sequentially *}
   286 
   287 definition sequentially :: "nat filter"
   288   where "sequentially = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   289 
   290 lemma eventually_sequentially:
   291   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   292 unfolding sequentially_def
   293 proof (rule eventually_Abs_filter, rule is_filter.intro)
   294   fix P Q :: "nat \<Rightarrow> bool"
   295   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
   296   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
   297   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
   298   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
   299 qed auto
   300 
   301 lemma sequentially_bot [simp, intro]: "sequentially \<noteq> bot"
   302   unfolding filter_eq_iff eventually_sequentially by auto
   303 
   304 lemmas trivial_limit_sequentially = sequentially_bot
   305 
   306 lemma eventually_False_sequentially [simp]:
   307   "\<not> eventually (\<lambda>n. False) sequentially"
   308   by (simp add: eventually_False)
   309 
   310 lemma le_sequentially:
   311   "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
   312   unfolding le_filter_def eventually_sequentially
   313   by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
   314 
   315 lemma eventually_sequentiallyI:
   316   assumes "\<And>x. c \<le> x \<Longrightarrow> P x"
   317   shows "eventually P sequentially"
   318 using assms by (auto simp: eventually_sequentially)
   319 
   320 
   321 subsection {* Standard filters *}
   322 
   323 definition within :: "'a filter \<Rightarrow> 'a set \<Rightarrow> 'a filter" (infixr "within" 70)
   324   where "F within S = Abs_filter (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F)"
   325 
   326 definition (in topological_space) nhds :: "'a \<Rightarrow> 'a filter"
   327   where "nhds a = Abs_filter (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   328 
   329 definition (in topological_space) at :: "'a \<Rightarrow> 'a filter"
   330   where "at a = nhds a within - {a}"
   331 
   332 lemma eventually_within:
   333   "eventually P (F within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F"
   334   unfolding within_def
   335   by (rule eventually_Abs_filter, rule is_filter.intro)
   336      (auto elim!: eventually_rev_mp)
   337 
   338 lemma within_UNIV [simp]: "F within UNIV = F"
   339   unfolding filter_eq_iff eventually_within by simp
   340 
   341 lemma within_empty [simp]: "F within {} = bot"
   342   unfolding filter_eq_iff eventually_within by simp
   343 
   344 lemma eventually_nhds:
   345   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   346 unfolding nhds_def
   347 proof (rule eventually_Abs_filter, rule is_filter.intro)
   348   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
   349   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" by - rule
   350 next
   351   fix P Q
   352   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   353      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
   354   then obtain S T where
   355     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   356     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
   357   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
   358     by (simp add: open_Int)
   359   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" by - rule
   360 qed auto
   361 
   362 lemma eventually_nhds_metric:
   363   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
   364 unfolding eventually_nhds open_dist
   365 apply safe
   366 apply fast
   367 apply (rule_tac x="{x. dist x a < d}" in exI, simp)
   368 apply clarsimp
   369 apply (rule_tac x="d - dist x a" in exI, clarsimp)
   370 apply (simp only: less_diff_eq)
   371 apply (erule le_less_trans [OF dist_triangle])
   372 done
   373 
   374 lemma nhds_neq_bot [simp]: "nhds a \<noteq> bot"
   375   unfolding trivial_limit_def eventually_nhds by simp
   376 
   377 lemma eventually_at_topological:
   378   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   379 unfolding at_def eventually_within eventually_nhds by simp
   380 
   381 lemma eventually_at:
   382   fixes a :: "'a::metric_space"
   383   shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
   384 unfolding at_def eventually_within eventually_nhds_metric by auto
   385 
   386 lemma at_eq_bot_iff: "at a = bot \<longleftrightarrow> open {a}"
   387   unfolding trivial_limit_def eventually_at_topological
   388   by (safe, case_tac "S = {a}", simp, fast, fast)
   389 
   390 lemma at_neq_bot [simp]: "at (a::'a::perfect_space) \<noteq> bot"
   391   by (simp add: at_eq_bot_iff not_open_singleton)
   392 
   393 
   394 subsection {* Boundedness *}
   395 
   396 definition Bfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   397   where "Bfun f F = (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) F)"
   398 
   399 lemma BfunI:
   400   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) F" shows "Bfun f F"
   401 unfolding Bfun_def
   402 proof (intro exI conjI allI)
   403   show "0 < max K 1" by simp
   404 next
   405   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) F"
   406     using K by (rule eventually_elim1, simp)
   407 qed
   408 
   409 lemma BfunE:
   410   assumes "Bfun f F"
   411   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) F"
   412 using assms unfolding Bfun_def by fast
   413 
   414 
   415 subsection {* Convergence to Zero *}
   416 
   417 definition Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   418   where "Zfun f F = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) F)"
   419 
   420 lemma ZfunI:
   421   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F) \<Longrightarrow> Zfun f F"
   422   unfolding Zfun_def by simp
   423 
   424 lemma ZfunD:
   425   "\<lbrakk>Zfun f F; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F"
   426   unfolding Zfun_def by simp
   427 
   428 lemma Zfun_ssubst:
   429   "eventually (\<lambda>x. f x = g x) F \<Longrightarrow> Zfun g F \<Longrightarrow> Zfun f F"
   430   unfolding Zfun_def by (auto elim!: eventually_rev_mp)
   431 
   432 lemma Zfun_zero: "Zfun (\<lambda>x. 0) F"
   433   unfolding Zfun_def by simp
   434 
   435 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) F = Zfun (\<lambda>x. f x) F"
   436   unfolding Zfun_def by simp
   437 
   438 lemma Zfun_imp_Zfun:
   439   assumes f: "Zfun f F"
   440   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) F"
   441   shows "Zfun (\<lambda>x. g x) F"
   442 proof (cases)
   443   assume K: "0 < K"
   444   show ?thesis
   445   proof (rule ZfunI)
   446     fix r::real assume "0 < r"
   447     hence "0 < r / K"
   448       using K by (rule divide_pos_pos)
   449     then have "eventually (\<lambda>x. norm (f x) < r / K) F"
   450       using ZfunD [OF f] by fast
   451     with g show "eventually (\<lambda>x. norm (g x) < r) F"
   452     proof eventually_elim
   453       case (elim x)
   454       hence "norm (f x) * K < r"
   455         by (simp add: pos_less_divide_eq K)
   456       thus ?case
   457         by (simp add: order_le_less_trans [OF elim(1)])
   458     qed
   459   qed
   460 next
   461   assume "\<not> 0 < K"
   462   hence K: "K \<le> 0" by (simp only: not_less)
   463   show ?thesis
   464   proof (rule ZfunI)
   465     fix r :: real
   466     assume "0 < r"
   467     from g show "eventually (\<lambda>x. norm (g x) < r) F"
   468     proof eventually_elim
   469       case (elim x)
   470       also have "norm (f x) * K \<le> norm (f x) * 0"
   471         using K norm_ge_zero by (rule mult_left_mono)
   472       finally show ?case
   473         using `0 < r` by simp
   474     qed
   475   qed
   476 qed
   477 
   478 lemma Zfun_le: "\<lbrakk>Zfun g F; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f F"
   479   by (erule_tac K="1" in Zfun_imp_Zfun, simp)
   480 
   481 lemma Zfun_add:
   482   assumes f: "Zfun f F" and g: "Zfun g F"
   483   shows "Zfun (\<lambda>x. f x + g x) F"
   484 proof (rule ZfunI)
   485   fix r::real assume "0 < r"
   486   hence r: "0 < r / 2" by simp
   487   have "eventually (\<lambda>x. norm (f x) < r/2) F"
   488     using f r by (rule ZfunD)
   489   moreover
   490   have "eventually (\<lambda>x. norm (g x) < r/2) F"
   491     using g r by (rule ZfunD)
   492   ultimately
   493   show "eventually (\<lambda>x. norm (f x + g x) < r) F"
   494   proof eventually_elim
   495     case (elim x)
   496     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
   497       by (rule norm_triangle_ineq)
   498     also have "\<dots> < r/2 + r/2"
   499       using elim by (rule add_strict_mono)
   500     finally show ?case
   501       by simp
   502   qed
   503 qed
   504 
   505 lemma Zfun_minus: "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. - f x) F"
   506   unfolding Zfun_def by simp
   507 
   508 lemma Zfun_diff: "\<lbrakk>Zfun f F; Zfun g F\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) F"
   509   by (simp only: diff_minus Zfun_add Zfun_minus)
   510 
   511 lemma (in bounded_linear) Zfun:
   512   assumes g: "Zfun g F"
   513   shows "Zfun (\<lambda>x. f (g x)) F"
   514 proof -
   515   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
   516     using bounded by fast
   517   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) F"
   518     by simp
   519   with g show ?thesis
   520     by (rule Zfun_imp_Zfun)
   521 qed
   522 
   523 lemma (in bounded_bilinear) Zfun:
   524   assumes f: "Zfun f F"
   525   assumes g: "Zfun g F"
   526   shows "Zfun (\<lambda>x. f x ** g x) F"
   527 proof (rule ZfunI)
   528   fix r::real assume r: "0 < r"
   529   obtain K where K: "0 < K"
   530     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   531     using pos_bounded by fast
   532   from K have K': "0 < inverse K"
   533     by (rule positive_imp_inverse_positive)
   534   have "eventually (\<lambda>x. norm (f x) < r) F"
   535     using f r by (rule ZfunD)
   536   moreover
   537   have "eventually (\<lambda>x. norm (g x) < inverse K) F"
   538     using g K' by (rule ZfunD)
   539   ultimately
   540   show "eventually (\<lambda>x. norm (f x ** g x) < r) F"
   541   proof eventually_elim
   542     case (elim x)
   543     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   544       by (rule norm_le)
   545     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
   546       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero elim K)
   547     also from K have "r * inverse K * K = r"
   548       by simp
   549     finally show ?case .
   550   qed
   551 qed
   552 
   553 lemma (in bounded_bilinear) Zfun_left:
   554   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. f x ** a) F"
   555   by (rule bounded_linear_left [THEN bounded_linear.Zfun])
   556 
   557 lemma (in bounded_bilinear) Zfun_right:
   558   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. a ** f x) F"
   559   by (rule bounded_linear_right [THEN bounded_linear.Zfun])
   560 
   561 lemmas Zfun_mult = bounded_bilinear.Zfun [OF bounded_bilinear_mult]
   562 lemmas Zfun_mult_right = bounded_bilinear.Zfun_right [OF bounded_bilinear_mult]
   563 lemmas Zfun_mult_left = bounded_bilinear.Zfun_left [OF bounded_bilinear_mult]
   564 
   565 
   566 subsection {* Limits *}
   567 
   568 definition (in topological_space)
   569   tendsto :: "('b \<Rightarrow> 'a) \<Rightarrow> 'a \<Rightarrow> 'b filter \<Rightarrow> bool" (infixr "--->" 55) where
   570   "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   571 
   572 definition real_tendsto_inf :: "('a \<Rightarrow> real) \<Rightarrow> 'a filter \<Rightarrow> bool" where
   573   "real_tendsto_inf f F \<equiv> \<forall>x. eventually (\<lambda>y. x < f y) F"
   574 
   575 ML {*
   576 structure Tendsto_Intros = Named_Thms
   577 (
   578   val name = @{binding tendsto_intros}
   579   val description = "introduction rules for tendsto"
   580 )
   581 *}
   582 
   583 setup Tendsto_Intros.setup
   584 
   585 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
   586   unfolding tendsto_def le_filter_def by fast
   587 
   588 lemma topological_tendstoI:
   589   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F)
   590     \<Longrightarrow> (f ---> l) F"
   591   unfolding tendsto_def by auto
   592 
   593 lemma topological_tendstoD:
   594   "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   595   unfolding tendsto_def by auto
   596 
   597 lemma tendstoI:
   598   assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   599   shows "(f ---> l) F"
   600   apply (rule topological_tendstoI)
   601   apply (simp add: open_dist)
   602   apply (drule (1) bspec, clarify)
   603   apply (drule assms)
   604   apply (erule eventually_elim1, simp)
   605   done
   606 
   607 lemma tendstoD:
   608   "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   609   apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
   610   apply (clarsimp simp add: open_dist)
   611   apply (rule_tac x="e - dist x l" in exI, clarsimp)
   612   apply (simp only: less_diff_eq)
   613   apply (erule le_less_trans [OF dist_triangle])
   614   apply simp
   615   apply simp
   616   done
   617 
   618 lemma tendsto_iff:
   619   "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
   620   using tendstoI tendstoD by fast
   621 
   622 lemma tendsto_Zfun_iff: "(f ---> a) F = Zfun (\<lambda>x. f x - a) F"
   623   by (simp only: tendsto_iff Zfun_def dist_norm)
   624 
   625 lemma tendsto_bot [simp]: "(f ---> a) bot"
   626   unfolding tendsto_def by simp
   627 
   628 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
   629   unfolding tendsto_def eventually_at_topological by auto
   630 
   631 lemma tendsto_ident_at_within [tendsto_intros]:
   632   "((\<lambda>x. x) ---> a) (at a within S)"
   633   unfolding tendsto_def eventually_within eventually_at_topological by auto
   634 
   635 lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) F"
   636   by (simp add: tendsto_def)
   637 
   638 lemma tendsto_unique:
   639   fixes f :: "'a \<Rightarrow> 'b::t2_space"
   640   assumes "\<not> trivial_limit F" and "(f ---> a) F" and "(f ---> b) F"
   641   shows "a = b"
   642 proof (rule ccontr)
   643   assume "a \<noteq> b"
   644   obtain U V where "open U" "open V" "a \<in> U" "b \<in> V" "U \<inter> V = {}"
   645     using hausdorff [OF `a \<noteq> b`] by fast
   646   have "eventually (\<lambda>x. f x \<in> U) F"
   647     using `(f ---> a) F` `open U` `a \<in> U` by (rule topological_tendstoD)
   648   moreover
   649   have "eventually (\<lambda>x. f x \<in> V) F"
   650     using `(f ---> b) F` `open V` `b \<in> V` by (rule topological_tendstoD)
   651   ultimately
   652   have "eventually (\<lambda>x. False) F"
   653   proof eventually_elim
   654     case (elim x)
   655     hence "f x \<in> U \<inter> V" by simp
   656     with `U \<inter> V = {}` show ?case by simp
   657   qed
   658   with `\<not> trivial_limit F` show "False"
   659     by (simp add: trivial_limit_def)
   660 qed
   661 
   662 lemma tendsto_const_iff:
   663   fixes a b :: "'a::t2_space"
   664   assumes "\<not> trivial_limit F" shows "((\<lambda>x. a) ---> b) F \<longleftrightarrow> a = b"
   665   by (safe intro!: tendsto_const tendsto_unique [OF assms tendsto_const])
   666 
   667 lemma tendsto_compose:
   668   assumes g: "(g ---> g l) (at l)"
   669   assumes f: "(f ---> l) F"
   670   shows "((\<lambda>x. g (f x)) ---> g l) F"
   671 proof (rule topological_tendstoI)
   672   fix B assume B: "open B" "g l \<in> B"
   673   obtain A where A: "open A" "l \<in> A"
   674     and gB: "\<forall>y. y \<in> A \<longrightarrow> g y \<in> B"
   675     using topological_tendstoD [OF g B] B(2)
   676     unfolding eventually_at_topological by fast
   677   hence "\<forall>x. f x \<in> A \<longrightarrow> g (f x) \<in> B" by simp
   678   from this topological_tendstoD [OF f A]
   679   show "eventually (\<lambda>x. g (f x) \<in> B) F"
   680     by (rule eventually_mono)
   681 qed
   682 
   683 lemma tendsto_compose_eventually:
   684   assumes g: "(g ---> m) (at l)"
   685   assumes f: "(f ---> l) F"
   686   assumes inj: "eventually (\<lambda>x. f x \<noteq> l) F"
   687   shows "((\<lambda>x. g (f x)) ---> m) F"
   688 proof (rule topological_tendstoI)
   689   fix B assume B: "open B" "m \<in> B"
   690   obtain A where A: "open A" "l \<in> A"
   691     and gB: "\<And>y. y \<in> A \<Longrightarrow> y \<noteq> l \<Longrightarrow> g y \<in> B"
   692     using topological_tendstoD [OF g B]
   693     unfolding eventually_at_topological by fast
   694   show "eventually (\<lambda>x. g (f x) \<in> B) F"
   695     using topological_tendstoD [OF f A] inj
   696     by (rule eventually_elim2) (simp add: gB)
   697 qed
   698 
   699 lemma metric_tendsto_imp_tendsto:
   700   assumes f: "(f ---> a) F"
   701   assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
   702   shows "(g ---> b) F"
   703 proof (rule tendstoI)
   704   fix e :: real assume "0 < e"
   705   with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
   706   with le show "eventually (\<lambda>x. dist (g x) b < e) F"
   707     using le_less_trans by (rule eventually_elim2)
   708 qed
   709 
   710 lemma real_tendsto_inf_real: "real_tendsto_inf real sequentially"
   711 proof (unfold real_tendsto_inf_def, rule allI)
   712   fix x show "eventually (\<lambda>y. x < real y) sequentially"
   713     by (rule eventually_sequentiallyI[of "natceiling (x + 1)"])
   714         (simp add: natceiling_le_eq)
   715 qed
   716 
   717 
   718 
   719 subsubsection {* Distance and norms *}
   720 
   721 lemma tendsto_dist [tendsto_intros]:
   722   assumes f: "(f ---> l) F" and g: "(g ---> m) F"
   723   shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) F"
   724 proof (rule tendstoI)
   725   fix e :: real assume "0 < e"
   726   hence e2: "0 < e/2" by simp
   727   from tendstoD [OF f e2] tendstoD [OF g e2]
   728   show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
   729   proof (eventually_elim)
   730     case (elim x)
   731     then show "dist (dist (f x) (g x)) (dist l m) < e"
   732       unfolding dist_real_def
   733       using dist_triangle2 [of "f x" "g x" "l"]
   734       using dist_triangle2 [of "g x" "l" "m"]
   735       using dist_triangle3 [of "l" "m" "f x"]
   736       using dist_triangle [of "f x" "m" "g x"]
   737       by arith
   738   qed
   739 qed
   740 
   741 lemma norm_conv_dist: "norm x = dist x 0"
   742   unfolding dist_norm by simp
   743 
   744 lemma tendsto_norm [tendsto_intros]:
   745   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> norm a) F"
   746   unfolding norm_conv_dist by (intro tendsto_intros)
   747 
   748 lemma tendsto_norm_zero:
   749   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> 0) F"
   750   by (drule tendsto_norm, simp)
   751 
   752 lemma tendsto_norm_zero_cancel:
   753   "((\<lambda>x. norm (f x)) ---> 0) F \<Longrightarrow> (f ---> 0) F"
   754   unfolding tendsto_iff dist_norm by simp
   755 
   756 lemma tendsto_norm_zero_iff:
   757   "((\<lambda>x. norm (f x)) ---> 0) F \<longleftrightarrow> (f ---> 0) F"
   758   unfolding tendsto_iff dist_norm by simp
   759 
   760 lemma tendsto_rabs [tendsto_intros]:
   761   "(f ---> (l::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> \<bar>l\<bar>) F"
   762   by (fold real_norm_def, rule tendsto_norm)
   763 
   764 lemma tendsto_rabs_zero:
   765   "(f ---> (0::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> 0) F"
   766   by (fold real_norm_def, rule tendsto_norm_zero)
   767 
   768 lemma tendsto_rabs_zero_cancel:
   769   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<Longrightarrow> (f ---> 0) F"
   770   by (fold real_norm_def, rule tendsto_norm_zero_cancel)
   771 
   772 lemma tendsto_rabs_zero_iff:
   773   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<longleftrightarrow> (f ---> 0) F"
   774   by (fold real_norm_def, rule tendsto_norm_zero_iff)
   775 
   776 subsubsection {* Addition and subtraction *}
   777 
   778 lemma tendsto_add [tendsto_intros]:
   779   fixes a b :: "'a::real_normed_vector"
   780   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> a + b) F"
   781   by (simp only: tendsto_Zfun_iff add_diff_add Zfun_add)
   782 
   783 lemma tendsto_add_zero:
   784   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
   785   shows "\<lbrakk>(f ---> 0) F; (g ---> 0) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> 0) F"
   786   by (drule (1) tendsto_add, simp)
   787 
   788 lemma tendsto_minus [tendsto_intros]:
   789   fixes a :: "'a::real_normed_vector"
   790   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. - f x) ---> - a) F"
   791   by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)
   792 
   793 lemma tendsto_minus_cancel:
   794   fixes a :: "'a::real_normed_vector"
   795   shows "((\<lambda>x. - f x) ---> - a) F \<Longrightarrow> (f ---> a) F"
   796   by (drule tendsto_minus, simp)
   797 
   798 lemma tendsto_diff [tendsto_intros]:
   799   fixes a b :: "'a::real_normed_vector"
   800   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) ---> a - b) F"
   801   by (simp add: diff_minus tendsto_add tendsto_minus)
   802 
   803 lemma tendsto_setsum [tendsto_intros]:
   804   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
   805   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> a i) F"
   806   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) ---> (\<Sum>i\<in>S. a i)) F"
   807 proof (cases "finite S")
   808   assume "finite S" thus ?thesis using assms
   809     by (induct, simp add: tendsto_const, simp add: tendsto_add)
   810 next
   811   assume "\<not> finite S" thus ?thesis
   812     by (simp add: tendsto_const)
   813 qed
   814 
   815 lemma real_tendsto_sandwich:
   816   fixes f g h :: "'a \<Rightarrow> real"
   817   assumes ev: "eventually (\<lambda>n. f n \<le> g n) net" "eventually (\<lambda>n. g n \<le> h n) net"
   818   assumes lim: "(f ---> c) net" "(h ---> c) net"
   819   shows "(g ---> c) net"
   820 proof -
   821   have "((\<lambda>n. g n - f n) ---> 0) net"
   822   proof (rule metric_tendsto_imp_tendsto)
   823     show "eventually (\<lambda>n. dist (g n - f n) 0 \<le> dist (h n - f n) 0) net"
   824       using ev by (rule eventually_elim2) (simp add: dist_real_def)
   825     show "((\<lambda>n. h n - f n) ---> 0) net"
   826       using tendsto_diff[OF lim(2,1)] by simp
   827   qed
   828   from tendsto_add[OF this lim(1)] show ?thesis by simp
   829 qed
   830 
   831 subsubsection {* Linear operators and multiplication *}
   832 
   833 lemma (in bounded_linear) tendsto:
   834   "(g ---> a) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> f a) F"
   835   by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
   836 
   837 lemma (in bounded_linear) tendsto_zero:
   838   "(g ---> 0) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> 0) F"
   839   by (drule tendsto, simp only: zero)
   840 
   841 lemma (in bounded_bilinear) tendsto:
   842   "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) ---> a ** b) F"
   843   by (simp only: tendsto_Zfun_iff prod_diff_prod
   844                  Zfun_add Zfun Zfun_left Zfun_right)
   845 
   846 lemma (in bounded_bilinear) tendsto_zero:
   847   assumes f: "(f ---> 0) F"
   848   assumes g: "(g ---> 0) F"
   849   shows "((\<lambda>x. f x ** g x) ---> 0) F"
   850   using tendsto [OF f g] by (simp add: zero_left)
   851 
   852 lemma (in bounded_bilinear) tendsto_left_zero:
   853   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. f x ** c) ---> 0) F"
   854   by (rule bounded_linear.tendsto_zero [OF bounded_linear_left])
   855 
   856 lemma (in bounded_bilinear) tendsto_right_zero:
   857   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. c ** f x) ---> 0) F"
   858   by (rule bounded_linear.tendsto_zero [OF bounded_linear_right])
   859 
   860 lemmas tendsto_of_real [tendsto_intros] =
   861   bounded_linear.tendsto [OF bounded_linear_of_real]
   862 
   863 lemmas tendsto_scaleR [tendsto_intros] =
   864   bounded_bilinear.tendsto [OF bounded_bilinear_scaleR]
   865 
   866 lemmas tendsto_mult [tendsto_intros] =
   867   bounded_bilinear.tendsto [OF bounded_bilinear_mult]
   868 
   869 lemmas tendsto_mult_zero =
   870   bounded_bilinear.tendsto_zero [OF bounded_bilinear_mult]
   871 
   872 lemmas tendsto_mult_left_zero =
   873   bounded_bilinear.tendsto_left_zero [OF bounded_bilinear_mult]
   874 
   875 lemmas tendsto_mult_right_zero =
   876   bounded_bilinear.tendsto_right_zero [OF bounded_bilinear_mult]
   877 
   878 lemma tendsto_power [tendsto_intros]:
   879   fixes f :: "'a \<Rightarrow> 'b::{power,real_normed_algebra}"
   880   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. f x ^ n) ---> a ^ n) F"
   881   by (induct n) (simp_all add: tendsto_const tendsto_mult)
   882 
   883 lemma tendsto_setprod [tendsto_intros]:
   884   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
   885   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> L i) F"
   886   shows "((\<lambda>x. \<Prod>i\<in>S. f i x) ---> (\<Prod>i\<in>S. L i)) F"
   887 proof (cases "finite S")
   888   assume "finite S" thus ?thesis using assms
   889     by (induct, simp add: tendsto_const, simp add: tendsto_mult)
   890 next
   891   assume "\<not> finite S" thus ?thesis
   892     by (simp add: tendsto_const)
   893 qed
   894 
   895 subsubsection {* Inverse and division *}
   896 
   897 lemma (in bounded_bilinear) Zfun_prod_Bfun:
   898   assumes f: "Zfun f F"
   899   assumes g: "Bfun g F"
   900   shows "Zfun (\<lambda>x. f x ** g x) F"
   901 proof -
   902   obtain K where K: "0 \<le> K"
   903     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   904     using nonneg_bounded by fast
   905   obtain B where B: "0 < B"
   906     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) F"
   907     using g by (rule BfunE)
   908   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) F"
   909   using norm_g proof eventually_elim
   910     case (elim x)
   911     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   912       by (rule norm_le)
   913     also have "\<dots> \<le> norm (f x) * B * K"
   914       by (intro mult_mono' order_refl norm_g norm_ge_zero
   915                 mult_nonneg_nonneg K elim)
   916     also have "\<dots> = norm (f x) * (B * K)"
   917       by (rule mult_assoc)
   918     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
   919   qed
   920   with f show ?thesis
   921     by (rule Zfun_imp_Zfun)
   922 qed
   923 
   924 lemma (in bounded_bilinear) flip:
   925   "bounded_bilinear (\<lambda>x y. y ** x)"
   926   apply default
   927   apply (rule add_right)
   928   apply (rule add_left)
   929   apply (rule scaleR_right)
   930   apply (rule scaleR_left)
   931   apply (subst mult_commute)
   932   using bounded by fast
   933 
   934 lemma (in bounded_bilinear) Bfun_prod_Zfun:
   935   assumes f: "Bfun f F"
   936   assumes g: "Zfun g F"
   937   shows "Zfun (\<lambda>x. f x ** g x) F"
   938   using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
   939 
   940 lemma Bfun_inverse_lemma:
   941   fixes x :: "'a::real_normed_div_algebra"
   942   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
   943   apply (subst nonzero_norm_inverse, clarsimp)
   944   apply (erule (1) le_imp_inverse_le)
   945   done
   946 
   947 lemma Bfun_inverse:
   948   fixes a :: "'a::real_normed_div_algebra"
   949   assumes f: "(f ---> a) F"
   950   assumes a: "a \<noteq> 0"
   951   shows "Bfun (\<lambda>x. inverse (f x)) F"
   952 proof -
   953   from a have "0 < norm a" by simp
   954   hence "\<exists>r>0. r < norm a" by (rule dense)
   955   then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
   956   have "eventually (\<lambda>x. dist (f x) a < r) F"
   957     using tendstoD [OF f r1] by fast
   958   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) F"
   959   proof eventually_elim
   960     case (elim x)
   961     hence 1: "norm (f x - a) < r"
   962       by (simp add: dist_norm)
   963     hence 2: "f x \<noteq> 0" using r2 by auto
   964     hence "norm (inverse (f x)) = inverse (norm (f x))"
   965       by (rule nonzero_norm_inverse)
   966     also have "\<dots> \<le> inverse (norm a - r)"
   967     proof (rule le_imp_inverse_le)
   968       show "0 < norm a - r" using r2 by simp
   969     next
   970       have "norm a - norm (f x) \<le> norm (a - f x)"
   971         by (rule norm_triangle_ineq2)
   972       also have "\<dots> = norm (f x - a)"
   973         by (rule norm_minus_commute)
   974       also have "\<dots> < r" using 1 .
   975       finally show "norm a - r \<le> norm (f x)" by simp
   976     qed
   977     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
   978   qed
   979   thus ?thesis by (rule BfunI)
   980 qed
   981 
   982 lemma tendsto_inverse [tendsto_intros]:
   983   fixes a :: "'a::real_normed_div_algebra"
   984   assumes f: "(f ---> a) F"
   985   assumes a: "a \<noteq> 0"
   986   shows "((\<lambda>x. inverse (f x)) ---> inverse a) F"
   987 proof -
   988   from a have "0 < norm a" by simp
   989   with f have "eventually (\<lambda>x. dist (f x) a < norm a) F"
   990     by (rule tendstoD)
   991   then have "eventually (\<lambda>x. f x \<noteq> 0) F"
   992     unfolding dist_norm by (auto elim!: eventually_elim1)
   993   with a have "eventually (\<lambda>x. inverse (f x) - inverse a =
   994     - (inverse (f x) * (f x - a) * inverse a)) F"
   995     by (auto elim!: eventually_elim1 simp: inverse_diff_inverse)
   996   moreover have "Zfun (\<lambda>x. - (inverse (f x) * (f x - a) * inverse a)) F"
   997     by (intro Zfun_minus Zfun_mult_left
   998       bounded_bilinear.Bfun_prod_Zfun [OF bounded_bilinear_mult]
   999       Bfun_inverse [OF f a] f [unfolded tendsto_Zfun_iff])
  1000   ultimately show ?thesis
  1001     unfolding tendsto_Zfun_iff by (rule Zfun_ssubst)
  1002 qed
  1003 
  1004 lemma tendsto_divide [tendsto_intros]:
  1005   fixes a b :: "'a::real_normed_field"
  1006   shows "\<lbrakk>(f ---> a) F; (g ---> b) F; b \<noteq> 0\<rbrakk>
  1007     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) F"
  1008   by (simp add: tendsto_mult tendsto_inverse divide_inverse)
  1009 
  1010 lemma tendsto_sgn [tendsto_intros]:
  1011   fixes l :: "'a::real_normed_vector"
  1012   shows "\<lbrakk>(f ---> l) F; l \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. sgn (f x)) ---> sgn l) F"
  1013   unfolding sgn_div_norm by (simp add: tendsto_intros)
  1014 
  1015 end