src/HOL/Limits.thy
author huffman
Sun Aug 14 08:45:38 2011 -0700 (2011-08-14)
changeset 44195 f5363511b212
parent 44194 0639898074ae
child 44205 18da2a87421c
permissions -rw-r--r--
consistently use variable name 'F' for filters
     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 subsection {* Finer-than relation *}
   105 
   106 text {* @{term "F \<le> F'"} means that filter @{term F} is finer than
   107 filter @{term F'}. *}
   108 
   109 instantiation filter :: (type) complete_lattice
   110 begin
   111 
   112 definition le_filter_def:
   113   "F \<le> F' \<longleftrightarrow> (\<forall>P. eventually P F' \<longrightarrow> eventually P F)"
   114 
   115 definition
   116   "(F :: 'a filter) < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   117 
   118 definition
   119   "top = Abs_filter (\<lambda>P. \<forall>x. P x)"
   120 
   121 definition
   122   "bot = Abs_filter (\<lambda>P. True)"
   123 
   124 definition
   125   "sup F F' = Abs_filter (\<lambda>P. eventually P F \<and> eventually P F')"
   126 
   127 definition
   128   "inf F F' = Abs_filter
   129       (\<lambda>P. \<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   130 
   131 definition
   132   "Sup S = Abs_filter (\<lambda>P. \<forall>F\<in>S. eventually P F)"
   133 
   134 definition
   135   "Inf S = Sup {F::'a filter. \<forall>F'\<in>S. F \<le> F'}"
   136 
   137 lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
   138   unfolding top_filter_def
   139   by (rule eventually_Abs_filter, rule is_filter.intro, auto)
   140 
   141 lemma eventually_bot [simp]: "eventually P bot"
   142   unfolding bot_filter_def
   143   by (subst eventually_Abs_filter, rule is_filter.intro, auto)
   144 
   145 lemma eventually_sup:
   146   "eventually P (sup F F') \<longleftrightarrow> eventually P F \<and> eventually P F'"
   147   unfolding sup_filter_def
   148   by (rule eventually_Abs_filter, rule is_filter.intro)
   149      (auto elim!: eventually_rev_mp)
   150 
   151 lemma eventually_inf:
   152   "eventually P (inf F F') \<longleftrightarrow>
   153    (\<exists>Q R. eventually Q F \<and> eventually R F' \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   154   unfolding inf_filter_def
   155   apply (rule eventually_Abs_filter, rule is_filter.intro)
   156   apply (fast intro: eventually_True)
   157   apply clarify
   158   apply (intro exI conjI)
   159   apply (erule (1) eventually_conj)
   160   apply (erule (1) eventually_conj)
   161   apply simp
   162   apply auto
   163   done
   164 
   165 lemma eventually_Sup:
   166   "eventually P (Sup S) \<longleftrightarrow> (\<forall>F\<in>S. eventually P F)"
   167   unfolding Sup_filter_def
   168   apply (rule eventually_Abs_filter, rule is_filter.intro)
   169   apply (auto intro: eventually_conj elim!: eventually_rev_mp)
   170   done
   171 
   172 instance proof
   173   fix F F' F'' :: "'a filter" and S :: "'a filter set"
   174   { show "F < F' \<longleftrightarrow> F \<le> F' \<and> \<not> F' \<le> F"
   175     by (rule less_filter_def) }
   176   { show "F \<le> F"
   177     unfolding le_filter_def by simp }
   178   { assume "F \<le> F'" and "F' \<le> F''" thus "F \<le> F''"
   179     unfolding le_filter_def by simp }
   180   { assume "F \<le> F'" and "F' \<le> F" thus "F = F'"
   181     unfolding le_filter_def filter_eq_iff by fast }
   182   { show "F \<le> top"
   183     unfolding le_filter_def eventually_top by (simp add: always_eventually) }
   184   { show "bot \<le> F"
   185     unfolding le_filter_def by simp }
   186   { show "F \<le> sup F F'" and "F' \<le> sup F F'"
   187     unfolding le_filter_def eventually_sup by simp_all }
   188   { assume "F \<le> F''" and "F' \<le> F''" thus "sup F F' \<le> F''"
   189     unfolding le_filter_def eventually_sup by simp }
   190   { show "inf F F' \<le> F" and "inf F F' \<le> F'"
   191     unfolding le_filter_def eventually_inf by (auto intro: eventually_True) }
   192   { assume "F \<le> F'" and "F \<le> F''" thus "F \<le> inf F' F''"
   193     unfolding le_filter_def eventually_inf
   194     by (auto elim!: eventually_mono intro: eventually_conj) }
   195   { assume "F \<in> S" thus "F \<le> Sup S"
   196     unfolding le_filter_def eventually_Sup by simp }
   197   { assume "\<And>F. F \<in> S \<Longrightarrow> F \<le> F'" thus "Sup S \<le> F'"
   198     unfolding le_filter_def eventually_Sup by simp }
   199   { assume "F'' \<in> S" thus "Inf S \<le> F''"
   200     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   201   { assume "\<And>F'. F' \<in> S \<Longrightarrow> F \<le> F'" thus "F \<le> Inf S"
   202     unfolding le_filter_def Inf_filter_def eventually_Sup Ball_def by simp }
   203 qed
   204 
   205 end
   206 
   207 lemma filter_leD:
   208   "F \<le> F' \<Longrightarrow> eventually P F' \<Longrightarrow> eventually P F"
   209   unfolding le_filter_def by simp
   210 
   211 lemma filter_leI:
   212   "(\<And>P. eventually P F' \<Longrightarrow> eventually P F) \<Longrightarrow> F \<le> F'"
   213   unfolding le_filter_def by simp
   214 
   215 lemma eventually_False:
   216   "eventually (\<lambda>x. False) F \<longleftrightarrow> F = bot"
   217   unfolding filter_eq_iff by (auto elim: eventually_rev_mp)
   218 
   219 subsection {* Map function for filters *}
   220 
   221 definition filtermap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a filter \<Rightarrow> 'b filter"
   222   where "filtermap f F = Abs_filter (\<lambda>P. eventually (\<lambda>x. P (f x)) F)"
   223 
   224 lemma eventually_filtermap:
   225   "eventually P (filtermap f F) = eventually (\<lambda>x. P (f x)) F"
   226   unfolding filtermap_def
   227   apply (rule eventually_Abs_filter)
   228   apply (rule is_filter.intro)
   229   apply (auto elim!: eventually_rev_mp)
   230   done
   231 
   232 lemma filtermap_ident: "filtermap (\<lambda>x. x) F = F"
   233   by (simp add: filter_eq_iff eventually_filtermap)
   234 
   235 lemma filtermap_filtermap:
   236   "filtermap f (filtermap g F) = filtermap (\<lambda>x. f (g x)) F"
   237   by (simp add: filter_eq_iff eventually_filtermap)
   238 
   239 lemma filtermap_mono: "F \<le> F' \<Longrightarrow> filtermap f F \<le> filtermap f F'"
   240   unfolding le_filter_def eventually_filtermap by simp
   241 
   242 lemma filtermap_bot [simp]: "filtermap f bot = bot"
   243   by (simp add: filter_eq_iff eventually_filtermap)
   244 
   245 
   246 subsection {* Sequentially *}
   247 
   248 definition sequentially :: "nat filter"
   249   where "sequentially = Abs_filter (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   250 
   251 lemma eventually_sequentially:
   252   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   253 unfolding sequentially_def
   254 proof (rule eventually_Abs_filter, rule is_filter.intro)
   255   fix P Q :: "nat \<Rightarrow> bool"
   256   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
   257   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
   258   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
   259   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
   260 qed auto
   261 
   262 lemma sequentially_bot [simp]: "sequentially \<noteq> bot"
   263   unfolding filter_eq_iff eventually_sequentially by auto
   264 
   265 lemma eventually_False_sequentially [simp]:
   266   "\<not> eventually (\<lambda>n. False) sequentially"
   267   by (simp add: eventually_False)
   268 
   269 lemma le_sequentially:
   270   "F \<le> sequentially \<longleftrightarrow> (\<forall>N. eventually (\<lambda>n. N \<le> n) F)"
   271   unfolding le_filter_def eventually_sequentially
   272   by (safe, fast, drule_tac x=N in spec, auto elim: eventually_rev_mp)
   273 
   274 
   275 definition trivial_limit :: "'a filter \<Rightarrow> bool"
   276   where "trivial_limit F \<longleftrightarrow> eventually (\<lambda>x. False) F"
   277 
   278 lemma trivial_limit_sequentially [intro]: "\<not> trivial_limit sequentially"
   279   by (auto simp add: trivial_limit_def eventually_sequentially)
   280 
   281 subsection {* Standard filters *}
   282 
   283 definition within :: "'a filter \<Rightarrow> 'a set \<Rightarrow> 'a filter" (infixr "within" 70)
   284   where "F within S = Abs_filter (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F)"
   285 
   286 definition nhds :: "'a::topological_space \<Rightarrow> 'a filter"
   287   where "nhds a = Abs_filter (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   288 
   289 definition at :: "'a::topological_space \<Rightarrow> 'a filter"
   290   where "at a = nhds a within - {a}"
   291 
   292 lemma eventually_within:
   293   "eventually P (F within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) F"
   294   unfolding within_def
   295   by (rule eventually_Abs_filter, rule is_filter.intro)
   296      (auto elim!: eventually_rev_mp)
   297 
   298 lemma within_UNIV: "F within UNIV = F"
   299   unfolding filter_eq_iff eventually_within by simp
   300 
   301 lemma eventually_nhds:
   302   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   303 unfolding nhds_def
   304 proof (rule eventually_Abs_filter, rule is_filter.intro)
   305   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
   306   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" by - rule
   307 next
   308   fix P Q
   309   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   310      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
   311   then obtain S T where
   312     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   313     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
   314   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
   315     by (simp add: open_Int)
   316   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" by - rule
   317 qed auto
   318 
   319 lemma eventually_nhds_metric:
   320   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
   321 unfolding eventually_nhds open_dist
   322 apply safe
   323 apply fast
   324 apply (rule_tac x="{x. dist x a < d}" in exI, simp)
   325 apply clarsimp
   326 apply (rule_tac x="d - dist x a" in exI, clarsimp)
   327 apply (simp only: less_diff_eq)
   328 apply (erule le_less_trans [OF dist_triangle])
   329 done
   330 
   331 lemma eventually_at_topological:
   332   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   333 unfolding at_def eventually_within eventually_nhds by simp
   334 
   335 lemma eventually_at:
   336   fixes a :: "'a::metric_space"
   337   shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
   338 unfolding at_def eventually_within eventually_nhds_metric by auto
   339 
   340 
   341 subsection {* Boundedness *}
   342 
   343 definition Bfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   344   where "Bfun f F = (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) F)"
   345 
   346 lemma BfunI:
   347   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) F" shows "Bfun f F"
   348 unfolding Bfun_def
   349 proof (intro exI conjI allI)
   350   show "0 < max K 1" by simp
   351 next
   352   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) F"
   353     using K by (rule eventually_elim1, simp)
   354 qed
   355 
   356 lemma BfunE:
   357   assumes "Bfun f F"
   358   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) F"
   359 using assms unfolding Bfun_def by fast
   360 
   361 
   362 subsection {* Convergence to Zero *}
   363 
   364 definition Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a filter \<Rightarrow> bool"
   365   where "Zfun f F = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) F)"
   366 
   367 lemma ZfunI:
   368   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F) \<Longrightarrow> Zfun f F"
   369   unfolding Zfun_def by simp
   370 
   371 lemma ZfunD:
   372   "\<lbrakk>Zfun f F; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) F"
   373   unfolding Zfun_def by simp
   374 
   375 lemma Zfun_ssubst:
   376   "eventually (\<lambda>x. f x = g x) F \<Longrightarrow> Zfun g F \<Longrightarrow> Zfun f F"
   377   unfolding Zfun_def by (auto elim!: eventually_rev_mp)
   378 
   379 lemma Zfun_zero: "Zfun (\<lambda>x. 0) F"
   380   unfolding Zfun_def by simp
   381 
   382 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) F = Zfun (\<lambda>x. f x) F"
   383   unfolding Zfun_def by simp
   384 
   385 lemma Zfun_imp_Zfun:
   386   assumes f: "Zfun f F"
   387   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) F"
   388   shows "Zfun (\<lambda>x. g x) F"
   389 proof (cases)
   390   assume K: "0 < K"
   391   show ?thesis
   392   proof (rule ZfunI)
   393     fix r::real assume "0 < r"
   394     hence "0 < r / K"
   395       using K by (rule divide_pos_pos)
   396     then have "eventually (\<lambda>x. norm (f x) < r / K) F"
   397       using ZfunD [OF f] by fast
   398     with g show "eventually (\<lambda>x. norm (g x) < r) F"
   399     proof (rule eventually_elim2)
   400       fix x
   401       assume *: "norm (g x) \<le> norm (f x) * K"
   402       assume "norm (f x) < r / K"
   403       hence "norm (f x) * K < r"
   404         by (simp add: pos_less_divide_eq K)
   405       thus "norm (g x) < r"
   406         by (simp add: order_le_less_trans [OF *])
   407     qed
   408   qed
   409 next
   410   assume "\<not> 0 < K"
   411   hence K: "K \<le> 0" by (simp only: not_less)
   412   show ?thesis
   413   proof (rule ZfunI)
   414     fix r :: real
   415     assume "0 < r"
   416     from g show "eventually (\<lambda>x. norm (g x) < r) F"
   417     proof (rule eventually_elim1)
   418       fix x
   419       assume "norm (g x) \<le> norm (f x) * K"
   420       also have "\<dots> \<le> norm (f x) * 0"
   421         using K norm_ge_zero by (rule mult_left_mono)
   422       finally show "norm (g x) < r"
   423         using `0 < r` by simp
   424     qed
   425   qed
   426 qed
   427 
   428 lemma Zfun_le: "\<lbrakk>Zfun g F; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f F"
   429   by (erule_tac K="1" in Zfun_imp_Zfun, simp)
   430 
   431 lemma Zfun_add:
   432   assumes f: "Zfun f F" and g: "Zfun g F"
   433   shows "Zfun (\<lambda>x. f x + g x) F"
   434 proof (rule ZfunI)
   435   fix r::real assume "0 < r"
   436   hence r: "0 < r / 2" by simp
   437   have "eventually (\<lambda>x. norm (f x) < r/2) F"
   438     using f r by (rule ZfunD)
   439   moreover
   440   have "eventually (\<lambda>x. norm (g x) < r/2) F"
   441     using g r by (rule ZfunD)
   442   ultimately
   443   show "eventually (\<lambda>x. norm (f x + g x) < r) F"
   444   proof (rule eventually_elim2)
   445     fix x
   446     assume *: "norm (f x) < r/2" "norm (g x) < r/2"
   447     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
   448       by (rule norm_triangle_ineq)
   449     also have "\<dots> < r/2 + r/2"
   450       using * by (rule add_strict_mono)
   451     finally show "norm (f x + g x) < r"
   452       by simp
   453   qed
   454 qed
   455 
   456 lemma Zfun_minus: "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. - f x) F"
   457   unfolding Zfun_def by simp
   458 
   459 lemma Zfun_diff: "\<lbrakk>Zfun f F; Zfun g F\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) F"
   460   by (simp only: diff_minus Zfun_add Zfun_minus)
   461 
   462 lemma (in bounded_linear) Zfun:
   463   assumes g: "Zfun g F"
   464   shows "Zfun (\<lambda>x. f (g x)) F"
   465 proof -
   466   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
   467     using bounded by fast
   468   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) F"
   469     by simp
   470   with g show ?thesis
   471     by (rule Zfun_imp_Zfun)
   472 qed
   473 
   474 lemma (in bounded_bilinear) Zfun:
   475   assumes f: "Zfun f F"
   476   assumes g: "Zfun g F"
   477   shows "Zfun (\<lambda>x. f x ** g x) F"
   478 proof (rule ZfunI)
   479   fix r::real assume r: "0 < r"
   480   obtain K where K: "0 < K"
   481     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   482     using pos_bounded by fast
   483   from K have K': "0 < inverse K"
   484     by (rule positive_imp_inverse_positive)
   485   have "eventually (\<lambda>x. norm (f x) < r) F"
   486     using f r by (rule ZfunD)
   487   moreover
   488   have "eventually (\<lambda>x. norm (g x) < inverse K) F"
   489     using g K' by (rule ZfunD)
   490   ultimately
   491   show "eventually (\<lambda>x. norm (f x ** g x) < r) F"
   492   proof (rule eventually_elim2)
   493     fix x
   494     assume *: "norm (f x) < r" "norm (g x) < inverse K"
   495     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   496       by (rule norm_le)
   497     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
   498       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero * K)
   499     also from K have "r * inverse K * K = r"
   500       by simp
   501     finally show "norm (f x ** g x) < r" .
   502   qed
   503 qed
   504 
   505 lemma (in bounded_bilinear) Zfun_left:
   506   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. f x ** a) F"
   507   by (rule bounded_linear_left [THEN bounded_linear.Zfun])
   508 
   509 lemma (in bounded_bilinear) Zfun_right:
   510   "Zfun f F \<Longrightarrow> Zfun (\<lambda>x. a ** f x) F"
   511   by (rule bounded_linear_right [THEN bounded_linear.Zfun])
   512 
   513 lemmas Zfun_mult = mult.Zfun
   514 lemmas Zfun_mult_right = mult.Zfun_right
   515 lemmas Zfun_mult_left = mult.Zfun_left
   516 
   517 
   518 subsection {* Limits *}
   519 
   520 definition tendsto :: "('a \<Rightarrow> 'b::topological_space) \<Rightarrow> 'b \<Rightarrow> 'a filter \<Rightarrow> bool"
   521     (infixr "--->" 55) where
   522   "(f ---> l) F \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) F)"
   523 
   524 ML {*
   525 structure Tendsto_Intros = Named_Thms
   526 (
   527   val name = "tendsto_intros"
   528   val description = "introduction rules for tendsto"
   529 )
   530 *}
   531 
   532 setup Tendsto_Intros.setup
   533 
   534 lemma tendsto_mono: "F \<le> F' \<Longrightarrow> (f ---> l) F' \<Longrightarrow> (f ---> l) F"
   535   unfolding tendsto_def le_filter_def by fast
   536 
   537 lemma topological_tendstoI:
   538   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F)
   539     \<Longrightarrow> (f ---> l) F"
   540   unfolding tendsto_def by auto
   541 
   542 lemma topological_tendstoD:
   543   "(f ---> l) F \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) F"
   544   unfolding tendsto_def by auto
   545 
   546 lemma tendstoI:
   547   assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   548   shows "(f ---> l) F"
   549   apply (rule topological_tendstoI)
   550   apply (simp add: open_dist)
   551   apply (drule (1) bspec, clarify)
   552   apply (drule assms)
   553   apply (erule eventually_elim1, simp)
   554   done
   555 
   556 lemma tendstoD:
   557   "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
   558   apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
   559   apply (clarsimp simp add: open_dist)
   560   apply (rule_tac x="e - dist x l" in exI, clarsimp)
   561   apply (simp only: less_diff_eq)
   562   apply (erule le_less_trans [OF dist_triangle])
   563   apply simp
   564   apply simp
   565   done
   566 
   567 lemma tendsto_iff:
   568   "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
   569   using tendstoI tendstoD by fast
   570 
   571 lemma tendsto_Zfun_iff: "(f ---> a) F = Zfun (\<lambda>x. f x - a) F"
   572   by (simp only: tendsto_iff Zfun_def dist_norm)
   573 
   574 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
   575   unfolding tendsto_def eventually_at_topological by auto
   576 
   577 lemma tendsto_ident_at_within [tendsto_intros]:
   578   "((\<lambda>x. x) ---> a) (at a within S)"
   579   unfolding tendsto_def eventually_within eventually_at_topological by auto
   580 
   581 lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) F"
   582   by (simp add: tendsto_def)
   583 
   584 lemma tendsto_const_iff:
   585   fixes k l :: "'a::metric_space"
   586   assumes "F \<noteq> bot" shows "((\<lambda>n. k) ---> l) F \<longleftrightarrow> k = l"
   587   apply (safe intro!: tendsto_const)
   588   apply (rule ccontr)
   589   apply (drule_tac e="dist k l" in tendstoD)
   590   apply (simp add: zero_less_dist_iff)
   591   apply (simp add: eventually_False assms)
   592   done
   593 
   594 lemma tendsto_dist [tendsto_intros]:
   595   assumes f: "(f ---> l) F" and g: "(g ---> m) F"
   596   shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) F"
   597 proof (rule tendstoI)
   598   fix e :: real assume "0 < e"
   599   hence e2: "0 < e/2" by simp
   600   from tendstoD [OF f e2] tendstoD [OF g e2]
   601   show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) F"
   602   proof (rule eventually_elim2)
   603     fix x assume "dist (f x) l < e/2" "dist (g x) m < e/2"
   604     then show "dist (dist (f x) (g x)) (dist l m) < e"
   605       unfolding dist_real_def
   606       using dist_triangle2 [of "f x" "g x" "l"]
   607       using dist_triangle2 [of "g x" "l" "m"]
   608       using dist_triangle3 [of "l" "m" "f x"]
   609       using dist_triangle [of "f x" "m" "g x"]
   610       by arith
   611   qed
   612 qed
   613 
   614 subsubsection {* Norms *}
   615 
   616 lemma norm_conv_dist: "norm x = dist x 0"
   617   unfolding dist_norm by simp
   618 
   619 lemma tendsto_norm [tendsto_intros]:
   620   "(f ---> a) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> norm a) F"
   621   unfolding norm_conv_dist by (intro tendsto_intros)
   622 
   623 lemma tendsto_norm_zero:
   624   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> 0) F"
   625   by (drule tendsto_norm, simp)
   626 
   627 lemma tendsto_norm_zero_cancel:
   628   "((\<lambda>x. norm (f x)) ---> 0) F \<Longrightarrow> (f ---> 0) F"
   629   unfolding tendsto_iff dist_norm by simp
   630 
   631 lemma tendsto_norm_zero_iff:
   632   "((\<lambda>x. norm (f x)) ---> 0) F \<longleftrightarrow> (f ---> 0) F"
   633   unfolding tendsto_iff dist_norm by simp
   634 
   635 lemma tendsto_rabs [tendsto_intros]:
   636   "(f ---> (l::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> \<bar>l\<bar>) F"
   637   by (fold real_norm_def, rule tendsto_norm)
   638 
   639 lemma tendsto_rabs_zero:
   640   "(f ---> (0::real)) F \<Longrightarrow> ((\<lambda>x. \<bar>f x\<bar>) ---> 0) F"
   641   by (fold real_norm_def, rule tendsto_norm_zero)
   642 
   643 lemma tendsto_rabs_zero_cancel:
   644   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<Longrightarrow> (f ---> 0) F"
   645   by (fold real_norm_def, rule tendsto_norm_zero_cancel)
   646 
   647 lemma tendsto_rabs_zero_iff:
   648   "((\<lambda>x. \<bar>f x\<bar>) ---> (0::real)) F \<longleftrightarrow> (f ---> 0) F"
   649   by (fold real_norm_def, rule tendsto_norm_zero_iff)
   650 
   651 subsubsection {* Addition and subtraction *}
   652 
   653 lemma tendsto_add [tendsto_intros]:
   654   fixes a b :: "'a::real_normed_vector"
   655   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> a + b) F"
   656   by (simp only: tendsto_Zfun_iff add_diff_add Zfun_add)
   657 
   658 lemma tendsto_add_zero:
   659   fixes f g :: "'a::type \<Rightarrow> 'b::real_normed_vector"
   660   shows "\<lbrakk>(f ---> 0) F; (g ---> 0) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> 0) F"
   661   by (drule (1) tendsto_add, simp)
   662 
   663 lemma tendsto_minus [tendsto_intros]:
   664   fixes a :: "'a::real_normed_vector"
   665   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. - f x) ---> - a) F"
   666   by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)
   667 
   668 lemma tendsto_minus_cancel:
   669   fixes a :: "'a::real_normed_vector"
   670   shows "((\<lambda>x. - f x) ---> - a) F \<Longrightarrow> (f ---> a) F"
   671   by (drule tendsto_minus, simp)
   672 
   673 lemma tendsto_diff [tendsto_intros]:
   674   fixes a b :: "'a::real_normed_vector"
   675   shows "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) ---> a - b) F"
   676   by (simp add: diff_minus tendsto_add tendsto_minus)
   677 
   678 lemma tendsto_setsum [tendsto_intros]:
   679   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
   680   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> a i) F"
   681   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) ---> (\<Sum>i\<in>S. a i)) F"
   682 proof (cases "finite S")
   683   assume "finite S" thus ?thesis using assms
   684     by (induct, simp add: tendsto_const, simp add: tendsto_add)
   685 next
   686   assume "\<not> finite S" thus ?thesis
   687     by (simp add: tendsto_const)
   688 qed
   689 
   690 subsubsection {* Linear operators and multiplication *}
   691 
   692 lemma (in bounded_linear) tendsto [tendsto_intros]:
   693   "(g ---> a) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> f a) F"
   694   by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
   695 
   696 lemma (in bounded_linear) tendsto_zero:
   697   "(g ---> 0) F \<Longrightarrow> ((\<lambda>x. f (g x)) ---> 0) F"
   698   by (drule tendsto, simp only: zero)
   699 
   700 lemma (in bounded_bilinear) tendsto [tendsto_intros]:
   701   "\<lbrakk>(f ---> a) F; (g ---> b) F\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) ---> a ** b) F"
   702   by (simp only: tendsto_Zfun_iff prod_diff_prod
   703                  Zfun_add Zfun Zfun_left Zfun_right)
   704 
   705 lemma (in bounded_bilinear) tendsto_zero:
   706   assumes f: "(f ---> 0) F"
   707   assumes g: "(g ---> 0) F"
   708   shows "((\<lambda>x. f x ** g x) ---> 0) F"
   709   using tendsto [OF f g] by (simp add: zero_left)
   710 
   711 lemma (in bounded_bilinear) tendsto_left_zero:
   712   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. f x ** c) ---> 0) F"
   713   by (rule bounded_linear.tendsto_zero [OF bounded_linear_left])
   714 
   715 lemma (in bounded_bilinear) tendsto_right_zero:
   716   "(f ---> 0) F \<Longrightarrow> ((\<lambda>x. c ** f x) ---> 0) F"
   717   by (rule bounded_linear.tendsto_zero [OF bounded_linear_right])
   718 
   719 lemmas tendsto_mult = mult.tendsto
   720 
   721 lemma tendsto_power [tendsto_intros]:
   722   fixes f :: "'a \<Rightarrow> 'b::{power,real_normed_algebra}"
   723   shows "(f ---> a) F \<Longrightarrow> ((\<lambda>x. f x ^ n) ---> a ^ n) F"
   724   by (induct n) (simp_all add: tendsto_const tendsto_mult)
   725 
   726 lemma tendsto_setprod [tendsto_intros]:
   727   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::{real_normed_algebra,comm_ring_1}"
   728   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> L i) F"
   729   shows "((\<lambda>x. \<Prod>i\<in>S. f i x) ---> (\<Prod>i\<in>S. L i)) F"
   730 proof (cases "finite S")
   731   assume "finite S" thus ?thesis using assms
   732     by (induct, simp add: tendsto_const, simp add: tendsto_mult)
   733 next
   734   assume "\<not> finite S" thus ?thesis
   735     by (simp add: tendsto_const)
   736 qed
   737 
   738 subsubsection {* Inverse and division *}
   739 
   740 lemma (in bounded_bilinear) Zfun_prod_Bfun:
   741   assumes f: "Zfun f F"
   742   assumes g: "Bfun g F"
   743   shows "Zfun (\<lambda>x. f x ** g x) F"
   744 proof -
   745   obtain K where K: "0 \<le> K"
   746     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   747     using nonneg_bounded by fast
   748   obtain B where B: "0 < B"
   749     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) F"
   750     using g by (rule BfunE)
   751   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) F"
   752   using norm_g proof (rule eventually_elim1)
   753     fix x
   754     assume *: "norm (g x) \<le> B"
   755     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   756       by (rule norm_le)
   757     also have "\<dots> \<le> norm (f x) * B * K"
   758       by (intro mult_mono' order_refl norm_g norm_ge_zero
   759                 mult_nonneg_nonneg K *)
   760     also have "\<dots> = norm (f x) * (B * K)"
   761       by (rule mult_assoc)
   762     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
   763   qed
   764   with f show ?thesis
   765     by (rule Zfun_imp_Zfun)
   766 qed
   767 
   768 lemma (in bounded_bilinear) flip:
   769   "bounded_bilinear (\<lambda>x y. y ** x)"
   770   apply default
   771   apply (rule add_right)
   772   apply (rule add_left)
   773   apply (rule scaleR_right)
   774   apply (rule scaleR_left)
   775   apply (subst mult_commute)
   776   using bounded by fast
   777 
   778 lemma (in bounded_bilinear) Bfun_prod_Zfun:
   779   assumes f: "Bfun f F"
   780   assumes g: "Zfun g F"
   781   shows "Zfun (\<lambda>x. f x ** g x) F"
   782   using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
   783 
   784 lemma Bfun_inverse_lemma:
   785   fixes x :: "'a::real_normed_div_algebra"
   786   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
   787   apply (subst nonzero_norm_inverse, clarsimp)
   788   apply (erule (1) le_imp_inverse_le)
   789   done
   790 
   791 lemma Bfun_inverse:
   792   fixes a :: "'a::real_normed_div_algebra"
   793   assumes f: "(f ---> a) F"
   794   assumes a: "a \<noteq> 0"
   795   shows "Bfun (\<lambda>x. inverse (f x)) F"
   796 proof -
   797   from a have "0 < norm a" by simp
   798   hence "\<exists>r>0. r < norm a" by (rule dense)
   799   then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
   800   have "eventually (\<lambda>x. dist (f x) a < r) F"
   801     using tendstoD [OF f r1] by fast
   802   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) F"
   803   proof (rule eventually_elim1)
   804     fix x
   805     assume "dist (f x) a < r"
   806     hence 1: "norm (f x - a) < r"
   807       by (simp add: dist_norm)
   808     hence 2: "f x \<noteq> 0" using r2 by auto
   809     hence "norm (inverse (f x)) = inverse (norm (f x))"
   810       by (rule nonzero_norm_inverse)
   811     also have "\<dots> \<le> inverse (norm a - r)"
   812     proof (rule le_imp_inverse_le)
   813       show "0 < norm a - r" using r2 by simp
   814     next
   815       have "norm a - norm (f x) \<le> norm (a - f x)"
   816         by (rule norm_triangle_ineq2)
   817       also have "\<dots> = norm (f x - a)"
   818         by (rule norm_minus_commute)
   819       also have "\<dots> < r" using 1 .
   820       finally show "norm a - r \<le> norm (f x)" by simp
   821     qed
   822     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
   823   qed
   824   thus ?thesis by (rule BfunI)
   825 qed
   826 
   827 lemma tendsto_inverse_lemma:
   828   fixes a :: "'a::real_normed_div_algebra"
   829   shows "\<lbrakk>(f ---> a) F; a \<noteq> 0; eventually (\<lambda>x. f x \<noteq> 0) F\<rbrakk>
   830          \<Longrightarrow> ((\<lambda>x. inverse (f x)) ---> inverse a) F"
   831   apply (subst tendsto_Zfun_iff)
   832   apply (rule Zfun_ssubst)
   833   apply (erule eventually_elim1)
   834   apply (erule (1) inverse_diff_inverse)
   835   apply (rule Zfun_minus)
   836   apply (rule Zfun_mult_left)
   837   apply (rule mult.Bfun_prod_Zfun)
   838   apply (erule (1) Bfun_inverse)
   839   apply (simp add: tendsto_Zfun_iff)
   840   done
   841 
   842 lemma tendsto_inverse [tendsto_intros]:
   843   fixes a :: "'a::real_normed_div_algebra"
   844   assumes f: "(f ---> a) F"
   845   assumes a: "a \<noteq> 0"
   846   shows "((\<lambda>x. inverse (f x)) ---> inverse a) F"
   847 proof -
   848   from a have "0 < norm a" by simp
   849   with f have "eventually (\<lambda>x. dist (f x) a < norm a) F"
   850     by (rule tendstoD)
   851   then have "eventually (\<lambda>x. f x \<noteq> 0) F"
   852     unfolding dist_norm by (auto elim!: eventually_elim1)
   853   with f a show ?thesis
   854     by (rule tendsto_inverse_lemma)
   855 qed
   856 
   857 lemma tendsto_divide [tendsto_intros]:
   858   fixes a b :: "'a::real_normed_field"
   859   shows "\<lbrakk>(f ---> a) F; (g ---> b) F; b \<noteq> 0\<rbrakk>
   860     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) F"
   861   by (simp add: mult.tendsto tendsto_inverse divide_inverse)
   862 
   863 lemma tendsto_sgn [tendsto_intros]:
   864   fixes l :: "'a::real_normed_vector"
   865   shows "\<lbrakk>(f ---> l) F; l \<noteq> 0\<rbrakk> \<Longrightarrow> ((\<lambda>x. sgn (f x)) ---> sgn l) F"
   866   unfolding sgn_div_norm by (simp add: tendsto_intros)
   867 
   868 subsubsection {* Uniqueness *}
   869 
   870 lemma tendsto_unique:
   871   fixes f :: "'a \<Rightarrow> 'b::t2_space"
   872   assumes "\<not> trivial_limit F"  "(f ---> l) F"  "(f ---> l') F"
   873   shows "l = l'"
   874 proof (rule ccontr)
   875   assume "l \<noteq> l'"
   876   obtain U V where "open U" "open V" "l \<in> U" "l' \<in> V" "U \<inter> V = {}"
   877     using hausdorff [OF `l \<noteq> l'`] by fast
   878   have "eventually (\<lambda>x. f x \<in> U) F"
   879     using `(f ---> l) F` `open U` `l \<in> U` by (rule topological_tendstoD)
   880   moreover
   881   have "eventually (\<lambda>x. f x \<in> V) F"
   882     using `(f ---> l') F` `open V` `l' \<in> V` by (rule topological_tendstoD)
   883   ultimately
   884   have "eventually (\<lambda>x. False) F"
   885   proof (rule eventually_elim2)
   886     fix x
   887     assume "f x \<in> U" "f x \<in> V"
   888     hence "f x \<in> U \<inter> V" by simp
   889     with `U \<inter> V = {}` show "False" by simp
   890   qed
   891   with `\<not> trivial_limit F` show "False"
   892     by (simp add: trivial_limit_def)
   893 qed
   894 
   895 end