src/HOL/Limits.thy
author huffman
Sat May 01 11:46:47 2010 -0700 (2010-05-01)
changeset 36630 aa1f8acdcc1c
parent 36629 de62713aec6e
child 36654 7c8eb32724ce
permissions -rw-r--r--
complete_lattice instance for net type
     1 (*  Title       : Limits.thy
     2     Author      : Brian Huffman
     3 *)
     4 
     5 header {* Filters and Limits *}
     6 
     7 theory Limits
     8 imports RealVector RComplete
     9 begin
    10 
    11 subsection {* Nets *}
    12 
    13 text {*
    14   A net is now defined simply as a filter on a set.
    15   The definition also allows non-proper filters.
    16 *}
    17 
    18 locale is_filter =
    19   fixes net :: "('a \<Rightarrow> bool) \<Rightarrow> bool"
    20   assumes True: "net (\<lambda>x. True)"
    21   assumes conj: "net (\<lambda>x. P x) \<Longrightarrow> net (\<lambda>x. Q x) \<Longrightarrow> net (\<lambda>x. P x \<and> Q x)"
    22   assumes mono: "\<forall>x. P x \<longrightarrow> Q x \<Longrightarrow> net (\<lambda>x. P x) \<Longrightarrow> net (\<lambda>x. Q x)"
    23 
    24 typedef (open) 'a net =
    25   "{net :: ('a \<Rightarrow> bool) \<Rightarrow> bool. is_filter net}"
    26 proof
    27   show "(\<lambda>x. True) \<in> ?net" by (auto intro: is_filter.intro)
    28 qed
    29 
    30 lemma is_filter_Rep_net: "is_filter (Rep_net net)"
    31 using Rep_net [of net] by simp
    32 
    33 lemma Abs_net_inverse':
    34   assumes "is_filter net" shows "Rep_net (Abs_net net) = net"
    35 using assms by (simp add: Abs_net_inverse)
    36 
    37 
    38 subsection {* Eventually *}
    39 
    40 definition
    41   eventually :: "('a \<Rightarrow> bool) \<Rightarrow> 'a net \<Rightarrow> bool" where
    42   [code del]: "eventually P net \<longleftrightarrow> Rep_net net P"
    43 
    44 lemma eventually_Abs_net:
    45   assumes "is_filter net" shows "eventually P (Abs_net net) = net P"
    46 unfolding eventually_def using assms by (simp add: Abs_net_inverse)
    47 
    48 lemma expand_net_eq:
    49   shows "net = net' \<longleftrightarrow> (\<forall>P. eventually P net = eventually P net')"
    50 unfolding Rep_net_inject [symmetric] expand_fun_eq eventually_def ..
    51 
    52 lemma eventually_True [simp]: "eventually (\<lambda>x. True) net"
    53 unfolding eventually_def
    54 by (rule is_filter.True [OF is_filter_Rep_net])
    55 
    56 lemma always_eventually: "\<forall>x. P x \<Longrightarrow> eventually P net"
    57 proof -
    58   assume "\<forall>x. P x" hence "P = (\<lambda>x. True)" by (simp add: ext)
    59   thus "eventually P net" by simp
    60 qed
    61 
    62 lemma eventually_mono:
    63   "(\<forall>x. P x \<longrightarrow> Q x) \<Longrightarrow> eventually P net \<Longrightarrow> eventually Q net"
    64 unfolding eventually_def
    65 by (rule is_filter.mono [OF is_filter_Rep_net])
    66 
    67 lemma eventually_conj:
    68   assumes P: "eventually (\<lambda>x. P x) net"
    69   assumes Q: "eventually (\<lambda>x. Q x) net"
    70   shows "eventually (\<lambda>x. P x \<and> Q x) net"
    71 using assms unfolding eventually_def
    72 by (rule is_filter.conj [OF is_filter_Rep_net])
    73 
    74 lemma eventually_mp:
    75   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) net"
    76   assumes "eventually (\<lambda>x. P x) net"
    77   shows "eventually (\<lambda>x. Q x) net"
    78 proof (rule eventually_mono)
    79   show "\<forall>x. (P x \<longrightarrow> Q x) \<and> P x \<longrightarrow> Q x" by simp
    80   show "eventually (\<lambda>x. (P x \<longrightarrow> Q x) \<and> P x) net"
    81     using assms by (rule eventually_conj)
    82 qed
    83 
    84 lemma eventually_rev_mp:
    85   assumes "eventually (\<lambda>x. P x) net"
    86   assumes "eventually (\<lambda>x. P x \<longrightarrow> Q x) net"
    87   shows "eventually (\<lambda>x. Q x) net"
    88 using assms(2) assms(1) by (rule eventually_mp)
    89 
    90 lemma eventually_conj_iff:
    91   "eventually (\<lambda>x. P x \<and> Q x) net \<longleftrightarrow> eventually P net \<and> eventually Q net"
    92 by (auto intro: eventually_conj elim: eventually_rev_mp)
    93 
    94 lemma eventually_elim1:
    95   assumes "eventually (\<lambda>i. P i) net"
    96   assumes "\<And>i. P i \<Longrightarrow> Q i"
    97   shows "eventually (\<lambda>i. Q i) net"
    98 using assms by (auto elim!: eventually_rev_mp)
    99 
   100 lemma eventually_elim2:
   101   assumes "eventually (\<lambda>i. P i) net"
   102   assumes "eventually (\<lambda>i. Q i) net"
   103   assumes "\<And>i. P i \<Longrightarrow> Q i \<Longrightarrow> R i"
   104   shows "eventually (\<lambda>i. R i) net"
   105 using assms by (auto elim!: eventually_rev_mp)
   106 
   107 
   108 subsection {* Finer-than relation *}
   109 
   110 text {* @{term "net \<le> net'"} means that @{term net} is finer than
   111 @{term net'}. *}
   112 
   113 instantiation net :: (type) complete_lattice
   114 begin
   115 
   116 definition
   117   le_net_def [code del]:
   118     "net \<le> net' \<longleftrightarrow> (\<forall>P. eventually P net' \<longrightarrow> eventually P net)"
   119 
   120 definition
   121   less_net_def [code del]:
   122     "(net :: 'a net) < net' \<longleftrightarrow> net \<le> net' \<and> \<not> net' \<le> net"
   123 
   124 definition
   125   top_net_def [code del]:
   126     "top = Abs_net (\<lambda>P. \<forall>x. P x)"
   127 
   128 definition
   129   bot_net_def [code del]:
   130     "bot = Abs_net (\<lambda>P. True)"
   131 
   132 definition
   133   sup_net_def [code del]:
   134     "sup net net' = Abs_net (\<lambda>P. eventually P net \<and> eventually P net')"
   135 
   136 definition
   137   inf_net_def [code del]:
   138     "inf a b = Abs_net
   139       (\<lambda>P. \<exists>Q R. eventually Q a \<and> eventually R b \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   140 
   141 definition
   142   Sup_net_def [code del]:
   143     "Sup A = Abs_net (\<lambda>P. \<forall>net\<in>A. eventually P net)"
   144 
   145 definition
   146   Inf_net_def [code del]:
   147     "Inf A = Sup {x::'a net. \<forall>y\<in>A. x \<le> y}"
   148 
   149 lemma eventually_top [simp]: "eventually P top \<longleftrightarrow> (\<forall>x. P x)"
   150 unfolding top_net_def
   151 by (rule eventually_Abs_net, rule is_filter.intro, auto)
   152 
   153 lemma eventually_bot [simp]: "eventually P bot"
   154 unfolding bot_net_def
   155 by (subst eventually_Abs_net, rule is_filter.intro, auto)
   156 
   157 lemma eventually_sup:
   158   "eventually P (sup net net') \<longleftrightarrow> eventually P net \<and> eventually P net'"
   159 unfolding sup_net_def
   160 by (rule eventually_Abs_net, rule is_filter.intro)
   161    (auto elim!: eventually_rev_mp)
   162 
   163 lemma eventually_inf:
   164   "eventually P (inf a b) \<longleftrightarrow>
   165    (\<exists>Q R. eventually Q a \<and> eventually R b \<and> (\<forall>x. Q x \<and> R x \<longrightarrow> P x))"
   166 unfolding inf_net_def
   167 apply (rule eventually_Abs_net, rule is_filter.intro)
   168 apply (fast intro: eventually_True)
   169 apply clarify
   170 apply (intro exI conjI)
   171 apply (erule (1) eventually_conj)
   172 apply (erule (1) eventually_conj)
   173 apply simp
   174 apply auto
   175 done
   176 
   177 lemma eventually_Sup:
   178   "eventually P (Sup A) \<longleftrightarrow> (\<forall>net\<in>A. eventually P net)"
   179 unfolding Sup_net_def
   180 apply (rule eventually_Abs_net, rule is_filter.intro)
   181 apply (auto intro: eventually_conj elim!: eventually_rev_mp)
   182 done
   183 
   184 instance proof
   185   fix x y :: "'a net" show "x < y \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
   186     by (rule less_net_def)
   187 next
   188   fix x :: "'a net" show "x \<le> x"
   189     unfolding le_net_def by simp
   190 next
   191   fix x y z :: "'a net" assume "x \<le> y" and "y \<le> z" thus "x \<le> z"
   192     unfolding le_net_def by simp
   193 next
   194   fix x y :: "'a net" assume "x \<le> y" and "y \<le> x" thus "x = y"
   195     unfolding le_net_def expand_net_eq by fast
   196 next
   197   fix x :: "'a net" show "x \<le> top"
   198     unfolding le_net_def eventually_top by (simp add: always_eventually)
   199 next
   200   fix x :: "'a net" show "bot \<le> x"
   201     unfolding le_net_def by simp
   202 next
   203   fix x y :: "'a net" show "x \<le> sup x y" and "y \<le> sup x y"
   204     unfolding le_net_def eventually_sup by simp_all
   205 next
   206   fix x y z :: "'a net" assume "x \<le> z" and "y \<le> z" thus "sup x y \<le> z"
   207     unfolding le_net_def eventually_sup by simp
   208 next
   209   fix x y :: "'a net" show "inf x y \<le> x" and "inf x y \<le> y"
   210     unfolding le_net_def eventually_inf by (auto intro: eventually_True)
   211 next
   212   fix x y z :: "'a net" assume "x \<le> y" and "x \<le> z" thus "x \<le> inf y z"
   213     unfolding le_net_def eventually_inf
   214     by (auto elim!: eventually_mono intro: eventually_conj)
   215 next
   216   fix x :: "'a net" and A assume "x \<in> A" thus "x \<le> Sup A"
   217     unfolding le_net_def eventually_Sup by simp
   218 next
   219   fix A and y :: "'a net" assume "\<And>x. x \<in> A \<Longrightarrow> x \<le> y" thus "Sup A \<le> y"
   220     unfolding le_net_def eventually_Sup by simp
   221 next
   222   fix z :: "'a net" and A assume "z \<in> A" thus "Inf A \<le> z"
   223     unfolding le_net_def Inf_net_def eventually_Sup Ball_def by simp
   224 next
   225   fix A and x :: "'a net" assume "\<And>y. y \<in> A \<Longrightarrow> x \<le> y" thus "x \<le> Inf A"
   226     unfolding le_net_def Inf_net_def eventually_Sup Ball_def by simp
   227 qed
   228 
   229 end
   230 
   231 lemma net_leD:
   232   "net \<le> net' \<Longrightarrow> eventually P net' \<Longrightarrow> eventually P net"
   233 unfolding le_net_def by simp
   234 
   235 lemma net_leI:
   236   "(\<And>P. eventually P net' \<Longrightarrow> eventually P net) \<Longrightarrow> net \<le> net'"
   237 unfolding le_net_def by simp
   238 
   239 lemma eventually_False:
   240   "eventually (\<lambda>x. False) net \<longleftrightarrow> net = bot"
   241 unfolding expand_net_eq by (auto elim: eventually_rev_mp)
   242 
   243 
   244 subsection {* Standard Nets *}
   245 
   246 definition
   247   sequentially :: "nat net"
   248 where [code del]:
   249   "sequentially = Abs_net (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   250 
   251 definition
   252   within :: "'a net \<Rightarrow> 'a set \<Rightarrow> 'a net" (infixr "within" 70)
   253 where [code del]:
   254   "net within S = Abs_net (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) net)"
   255 
   256 definition
   257   at :: "'a::topological_space \<Rightarrow> 'a net"
   258 where [code del]:
   259   "at a = Abs_net (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   260 
   261 lemma eventually_sequentially:
   262   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   263 unfolding sequentially_def
   264 proof (rule eventually_Abs_net, rule is_filter.intro)
   265   fix P Q :: "nat \<Rightarrow> bool"
   266   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
   267   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
   268   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
   269   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
   270 qed auto
   271 
   272 lemma eventually_within:
   273   "eventually P (net within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) net"
   274 unfolding within_def
   275 by (rule eventually_Abs_net, rule is_filter.intro)
   276    (auto elim!: eventually_rev_mp)
   277 
   278 lemma within_UNIV: "net within UNIV = net"
   279   unfolding expand_net_eq eventually_within by simp
   280 
   281 lemma eventually_at_topological:
   282   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   283 unfolding at_def
   284 proof (rule eventually_Abs_net, rule is_filter.intro)
   285   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. x \<noteq> a \<longrightarrow> True)" by simp
   286   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> True)" by - rule
   287 next
   288   fix P Q
   289   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x)"
   290      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. x \<noteq> a \<longrightarrow> Q x)"
   291   then obtain S T where
   292     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x)"
   293     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. x \<noteq> a \<longrightarrow> Q x)" by auto
   294   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). x \<noteq> a \<longrightarrow> P x \<and> Q x)"
   295     by (simp add: open_Int)
   296   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x \<and> Q x)" by - rule
   297 qed auto
   298 
   299 lemma eventually_at:
   300   fixes a :: "'a::metric_space"
   301   shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
   302 unfolding eventually_at_topological open_dist
   303 apply safe
   304 apply fast
   305 apply (rule_tac x="{x. dist x a < d}" in exI, simp)
   306 apply clarsimp
   307 apply (rule_tac x="d - dist x a" in exI, clarsimp)
   308 apply (simp only: less_diff_eq)
   309 apply (erule le_less_trans [OF dist_triangle])
   310 done
   311 
   312 
   313 subsection {* Boundedness *}
   314 
   315 definition
   316   Bfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a net \<Rightarrow> bool" where
   317   [code del]: "Bfun f net = (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) net)"
   318 
   319 lemma BfunI:
   320   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) net" shows "Bfun f net"
   321 unfolding Bfun_def
   322 proof (intro exI conjI allI)
   323   show "0 < max K 1" by simp
   324 next
   325   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) net"
   326     using K by (rule eventually_elim1, simp)
   327 qed
   328 
   329 lemma BfunE:
   330   assumes "Bfun f net"
   331   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) net"
   332 using assms unfolding Bfun_def by fast
   333 
   334 
   335 subsection {* Convergence to Zero *}
   336 
   337 definition
   338   Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a net \<Rightarrow> bool" where
   339   [code del]: "Zfun f net = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) net)"
   340 
   341 lemma ZfunI:
   342   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) net) \<Longrightarrow> Zfun f net"
   343 unfolding Zfun_def by simp
   344 
   345 lemma ZfunD:
   346   "\<lbrakk>Zfun f net; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) net"
   347 unfolding Zfun_def by simp
   348 
   349 lemma Zfun_ssubst:
   350   "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> Zfun g net \<Longrightarrow> Zfun f net"
   351 unfolding Zfun_def by (auto elim!: eventually_rev_mp)
   352 
   353 lemma Zfun_zero: "Zfun (\<lambda>x. 0) net"
   354 unfolding Zfun_def by simp
   355 
   356 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) net = Zfun (\<lambda>x. f x) net"
   357 unfolding Zfun_def by simp
   358 
   359 lemma Zfun_imp_Zfun:
   360   assumes f: "Zfun f net"
   361   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) net"
   362   shows "Zfun (\<lambda>x. g x) net"
   363 proof (cases)
   364   assume K: "0 < K"
   365   show ?thesis
   366   proof (rule ZfunI)
   367     fix r::real assume "0 < r"
   368     hence "0 < r / K"
   369       using K by (rule divide_pos_pos)
   370     then have "eventually (\<lambda>x. norm (f x) < r / K) net"
   371       using ZfunD [OF f] by fast
   372     with g show "eventually (\<lambda>x. norm (g x) < r) net"
   373     proof (rule eventually_elim2)
   374       fix x
   375       assume *: "norm (g x) \<le> norm (f x) * K"
   376       assume "norm (f x) < r / K"
   377       hence "norm (f x) * K < r"
   378         by (simp add: pos_less_divide_eq K)
   379       thus "norm (g x) < r"
   380         by (simp add: order_le_less_trans [OF *])
   381     qed
   382   qed
   383 next
   384   assume "\<not> 0 < K"
   385   hence K: "K \<le> 0" by (simp only: not_less)
   386   show ?thesis
   387   proof (rule ZfunI)
   388     fix r :: real
   389     assume "0 < r"
   390     from g show "eventually (\<lambda>x. norm (g x) < r) net"
   391     proof (rule eventually_elim1)
   392       fix x
   393       assume "norm (g x) \<le> norm (f x) * K"
   394       also have "\<dots> \<le> norm (f x) * 0"
   395         using K norm_ge_zero by (rule mult_left_mono)
   396       finally show "norm (g x) < r"
   397         using `0 < r` by simp
   398     qed
   399   qed
   400 qed
   401 
   402 lemma Zfun_le: "\<lbrakk>Zfun g net; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f net"
   403 by (erule_tac K="1" in Zfun_imp_Zfun, simp)
   404 
   405 lemma Zfun_add:
   406   assumes f: "Zfun f net" and g: "Zfun g net"
   407   shows "Zfun (\<lambda>x. f x + g x) net"
   408 proof (rule ZfunI)
   409   fix r::real assume "0 < r"
   410   hence r: "0 < r / 2" by simp
   411   have "eventually (\<lambda>x. norm (f x) < r/2) net"
   412     using f r by (rule ZfunD)
   413   moreover
   414   have "eventually (\<lambda>x. norm (g x) < r/2) net"
   415     using g r by (rule ZfunD)
   416   ultimately
   417   show "eventually (\<lambda>x. norm (f x + g x) < r) net"
   418   proof (rule eventually_elim2)
   419     fix x
   420     assume *: "norm (f x) < r/2" "norm (g x) < r/2"
   421     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
   422       by (rule norm_triangle_ineq)
   423     also have "\<dots> < r/2 + r/2"
   424       using * by (rule add_strict_mono)
   425     finally show "norm (f x + g x) < r"
   426       by simp
   427   qed
   428 qed
   429 
   430 lemma Zfun_minus: "Zfun f net \<Longrightarrow> Zfun (\<lambda>x. - f x) net"
   431 unfolding Zfun_def by simp
   432 
   433 lemma Zfun_diff: "\<lbrakk>Zfun f net; Zfun g net\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) net"
   434 by (simp only: diff_minus Zfun_add Zfun_minus)
   435 
   436 lemma (in bounded_linear) Zfun:
   437   assumes g: "Zfun g net"
   438   shows "Zfun (\<lambda>x. f (g x)) net"
   439 proof -
   440   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
   441     using bounded by fast
   442   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) net"
   443     by simp
   444   with g show ?thesis
   445     by (rule Zfun_imp_Zfun)
   446 qed
   447 
   448 lemma (in bounded_bilinear) Zfun:
   449   assumes f: "Zfun f net"
   450   assumes g: "Zfun g net"
   451   shows "Zfun (\<lambda>x. f x ** g x) net"
   452 proof (rule ZfunI)
   453   fix r::real assume r: "0 < r"
   454   obtain K where K: "0 < K"
   455     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   456     using pos_bounded by fast
   457   from K have K': "0 < inverse K"
   458     by (rule positive_imp_inverse_positive)
   459   have "eventually (\<lambda>x. norm (f x) < r) net"
   460     using f r by (rule ZfunD)
   461   moreover
   462   have "eventually (\<lambda>x. norm (g x) < inverse K) net"
   463     using g K' by (rule ZfunD)
   464   ultimately
   465   show "eventually (\<lambda>x. norm (f x ** g x) < r) net"
   466   proof (rule eventually_elim2)
   467     fix x
   468     assume *: "norm (f x) < r" "norm (g x) < inverse K"
   469     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   470       by (rule norm_le)
   471     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
   472       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero * K)
   473     also from K have "r * inverse K * K = r"
   474       by simp
   475     finally show "norm (f x ** g x) < r" .
   476   qed
   477 qed
   478 
   479 lemma (in bounded_bilinear) Zfun_left:
   480   "Zfun f net \<Longrightarrow> Zfun (\<lambda>x. f x ** a) net"
   481 by (rule bounded_linear_left [THEN bounded_linear.Zfun])
   482 
   483 lemma (in bounded_bilinear) Zfun_right:
   484   "Zfun f net \<Longrightarrow> Zfun (\<lambda>x. a ** f x) net"
   485 by (rule bounded_linear_right [THEN bounded_linear.Zfun])
   486 
   487 lemmas Zfun_mult = mult.Zfun
   488 lemmas Zfun_mult_right = mult.Zfun_right
   489 lemmas Zfun_mult_left = mult.Zfun_left
   490 
   491 
   492 subsection {* Limits *}
   493 
   494 definition
   495   tendsto :: "('a \<Rightarrow> 'b::topological_space) \<Rightarrow> 'b \<Rightarrow> 'a net \<Rightarrow> bool"
   496     (infixr "--->" 55)
   497 where [code del]:
   498   "(f ---> l) net \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net)"
   499 
   500 ML {*
   501 structure Tendsto_Intros = Named_Thms
   502 (
   503   val name = "tendsto_intros"
   504   val description = "introduction rules for tendsto"
   505 )
   506 *}
   507 
   508 setup Tendsto_Intros.setup
   509 
   510 lemma topological_tendstoI:
   511   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) net)
   512     \<Longrightarrow> (f ---> l) net"
   513   unfolding tendsto_def by auto
   514 
   515 lemma topological_tendstoD:
   516   "(f ---> l) net \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) net"
   517   unfolding tendsto_def by auto
   518 
   519 lemma tendstoI:
   520   assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) net"
   521   shows "(f ---> l) net"
   522 apply (rule topological_tendstoI)
   523 apply (simp add: open_dist)
   524 apply (drule (1) bspec, clarify)
   525 apply (drule assms)
   526 apply (erule eventually_elim1, simp)
   527 done
   528 
   529 lemma tendstoD:
   530   "(f ---> l) net \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) net"
   531 apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
   532 apply (clarsimp simp add: open_dist)
   533 apply (rule_tac x="e - dist x l" in exI, clarsimp)
   534 apply (simp only: less_diff_eq)
   535 apply (erule le_less_trans [OF dist_triangle])
   536 apply simp
   537 apply simp
   538 done
   539 
   540 lemma tendsto_iff:
   541   "(f ---> l) net \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
   542 using tendstoI tendstoD by fast
   543 
   544 lemma tendsto_Zfun_iff: "(f ---> a) net = Zfun (\<lambda>x. f x - a) net"
   545 by (simp only: tendsto_iff Zfun_def dist_norm)
   546 
   547 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
   548 unfolding tendsto_def eventually_at_topological by auto
   549 
   550 lemma tendsto_ident_at_within [tendsto_intros]:
   551   "a \<in> S \<Longrightarrow> ((\<lambda>x. x) ---> a) (at a within S)"
   552 unfolding tendsto_def eventually_within eventually_at_topological by auto
   553 
   554 lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) net"
   555 by (simp add: tendsto_def)
   556 
   557 lemma tendsto_dist [tendsto_intros]:
   558   assumes f: "(f ---> l) net" and g: "(g ---> m) net"
   559   shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) net"
   560 proof (rule tendstoI)
   561   fix e :: real assume "0 < e"
   562   hence e2: "0 < e/2" by simp
   563   from tendstoD [OF f e2] tendstoD [OF g e2]
   564   show "eventually (\<lambda>x. dist (dist (f x) (g x)) (dist l m) < e) net"
   565   proof (rule eventually_elim2)
   566     fix x assume "dist (f x) l < e/2" "dist (g x) m < e/2"
   567     then show "dist (dist (f x) (g x)) (dist l m) < e"
   568       unfolding dist_real_def
   569       using dist_triangle2 [of "f x" "g x" "l"]
   570       using dist_triangle2 [of "g x" "l" "m"]
   571       using dist_triangle3 [of "l" "m" "f x"]
   572       using dist_triangle [of "f x" "m" "g x"]
   573       by arith
   574   qed
   575 qed
   576 
   577 lemma tendsto_norm [tendsto_intros]:
   578   "(f ---> a) net \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> norm a) net"
   579 apply (simp add: tendsto_iff dist_norm, safe)
   580 apply (drule_tac x="e" in spec, safe)
   581 apply (erule eventually_elim1)
   582 apply (erule order_le_less_trans [OF norm_triangle_ineq3])
   583 done
   584 
   585 lemma add_diff_add:
   586   fixes a b c d :: "'a::ab_group_add"
   587   shows "(a + c) - (b + d) = (a - b) + (c - d)"
   588 by simp
   589 
   590 lemma minus_diff_minus:
   591   fixes a b :: "'a::ab_group_add"
   592   shows "(- a) - (- b) = - (a - b)"
   593 by simp
   594 
   595 lemma tendsto_add [tendsto_intros]:
   596   fixes a b :: "'a::real_normed_vector"
   597   shows "\<lbrakk>(f ---> a) net; (g ---> b) net\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> a + b) net"
   598 by (simp only: tendsto_Zfun_iff add_diff_add Zfun_add)
   599 
   600 lemma tendsto_minus [tendsto_intros]:
   601   fixes a :: "'a::real_normed_vector"
   602   shows "(f ---> a) net \<Longrightarrow> ((\<lambda>x. - f x) ---> - a) net"
   603 by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)
   604 
   605 lemma tendsto_minus_cancel:
   606   fixes a :: "'a::real_normed_vector"
   607   shows "((\<lambda>x. - f x) ---> - a) net \<Longrightarrow> (f ---> a) net"
   608 by (drule tendsto_minus, simp)
   609 
   610 lemma tendsto_diff [tendsto_intros]:
   611   fixes a b :: "'a::real_normed_vector"
   612   shows "\<lbrakk>(f ---> a) net; (g ---> b) net\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) ---> a - b) net"
   613 by (simp add: diff_minus tendsto_add tendsto_minus)
   614 
   615 lemma tendsto_setsum [tendsto_intros]:
   616   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
   617   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> a i) net"
   618   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) ---> (\<Sum>i\<in>S. a i)) net"
   619 proof (cases "finite S")
   620   assume "finite S" thus ?thesis using assms
   621   proof (induct set: finite)
   622     case empty show ?case
   623       by (simp add: tendsto_const)
   624   next
   625     case (insert i F) thus ?case
   626       by (simp add: tendsto_add)
   627   qed
   628 next
   629   assume "\<not> finite S" thus ?thesis
   630     by (simp add: tendsto_const)
   631 qed
   632 
   633 lemma (in bounded_linear) tendsto [tendsto_intros]:
   634   "(g ---> a) net \<Longrightarrow> ((\<lambda>x. f (g x)) ---> f a) net"
   635 by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
   636 
   637 lemma (in bounded_bilinear) tendsto [tendsto_intros]:
   638   "\<lbrakk>(f ---> a) net; (g ---> b) net\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) ---> a ** b) net"
   639 by (simp only: tendsto_Zfun_iff prod_diff_prod
   640                Zfun_add Zfun Zfun_left Zfun_right)
   641 
   642 
   643 subsection {* Continuity of Inverse *}
   644 
   645 lemma (in bounded_bilinear) Zfun_prod_Bfun:
   646   assumes f: "Zfun f net"
   647   assumes g: "Bfun g net"
   648   shows "Zfun (\<lambda>x. f x ** g x) net"
   649 proof -
   650   obtain K where K: "0 \<le> K"
   651     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   652     using nonneg_bounded by fast
   653   obtain B where B: "0 < B"
   654     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) net"
   655     using g by (rule BfunE)
   656   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) net"
   657   using norm_g proof (rule eventually_elim1)
   658     fix x
   659     assume *: "norm (g x) \<le> B"
   660     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   661       by (rule norm_le)
   662     also have "\<dots> \<le> norm (f x) * B * K"
   663       by (intro mult_mono' order_refl norm_g norm_ge_zero
   664                 mult_nonneg_nonneg K *)
   665     also have "\<dots> = norm (f x) * (B * K)"
   666       by (rule mult_assoc)
   667     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
   668   qed
   669   with f show ?thesis
   670     by (rule Zfun_imp_Zfun)
   671 qed
   672 
   673 lemma (in bounded_bilinear) flip:
   674   "bounded_bilinear (\<lambda>x y. y ** x)"
   675 apply default
   676 apply (rule add_right)
   677 apply (rule add_left)
   678 apply (rule scaleR_right)
   679 apply (rule scaleR_left)
   680 apply (subst mult_commute)
   681 using bounded by fast
   682 
   683 lemma (in bounded_bilinear) Bfun_prod_Zfun:
   684   assumes f: "Bfun f net"
   685   assumes g: "Zfun g net"
   686   shows "Zfun (\<lambda>x. f x ** g x) net"
   687 using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
   688 
   689 lemma inverse_diff_inverse:
   690   "\<lbrakk>(a::'a::division_ring) \<noteq> 0; b \<noteq> 0\<rbrakk>
   691    \<Longrightarrow> inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
   692 by (simp add: algebra_simps)
   693 
   694 lemma Bfun_inverse_lemma:
   695   fixes x :: "'a::real_normed_div_algebra"
   696   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
   697 apply (subst nonzero_norm_inverse, clarsimp)
   698 apply (erule (1) le_imp_inverse_le)
   699 done
   700 
   701 lemma Bfun_inverse:
   702   fixes a :: "'a::real_normed_div_algebra"
   703   assumes f: "(f ---> a) net"
   704   assumes a: "a \<noteq> 0"
   705   shows "Bfun (\<lambda>x. inverse (f x)) net"
   706 proof -
   707   from a have "0 < norm a" by simp
   708   hence "\<exists>r>0. r < norm a" by (rule dense)
   709   then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
   710   have "eventually (\<lambda>x. dist (f x) a < r) net"
   711     using tendstoD [OF f r1] by fast
   712   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) net"
   713   proof (rule eventually_elim1)
   714     fix x
   715     assume "dist (f x) a < r"
   716     hence 1: "norm (f x - a) < r"
   717       by (simp add: dist_norm)
   718     hence 2: "f x \<noteq> 0" using r2 by auto
   719     hence "norm (inverse (f x)) = inverse (norm (f x))"
   720       by (rule nonzero_norm_inverse)
   721     also have "\<dots> \<le> inverse (norm a - r)"
   722     proof (rule le_imp_inverse_le)
   723       show "0 < norm a - r" using r2 by simp
   724     next
   725       have "norm a - norm (f x) \<le> norm (a - f x)"
   726         by (rule norm_triangle_ineq2)
   727       also have "\<dots> = norm (f x - a)"
   728         by (rule norm_minus_commute)
   729       also have "\<dots> < r" using 1 .
   730       finally show "norm a - r \<le> norm (f x)" by simp
   731     qed
   732     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
   733   qed
   734   thus ?thesis by (rule BfunI)
   735 qed
   736 
   737 lemma tendsto_inverse_lemma:
   738   fixes a :: "'a::real_normed_div_algebra"
   739   shows "\<lbrakk>(f ---> a) net; a \<noteq> 0; eventually (\<lambda>x. f x \<noteq> 0) net\<rbrakk>
   740          \<Longrightarrow> ((\<lambda>x. inverse (f x)) ---> inverse a) net"
   741 apply (subst tendsto_Zfun_iff)
   742 apply (rule Zfun_ssubst)
   743 apply (erule eventually_elim1)
   744 apply (erule (1) inverse_diff_inverse)
   745 apply (rule Zfun_minus)
   746 apply (rule Zfun_mult_left)
   747 apply (rule mult.Bfun_prod_Zfun)
   748 apply (erule (1) Bfun_inverse)
   749 apply (simp add: tendsto_Zfun_iff)
   750 done
   751 
   752 lemma tendsto_inverse [tendsto_intros]:
   753   fixes a :: "'a::real_normed_div_algebra"
   754   assumes f: "(f ---> a) net"
   755   assumes a: "a \<noteq> 0"
   756   shows "((\<lambda>x. inverse (f x)) ---> inverse a) net"
   757 proof -
   758   from a have "0 < norm a" by simp
   759   with f have "eventually (\<lambda>x. dist (f x) a < norm a) net"
   760     by (rule tendstoD)
   761   then have "eventually (\<lambda>x. f x \<noteq> 0) net"
   762     unfolding dist_norm by (auto elim!: eventually_elim1)
   763   with f a show ?thesis
   764     by (rule tendsto_inverse_lemma)
   765 qed
   766 
   767 lemma tendsto_divide [tendsto_intros]:
   768   fixes a b :: "'a::real_normed_field"
   769   shows "\<lbrakk>(f ---> a) net; (g ---> b) net; b \<noteq> 0\<rbrakk>
   770     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) net"
   771 by (simp add: mult.tendsto tendsto_inverse divide_inverse)
   772 
   773 end