src/HOL/Limits.thy
author huffman
Mon May 03 17:13:37 2010 -0700 (2010-05-03)
changeset 36654 7c8eb32724ce
parent 36630 aa1f8acdcc1c
child 36655 88f0125c3bd2
permissions -rw-r--r--
add constants netmap and nhds
     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 {* Map function for nets *}
   245 
   246 definition
   247   netmap :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a net \<Rightarrow> 'b net"
   248 where [code del]:
   249   "netmap f net = Abs_net (\<lambda>P. eventually (\<lambda>x. P (f x)) net)"
   250 
   251 lemma eventually_netmap:
   252   "eventually P (netmap f net) = eventually (\<lambda>x. P (f x)) net"
   253 unfolding netmap_def
   254 apply (rule eventually_Abs_net)
   255 apply (rule is_filter.intro)
   256 apply (auto elim!: eventually_rev_mp)
   257 done
   258 
   259 lemma netmap_ident: "netmap (\<lambda>x. x) net = net"
   260 by (simp add: expand_net_eq eventually_netmap)
   261 
   262 lemma netmap_netmap: "netmap f (netmap g net) = netmap (\<lambda>x. f (g x)) net"
   263 by (simp add: expand_net_eq eventually_netmap)
   264 
   265 lemma netmap_mono: "net \<le> net' \<Longrightarrow> netmap f net \<le> netmap f net'"
   266 unfolding le_net_def eventually_netmap by simp
   267 
   268 lemma netmap_bot [simp]: "netmap f bot = bot"
   269 by (simp add: expand_net_eq eventually_netmap)
   270 
   271 
   272 subsection {* Standard Nets *}
   273 
   274 definition
   275   sequentially :: "nat net"
   276 where [code del]:
   277   "sequentially = Abs_net (\<lambda>P. \<exists>k. \<forall>n\<ge>k. P n)"
   278 
   279 definition
   280   within :: "'a net \<Rightarrow> 'a set \<Rightarrow> 'a net" (infixr "within" 70)
   281 where [code del]:
   282   "net within S = Abs_net (\<lambda>P. eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) net)"
   283 
   284 definition
   285   nhds :: "'a::topological_space \<Rightarrow> 'a net"
   286 where [code del]:
   287   "nhds a = Abs_net (\<lambda>P. \<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   288 
   289 definition
   290   at :: "'a::topological_space \<Rightarrow> 'a net"
   291 where [code del]:
   292   "at a = nhds a within - {a}"
   293 
   294 lemma eventually_sequentially:
   295   "eventually P sequentially \<longleftrightarrow> (\<exists>N. \<forall>n\<ge>N. P n)"
   296 unfolding sequentially_def
   297 proof (rule eventually_Abs_net, rule is_filter.intro)
   298   fix P Q :: "nat \<Rightarrow> bool"
   299   assume "\<exists>i. \<forall>n\<ge>i. P n" and "\<exists>j. \<forall>n\<ge>j. Q n"
   300   then obtain i j where "\<forall>n\<ge>i. P n" and "\<forall>n\<ge>j. Q n" by auto
   301   then have "\<forall>n\<ge>max i j. P n \<and> Q n" by simp
   302   then show "\<exists>k. \<forall>n\<ge>k. P n \<and> Q n" ..
   303 qed auto
   304 
   305 lemma eventually_within:
   306   "eventually P (net within S) = eventually (\<lambda>x. x \<in> S \<longrightarrow> P x) net"
   307 unfolding within_def
   308 by (rule eventually_Abs_net, rule is_filter.intro)
   309    (auto elim!: eventually_rev_mp)
   310 
   311 lemma within_UNIV: "net within UNIV = net"
   312   unfolding expand_net_eq eventually_within by simp
   313 
   314 lemma eventually_nhds:
   315   "eventually P (nhds a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x))"
   316 unfolding nhds_def
   317 proof (rule eventually_Abs_net, rule is_filter.intro)
   318   have "open UNIV \<and> a \<in> UNIV \<and> (\<forall>x\<in>UNIV. True)" by simp
   319   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. True)" by - rule
   320 next
   321   fix P Q
   322   assume "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   323      and "\<exists>T. open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)"
   324   then obtain S T where
   325     "open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x)"
   326     "open T \<and> a \<in> T \<and> (\<forall>x\<in>T. Q x)" by auto
   327   hence "open (S \<inter> T) \<and> a \<in> S \<inter> T \<and> (\<forall>x\<in>(S \<inter> T). P x \<and> Q x)"
   328     by (simp add: open_Int)
   329   thus "\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. P x \<and> Q x)" by - rule
   330 qed auto
   331 
   332 lemma eventually_at_topological:
   333   "eventually P (at a) \<longleftrightarrow> (\<exists>S. open S \<and> a \<in> S \<and> (\<forall>x\<in>S. x \<noteq> a \<longrightarrow> P x))"
   334 unfolding at_def eventually_within eventually_nhds by simp
   335 
   336 lemma eventually_at:
   337   fixes a :: "'a::metric_space"
   338   shows "eventually P (at a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
   339 unfolding eventually_at_topological open_dist
   340 apply safe
   341 apply fast
   342 apply (rule_tac x="{x. dist x a < d}" in exI, simp)
   343 apply clarsimp
   344 apply (rule_tac x="d - dist x a" in exI, clarsimp)
   345 apply (simp only: less_diff_eq)
   346 apply (erule le_less_trans [OF dist_triangle])
   347 done
   348 
   349 
   350 subsection {* Boundedness *}
   351 
   352 definition
   353   Bfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a net \<Rightarrow> bool" where
   354   [code del]: "Bfun f net = (\<exists>K>0. eventually (\<lambda>x. norm (f x) \<le> K) net)"
   355 
   356 lemma BfunI:
   357   assumes K: "eventually (\<lambda>x. norm (f x) \<le> K) net" shows "Bfun f net"
   358 unfolding Bfun_def
   359 proof (intro exI conjI allI)
   360   show "0 < max K 1" by simp
   361 next
   362   show "eventually (\<lambda>x. norm (f x) \<le> max K 1) net"
   363     using K by (rule eventually_elim1, simp)
   364 qed
   365 
   366 lemma BfunE:
   367   assumes "Bfun f net"
   368   obtains B where "0 < B" and "eventually (\<lambda>x. norm (f x) \<le> B) net"
   369 using assms unfolding Bfun_def by fast
   370 
   371 
   372 subsection {* Convergence to Zero *}
   373 
   374 definition
   375   Zfun :: "('a \<Rightarrow> 'b::real_normed_vector) \<Rightarrow> 'a net \<Rightarrow> bool" where
   376   [code del]: "Zfun f net = (\<forall>r>0. eventually (\<lambda>x. norm (f x) < r) net)"
   377 
   378 lemma ZfunI:
   379   "(\<And>r. 0 < r \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) net) \<Longrightarrow> Zfun f net"
   380 unfolding Zfun_def by simp
   381 
   382 lemma ZfunD:
   383   "\<lbrakk>Zfun f net; 0 < r\<rbrakk> \<Longrightarrow> eventually (\<lambda>x. norm (f x) < r) net"
   384 unfolding Zfun_def by simp
   385 
   386 lemma Zfun_ssubst:
   387   "eventually (\<lambda>x. f x = g x) net \<Longrightarrow> Zfun g net \<Longrightarrow> Zfun f net"
   388 unfolding Zfun_def by (auto elim!: eventually_rev_mp)
   389 
   390 lemma Zfun_zero: "Zfun (\<lambda>x. 0) net"
   391 unfolding Zfun_def by simp
   392 
   393 lemma Zfun_norm_iff: "Zfun (\<lambda>x. norm (f x)) net = Zfun (\<lambda>x. f x) net"
   394 unfolding Zfun_def by simp
   395 
   396 lemma Zfun_imp_Zfun:
   397   assumes f: "Zfun f net"
   398   assumes g: "eventually (\<lambda>x. norm (g x) \<le> norm (f x) * K) net"
   399   shows "Zfun (\<lambda>x. g x) net"
   400 proof (cases)
   401   assume K: "0 < K"
   402   show ?thesis
   403   proof (rule ZfunI)
   404     fix r::real assume "0 < r"
   405     hence "0 < r / K"
   406       using K by (rule divide_pos_pos)
   407     then have "eventually (\<lambda>x. norm (f x) < r / K) net"
   408       using ZfunD [OF f] by fast
   409     with g show "eventually (\<lambda>x. norm (g x) < r) net"
   410     proof (rule eventually_elim2)
   411       fix x
   412       assume *: "norm (g x) \<le> norm (f x) * K"
   413       assume "norm (f x) < r / K"
   414       hence "norm (f x) * K < r"
   415         by (simp add: pos_less_divide_eq K)
   416       thus "norm (g x) < r"
   417         by (simp add: order_le_less_trans [OF *])
   418     qed
   419   qed
   420 next
   421   assume "\<not> 0 < K"
   422   hence K: "K \<le> 0" by (simp only: not_less)
   423   show ?thesis
   424   proof (rule ZfunI)
   425     fix r :: real
   426     assume "0 < r"
   427     from g show "eventually (\<lambda>x. norm (g x) < r) net"
   428     proof (rule eventually_elim1)
   429       fix x
   430       assume "norm (g x) \<le> norm (f x) * K"
   431       also have "\<dots> \<le> norm (f x) * 0"
   432         using K norm_ge_zero by (rule mult_left_mono)
   433       finally show "norm (g x) < r"
   434         using `0 < r` by simp
   435     qed
   436   qed
   437 qed
   438 
   439 lemma Zfun_le: "\<lbrakk>Zfun g net; \<forall>x. norm (f x) \<le> norm (g x)\<rbrakk> \<Longrightarrow> Zfun f net"
   440 by (erule_tac K="1" in Zfun_imp_Zfun, simp)
   441 
   442 lemma Zfun_add:
   443   assumes f: "Zfun f net" and g: "Zfun g net"
   444   shows "Zfun (\<lambda>x. f x + g x) net"
   445 proof (rule ZfunI)
   446   fix r::real assume "0 < r"
   447   hence r: "0 < r / 2" by simp
   448   have "eventually (\<lambda>x. norm (f x) < r/2) net"
   449     using f r by (rule ZfunD)
   450   moreover
   451   have "eventually (\<lambda>x. norm (g x) < r/2) net"
   452     using g r by (rule ZfunD)
   453   ultimately
   454   show "eventually (\<lambda>x. norm (f x + g x) < r) net"
   455   proof (rule eventually_elim2)
   456     fix x
   457     assume *: "norm (f x) < r/2" "norm (g x) < r/2"
   458     have "norm (f x + g x) \<le> norm (f x) + norm (g x)"
   459       by (rule norm_triangle_ineq)
   460     also have "\<dots> < r/2 + r/2"
   461       using * by (rule add_strict_mono)
   462     finally show "norm (f x + g x) < r"
   463       by simp
   464   qed
   465 qed
   466 
   467 lemma Zfun_minus: "Zfun f net \<Longrightarrow> Zfun (\<lambda>x. - f x) net"
   468 unfolding Zfun_def by simp
   469 
   470 lemma Zfun_diff: "\<lbrakk>Zfun f net; Zfun g net\<rbrakk> \<Longrightarrow> Zfun (\<lambda>x. f x - g x) net"
   471 by (simp only: diff_minus Zfun_add Zfun_minus)
   472 
   473 lemma (in bounded_linear) Zfun:
   474   assumes g: "Zfun g net"
   475   shows "Zfun (\<lambda>x. f (g x)) net"
   476 proof -
   477   obtain K where "\<And>x. norm (f x) \<le> norm x * K"
   478     using bounded by fast
   479   then have "eventually (\<lambda>x. norm (f (g x)) \<le> norm (g x) * K) net"
   480     by simp
   481   with g show ?thesis
   482     by (rule Zfun_imp_Zfun)
   483 qed
   484 
   485 lemma (in bounded_bilinear) Zfun:
   486   assumes f: "Zfun f net"
   487   assumes g: "Zfun g net"
   488   shows "Zfun (\<lambda>x. f x ** g x) net"
   489 proof (rule ZfunI)
   490   fix r::real assume r: "0 < r"
   491   obtain K where K: "0 < K"
   492     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   493     using pos_bounded by fast
   494   from K have K': "0 < inverse K"
   495     by (rule positive_imp_inverse_positive)
   496   have "eventually (\<lambda>x. norm (f x) < r) net"
   497     using f r by (rule ZfunD)
   498   moreover
   499   have "eventually (\<lambda>x. norm (g x) < inverse K) net"
   500     using g K' by (rule ZfunD)
   501   ultimately
   502   show "eventually (\<lambda>x. norm (f x ** g x) < r) net"
   503   proof (rule eventually_elim2)
   504     fix x
   505     assume *: "norm (f x) < r" "norm (g x) < inverse K"
   506     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   507       by (rule norm_le)
   508     also have "norm (f x) * norm (g x) * K < r * inverse K * K"
   509       by (intro mult_strict_right_mono mult_strict_mono' norm_ge_zero * K)
   510     also from K have "r * inverse K * K = r"
   511       by simp
   512     finally show "norm (f x ** g x) < r" .
   513   qed
   514 qed
   515 
   516 lemma (in bounded_bilinear) Zfun_left:
   517   "Zfun f net \<Longrightarrow> Zfun (\<lambda>x. f x ** a) net"
   518 by (rule bounded_linear_left [THEN bounded_linear.Zfun])
   519 
   520 lemma (in bounded_bilinear) Zfun_right:
   521   "Zfun f net \<Longrightarrow> Zfun (\<lambda>x. a ** f x) net"
   522 by (rule bounded_linear_right [THEN bounded_linear.Zfun])
   523 
   524 lemmas Zfun_mult = mult.Zfun
   525 lemmas Zfun_mult_right = mult.Zfun_right
   526 lemmas Zfun_mult_left = mult.Zfun_left
   527 
   528 
   529 subsection {* Limits *}
   530 
   531 definition
   532   tendsto :: "('a \<Rightarrow> 'b::topological_space) \<Rightarrow> 'b \<Rightarrow> 'a net \<Rightarrow> bool"
   533     (infixr "--->" 55)
   534 where [code del]:
   535   "(f ---> l) net \<longleftrightarrow> (\<forall>S. open S \<longrightarrow> l \<in> S \<longrightarrow> eventually (\<lambda>x. f x \<in> S) net)"
   536 
   537 ML {*
   538 structure Tendsto_Intros = Named_Thms
   539 (
   540   val name = "tendsto_intros"
   541   val description = "introduction rules for tendsto"
   542 )
   543 *}
   544 
   545 setup Tendsto_Intros.setup
   546 
   547 lemma topological_tendstoI:
   548   "(\<And>S. open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) net)
   549     \<Longrightarrow> (f ---> l) net"
   550   unfolding tendsto_def by auto
   551 
   552 lemma topological_tendstoD:
   553   "(f ---> l) net \<Longrightarrow> open S \<Longrightarrow> l \<in> S \<Longrightarrow> eventually (\<lambda>x. f x \<in> S) net"
   554   unfolding tendsto_def by auto
   555 
   556 lemma tendstoI:
   557   assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) net"
   558   shows "(f ---> l) net"
   559 apply (rule topological_tendstoI)
   560 apply (simp add: open_dist)
   561 apply (drule (1) bspec, clarify)
   562 apply (drule assms)
   563 apply (erule eventually_elim1, simp)
   564 done
   565 
   566 lemma tendstoD:
   567   "(f ---> l) net \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) net"
   568 apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
   569 apply (clarsimp simp add: open_dist)
   570 apply (rule_tac x="e - dist x l" in exI, clarsimp)
   571 apply (simp only: less_diff_eq)
   572 apply (erule le_less_trans [OF dist_triangle])
   573 apply simp
   574 apply simp
   575 done
   576 
   577 lemma tendsto_iff:
   578   "(f ---> l) net \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) net)"
   579 using tendstoI tendstoD by fast
   580 
   581 lemma tendsto_Zfun_iff: "(f ---> a) net = Zfun (\<lambda>x. f x - a) net"
   582 by (simp only: tendsto_iff Zfun_def dist_norm)
   583 
   584 lemma tendsto_ident_at [tendsto_intros]: "((\<lambda>x. x) ---> a) (at a)"
   585 unfolding tendsto_def eventually_at_topological by auto
   586 
   587 lemma tendsto_ident_at_within [tendsto_intros]:
   588   "a \<in> S \<Longrightarrow> ((\<lambda>x. x) ---> a) (at a within S)"
   589 unfolding tendsto_def eventually_within eventually_at_topological by auto
   590 
   591 lemma tendsto_const [tendsto_intros]: "((\<lambda>x. k) ---> k) net"
   592 by (simp add: tendsto_def)
   593 
   594 lemma tendsto_dist [tendsto_intros]:
   595   assumes f: "(f ---> l) net" and g: "(g ---> m) net"
   596   shows "((\<lambda>x. dist (f x) (g x)) ---> dist l m) net"
   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) net"
   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 lemma tendsto_norm [tendsto_intros]:
   615   "(f ---> a) net \<Longrightarrow> ((\<lambda>x. norm (f x)) ---> norm a) net"
   616 apply (simp add: tendsto_iff dist_norm, safe)
   617 apply (drule_tac x="e" in spec, safe)
   618 apply (erule eventually_elim1)
   619 apply (erule order_le_less_trans [OF norm_triangle_ineq3])
   620 done
   621 
   622 lemma add_diff_add:
   623   fixes a b c d :: "'a::ab_group_add"
   624   shows "(a + c) - (b + d) = (a - b) + (c - d)"
   625 by simp
   626 
   627 lemma minus_diff_minus:
   628   fixes a b :: "'a::ab_group_add"
   629   shows "(- a) - (- b) = - (a - b)"
   630 by simp
   631 
   632 lemma tendsto_add [tendsto_intros]:
   633   fixes a b :: "'a::real_normed_vector"
   634   shows "\<lbrakk>(f ---> a) net; (g ---> b) net\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x + g x) ---> a + b) net"
   635 by (simp only: tendsto_Zfun_iff add_diff_add Zfun_add)
   636 
   637 lemma tendsto_minus [tendsto_intros]:
   638   fixes a :: "'a::real_normed_vector"
   639   shows "(f ---> a) net \<Longrightarrow> ((\<lambda>x. - f x) ---> - a) net"
   640 by (simp only: tendsto_Zfun_iff minus_diff_minus Zfun_minus)
   641 
   642 lemma tendsto_minus_cancel:
   643   fixes a :: "'a::real_normed_vector"
   644   shows "((\<lambda>x. - f x) ---> - a) net \<Longrightarrow> (f ---> a) net"
   645 by (drule tendsto_minus, simp)
   646 
   647 lemma tendsto_diff [tendsto_intros]:
   648   fixes a b :: "'a::real_normed_vector"
   649   shows "\<lbrakk>(f ---> a) net; (g ---> b) net\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x - g x) ---> a - b) net"
   650 by (simp add: diff_minus tendsto_add tendsto_minus)
   651 
   652 lemma tendsto_setsum [tendsto_intros]:
   653   fixes f :: "'a \<Rightarrow> 'b \<Rightarrow> 'c::real_normed_vector"
   654   assumes "\<And>i. i \<in> S \<Longrightarrow> (f i ---> a i) net"
   655   shows "((\<lambda>x. \<Sum>i\<in>S. f i x) ---> (\<Sum>i\<in>S. a i)) net"
   656 proof (cases "finite S")
   657   assume "finite S" thus ?thesis using assms
   658   proof (induct set: finite)
   659     case empty show ?case
   660       by (simp add: tendsto_const)
   661   next
   662     case (insert i F) thus ?case
   663       by (simp add: tendsto_add)
   664   qed
   665 next
   666   assume "\<not> finite S" thus ?thesis
   667     by (simp add: tendsto_const)
   668 qed
   669 
   670 lemma (in bounded_linear) tendsto [tendsto_intros]:
   671   "(g ---> a) net \<Longrightarrow> ((\<lambda>x. f (g x)) ---> f a) net"
   672 by (simp only: tendsto_Zfun_iff diff [symmetric] Zfun)
   673 
   674 lemma (in bounded_bilinear) tendsto [tendsto_intros]:
   675   "\<lbrakk>(f ---> a) net; (g ---> b) net\<rbrakk> \<Longrightarrow> ((\<lambda>x. f x ** g x) ---> a ** b) net"
   676 by (simp only: tendsto_Zfun_iff prod_diff_prod
   677                Zfun_add Zfun Zfun_left Zfun_right)
   678 
   679 
   680 subsection {* Continuity of Inverse *}
   681 
   682 lemma (in bounded_bilinear) Zfun_prod_Bfun:
   683   assumes f: "Zfun f net"
   684   assumes g: "Bfun g net"
   685   shows "Zfun (\<lambda>x. f x ** g x) net"
   686 proof -
   687   obtain K where K: "0 \<le> K"
   688     and norm_le: "\<And>x y. norm (x ** y) \<le> norm x * norm y * K"
   689     using nonneg_bounded by fast
   690   obtain B where B: "0 < B"
   691     and norm_g: "eventually (\<lambda>x. norm (g x) \<le> B) net"
   692     using g by (rule BfunE)
   693   have "eventually (\<lambda>x. norm (f x ** g x) \<le> norm (f x) * (B * K)) net"
   694   using norm_g proof (rule eventually_elim1)
   695     fix x
   696     assume *: "norm (g x) \<le> B"
   697     have "norm (f x ** g x) \<le> norm (f x) * norm (g x) * K"
   698       by (rule norm_le)
   699     also have "\<dots> \<le> norm (f x) * B * K"
   700       by (intro mult_mono' order_refl norm_g norm_ge_zero
   701                 mult_nonneg_nonneg K *)
   702     also have "\<dots> = norm (f x) * (B * K)"
   703       by (rule mult_assoc)
   704     finally show "norm (f x ** g x) \<le> norm (f x) * (B * K)" .
   705   qed
   706   with f show ?thesis
   707     by (rule Zfun_imp_Zfun)
   708 qed
   709 
   710 lemma (in bounded_bilinear) flip:
   711   "bounded_bilinear (\<lambda>x y. y ** x)"
   712 apply default
   713 apply (rule add_right)
   714 apply (rule add_left)
   715 apply (rule scaleR_right)
   716 apply (rule scaleR_left)
   717 apply (subst mult_commute)
   718 using bounded by fast
   719 
   720 lemma (in bounded_bilinear) Bfun_prod_Zfun:
   721   assumes f: "Bfun f net"
   722   assumes g: "Zfun g net"
   723   shows "Zfun (\<lambda>x. f x ** g x) net"
   724 using flip g f by (rule bounded_bilinear.Zfun_prod_Bfun)
   725 
   726 lemma inverse_diff_inverse:
   727   "\<lbrakk>(a::'a::division_ring) \<noteq> 0; b \<noteq> 0\<rbrakk>
   728    \<Longrightarrow> inverse a - inverse b = - (inverse a * (a - b) * inverse b)"
   729 by (simp add: algebra_simps)
   730 
   731 lemma Bfun_inverse_lemma:
   732   fixes x :: "'a::real_normed_div_algebra"
   733   shows "\<lbrakk>r \<le> norm x; 0 < r\<rbrakk> \<Longrightarrow> norm (inverse x) \<le> inverse r"
   734 apply (subst nonzero_norm_inverse, clarsimp)
   735 apply (erule (1) le_imp_inverse_le)
   736 done
   737 
   738 lemma Bfun_inverse:
   739   fixes a :: "'a::real_normed_div_algebra"
   740   assumes f: "(f ---> a) net"
   741   assumes a: "a \<noteq> 0"
   742   shows "Bfun (\<lambda>x. inverse (f x)) net"
   743 proof -
   744   from a have "0 < norm a" by simp
   745   hence "\<exists>r>0. r < norm a" by (rule dense)
   746   then obtain r where r1: "0 < r" and r2: "r < norm a" by fast
   747   have "eventually (\<lambda>x. dist (f x) a < r) net"
   748     using tendstoD [OF f r1] by fast
   749   hence "eventually (\<lambda>x. norm (inverse (f x)) \<le> inverse (norm a - r)) net"
   750   proof (rule eventually_elim1)
   751     fix x
   752     assume "dist (f x) a < r"
   753     hence 1: "norm (f x - a) < r"
   754       by (simp add: dist_norm)
   755     hence 2: "f x \<noteq> 0" using r2 by auto
   756     hence "norm (inverse (f x)) = inverse (norm (f x))"
   757       by (rule nonzero_norm_inverse)
   758     also have "\<dots> \<le> inverse (norm a - r)"
   759     proof (rule le_imp_inverse_le)
   760       show "0 < norm a - r" using r2 by simp
   761     next
   762       have "norm a - norm (f x) \<le> norm (a - f x)"
   763         by (rule norm_triangle_ineq2)
   764       also have "\<dots> = norm (f x - a)"
   765         by (rule norm_minus_commute)
   766       also have "\<dots> < r" using 1 .
   767       finally show "norm a - r \<le> norm (f x)" by simp
   768     qed
   769     finally show "norm (inverse (f x)) \<le> inverse (norm a - r)" .
   770   qed
   771   thus ?thesis by (rule BfunI)
   772 qed
   773 
   774 lemma tendsto_inverse_lemma:
   775   fixes a :: "'a::real_normed_div_algebra"
   776   shows "\<lbrakk>(f ---> a) net; a \<noteq> 0; eventually (\<lambda>x. f x \<noteq> 0) net\<rbrakk>
   777          \<Longrightarrow> ((\<lambda>x. inverse (f x)) ---> inverse a) net"
   778 apply (subst tendsto_Zfun_iff)
   779 apply (rule Zfun_ssubst)
   780 apply (erule eventually_elim1)
   781 apply (erule (1) inverse_diff_inverse)
   782 apply (rule Zfun_minus)
   783 apply (rule Zfun_mult_left)
   784 apply (rule mult.Bfun_prod_Zfun)
   785 apply (erule (1) Bfun_inverse)
   786 apply (simp add: tendsto_Zfun_iff)
   787 done
   788 
   789 lemma tendsto_inverse [tendsto_intros]:
   790   fixes a :: "'a::real_normed_div_algebra"
   791   assumes f: "(f ---> a) net"
   792   assumes a: "a \<noteq> 0"
   793   shows "((\<lambda>x. inverse (f x)) ---> inverse a) net"
   794 proof -
   795   from a have "0 < norm a" by simp
   796   with f have "eventually (\<lambda>x. dist (f x) a < norm a) net"
   797     by (rule tendstoD)
   798   then have "eventually (\<lambda>x. f x \<noteq> 0) net"
   799     unfolding dist_norm by (auto elim!: eventually_elim1)
   800   with f a show ?thesis
   801     by (rule tendsto_inverse_lemma)
   802 qed
   803 
   804 lemma tendsto_divide [tendsto_intros]:
   805   fixes a b :: "'a::real_normed_field"
   806   shows "\<lbrakk>(f ---> a) net; (g ---> b) net; b \<noteq> 0\<rbrakk>
   807     \<Longrightarrow> ((\<lambda>x. f x / g x) ---> a / b) net"
   808 by (simp add: mult.tendsto tendsto_inverse divide_inverse)
   809 
   810 end