src/HOL/Real_Vector_Spaces.thy
author paulson <lp15@cam.ac.uk>
Mon May 23 15:33:24 2016 +0100 (2016-05-23)
changeset 63114 27afe7af7379
parent 63040 eb4ddd18d635
child 63128 24708cf4ba61
permissions -rw-r--r--
Lots of new material for multivariate analysis
     1 (*  Title:      HOL/Real_Vector_Spaces.thy
     2     Author:     Brian Huffman
     3     Author:     Johannes Hölzl
     4 *)
     5 
     6 section \<open>Vector Spaces and Algebras over the Reals\<close>
     7 
     8 theory Real_Vector_Spaces
     9 imports Real Topological_Spaces
    10 begin
    11 
    12 subsection \<open>Locale for additive functions\<close>
    13 
    14 locale additive =
    15   fixes f :: "'a::ab_group_add \<Rightarrow> 'b::ab_group_add"
    16   assumes add: "f (x + y) = f x + f y"
    17 begin
    18 
    19 lemma zero: "f 0 = 0"
    20 proof -
    21   have "f 0 = f (0 + 0)" by simp
    22   also have "\<dots> = f 0 + f 0" by (rule add)
    23   finally show "f 0 = 0" by simp
    24 qed
    25 
    26 lemma minus: "f (- x) = - f x"
    27 proof -
    28   have "f (- x) + f x = f (- x + x)" by (rule add [symmetric])
    29   also have "\<dots> = - f x + f x" by (simp add: zero)
    30   finally show "f (- x) = - f x" by (rule add_right_imp_eq)
    31 qed
    32 
    33 lemma diff: "f (x - y) = f x - f y"
    34   using add [of x "- y"] by (simp add: minus)
    35 
    36 lemma setsum: "f (setsum g A) = (\<Sum>x\<in>A. f (g x))"
    37 apply (cases "finite A")
    38 apply (induct set: finite)
    39 apply (simp add: zero)
    40 apply (simp add: add)
    41 apply (simp add: zero)
    42 done
    43 
    44 end
    45 
    46 subsection \<open>Vector spaces\<close>
    47 
    48 locale vector_space =
    49   fixes scale :: "'a::field \<Rightarrow> 'b::ab_group_add \<Rightarrow> 'b"
    50   assumes scale_right_distrib [algebra_simps]:
    51     "scale a (x + y) = scale a x + scale a y"
    52   and scale_left_distrib [algebra_simps]:
    53     "scale (a + b) x = scale a x + scale b x"
    54   and scale_scale [simp]: "scale a (scale b x) = scale (a * b) x"
    55   and scale_one [simp]: "scale 1 x = x"
    56 begin
    57 
    58 lemma scale_left_commute:
    59   "scale a (scale b x) = scale b (scale a x)"
    60 by (simp add: mult.commute)
    61 
    62 lemma scale_zero_left [simp]: "scale 0 x = 0"
    63   and scale_minus_left [simp]: "scale (- a) x = - (scale a x)"
    64   and scale_left_diff_distrib [algebra_simps]:
    65         "scale (a - b) x = scale a x - scale b x"
    66   and scale_setsum_left: "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)"
    67 proof -
    68   interpret s: additive "\<lambda>a. scale a x"
    69     proof qed (rule scale_left_distrib)
    70   show "scale 0 x = 0" by (rule s.zero)
    71   show "scale (- a) x = - (scale a x)" by (rule s.minus)
    72   show "scale (a - b) x = scale a x - scale b x" by (rule s.diff)
    73   show "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)" by (rule s.setsum)
    74 qed
    75 
    76 lemma scale_zero_right [simp]: "scale a 0 = 0"
    77   and scale_minus_right [simp]: "scale a (- x) = - (scale a x)"
    78   and scale_right_diff_distrib [algebra_simps]:
    79         "scale a (x - y) = scale a x - scale a y"
    80   and scale_setsum_right: "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))"
    81 proof -
    82   interpret s: additive "\<lambda>x. scale a x"
    83     proof qed (rule scale_right_distrib)
    84   show "scale a 0 = 0" by (rule s.zero)
    85   show "scale a (- x) = - (scale a x)" by (rule s.minus)
    86   show "scale a (x - y) = scale a x - scale a y" by (rule s.diff)
    87   show "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))" by (rule s.setsum)
    88 qed
    89 
    90 lemma scale_eq_0_iff [simp]:
    91   "scale a x = 0 \<longleftrightarrow> a = 0 \<or> x = 0"
    92 proof cases
    93   assume "a = 0" thus ?thesis by simp
    94 next
    95   assume anz [simp]: "a \<noteq> 0"
    96   { assume "scale a x = 0"
    97     hence "scale (inverse a) (scale a x) = 0" by simp
    98     hence "x = 0" by simp }
    99   thus ?thesis by force
   100 qed
   101 
   102 lemma scale_left_imp_eq:
   103   "\<lbrakk>a \<noteq> 0; scale a x = scale a y\<rbrakk> \<Longrightarrow> x = y"
   104 proof -
   105   assume nonzero: "a \<noteq> 0"
   106   assume "scale a x = scale a y"
   107   hence "scale a (x - y) = 0"
   108      by (simp add: scale_right_diff_distrib)
   109   hence "x - y = 0" by (simp add: nonzero)
   110   thus "x = y" by (simp only: right_minus_eq)
   111 qed
   112 
   113 lemma scale_right_imp_eq:
   114   "\<lbrakk>x \<noteq> 0; scale a x = scale b x\<rbrakk> \<Longrightarrow> a = b"
   115 proof -
   116   assume nonzero: "x \<noteq> 0"
   117   assume "scale a x = scale b x"
   118   hence "scale (a - b) x = 0"
   119      by (simp add: scale_left_diff_distrib)
   120   hence "a - b = 0" by (simp add: nonzero)
   121   thus "a = b" by (simp only: right_minus_eq)
   122 qed
   123 
   124 lemma scale_cancel_left [simp]:
   125   "scale a x = scale a y \<longleftrightarrow> x = y \<or> a = 0"
   126 by (auto intro: scale_left_imp_eq)
   127 
   128 lemma scale_cancel_right [simp]:
   129   "scale a x = scale b x \<longleftrightarrow> a = b \<or> x = 0"
   130 by (auto intro: scale_right_imp_eq)
   131 
   132 end
   133 
   134 subsection \<open>Real vector spaces\<close>
   135 
   136 class scaleR =
   137   fixes scaleR :: "real \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "*\<^sub>R" 75)
   138 begin
   139 
   140 abbreviation
   141   divideR :: "'a \<Rightarrow> real \<Rightarrow> 'a" (infixl "'/\<^sub>R" 70)
   142 where
   143   "x /\<^sub>R r == scaleR (inverse r) x"
   144 
   145 end
   146 
   147 class real_vector = scaleR + ab_group_add +
   148   assumes scaleR_add_right: "scaleR a (x + y) = scaleR a x + scaleR a y"
   149   and scaleR_add_left: "scaleR (a + b) x = scaleR a x + scaleR b x"
   150   and scaleR_scaleR: "scaleR a (scaleR b x) = scaleR (a * b) x"
   151   and scaleR_one: "scaleR 1 x = x"
   152 
   153 interpretation real_vector:
   154   vector_space "scaleR :: real \<Rightarrow> 'a \<Rightarrow> 'a::real_vector"
   155 apply unfold_locales
   156 apply (rule scaleR_add_right)
   157 apply (rule scaleR_add_left)
   158 apply (rule scaleR_scaleR)
   159 apply (rule scaleR_one)
   160 done
   161 
   162 text \<open>Recover original theorem names\<close>
   163 
   164 lemmas scaleR_left_commute = real_vector.scale_left_commute
   165 lemmas scaleR_zero_left = real_vector.scale_zero_left
   166 lemmas scaleR_minus_left = real_vector.scale_minus_left
   167 lemmas scaleR_diff_left = real_vector.scale_left_diff_distrib
   168 lemmas scaleR_setsum_left = real_vector.scale_setsum_left
   169 lemmas scaleR_zero_right = real_vector.scale_zero_right
   170 lemmas scaleR_minus_right = real_vector.scale_minus_right
   171 lemmas scaleR_diff_right = real_vector.scale_right_diff_distrib
   172 lemmas scaleR_setsum_right = real_vector.scale_setsum_right
   173 lemmas scaleR_eq_0_iff = real_vector.scale_eq_0_iff
   174 lemmas scaleR_left_imp_eq = real_vector.scale_left_imp_eq
   175 lemmas scaleR_right_imp_eq = real_vector.scale_right_imp_eq
   176 lemmas scaleR_cancel_left = real_vector.scale_cancel_left
   177 lemmas scaleR_cancel_right = real_vector.scale_cancel_right
   178 
   179 text \<open>Legacy names\<close>
   180 
   181 lemmas scaleR_left_distrib = scaleR_add_left
   182 lemmas scaleR_right_distrib = scaleR_add_right
   183 lemmas scaleR_left_diff_distrib = scaleR_diff_left
   184 lemmas scaleR_right_diff_distrib = scaleR_diff_right
   185 
   186 lemma scaleR_minus1_left [simp]:
   187   fixes x :: "'a::real_vector"
   188   shows "scaleR (-1) x = - x"
   189   using scaleR_minus_left [of 1 x] by simp
   190 
   191 class real_algebra = real_vector + ring +
   192   assumes mult_scaleR_left [simp]: "scaleR a x * y = scaleR a (x * y)"
   193   and mult_scaleR_right [simp]: "x * scaleR a y = scaleR a (x * y)"
   194 
   195 class real_algebra_1 = real_algebra + ring_1
   196 
   197 class real_div_algebra = real_algebra_1 + division_ring
   198 
   199 class real_field = real_div_algebra + field
   200 
   201 instantiation real :: real_field
   202 begin
   203 
   204 definition
   205   real_scaleR_def [simp]: "scaleR a x = a * x"
   206 
   207 instance proof
   208 qed (simp_all add: algebra_simps)
   209 
   210 end
   211 
   212 interpretation scaleR_left: additive "(\<lambda>a. scaleR a x::'a::real_vector)"
   213 proof qed (rule scaleR_left_distrib)
   214 
   215 interpretation scaleR_right: additive "(\<lambda>x. scaleR a x::'a::real_vector)"
   216 proof qed (rule scaleR_right_distrib)
   217 
   218 lemma nonzero_inverse_scaleR_distrib:
   219   fixes x :: "'a::real_div_algebra" shows
   220   "\<lbrakk>a \<noteq> 0; x \<noteq> 0\<rbrakk> \<Longrightarrow> inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
   221 by (rule inverse_unique, simp)
   222 
   223 lemma inverse_scaleR_distrib:
   224   fixes x :: "'a::{real_div_algebra, division_ring}"
   225   shows "inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
   226 apply (case_tac "a = 0", simp)
   227 apply (case_tac "x = 0", simp)
   228 apply (erule (1) nonzero_inverse_scaleR_distrib)
   229 done
   230 
   231 lemma setsum_constant_scaleR:
   232   fixes y :: "'a::real_vector"
   233   shows "(\<Sum>x\<in>A. y) = of_nat (card A) *\<^sub>R y"
   234   apply (cases "finite A")
   235   apply (induct set: finite)
   236   apply (simp_all add: algebra_simps)
   237   done
   238 
   239 lemma vector_add_divide_simps :
   240   fixes v :: "'a :: real_vector"
   241   shows "v + (b / z) *\<^sub>R w = (if z = 0 then v else (z *\<^sub>R v + b *\<^sub>R w) /\<^sub>R z)"
   242         "a *\<^sub>R v + (b / z) *\<^sub>R w = (if z = 0 then a *\<^sub>R v else ((a * z) *\<^sub>R v + b *\<^sub>R w) /\<^sub>R z)"
   243         "(a / z) *\<^sub>R v + w = (if z = 0 then w else (a *\<^sub>R v + z *\<^sub>R w) /\<^sub>R z)"
   244         "(a / z) *\<^sub>R v + b *\<^sub>R w = (if z = 0 then b *\<^sub>R w else (a *\<^sub>R v + (b * z) *\<^sub>R w) /\<^sub>R z)"
   245         "v - (b / z) *\<^sub>R w = (if z = 0 then v else (z *\<^sub>R v - b *\<^sub>R w) /\<^sub>R z)"
   246         "a *\<^sub>R v - (b / z) *\<^sub>R w = (if z = 0 then a *\<^sub>R v else ((a * z) *\<^sub>R v - b *\<^sub>R w) /\<^sub>R z)"
   247         "(a / z) *\<^sub>R v - w = (if z = 0 then -w else (a *\<^sub>R v - z *\<^sub>R w) /\<^sub>R z)"
   248         "(a / z) *\<^sub>R v - b *\<^sub>R w = (if z = 0 then -b *\<^sub>R w else (a *\<^sub>R v - (b * z) *\<^sub>R w) /\<^sub>R z)"
   249 by (simp_all add: divide_inverse_commute scaleR_add_right real_vector.scale_right_diff_distrib)
   250 
   251 lemma real_vector_affinity_eq:
   252   fixes x :: "'a :: real_vector"
   253   assumes m0: "m \<noteq> 0"
   254   shows "m *\<^sub>R x + c = y \<longleftrightarrow> x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
   255 proof
   256   assume h: "m *\<^sub>R x + c = y"
   257   hence "m *\<^sub>R x = y - c" by (simp add: field_simps)
   258   hence "inverse m *\<^sub>R (m *\<^sub>R x) = inverse m *\<^sub>R (y - c)" by simp
   259   then show "x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
   260     using m0
   261   by (simp add: real_vector.scale_right_diff_distrib)
   262 next
   263   assume h: "x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
   264   show "m *\<^sub>R x + c = y" unfolding h
   265     using m0  by (simp add: real_vector.scale_right_diff_distrib)
   266 qed
   267 
   268 lemma real_vector_eq_affinity:
   269   fixes x :: "'a :: real_vector"
   270   shows "m \<noteq> 0 ==> (y = m *\<^sub>R x + c \<longleftrightarrow> inverse m *\<^sub>R y - (inverse m *\<^sub>R c) = x)"
   271   using real_vector_affinity_eq[where m=m and x=x and y=y and c=c]
   272   by metis
   273 
   274 lemma scaleR_eq_iff [simp]:
   275   fixes a :: "'a :: real_vector"
   276   shows "b + u *\<^sub>R a = a + u *\<^sub>R b \<longleftrightarrow> a=b \<or> u=1"
   277 proof (cases "u=1")
   278   case True then show ?thesis by auto
   279 next
   280   case False
   281   { assume "b + u *\<^sub>R a = a + u *\<^sub>R b"
   282     then have "(u - 1) *\<^sub>R a = (u - 1) *\<^sub>R b"
   283       by (simp add: algebra_simps)
   284     with False have "a=b"
   285       by auto
   286   }
   287   then show ?thesis by auto
   288 qed
   289 
   290 lemma scaleR_collapse [simp]:
   291   fixes a :: "'a :: real_vector"
   292   shows "(1 - u) *\<^sub>R a + u *\<^sub>R a = a"
   293 by (simp add: algebra_simps)
   294 
   295 
   296 subsection \<open>Embedding of the Reals into any \<open>real_algebra_1\<close>:
   297 @{term of_real}\<close>
   298 
   299 definition
   300   of_real :: "real \<Rightarrow> 'a::real_algebra_1" where
   301   "of_real r = scaleR r 1"
   302 
   303 lemma scaleR_conv_of_real: "scaleR r x = of_real r * x"
   304 by (simp add: of_real_def)
   305 
   306 lemma of_real_0 [simp]: "of_real 0 = 0"
   307 by (simp add: of_real_def)
   308 
   309 lemma of_real_1 [simp]: "of_real 1 = 1"
   310 by (simp add: of_real_def)
   311 
   312 lemma of_real_add [simp]: "of_real (x + y) = of_real x + of_real y"
   313 by (simp add: of_real_def scaleR_left_distrib)
   314 
   315 lemma of_real_minus [simp]: "of_real (- x) = - of_real x"
   316 by (simp add: of_real_def)
   317 
   318 lemma of_real_diff [simp]: "of_real (x - y) = of_real x - of_real y"
   319 by (simp add: of_real_def scaleR_left_diff_distrib)
   320 
   321 lemma of_real_mult [simp]: "of_real (x * y) = of_real x * of_real y"
   322 by (simp add: of_real_def mult.commute)
   323 
   324 lemma of_real_setsum[simp]: "of_real (setsum f s) = (\<Sum>x\<in>s. of_real (f x))"
   325   by (induct s rule: infinite_finite_induct) auto
   326 
   327 lemma of_real_setprod[simp]: "of_real (setprod f s) = (\<Prod>x\<in>s. of_real (f x))"
   328   by (induct s rule: infinite_finite_induct) auto
   329 
   330 lemma nonzero_of_real_inverse:
   331   "x \<noteq> 0 \<Longrightarrow> of_real (inverse x) =
   332    inverse (of_real x :: 'a::real_div_algebra)"
   333 by (simp add: of_real_def nonzero_inverse_scaleR_distrib)
   334 
   335 lemma of_real_inverse [simp]:
   336   "of_real (inverse x) =
   337    inverse (of_real x :: 'a::{real_div_algebra, division_ring})"
   338 by (simp add: of_real_def inverse_scaleR_distrib)
   339 
   340 lemma nonzero_of_real_divide:
   341   "y \<noteq> 0 \<Longrightarrow> of_real (x / y) =
   342    (of_real x / of_real y :: 'a::real_field)"
   343 by (simp add: divide_inverse nonzero_of_real_inverse)
   344 
   345 lemma of_real_divide [simp]:
   346   "of_real (x / y) = (of_real x / of_real y :: 'a::real_div_algebra)"
   347 by (simp add: divide_inverse)
   348 
   349 lemma of_real_power [simp]:
   350   "of_real (x ^ n) = (of_real x :: 'a::{real_algebra_1}) ^ n"
   351 by (induct n) simp_all
   352 
   353 lemma of_real_eq_iff [simp]: "(of_real x = of_real y) = (x = y)"
   354 by (simp add: of_real_def)
   355 
   356 lemma inj_of_real:
   357   "inj of_real"
   358   by (auto intro: injI)
   359 
   360 lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
   361 
   362 lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
   363 proof
   364   fix r
   365   show "of_real r = id r"
   366     by (simp add: of_real_def)
   367 qed
   368 
   369 text\<open>Collapse nested embeddings\<close>
   370 lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
   371 by (induct n) auto
   372 
   373 lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
   374 by (cases z rule: int_diff_cases, simp)
   375 
   376 lemma of_real_numeral [simp]: "of_real (numeral w) = numeral w"
   377 using of_real_of_int_eq [of "numeral w"] by simp
   378 
   379 lemma of_real_neg_numeral [simp]: "of_real (- numeral w) = - numeral w"
   380 using of_real_of_int_eq [of "- numeral w"] by simp
   381 
   382 text\<open>Every real algebra has characteristic zero\<close>
   383 
   384 instance real_algebra_1 < ring_char_0
   385 proof
   386   from inj_of_real inj_of_nat have "inj (of_real \<circ> of_nat)" by (rule inj_comp)
   387   then show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: comp_def)
   388 qed
   389 
   390 instance real_field < field_char_0 ..
   391 
   392 
   393 subsection \<open>The Set of Real Numbers\<close>
   394 
   395 definition Reals :: "'a::real_algebra_1 set"  ("\<real>")
   396   where "\<real> = range of_real"
   397 
   398 lemma Reals_of_real [simp]: "of_real r \<in> \<real>"
   399 by (simp add: Reals_def)
   400 
   401 lemma Reals_of_int [simp]: "of_int z \<in> \<real>"
   402 by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
   403 
   404 lemma Reals_of_nat [simp]: "of_nat n \<in> \<real>"
   405 by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
   406 
   407 lemma Reals_numeral [simp]: "numeral w \<in> \<real>"
   408 by (subst of_real_numeral [symmetric], rule Reals_of_real)
   409 
   410 lemma Reals_0 [simp]: "0 \<in> \<real>"
   411 apply (unfold Reals_def)
   412 apply (rule range_eqI)
   413 apply (rule of_real_0 [symmetric])
   414 done
   415 
   416 lemma Reals_1 [simp]: "1 \<in> \<real>"
   417 apply (unfold Reals_def)
   418 apply (rule range_eqI)
   419 apply (rule of_real_1 [symmetric])
   420 done
   421 
   422 lemma Reals_add [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a + b \<in> \<real>"
   423 apply (auto simp add: Reals_def)
   424 apply (rule range_eqI)
   425 apply (rule of_real_add [symmetric])
   426 done
   427 
   428 lemma Reals_minus [simp]: "a \<in> \<real> \<Longrightarrow> - a \<in> \<real>"
   429 apply (auto simp add: Reals_def)
   430 apply (rule range_eqI)
   431 apply (rule of_real_minus [symmetric])
   432 done
   433 
   434 lemma Reals_diff [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a - b \<in> \<real>"
   435 apply (auto simp add: Reals_def)
   436 apply (rule range_eqI)
   437 apply (rule of_real_diff [symmetric])
   438 done
   439 
   440 lemma Reals_mult [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a * b \<in> \<real>"
   441 apply (auto simp add: Reals_def)
   442 apply (rule range_eqI)
   443 apply (rule of_real_mult [symmetric])
   444 done
   445 
   446 lemma nonzero_Reals_inverse:
   447   fixes a :: "'a::real_div_algebra"
   448   shows "\<lbrakk>a \<in> \<real>; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> \<real>"
   449 apply (auto simp add: Reals_def)
   450 apply (rule range_eqI)
   451 apply (erule nonzero_of_real_inverse [symmetric])
   452 done
   453 
   454 lemma Reals_inverse:
   455   fixes a :: "'a::{real_div_algebra, division_ring}"
   456   shows "a \<in> \<real> \<Longrightarrow> inverse a \<in> \<real>"
   457 apply (auto simp add: Reals_def)
   458 apply (rule range_eqI)
   459 apply (rule of_real_inverse [symmetric])
   460 done
   461 
   462 lemma Reals_inverse_iff [simp]:
   463   fixes x:: "'a :: {real_div_algebra, division_ring}"
   464   shows "inverse x \<in> \<real> \<longleftrightarrow> x \<in> \<real>"
   465 by (metis Reals_inverse inverse_inverse_eq)
   466 
   467 lemma nonzero_Reals_divide:
   468   fixes a b :: "'a::real_field"
   469   shows "\<lbrakk>a \<in> \<real>; b \<in> \<real>; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> \<real>"
   470 apply (auto simp add: Reals_def)
   471 apply (rule range_eqI)
   472 apply (erule nonzero_of_real_divide [symmetric])
   473 done
   474 
   475 lemma Reals_divide [simp]:
   476   fixes a b :: "'a::{real_field, field}"
   477   shows "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a / b \<in> \<real>"
   478 apply (auto simp add: Reals_def)
   479 apply (rule range_eqI)
   480 apply (rule of_real_divide [symmetric])
   481 done
   482 
   483 lemma Reals_power [simp]:
   484   fixes a :: "'a::{real_algebra_1}"
   485   shows "a \<in> \<real> \<Longrightarrow> a ^ n \<in> \<real>"
   486 apply (auto simp add: Reals_def)
   487 apply (rule range_eqI)
   488 apply (rule of_real_power [symmetric])
   489 done
   490 
   491 lemma Reals_cases [cases set: Reals]:
   492   assumes "q \<in> \<real>"
   493   obtains (of_real) r where "q = of_real r"
   494   unfolding Reals_def
   495 proof -
   496   from \<open>q \<in> \<real>\<close> have "q \<in> range of_real" unfolding Reals_def .
   497   then obtain r where "q = of_real r" ..
   498   then show thesis ..
   499 qed
   500 
   501 lemma setsum_in_Reals [intro,simp]:
   502   assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setsum f s \<in> \<real>"
   503 proof (cases "finite s")
   504   case True then show ?thesis using assms
   505     by (induct s rule: finite_induct) auto
   506 next
   507   case False then show ?thesis using assms
   508     by (metis Reals_0 setsum.infinite)
   509 qed
   510 
   511 lemma setprod_in_Reals [intro,simp]:
   512   assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setprod f s \<in> \<real>"
   513 proof (cases "finite s")
   514   case True then show ?thesis using assms
   515     by (induct s rule: finite_induct) auto
   516 next
   517   case False then show ?thesis using assms
   518     by (metis Reals_1 setprod.infinite)
   519 qed
   520 
   521 lemma Reals_induct [case_names of_real, induct set: Reals]:
   522   "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
   523   by (rule Reals_cases) auto
   524 
   525 subsection \<open>Ordered real vector spaces\<close>
   526 
   527 class ordered_real_vector = real_vector + ordered_ab_group_add +
   528   assumes scaleR_left_mono: "x \<le> y \<Longrightarrow> 0 \<le> a \<Longrightarrow> a *\<^sub>R x \<le> a *\<^sub>R y"
   529   assumes scaleR_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R x"
   530 begin
   531 
   532 lemma scaleR_mono:
   533   "a \<le> b \<Longrightarrow> x \<le> y \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R y"
   534 apply (erule scaleR_right_mono [THEN order_trans], assumption)
   535 apply (erule scaleR_left_mono, assumption)
   536 done
   537 
   538 lemma scaleR_mono':
   539   "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> c \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R d"
   540   by (rule scaleR_mono) (auto intro: order.trans)
   541 
   542 lemma pos_le_divideRI:
   543   assumes "0 < c"
   544   assumes "c *\<^sub>R a \<le> b"
   545   shows "a \<le> b /\<^sub>R c"
   546 proof -
   547   from scaleR_left_mono[OF assms(2)] assms(1)
   548   have "c *\<^sub>R a /\<^sub>R c \<le> b /\<^sub>R c"
   549     by simp
   550   with assms show ?thesis
   551     by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
   552 qed
   553 
   554 lemma pos_le_divideR_eq:
   555   assumes "0 < c"
   556   shows "a \<le> b /\<^sub>R c \<longleftrightarrow> c *\<^sub>R a \<le> b"
   557 proof rule
   558   assume "a \<le> b /\<^sub>R c"
   559   from scaleR_left_mono[OF this] assms
   560   have "c *\<^sub>R a \<le> c *\<^sub>R (b /\<^sub>R c)"
   561     by simp
   562   with assms show "c *\<^sub>R a \<le> b"
   563     by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
   564 qed (rule pos_le_divideRI[OF assms])
   565 
   566 lemma scaleR_image_atLeastAtMost:
   567   "c > 0 \<Longrightarrow> scaleR c ` {x..y} = {c *\<^sub>R x..c *\<^sub>R y}"
   568   apply (auto intro!: scaleR_left_mono)
   569   apply (rule_tac x = "inverse c *\<^sub>R xa" in image_eqI)
   570   apply (simp_all add: pos_le_divideR_eq[symmetric] scaleR_scaleR scaleR_one)
   571   done
   572 
   573 end
   574 
   575 lemma neg_le_divideR_eq:
   576   fixes a :: "'a :: ordered_real_vector"
   577   assumes "c < 0"
   578   shows "a \<le> b /\<^sub>R c \<longleftrightarrow> b \<le> c *\<^sub>R a"
   579   using pos_le_divideR_eq [of "-c" a "-b"] assms
   580   by simp
   581 
   582 lemma scaleR_nonneg_nonneg: "0 \<le> a \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> 0 \<le> a *\<^sub>R x"
   583   using scaleR_left_mono [of 0 x a]
   584   by simp
   585 
   586 lemma scaleR_nonneg_nonpos: "0 \<le> a \<Longrightarrow> (x::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> a *\<^sub>R x \<le> 0"
   587   using scaleR_left_mono [of x 0 a] by simp
   588 
   589 lemma scaleR_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> a *\<^sub>R x \<le> 0"
   590   using scaleR_right_mono [of a 0 x] by simp
   591 
   592 lemma split_scaleR_neg_le: "(0 \<le> a & x \<le> 0) | (a \<le> 0 & 0 \<le> x) \<Longrightarrow>
   593   a *\<^sub>R (x::'a::ordered_real_vector) \<le> 0"
   594   by (auto simp add: scaleR_nonneg_nonpos scaleR_nonpos_nonneg)
   595 
   596 lemma le_add_iff1:
   597   fixes c d e::"'a::ordered_real_vector"
   598   shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> (a - b) *\<^sub>R e + c \<le> d"
   599   by (simp add: algebra_simps)
   600 
   601 lemma le_add_iff2:
   602   fixes c d e::"'a::ordered_real_vector"
   603   shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> c \<le> (b - a) *\<^sub>R e + d"
   604   by (simp add: algebra_simps)
   605 
   606 lemma scaleR_left_mono_neg:
   607   fixes a b::"'a::ordered_real_vector"
   608   shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b"
   609   apply (drule scaleR_left_mono [of _ _ "- c"])
   610   apply simp_all
   611   done
   612 
   613 lemma scaleR_right_mono_neg:
   614   fixes c::"'a::ordered_real_vector"
   615   shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R c"
   616   apply (drule scaleR_right_mono [of _ _ "- c"])
   617   apply simp_all
   618   done
   619 
   620 lemma scaleR_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> (b::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> 0 \<le> a *\<^sub>R b"
   621 using scaleR_right_mono_neg [of a 0 b] by simp
   622 
   623 lemma split_scaleR_pos_le:
   624   fixes b::"'a::ordered_real_vector"
   625   shows "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a *\<^sub>R b"
   626   by (auto simp add: scaleR_nonneg_nonneg scaleR_nonpos_nonpos)
   627 
   628 lemma zero_le_scaleR_iff:
   629   fixes b::"'a::ordered_real_vector"
   630   shows "0 \<le> a *\<^sub>R b \<longleftrightarrow> 0 < a \<and> 0 \<le> b \<or> a < 0 \<and> b \<le> 0 \<or> a = 0" (is "?lhs = ?rhs")
   631 proof cases
   632   assume "a \<noteq> 0"
   633   show ?thesis
   634   proof
   635     assume lhs: ?lhs
   636     {
   637       assume "0 < a"
   638       with lhs have "inverse a *\<^sub>R 0 \<le> inverse a *\<^sub>R (a *\<^sub>R b)"
   639         by (intro scaleR_mono) auto
   640       hence ?rhs using \<open>0 < a\<close>
   641         by simp
   642     } moreover {
   643       assume "0 > a"
   644       with lhs have "- inverse a *\<^sub>R 0 \<le> - inverse a *\<^sub>R (a *\<^sub>R b)"
   645         by (intro scaleR_mono) auto
   646       hence ?rhs using \<open>0 > a\<close>
   647         by simp
   648     } ultimately show ?rhs using \<open>a \<noteq> 0\<close> by arith
   649   qed (auto simp: not_le \<open>a \<noteq> 0\<close> intro!: split_scaleR_pos_le)
   650 qed simp
   651 
   652 lemma scaleR_le_0_iff:
   653   fixes b::"'a::ordered_real_vector"
   654   shows "a *\<^sub>R b \<le> 0 \<longleftrightarrow> 0 < a \<and> b \<le> 0 \<or> a < 0 \<and> 0 \<le> b \<or> a = 0"
   655   by (insert zero_le_scaleR_iff [of "-a" b]) force
   656 
   657 lemma scaleR_le_cancel_left:
   658   fixes b::"'a::ordered_real_vector"
   659   shows "c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   660   by (auto simp add: neq_iff scaleR_left_mono scaleR_left_mono_neg
   661     dest: scaleR_left_mono[where a="inverse c"] scaleR_left_mono_neg[where c="inverse c"])
   662 
   663 lemma scaleR_le_cancel_left_pos:
   664   fixes b::"'a::ordered_real_vector"
   665   shows "0 < c \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> a \<le> b"
   666   by (auto simp: scaleR_le_cancel_left)
   667 
   668 lemma scaleR_le_cancel_left_neg:
   669   fixes b::"'a::ordered_real_vector"
   670   shows "c < 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> b \<le> a"
   671   by (auto simp: scaleR_le_cancel_left)
   672 
   673 lemma scaleR_left_le_one_le:
   674   fixes x::"'a::ordered_real_vector" and a::real
   675   shows "0 \<le> x \<Longrightarrow> a \<le> 1 \<Longrightarrow> a *\<^sub>R x \<le> x"
   676   using scaleR_right_mono[of a 1 x] by simp
   677 
   678 
   679 subsection \<open>Real normed vector spaces\<close>
   680 
   681 class dist =
   682   fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
   683 
   684 class norm =
   685   fixes norm :: "'a \<Rightarrow> real"
   686 
   687 class sgn_div_norm = scaleR + norm + sgn +
   688   assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
   689 
   690 class dist_norm = dist + norm + minus +
   691   assumes dist_norm: "dist x y = norm (x - y)"
   692 
   693 class uniformity_dist = dist + uniformity +
   694   assumes uniformity_dist: "uniformity = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
   695 begin
   696 
   697 lemma eventually_uniformity_metric:
   698   "eventually P uniformity \<longleftrightarrow> (\<exists>e>0. \<forall>x y. dist x y < e \<longrightarrow> P (x, y))"
   699   unfolding uniformity_dist
   700   by (subst eventually_INF_base)
   701      (auto simp: eventually_principal subset_eq intro: bexI[of _ "min _ _"])
   702 
   703 end
   704 
   705 class real_normed_vector = real_vector + sgn_div_norm + dist_norm + uniformity_dist + open_uniformity +
   706   assumes norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
   707   and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
   708   and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   709 begin
   710 
   711 lemma norm_ge_zero [simp]: "0 \<le> norm x"
   712 proof -
   713   have "0 = norm (x + -1 *\<^sub>R x)"
   714     using scaleR_add_left[of 1 "-1" x] norm_scaleR[of 0 x] by (simp add: scaleR_one)
   715   also have "\<dots> \<le> norm x + norm (-1 *\<^sub>R x)" by (rule norm_triangle_ineq)
   716   finally show ?thesis by simp
   717 qed
   718 
   719 end
   720 
   721 class real_normed_algebra = real_algebra + real_normed_vector +
   722   assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
   723 
   724 class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   725   assumes norm_one [simp]: "norm 1 = 1"
   726 
   727 lemma (in real_normed_algebra_1) scaleR_power [simp]:
   728   "(scaleR x y) ^ n = scaleR (x^n) (y^n)"
   729   by (induction n) (simp_all add: scaleR_one scaleR_scaleR mult_ac)
   730 
   731 class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   732   assumes norm_mult: "norm (x * y) = norm x * norm y"
   733 
   734 class real_normed_field = real_field + real_normed_div_algebra
   735 
   736 instance real_normed_div_algebra < real_normed_algebra_1
   737 proof
   738   fix x y :: 'a
   739   show "norm (x * y) \<le> norm x * norm y"
   740     by (simp add: norm_mult)
   741 next
   742   have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
   743     by (rule norm_mult)
   744   thus "norm (1::'a) = 1" by simp
   745 qed
   746 
   747 lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
   748 by simp
   749 
   750 lemma zero_less_norm_iff [simp]:
   751   fixes x :: "'a::real_normed_vector"
   752   shows "(0 < norm x) = (x \<noteq> 0)"
   753 by (simp add: order_less_le)
   754 
   755 lemma norm_not_less_zero [simp]:
   756   fixes x :: "'a::real_normed_vector"
   757   shows "\<not> norm x < 0"
   758 by (simp add: linorder_not_less)
   759 
   760 lemma norm_le_zero_iff [simp]:
   761   fixes x :: "'a::real_normed_vector"
   762   shows "(norm x \<le> 0) = (x = 0)"
   763 by (simp add: order_le_less)
   764 
   765 lemma norm_minus_cancel [simp]:
   766   fixes x :: "'a::real_normed_vector"
   767   shows "norm (- x) = norm x"
   768 proof -
   769   have "norm (- x) = norm (scaleR (- 1) x)"
   770     by (simp only: scaleR_minus_left scaleR_one)
   771   also have "\<dots> = \<bar>- 1\<bar> * norm x"
   772     by (rule norm_scaleR)
   773   finally show ?thesis by simp
   774 qed
   775 
   776 lemma norm_minus_commute:
   777   fixes a b :: "'a::real_normed_vector"
   778   shows "norm (a - b) = norm (b - a)"
   779 proof -
   780   have "norm (- (b - a)) = norm (b - a)"
   781     by (rule norm_minus_cancel)
   782   thus ?thesis by simp
   783 qed
   784   
   785 lemma dist_add_cancel [simp]:
   786   fixes a :: "'a::real_normed_vector"
   787   shows "dist (a + b) (a + c) = dist b c"
   788 by (simp add: dist_norm)
   789 
   790 lemma dist_add_cancel2 [simp]:
   791   fixes a :: "'a::real_normed_vector"
   792   shows "dist (b + a) (c + a) = dist b c"
   793 by (simp add: dist_norm)
   794 
   795 lemma dist_scaleR [simp]:
   796   fixes a :: "'a::real_normed_vector"
   797   shows "dist (x *\<^sub>R a) (y *\<^sub>R a) = abs (x-y) * norm a"
   798 by (metis dist_norm norm_scaleR scaleR_left.diff)
   799 
   800 lemma norm_uminus_minus: "norm (-x - y :: 'a :: real_normed_vector) = norm (x + y)"
   801   by (subst (2) norm_minus_cancel[symmetric], subst minus_add_distrib) simp
   802 
   803 lemma norm_triangle_ineq2:
   804   fixes a b :: "'a::real_normed_vector"
   805   shows "norm a - norm b \<le> norm (a - b)"
   806 proof -
   807   have "norm (a - b + b) \<le> norm (a - b) + norm b"
   808     by (rule norm_triangle_ineq)
   809   thus ?thesis by simp
   810 qed
   811 
   812 lemma norm_triangle_ineq3:
   813   fixes a b :: "'a::real_normed_vector"
   814   shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
   815 apply (subst abs_le_iff)
   816 apply auto
   817 apply (rule norm_triangle_ineq2)
   818 apply (subst norm_minus_commute)
   819 apply (rule norm_triangle_ineq2)
   820 done
   821 
   822 lemma norm_triangle_ineq4:
   823   fixes a b :: "'a::real_normed_vector"
   824   shows "norm (a - b) \<le> norm a + norm b"
   825 proof -
   826   have "norm (a + - b) \<le> norm a + norm (- b)"
   827     by (rule norm_triangle_ineq)
   828   then show ?thesis by simp
   829 qed
   830 
   831 lemma norm_diff_ineq:
   832   fixes a b :: "'a::real_normed_vector"
   833   shows "norm a - norm b \<le> norm (a + b)"
   834 proof -
   835   have "norm a - norm (- b) \<le> norm (a - - b)"
   836     by (rule norm_triangle_ineq2)
   837   thus ?thesis by simp
   838 qed
   839 
   840 lemma norm_add_leD:
   841   fixes a b :: "'a::real_normed_vector"
   842   shows "norm (a + b) \<le> c \<Longrightarrow> norm b \<le> norm a + c"
   843     by (metis add.commute diff_le_eq norm_diff_ineq order.trans)
   844 
   845 lemma norm_diff_triangle_ineq:
   846   fixes a b c d :: "'a::real_normed_vector"
   847   shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
   848 proof -
   849   have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
   850     by (simp add: algebra_simps)
   851   also have "\<dots> \<le> norm (a - c) + norm (b - d)"
   852     by (rule norm_triangle_ineq)
   853   finally show ?thesis .
   854 qed
   855 
   856 lemma norm_diff_triangle_le:
   857   fixes x y z :: "'a::real_normed_vector"
   858   assumes "norm (x - y) \<le> e1"  "norm (y - z) \<le> e2"
   859     shows "norm (x - z) \<le> e1 + e2"
   860   using norm_diff_triangle_ineq [of x y y z] assms by simp
   861 
   862 lemma norm_diff_triangle_less:
   863   fixes x y z :: "'a::real_normed_vector"
   864   assumes "norm (x - y) < e1"  "norm (y - z) < e2"
   865     shows "norm (x - z) < e1 + e2"
   866   using norm_diff_triangle_ineq [of x y y z] assms by simp
   867 
   868 lemma norm_triangle_mono:
   869   fixes a b :: "'a::real_normed_vector"
   870   shows "\<lbrakk>norm a \<le> r; norm b \<le> s\<rbrakk> \<Longrightarrow> norm (a + b) \<le> r + s"
   871 by (metis add_mono_thms_linordered_semiring(1) norm_triangle_ineq order.trans)
   872 
   873 lemma norm_setsum:
   874   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   875   shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
   876   by (induct A rule: infinite_finite_induct) (auto intro: norm_triangle_mono)
   877 
   878 lemma setsum_norm_le:
   879   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   880   assumes fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
   881   shows "norm (setsum f S) \<le> setsum g S"
   882   by (rule order_trans [OF norm_setsum setsum_mono]) (simp add: fg)
   883 
   884 lemma abs_norm_cancel [simp]:
   885   fixes a :: "'a::real_normed_vector"
   886   shows "\<bar>norm a\<bar> = norm a"
   887 by (rule abs_of_nonneg [OF norm_ge_zero])
   888 
   889 lemma norm_add_less:
   890   fixes x y :: "'a::real_normed_vector"
   891   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
   892 by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
   893 
   894 lemma norm_mult_less:
   895   fixes x y :: "'a::real_normed_algebra"
   896   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
   897 apply (rule order_le_less_trans [OF norm_mult_ineq])
   898 apply (simp add: mult_strict_mono')
   899 done
   900 
   901 lemma norm_of_real [simp]:
   902   "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
   903 unfolding of_real_def by simp
   904 
   905 lemma norm_numeral [simp]:
   906   "norm (numeral w::'a::real_normed_algebra_1) = numeral w"
   907 by (subst of_real_numeral [symmetric], subst norm_of_real, simp)
   908 
   909 lemma norm_neg_numeral [simp]:
   910   "norm (- numeral w::'a::real_normed_algebra_1) = numeral w"
   911 by (subst of_real_neg_numeral [symmetric], subst norm_of_real, simp)
   912 
   913 lemma norm_of_real_add1 [simp]:
   914      "norm (of_real x + 1 :: 'a :: real_normed_div_algebra) = abs (x + 1)"
   915   by (metis norm_of_real of_real_1 of_real_add)
   916 
   917 lemma norm_of_real_addn [simp]:
   918      "norm (of_real x + numeral b :: 'a :: real_normed_div_algebra) = abs (x + numeral b)"
   919   by (metis norm_of_real of_real_add of_real_numeral)
   920 
   921 lemma norm_of_int [simp]:
   922   "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
   923 by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
   924 
   925 lemma norm_of_nat [simp]:
   926   "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
   927 apply (subst of_real_of_nat_eq [symmetric])
   928 apply (subst norm_of_real, simp)
   929 done
   930 
   931 lemma nonzero_norm_inverse:
   932   fixes a :: "'a::real_normed_div_algebra"
   933   shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
   934 apply (rule inverse_unique [symmetric])
   935 apply (simp add: norm_mult [symmetric])
   936 done
   937 
   938 lemma norm_inverse:
   939   fixes a :: "'a::{real_normed_div_algebra, division_ring}"
   940   shows "norm (inverse a) = inverse (norm a)"
   941 apply (case_tac "a = 0", simp)
   942 apply (erule nonzero_norm_inverse)
   943 done
   944 
   945 lemma nonzero_norm_divide:
   946   fixes a b :: "'a::real_normed_field"
   947   shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
   948 by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
   949 
   950 lemma norm_divide:
   951   fixes a b :: "'a::{real_normed_field, field}"
   952   shows "norm (a / b) = norm a / norm b"
   953 by (simp add: divide_inverse norm_mult norm_inverse)
   954 
   955 lemma norm_power_ineq:
   956   fixes x :: "'a::{real_normed_algebra_1}"
   957   shows "norm (x ^ n) \<le> norm x ^ n"
   958 proof (induct n)
   959   case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
   960 next
   961   case (Suc n)
   962   have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
   963     by (rule norm_mult_ineq)
   964   also from Suc have "\<dots> \<le> norm x * norm x ^ n"
   965     using norm_ge_zero by (rule mult_left_mono)
   966   finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
   967     by simp
   968 qed
   969 
   970 lemma norm_power:
   971   fixes x :: "'a::real_normed_div_algebra"
   972   shows "norm (x ^ n) = norm x ^ n"
   973 by (induct n) (simp_all add: norm_mult)
   974 
   975 lemma power_eq_imp_eq_norm:
   976   fixes w :: "'a::real_normed_div_algebra"
   977   assumes eq: "w ^ n = z ^ n" and "n > 0"
   978     shows "norm w = norm z"
   979 proof -
   980   have "norm w ^ n = norm z ^ n"
   981     by (metis (no_types) eq norm_power)
   982   then show ?thesis
   983     using assms by (force intro: power_eq_imp_eq_base)
   984 qed
   985 
   986 lemma norm_mult_numeral1 [simp]:
   987   fixes a b :: "'a::{real_normed_field, field}"
   988   shows "norm (numeral w * a) = numeral w * norm a"
   989 by (simp add: norm_mult)
   990 
   991 lemma norm_mult_numeral2 [simp]:
   992   fixes a b :: "'a::{real_normed_field, field}"
   993   shows "norm (a * numeral w) = norm a * numeral w"
   994 by (simp add: norm_mult)
   995 
   996 lemma norm_divide_numeral [simp]:
   997   fixes a b :: "'a::{real_normed_field, field}"
   998   shows "norm (a / numeral w) = norm a / numeral w"
   999 by (simp add: norm_divide)
  1000 
  1001 lemma norm_of_real_diff [simp]:
  1002     "norm (of_real b - of_real a :: 'a::real_normed_algebra_1) \<le> \<bar>b - a\<bar>"
  1003   by (metis norm_of_real of_real_diff order_refl)
  1004 
  1005 text\<open>Despite a superficial resemblance, \<open>norm_eq_1\<close> is not relevant.\<close>
  1006 lemma square_norm_one:
  1007   fixes x :: "'a::real_normed_div_algebra"
  1008   assumes "x^2 = 1" shows "norm x = 1"
  1009   by (metis assms norm_minus_cancel norm_one power2_eq_1_iff)
  1010 
  1011 lemma norm_less_p1:
  1012   fixes x :: "'a::real_normed_algebra_1"
  1013   shows "norm x < norm (of_real (norm x) + 1 :: 'a)"
  1014 proof -
  1015   have "norm x < norm (of_real (norm x + 1) :: 'a)"
  1016     by (simp add: of_real_def)
  1017   then show ?thesis
  1018     by simp
  1019 qed
  1020 
  1021 lemma setprod_norm:
  1022   fixes f :: "'a \<Rightarrow> 'b::{comm_semiring_1,real_normed_div_algebra}"
  1023   shows "setprod (\<lambda>x. norm(f x)) A = norm (setprod f A)"
  1024   by (induct A rule: infinite_finite_induct) (auto simp: norm_mult)
  1025 
  1026 lemma norm_setprod_le:
  1027   "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a :: 'a :: {real_normed_algebra_1, comm_monoid_mult}))"
  1028 proof (induction A rule: infinite_finite_induct)
  1029   case (insert a A)
  1030   then have "norm (setprod f (insert a A)) \<le> norm (f a) * norm (setprod f A)"
  1031     by (simp add: norm_mult_ineq)
  1032   also have "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a))"
  1033     by (rule insert)
  1034   finally show ?case
  1035     by (simp add: insert mult_left_mono)
  1036 qed simp_all
  1037 
  1038 lemma norm_setprod_diff:
  1039   fixes z w :: "'i \<Rightarrow> 'a::{real_normed_algebra_1, comm_monoid_mult}"
  1040   shows "(\<And>i. i \<in> I \<Longrightarrow> norm (z i) \<le> 1) \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> norm (w i) \<le> 1) \<Longrightarrow>
  1041     norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) \<le> (\<Sum>i\<in>I. norm (z i - w i))"
  1042 proof (induction I rule: infinite_finite_induct)
  1043   case (insert i I)
  1044   note insert.hyps[simp]
  1045 
  1046   have "norm ((\<Prod>i\<in>insert i I. z i) - (\<Prod>i\<in>insert i I. w i)) =
  1047     norm ((\<Prod>i\<in>I. z i) * (z i - w i) + ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) * w i)"
  1048     (is "_ = norm (?t1 + ?t2)")
  1049     by (auto simp add: field_simps)
  1050   also have "... \<le> norm ?t1 + norm ?t2"
  1051     by (rule norm_triangle_ineq)
  1052   also have "norm ?t1 \<le> norm (\<Prod>i\<in>I. z i) * norm (z i - w i)"
  1053     by (rule norm_mult_ineq)
  1054   also have "\<dots> \<le> (\<Prod>i\<in>I. norm (z i)) * norm(z i - w i)"
  1055     by (rule mult_right_mono) (auto intro: norm_setprod_le)
  1056   also have "(\<Prod>i\<in>I. norm (z i)) \<le> (\<Prod>i\<in>I. 1)"
  1057     by (intro setprod_mono) (auto intro!: insert)
  1058   also have "norm ?t2 \<le> norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) * norm (w i)"
  1059     by (rule norm_mult_ineq)
  1060   also have "norm (w i) \<le> 1"
  1061     by (auto intro: insert)
  1062   also have "norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) \<le> (\<Sum>i\<in>I. norm (z i - w i))"
  1063     using insert by auto
  1064   finally show ?case
  1065     by (auto simp add: ac_simps mult_right_mono mult_left_mono)
  1066 qed simp_all
  1067 
  1068 lemma norm_power_diff:
  1069   fixes z w :: "'a::{real_normed_algebra_1, comm_monoid_mult}"
  1070   assumes "norm z \<le> 1" "norm w \<le> 1"
  1071   shows "norm (z^m - w^m) \<le> m * norm (z - w)"
  1072 proof -
  1073   have "norm (z^m - w^m) = norm ((\<Prod> i < m. z) - (\<Prod> i < m. w))"
  1074     by (simp add: setprod_constant)
  1075   also have "\<dots> \<le> (\<Sum>i<m. norm (z - w))"
  1076     by (intro norm_setprod_diff) (auto simp add: assms)
  1077   also have "\<dots> = m * norm (z - w)"
  1078     by simp
  1079   finally show ?thesis .
  1080 qed
  1081 
  1082 subsection \<open>Metric spaces\<close>
  1083 
  1084 class metric_space = uniformity_dist + open_uniformity +
  1085   assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
  1086   assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
  1087 begin
  1088 
  1089 lemma dist_self [simp]: "dist x x = 0"
  1090 by simp
  1091 
  1092 lemma zero_le_dist [simp]: "0 \<le> dist x y"
  1093 using dist_triangle2 [of x x y] by simp
  1094 
  1095 lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
  1096 by (simp add: less_le)
  1097 
  1098 lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
  1099 by (simp add: not_less)
  1100 
  1101 lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
  1102 by (simp add: le_less)
  1103 
  1104 lemma dist_commute: "dist x y = dist y x"
  1105 proof (rule order_antisym)
  1106   show "dist x y \<le> dist y x"
  1107     using dist_triangle2 [of x y x] by simp
  1108   show "dist y x \<le> dist x y"
  1109     using dist_triangle2 [of y x y] by simp
  1110 qed
  1111 
  1112 lemma dist_commute_lessI: "dist y x < e \<Longrightarrow> dist x y < e"
  1113   by (simp add: dist_commute)
  1114 
  1115 lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
  1116   using dist_triangle2 [of x z y] by (simp add: dist_commute)
  1117 
  1118 lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
  1119   using dist_triangle2 [of x y a] by (simp add: dist_commute)
  1120 
  1121 lemma dist_pos_lt:
  1122   shows "x \<noteq> y ==> 0 < dist x y"
  1123 by (simp add: zero_less_dist_iff)
  1124 
  1125 lemma dist_nz:
  1126   shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
  1127 by (simp add: zero_less_dist_iff)
  1128 
  1129 declare dist_nz [symmetric, simp]
  1130 
  1131 lemma dist_triangle_le:
  1132   shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
  1133 by (rule order_trans [OF dist_triangle2])
  1134 
  1135 lemma dist_triangle_lt:
  1136   shows "dist x z + dist y z < e ==> dist x y < e"
  1137 by (rule le_less_trans [OF dist_triangle2])
  1138 
  1139 lemma dist_triangle_less_add:
  1140    "\<lbrakk>dist x1 y < e1; dist x2 y < e2\<rbrakk> \<Longrightarrow> dist x1 x2 < e1 + e2"
  1141 by (rule dist_triangle_lt [where z=y], simp)
  1142 
  1143 lemma dist_triangle_half_l:
  1144   shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
  1145 by (rule dist_triangle_lt [where z=y], simp)
  1146 
  1147 lemma dist_triangle_half_r:
  1148   shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
  1149 by (rule dist_triangle_half_l, simp_all add: dist_commute)
  1150 
  1151 subclass uniform_space
  1152 proof
  1153   fix E x assume "eventually E uniformity"
  1154   then obtain e where E: "0 < e" "\<And>x y. dist x y < e \<Longrightarrow> E (x, y)"
  1155     unfolding eventually_uniformity_metric by auto
  1156   then show "E (x, x)" "\<forall>\<^sub>F (x, y) in uniformity. E (y, x)"
  1157     unfolding eventually_uniformity_metric by (auto simp: dist_commute)
  1158 
  1159   show "\<exists>D. eventually D uniformity \<and> (\<forall>x y z. D (x, y) \<longrightarrow> D (y, z) \<longrightarrow> E (x, z))"
  1160     using E dist_triangle_half_l[where e=e] unfolding eventually_uniformity_metric
  1161     by (intro exI[of _ "\<lambda>(x, y). dist x y < e / 2"] exI[of _ "e/2"] conjI)
  1162        (auto simp: dist_commute)
  1163 qed
  1164 
  1165 lemma open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
  1166   unfolding open_uniformity eventually_uniformity_metric by (simp add: dist_commute)
  1167 
  1168 lemma open_ball: "open {y. dist x y < d}"
  1169 proof (unfold open_dist, intro ballI)
  1170   fix y assume *: "y \<in> {y. dist x y < d}"
  1171   then show "\<exists>e>0. \<forall>z. dist z y < e \<longrightarrow> z \<in> {y. dist x y < d}"
  1172     by (auto intro!: exI[of _ "d - dist x y"] simp: field_simps dist_triangle_lt)
  1173 qed
  1174 
  1175 subclass first_countable_topology
  1176 proof
  1177   fix x
  1178   show "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
  1179   proof (safe intro!: exI[of _ "\<lambda>n. {y. dist x y < inverse (Suc n)}"])
  1180     fix S assume "open S" "x \<in> S"
  1181     then obtain e where e: "0 < e" and "{y. dist x y < e} \<subseteq> S"
  1182       by (auto simp: open_dist subset_eq dist_commute)
  1183     moreover
  1184     from e obtain i where "inverse (Suc i) < e"
  1185       by (auto dest!: reals_Archimedean)
  1186     then have "{y. dist x y < inverse (Suc i)} \<subseteq> {y. dist x y < e}"
  1187       by auto
  1188     ultimately show "\<exists>i. {y. dist x y < inverse (Suc i)} \<subseteq> S"
  1189       by blast
  1190   qed (auto intro: open_ball)
  1191 qed
  1192 
  1193 end
  1194 
  1195 instance metric_space \<subseteq> t2_space
  1196 proof
  1197   fix x y :: "'a::metric_space"
  1198   assume xy: "x \<noteq> y"
  1199   let ?U = "{y'. dist x y' < dist x y / 2}"
  1200   let ?V = "{x'. dist y x' < dist x y / 2}"
  1201   have th0: "\<And>d x y z. (d x z :: real) \<le> d x y + d y z \<Longrightarrow> d y z = d z y
  1202                \<Longrightarrow> \<not>(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
  1203   have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
  1204     using dist_pos_lt[OF xy] th0[of dist, OF dist_triangle dist_commute]
  1205     using open_ball[of _ "dist x y / 2"] by auto
  1206   then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
  1207     by blast
  1208 qed
  1209 
  1210 text \<open>Every normed vector space is a metric space.\<close>
  1211 
  1212 instance real_normed_vector < metric_space
  1213 proof
  1214   fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
  1215     unfolding dist_norm by simp
  1216 next
  1217   fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
  1218     unfolding dist_norm
  1219     using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
  1220 qed
  1221 
  1222 subsection \<open>Class instances for real numbers\<close>
  1223 
  1224 instantiation real :: real_normed_field
  1225 begin
  1226 
  1227 definition dist_real_def:
  1228   "dist x y = \<bar>x - y\<bar>"
  1229 
  1230 definition uniformity_real_def [code del]:
  1231   "(uniformity :: (real \<times> real) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
  1232 
  1233 definition open_real_def [code del]:
  1234   "open (U :: real set) \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
  1235 
  1236 definition real_norm_def [simp]:
  1237   "norm r = \<bar>r\<bar>"
  1238 
  1239 instance
  1240 apply (intro_classes, unfold real_norm_def real_scaleR_def)
  1241 apply (rule dist_real_def)
  1242 apply (simp add: sgn_real_def)
  1243 apply (rule uniformity_real_def)
  1244 apply (rule open_real_def)
  1245 apply (rule abs_eq_0)
  1246 apply (rule abs_triangle_ineq)
  1247 apply (rule abs_mult)
  1248 apply (rule abs_mult)
  1249 done
  1250 
  1251 end
  1252 
  1253 declare uniformity_Abort[where 'a=real, code]
  1254 
  1255 lemma dist_of_real [simp]:
  1256   fixes a :: "'a::real_normed_div_algebra"
  1257   shows "dist (of_real x :: 'a) (of_real y) = dist x y"
  1258 by (metis dist_norm norm_of_real of_real_diff real_norm_def)
  1259 
  1260 declare [[code abort: "open :: real set \<Rightarrow> bool"]]
  1261 
  1262 instance real :: linorder_topology
  1263 proof
  1264   show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
  1265   proof (rule ext, safe)
  1266     fix S :: "real set" assume "open S"
  1267     then obtain f where "\<forall>x\<in>S. 0 < f x \<and> (\<forall>y. dist y x < f x \<longrightarrow> y \<in> S)"
  1268       unfolding open_dist bchoice_iff ..
  1269     then have *: "S = (\<Union>x\<in>S. {x - f x <..} \<inter> {..< x + f x})"
  1270       by (fastforce simp: dist_real_def)
  1271     show "generate_topology (range lessThan \<union> range greaterThan) S"
  1272       apply (subst *)
  1273       apply (intro generate_topology_Union generate_topology.Int)
  1274       apply (auto intro: generate_topology.Basis)
  1275       done
  1276   next
  1277     fix S :: "real set" assume "generate_topology (range lessThan \<union> range greaterThan) S"
  1278     moreover have "\<And>a::real. open {..<a}"
  1279       unfolding open_dist dist_real_def
  1280     proof clarify
  1281       fix x a :: real assume "x < a"
  1282       hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
  1283       thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
  1284     qed
  1285     moreover have "\<And>a::real. open {a <..}"
  1286       unfolding open_dist dist_real_def
  1287     proof clarify
  1288       fix x a :: real assume "a < x"
  1289       hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
  1290       thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
  1291     qed
  1292     ultimately show "open S"
  1293       by induct auto
  1294   qed
  1295 qed
  1296 
  1297 instance real :: linear_continuum_topology ..
  1298 
  1299 lemmas open_real_greaterThan = open_greaterThan[where 'a=real]
  1300 lemmas open_real_lessThan = open_lessThan[where 'a=real]
  1301 lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]
  1302 lemmas closed_real_atMost = closed_atMost[where 'a=real]
  1303 lemmas closed_real_atLeast = closed_atLeast[where 'a=real]
  1304 lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]
  1305 
  1306 subsection \<open>Extra type constraints\<close>
  1307 
  1308 text \<open>Only allow @{term "open"} in class \<open>topological_space\<close>.\<close>
  1309 
  1310 setup \<open>Sign.add_const_constraint
  1311   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"})\<close>
  1312 
  1313 text \<open>Only allow @{term "uniformity"} in class \<open>uniform_space\<close>.\<close>
  1314 
  1315 setup \<open>Sign.add_const_constraint
  1316   (@{const_name "uniformity"}, SOME @{typ "('a::uniformity \<times> 'a) filter"})\<close>
  1317 
  1318 text \<open>Only allow @{term dist} in class \<open>metric_space\<close>.\<close>
  1319 
  1320 setup \<open>Sign.add_const_constraint
  1321   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"})\<close>
  1322 
  1323 text \<open>Only allow @{term norm} in class \<open>real_normed_vector\<close>.\<close>
  1324 
  1325 setup \<open>Sign.add_const_constraint
  1326   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"})\<close>
  1327 
  1328 subsection \<open>Sign function\<close>
  1329 
  1330 lemma norm_sgn:
  1331   "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
  1332 by (simp add: sgn_div_norm)
  1333 
  1334 lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
  1335 by (simp add: sgn_div_norm)
  1336 
  1337 lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
  1338 by (simp add: sgn_div_norm)
  1339 
  1340 lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
  1341 by (simp add: sgn_div_norm)
  1342 
  1343 lemma sgn_scaleR:
  1344   "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
  1345 by (simp add: sgn_div_norm ac_simps)
  1346 
  1347 lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
  1348 by (simp add: sgn_div_norm)
  1349 
  1350 lemma sgn_of_real:
  1351   "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
  1352 unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
  1353 
  1354 lemma sgn_mult:
  1355   fixes x y :: "'a::real_normed_div_algebra"
  1356   shows "sgn (x * y) = sgn x * sgn y"
  1357 by (simp add: sgn_div_norm norm_mult mult.commute)
  1358 
  1359 lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
  1360   by (simp add: sgn_div_norm divide_inverse)
  1361 
  1362 lemma zero_le_sgn_iff [simp]: "0 \<le> sgn x \<longleftrightarrow> 0 \<le> (x::real)"
  1363   by (cases "0::real" x rule: linorder_cases) simp_all
  1364 
  1365 lemma sgn_le_0_iff [simp]: "sgn x \<le> 0 \<longleftrightarrow> (x::real) \<le> 0"
  1366   by (cases "0::real" x rule: linorder_cases) simp_all
  1367 
  1368 lemma norm_conv_dist: "norm x = dist x 0"
  1369   unfolding dist_norm by simp
  1370 
  1371 declare norm_conv_dist [symmetric, simp]
  1372 
  1373 lemma dist_0_norm [simp]:
  1374   fixes x :: "'a::real_normed_vector"
  1375   shows "dist 0 x = norm x"
  1376 unfolding dist_norm by simp
  1377 
  1378 lemma dist_diff [simp]: "dist a (a - b) = norm b"  "dist (a - b) a = norm b"
  1379   by (simp_all add: dist_norm)
  1380 
  1381 lemma dist_of_int: "dist (of_int m) (of_int n :: 'a :: real_normed_algebra_1) = of_int \<bar>m - n\<bar>"
  1382 proof -
  1383   have "dist (of_int m) (of_int n :: 'a) = dist (of_int m :: 'a) (of_int m - (of_int (m - n)))"
  1384     by simp
  1385   also have "\<dots> = of_int \<bar>m - n\<bar>" by (subst dist_diff, subst norm_of_int) simp
  1386   finally show ?thesis .
  1387 qed
  1388 
  1389 lemma dist_of_nat:
  1390   "dist (of_nat m) (of_nat n :: 'a :: real_normed_algebra_1) = of_int \<bar>int m - int n\<bar>"
  1391   by (subst (1 2) of_int_of_nat_eq [symmetric]) (rule dist_of_int)
  1392 
  1393 subsection \<open>Bounded Linear and Bilinear Operators\<close>
  1394 
  1395 locale linear = additive f for f :: "'a::real_vector \<Rightarrow> 'b::real_vector" +
  1396   assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
  1397 
  1398 lemma linear_imp_scaleR:
  1399   assumes "linear D" obtains d where "D = (\<lambda>x. x *\<^sub>R d)"
  1400   by (metis assms linear.scaleR mult.commute mult.left_neutral real_scaleR_def)
  1401 
  1402 corollary real_linearD:
  1403   fixes f :: "real \<Rightarrow> real"
  1404   assumes "linear f" obtains c where "f = op* c"
  1405 by (rule linear_imp_scaleR [OF assms]) (force simp: scaleR_conv_of_real)
  1406 
  1407 lemma linearI:
  1408   assumes "\<And>x y. f (x + y) = f x + f y"
  1409   assumes "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
  1410   shows "linear f"
  1411   by standard (rule assms)+
  1412 
  1413 locale bounded_linear = linear f for f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" +
  1414   assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
  1415 begin
  1416 
  1417 lemma pos_bounded:
  1418   "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
  1419 proof -
  1420   obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
  1421     using bounded by blast
  1422   show ?thesis
  1423   proof (intro exI impI conjI allI)
  1424     show "0 < max 1 K"
  1425       by (rule order_less_le_trans [OF zero_less_one max.cobounded1])
  1426   next
  1427     fix x
  1428     have "norm (f x) \<le> norm x * K" using K .
  1429     also have "\<dots> \<le> norm x * max 1 K"
  1430       by (rule mult_left_mono [OF max.cobounded2 norm_ge_zero])
  1431     finally show "norm (f x) \<le> norm x * max 1 K" .
  1432   qed
  1433 qed
  1434 
  1435 lemma nonneg_bounded:
  1436   "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
  1437 proof -
  1438   from pos_bounded
  1439   show ?thesis by (auto intro: order_less_imp_le)
  1440 qed
  1441 
  1442 lemma linear: "linear f" ..
  1443 
  1444 end
  1445 
  1446 lemma bounded_linear_intro:
  1447   assumes "\<And>x y. f (x + y) = f x + f y"
  1448   assumes "\<And>r x. f (scaleR r x) = scaleR r (f x)"
  1449   assumes "\<And>x. norm (f x) \<le> norm x * K"
  1450   shows "bounded_linear f"
  1451   by standard (blast intro: assms)+
  1452 
  1453 locale bounded_bilinear =
  1454   fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
  1455                  \<Rightarrow> 'c::real_normed_vector"
  1456     (infixl "**" 70)
  1457   assumes add_left: "prod (a + a') b = prod a b + prod a' b"
  1458   assumes add_right: "prod a (b + b') = prod a b + prod a b'"
  1459   assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
  1460   assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
  1461   assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
  1462 begin
  1463 
  1464 lemma pos_bounded:
  1465   "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1466 apply (cut_tac bounded, erule exE)
  1467 apply (rule_tac x="max 1 K" in exI, safe)
  1468 apply (rule order_less_le_trans [OF zero_less_one max.cobounded1])
  1469 apply (drule spec, drule spec, erule order_trans)
  1470 apply (rule mult_left_mono [OF max.cobounded2])
  1471 apply (intro mult_nonneg_nonneg norm_ge_zero)
  1472 done
  1473 
  1474 lemma nonneg_bounded:
  1475   "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1476 proof -
  1477   from pos_bounded
  1478   show ?thesis by (auto intro: order_less_imp_le)
  1479 qed
  1480 
  1481 lemma additive_right: "additive (\<lambda>b. prod a b)"
  1482 by (rule additive.intro, rule add_right)
  1483 
  1484 lemma additive_left: "additive (\<lambda>a. prod a b)"
  1485 by (rule additive.intro, rule add_left)
  1486 
  1487 lemma zero_left: "prod 0 b = 0"
  1488 by (rule additive.zero [OF additive_left])
  1489 
  1490 lemma zero_right: "prod a 0 = 0"
  1491 by (rule additive.zero [OF additive_right])
  1492 
  1493 lemma minus_left: "prod (- a) b = - prod a b"
  1494 by (rule additive.minus [OF additive_left])
  1495 
  1496 lemma minus_right: "prod a (- b) = - prod a b"
  1497 by (rule additive.minus [OF additive_right])
  1498 
  1499 lemma diff_left:
  1500   "prod (a - a') b = prod a b - prod a' b"
  1501 by (rule additive.diff [OF additive_left])
  1502 
  1503 lemma diff_right:
  1504   "prod a (b - b') = prod a b - prod a b'"
  1505 by (rule additive.diff [OF additive_right])
  1506 
  1507 lemma setsum_left:
  1508   "prod (setsum g S) x = setsum ((\<lambda>i. prod (g i) x)) S"
  1509 by (rule additive.setsum [OF additive_left])
  1510 
  1511 lemma setsum_right:
  1512   "prod x (setsum g S) = setsum ((\<lambda>i. (prod x (g i)))) S"
  1513 by (rule additive.setsum [OF additive_right])
  1514 
  1515 
  1516 lemma bounded_linear_left:
  1517   "bounded_linear (\<lambda>a. a ** b)"
  1518 apply (cut_tac bounded, safe)
  1519 apply (rule_tac K="norm b * K" in bounded_linear_intro)
  1520 apply (rule add_left)
  1521 apply (rule scaleR_left)
  1522 apply (simp add: ac_simps)
  1523 done
  1524 
  1525 lemma bounded_linear_right:
  1526   "bounded_linear (\<lambda>b. a ** b)"
  1527 apply (cut_tac bounded, safe)
  1528 apply (rule_tac K="norm a * K" in bounded_linear_intro)
  1529 apply (rule add_right)
  1530 apply (rule scaleR_right)
  1531 apply (simp add: ac_simps)
  1532 done
  1533 
  1534 lemma prod_diff_prod:
  1535   "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
  1536 by (simp add: diff_left diff_right)
  1537 
  1538 lemma flip: "bounded_bilinear (\<lambda>x y. y ** x)"
  1539   apply standard
  1540   apply (rule add_right)
  1541   apply (rule add_left)
  1542   apply (rule scaleR_right)
  1543   apply (rule scaleR_left)
  1544   apply (subst mult.commute)
  1545   using bounded
  1546   apply blast
  1547   done
  1548 
  1549 lemma comp1:
  1550   assumes "bounded_linear g"
  1551   shows "bounded_bilinear (\<lambda>x. op ** (g x))"
  1552 proof unfold_locales
  1553   interpret g: bounded_linear g by fact
  1554   show "\<And>a a' b. g (a + a') ** b = g a ** b + g a' ** b"
  1555     "\<And>a b b'. g a ** (b + b') = g a ** b + g a ** b'"
  1556     "\<And>r a b. g (r *\<^sub>R a) ** b = r *\<^sub>R (g a ** b)"
  1557     "\<And>a r b. g a ** (r *\<^sub>R b) = r *\<^sub>R (g a ** b)"
  1558     by (auto simp: g.add add_left add_right g.scaleR scaleR_left scaleR_right)
  1559   from g.nonneg_bounded nonneg_bounded
  1560   obtain K L
  1561   where nn: "0 \<le> K" "0 \<le> L"
  1562     and K: "\<And>x. norm (g x) \<le> norm x * K"
  1563     and L: "\<And>a b. norm (a ** b) \<le> norm a * norm b * L"
  1564     by auto
  1565   have "norm (g a ** b) \<le> norm a * K * norm b * L" for a b
  1566     by (auto intro!:  order_trans[OF K] order_trans[OF L] mult_mono simp: nn)
  1567   then show "\<exists>K. \<forall>a b. norm (g a ** b) \<le> norm a * norm b * K"
  1568     by (auto intro!: exI[where x="K * L"] simp: ac_simps)
  1569 qed
  1570 
  1571 lemma comp:
  1572   "bounded_linear f \<Longrightarrow> bounded_linear g \<Longrightarrow> bounded_bilinear (\<lambda>x y. f x ** g y)"
  1573   by (rule bounded_bilinear.flip[OF bounded_bilinear.comp1[OF bounded_bilinear.flip[OF comp1]]])
  1574 
  1575 end
  1576 
  1577 lemma bounded_linear_ident[simp]: "bounded_linear (\<lambda>x. x)"
  1578   by standard (auto intro!: exI[of _ 1])
  1579 
  1580 lemma bounded_linear_zero[simp]: "bounded_linear (\<lambda>x. 0)"
  1581   by standard (auto intro!: exI[of _ 1])
  1582 
  1583 lemma bounded_linear_add:
  1584   assumes "bounded_linear f"
  1585   assumes "bounded_linear g"
  1586   shows "bounded_linear (\<lambda>x. f x + g x)"
  1587 proof -
  1588   interpret f: bounded_linear f by fact
  1589   interpret g: bounded_linear g by fact
  1590   show ?thesis
  1591   proof
  1592     from f.bounded obtain Kf where Kf: "\<And>x. norm (f x) \<le> norm x * Kf" by blast
  1593     from g.bounded obtain Kg where Kg: "\<And>x. norm (g x) \<le> norm x * Kg" by blast
  1594     show "\<exists>K. \<forall>x. norm (f x + g x) \<le> norm x * K"
  1595       using add_mono[OF Kf Kg]
  1596       by (intro exI[of _ "Kf + Kg"]) (auto simp: field_simps intro: norm_triangle_ineq order_trans)
  1597   qed (simp_all add: f.add g.add f.scaleR g.scaleR scaleR_right_distrib)
  1598 qed
  1599 
  1600 lemma bounded_linear_minus:
  1601   assumes "bounded_linear f"
  1602   shows "bounded_linear (\<lambda>x. - f x)"
  1603 proof -
  1604   interpret f: bounded_linear f by fact
  1605   show ?thesis apply (unfold_locales)
  1606     apply (simp add: f.add)
  1607     apply (simp add: f.scaleR)
  1608     apply (simp add: f.bounded)
  1609     done
  1610 qed
  1611 
  1612 lemma bounded_linear_sub: "bounded_linear f \<Longrightarrow> bounded_linear g \<Longrightarrow> bounded_linear (\<lambda>x. f x - g x)"
  1613   using bounded_linear_add[of f "\<lambda>x. - g x"] bounded_linear_minus[of g]
  1614   by (auto simp add: algebra_simps)
  1615 
  1616 lemma bounded_linear_setsum:
  1617   fixes f :: "'i \<Rightarrow> 'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
  1618   assumes "\<And>i. i \<in> I \<Longrightarrow> bounded_linear (f i)"
  1619   shows "bounded_linear (\<lambda>x. \<Sum>i\<in>I. f i x)"
  1620 proof cases
  1621   assume "finite I"
  1622   from this show ?thesis
  1623     using assms
  1624     by (induct I) (auto intro!: bounded_linear_add)
  1625 qed simp
  1626 
  1627 lemma bounded_linear_compose:
  1628   assumes "bounded_linear f"
  1629   assumes "bounded_linear g"
  1630   shows "bounded_linear (\<lambda>x. f (g x))"
  1631 proof -
  1632   interpret f: bounded_linear f by fact
  1633   interpret g: bounded_linear g by fact
  1634   show ?thesis proof (unfold_locales)
  1635     fix x y show "f (g (x + y)) = f (g x) + f (g y)"
  1636       by (simp only: f.add g.add)
  1637   next
  1638     fix r x show "f (g (scaleR r x)) = scaleR r (f (g x))"
  1639       by (simp only: f.scaleR g.scaleR)
  1640   next
  1641     from f.pos_bounded
  1642     obtain Kf where f: "\<And>x. norm (f x) \<le> norm x * Kf" and Kf: "0 < Kf" by blast
  1643     from g.pos_bounded
  1644     obtain Kg where g: "\<And>x. norm (g x) \<le> norm x * Kg" by blast
  1645     show "\<exists>K. \<forall>x. norm (f (g x)) \<le> norm x * K"
  1646     proof (intro exI allI)
  1647       fix x
  1648       have "norm (f (g x)) \<le> norm (g x) * Kf"
  1649         using f .
  1650       also have "\<dots> \<le> (norm x * Kg) * Kf"
  1651         using g Kf [THEN order_less_imp_le] by (rule mult_right_mono)
  1652       also have "(norm x * Kg) * Kf = norm x * (Kg * Kf)"
  1653         by (rule mult.assoc)
  1654       finally show "norm (f (g x)) \<le> norm x * (Kg * Kf)" .
  1655     qed
  1656   qed
  1657 qed
  1658 
  1659 lemma bounded_bilinear_mult:
  1660   "bounded_bilinear (op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra)"
  1661 apply (rule bounded_bilinear.intro)
  1662 apply (rule distrib_right)
  1663 apply (rule distrib_left)
  1664 apply (rule mult_scaleR_left)
  1665 apply (rule mult_scaleR_right)
  1666 apply (rule_tac x="1" in exI)
  1667 apply (simp add: norm_mult_ineq)
  1668 done
  1669 
  1670 lemma bounded_linear_mult_left:
  1671   "bounded_linear (\<lambda>x::'a::real_normed_algebra. x * y)"
  1672   using bounded_bilinear_mult
  1673   by (rule bounded_bilinear.bounded_linear_left)
  1674 
  1675 lemma bounded_linear_mult_right:
  1676   "bounded_linear (\<lambda>y::'a::real_normed_algebra. x * y)"
  1677   using bounded_bilinear_mult
  1678   by (rule bounded_bilinear.bounded_linear_right)
  1679 
  1680 lemmas bounded_linear_mult_const =
  1681   bounded_linear_mult_left [THEN bounded_linear_compose]
  1682 
  1683 lemmas bounded_linear_const_mult =
  1684   bounded_linear_mult_right [THEN bounded_linear_compose]
  1685 
  1686 lemma bounded_linear_divide:
  1687   "bounded_linear (\<lambda>x::'a::real_normed_field. x / y)"
  1688   unfolding divide_inverse by (rule bounded_linear_mult_left)
  1689 
  1690 lemma bounded_bilinear_scaleR: "bounded_bilinear scaleR"
  1691 apply (rule bounded_bilinear.intro)
  1692 apply (rule scaleR_left_distrib)
  1693 apply (rule scaleR_right_distrib)
  1694 apply simp
  1695 apply (rule scaleR_left_commute)
  1696 apply (rule_tac x="1" in exI, simp)
  1697 done
  1698 
  1699 lemma bounded_linear_scaleR_left: "bounded_linear (\<lambda>r. scaleR r x)"
  1700   using bounded_bilinear_scaleR
  1701   by (rule bounded_bilinear.bounded_linear_left)
  1702 
  1703 lemma bounded_linear_scaleR_right: "bounded_linear (\<lambda>x. scaleR r x)"
  1704   using bounded_bilinear_scaleR
  1705   by (rule bounded_bilinear.bounded_linear_right)
  1706 
  1707 lemmas bounded_linear_scaleR_const =
  1708   bounded_linear_scaleR_left[THEN bounded_linear_compose]
  1709 
  1710 lemmas bounded_linear_const_scaleR =
  1711   bounded_linear_scaleR_right[THEN bounded_linear_compose]
  1712 
  1713 lemma bounded_linear_of_real: "bounded_linear (\<lambda>r. of_real r)"
  1714   unfolding of_real_def by (rule bounded_linear_scaleR_left)
  1715 
  1716 lemma real_bounded_linear:
  1717   fixes f :: "real \<Rightarrow> real"
  1718   shows "bounded_linear f \<longleftrightarrow> (\<exists>c::real. f = (\<lambda>x. x * c))"
  1719 proof -
  1720   { fix x assume "bounded_linear f"
  1721     then interpret bounded_linear f .
  1722     from scaleR[of x 1] have "f x = x * f 1"
  1723       by simp }
  1724   then show ?thesis
  1725     by (auto intro: exI[of _ "f 1"] bounded_linear_mult_left)
  1726 qed
  1727 
  1728 lemma bij_linear_imp_inv_linear:
  1729   assumes "linear f" "bij f" shows "linear (inv f)"
  1730   using assms unfolding linear_def linear_axioms_def additive_def
  1731   by (auto simp: bij_is_surj bij_is_inj surj_f_inv_f intro!:  Hilbert_Choice.inv_f_eq)
  1732 
  1733 instance real_normed_algebra_1 \<subseteq> perfect_space
  1734 proof
  1735   fix x::'a
  1736   show "\<not> open {x}"
  1737     unfolding open_dist dist_norm
  1738     by (clarsimp, rule_tac x="x + of_real (e/2)" in exI, simp)
  1739 qed
  1740 
  1741 subsection \<open>Filters and Limits on Metric Space\<close>
  1742 
  1743 lemma (in metric_space) nhds_metric: "nhds x = (INF e:{0 <..}. principal {y. dist y x < e})"
  1744   unfolding nhds_def
  1745 proof (safe intro!: INF_eq)
  1746   fix S assume "open S" "x \<in> S"
  1747   then obtain e where "{y. dist y x < e} \<subseteq> S" "0 < e"
  1748     by (auto simp: open_dist subset_eq)
  1749   then show "\<exists>e\<in>{0<..}. principal {y. dist y x < e} \<le> principal S"
  1750     by auto
  1751 qed (auto intro!: exI[of _ "{y. dist x y < e}" for e] open_ball simp: dist_commute)
  1752 
  1753 lemma (in metric_space) tendsto_iff:
  1754   "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
  1755   unfolding nhds_metric filterlim_INF filterlim_principal by auto
  1756 
  1757 lemma (in metric_space) tendstoI: "(\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F) \<Longrightarrow> (f \<longlongrightarrow> l) F"
  1758   by (auto simp: tendsto_iff)
  1759 
  1760 lemma (in metric_space) tendstoD: "(f \<longlongrightarrow> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
  1761   by (auto simp: tendsto_iff)
  1762 
  1763 lemma (in metric_space) eventually_nhds_metric:
  1764   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
  1765   unfolding nhds_metric
  1766   by (subst eventually_INF_base)
  1767      (auto simp: eventually_principal Bex_def subset_eq intro: exI[of _ "min a b" for a b])
  1768 
  1769 lemma eventually_at:
  1770   fixes a :: "'a :: metric_space"
  1771   shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
  1772   unfolding eventually_at_filter eventually_nhds_metric by auto
  1773 
  1774 lemma eventually_at_le:
  1775   fixes a :: "'a::metric_space"
  1776   shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<and> dist x a \<le> d \<longrightarrow> P x)"
  1777   unfolding eventually_at_filter eventually_nhds_metric
  1778   apply auto
  1779   apply (rule_tac x="d / 2" in exI)
  1780   apply auto
  1781   done
  1782 
  1783 lemma eventually_at_left_real: "a > (b :: real) \<Longrightarrow> eventually (\<lambda>x. x \<in> {b<..<a}) (at_left a)"
  1784   by (subst eventually_at, rule exI[of _ "a - b"]) (force simp: dist_real_def)
  1785 
  1786 lemma eventually_at_right_real: "a < (b :: real) \<Longrightarrow> eventually (\<lambda>x. x \<in> {a<..<b}) (at_right a)"
  1787   by (subst eventually_at, rule exI[of _ "b - a"]) (force simp: dist_real_def)
  1788 
  1789 lemma metric_tendsto_imp_tendsto:
  1790   fixes a :: "'a :: metric_space" and b :: "'b :: metric_space"
  1791   assumes f: "(f \<longlongrightarrow> a) F"
  1792   assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
  1793   shows "(g \<longlongrightarrow> b) F"
  1794 proof (rule tendstoI)
  1795   fix e :: real assume "0 < e"
  1796   with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
  1797   with le show "eventually (\<lambda>x. dist (g x) b < e) F"
  1798     using le_less_trans by (rule eventually_elim2)
  1799 qed
  1800 
  1801 lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
  1802   unfolding filterlim_at_top
  1803   apply (intro allI)
  1804   apply (rule_tac c="nat \<lceil>Z + 1\<rceil>" in eventually_sequentiallyI)
  1805   apply linarith
  1806   done
  1807 
  1808 
  1809 subsubsection \<open>Limits of Sequences\<close>
  1810 
  1811 lemma lim_sequentially: "X \<longlonglongrightarrow> (L::'a::metric_space) \<longleftrightarrow> (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
  1812   unfolding tendsto_iff eventually_sequentially ..
  1813 
  1814 lemmas LIMSEQ_def = lim_sequentially  (*legacy binding*)
  1815 
  1816 lemma LIMSEQ_iff_nz: "X \<longlonglongrightarrow> (L::'a::metric_space) = (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
  1817   unfolding lim_sequentially by (metis Suc_leD zero_less_Suc)
  1818 
  1819 lemma metric_LIMSEQ_I:
  1820   "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X \<longlonglongrightarrow> (L::'a::metric_space)"
  1821 by (simp add: lim_sequentially)
  1822 
  1823 lemma metric_LIMSEQ_D:
  1824   "\<lbrakk>X \<longlonglongrightarrow> (L::'a::metric_space); 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
  1825 by (simp add: lim_sequentially)
  1826 
  1827 
  1828 subsubsection \<open>Limits of Functions\<close>
  1829 
  1830 lemma LIM_def: "f \<midarrow>(a::'a::metric_space)\<rightarrow> (L::'b::metric_space) =
  1831      (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & dist x a < s
  1832         --> dist (f x) L < r)"
  1833   unfolding tendsto_iff eventually_at by simp
  1834 
  1835 lemma metric_LIM_I:
  1836   "(\<And>r. 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r)
  1837     \<Longrightarrow> f \<midarrow>(a::'a::metric_space)\<rightarrow> (L::'b::metric_space)"
  1838 by (simp add: LIM_def)
  1839 
  1840 lemma metric_LIM_D:
  1841   "\<lbrakk>f \<midarrow>(a::'a::metric_space)\<rightarrow> (L::'b::metric_space); 0 < r\<rbrakk>
  1842     \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
  1843 by (simp add: LIM_def)
  1844 
  1845 lemma metric_LIM_imp_LIM:
  1846   assumes f: "f \<midarrow>a\<rightarrow> (l::'a::metric_space)"
  1847   assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
  1848   shows "g \<midarrow>a\<rightarrow> (m::'b::metric_space)"
  1849   by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
  1850 
  1851 lemma metric_LIM_equal2:
  1852   assumes 1: "0 < R"
  1853   assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; dist x a < R\<rbrakk> \<Longrightarrow> f x = g x"
  1854   shows "g \<midarrow>a\<rightarrow> l \<Longrightarrow> f \<midarrow>(a::'a::metric_space)\<rightarrow> l"
  1855 apply (rule topological_tendstoI)
  1856 apply (drule (2) topological_tendstoD)
  1857 apply (simp add: eventually_at, safe)
  1858 apply (rule_tac x="min d R" in exI, safe)
  1859 apply (simp add: 1)
  1860 apply (simp add: 2)
  1861 done
  1862 
  1863 lemma metric_LIM_compose2:
  1864   assumes f: "f \<midarrow>(a::'a::metric_space)\<rightarrow> b"
  1865   assumes g: "g \<midarrow>b\<rightarrow> c"
  1866   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
  1867   shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> c"
  1868   using inj
  1869   by (intro tendsto_compose_eventually[OF g f]) (auto simp: eventually_at)
  1870 
  1871 lemma metric_isCont_LIM_compose2:
  1872   fixes f :: "'a :: metric_space \<Rightarrow> _"
  1873   assumes f [unfolded isCont_def]: "isCont f a"
  1874   assumes g: "g \<midarrow>f a\<rightarrow> l"
  1875   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
  1876   shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> l"
  1877 by (rule metric_LIM_compose2 [OF f g inj])
  1878 
  1879 subsection \<open>Complete metric spaces\<close>
  1880 
  1881 subsection \<open>Cauchy sequences\<close>
  1882 
  1883 lemma (in metric_space) Cauchy_def: "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e)"
  1884 proof -
  1885   have *: "eventually P (INF M. principal {(X m, X n) | n m. m \<ge> M \<and> n \<ge> M}) =
  1886     (\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. P (X m, X n))" for P
  1887   proof (subst eventually_INF_base, goal_cases)
  1888     case (2 a b) then show ?case
  1889       by (intro bexI[of _ "max a b"]) (auto simp: eventually_principal subset_eq)
  1890   qed (auto simp: eventually_principal, blast)
  1891   have "Cauchy X \<longleftrightarrow> (INF M. principal {(X m, X n) | n m. m \<ge> M \<and> n \<ge> M}) \<le> uniformity"
  1892     unfolding Cauchy_uniform_iff le_filter_def * ..
  1893   also have "\<dots> = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e)"
  1894     unfolding uniformity_dist le_INF_iff by (auto simp: * le_principal)
  1895   finally show ?thesis .
  1896 qed
  1897 
  1898 lemma (in metric_space) Cauchy_altdef:
  1899   "Cauchy f = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e)"
  1900 proof
  1901   assume A: "\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e"
  1902   show "Cauchy f" unfolding Cauchy_def
  1903   proof (intro allI impI)
  1904     fix e :: real assume e: "e > 0"
  1905     with A obtain M where M: "\<And>m n. m \<ge> M \<Longrightarrow> n > m \<Longrightarrow> dist (f m) (f n) < e" by blast
  1906     have "dist (f m) (f n) < e" if "m \<ge> M" "n \<ge> M" for m n
  1907       using M[of m n] M[of n m] e that by (cases m n rule: linorder_cases) (auto simp: dist_commute)
  1908     thus "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (f m) (f n) < e" by blast
  1909   qed
  1910 next
  1911   assume "Cauchy f"
  1912   show "\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e"
  1913   proof (intro allI impI)
  1914     fix e :: real assume e: "e > 0"
  1915     with \<open>Cauchy f\<close> obtain M where "\<And>m n. m \<ge> M \<Longrightarrow> n \<ge> M \<Longrightarrow> dist (f m) (f n) < e"
  1916       unfolding Cauchy_def by blast
  1917     thus "\<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e" by (intro exI[of _ M]) force
  1918   qed
  1919 qed
  1920 
  1921 lemma (in metric_space) metric_CauchyI:
  1922   "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
  1923   by (simp add: Cauchy_def)
  1924 
  1925 lemma (in metric_space) CauchyI': "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
  1926   unfolding Cauchy_altdef by blast
  1927 
  1928 lemma (in metric_space) metric_CauchyD:
  1929   "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
  1930   by (simp add: Cauchy_def)
  1931 
  1932 lemma (in metric_space) metric_Cauchy_iff2:
  1933   "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"
  1934 apply (simp add: Cauchy_def, auto)
  1935 apply (drule reals_Archimedean, safe)
  1936 apply (drule_tac x = n in spec, auto)
  1937 apply (rule_tac x = M in exI, auto)
  1938 apply (drule_tac x = m in spec, simp)
  1939 apply (drule_tac x = na in spec, auto)
  1940 done
  1941 
  1942 lemma Cauchy_iff2:
  1943   "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse(real (Suc j))))"
  1944   unfolding metric_Cauchy_iff2 dist_real_def ..
  1945 
  1946 lemma lim_1_over_n: "((\<lambda>n. 1 / of_nat n) \<longlongrightarrow> (0::'a::real_normed_field)) sequentially"
  1947 proof (subst lim_sequentially, intro allI impI exI)
  1948   fix e :: real assume e: "e > 0"
  1949   fix n :: nat assume n: "n \<ge> nat \<lceil>inverse e + 1\<rceil>"
  1950   have "inverse e < of_nat (nat \<lceil>inverse e + 1\<rceil>)" by linarith
  1951   also note n
  1952   finally show "dist (1 / of_nat n :: 'a) 0 < e" using e
  1953     by (simp add: divide_simps mult.commute norm_divide)
  1954 qed
  1955 
  1956 lemma (in metric_space) complete_def:
  1957   shows "complete S = (\<forall>f. (\<forall>n. f n \<in> S) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>S. f \<longlonglongrightarrow> l))"
  1958   unfolding complete_uniform
  1959 proof safe
  1960   fix f :: "nat \<Rightarrow> 'a" assume f: "\<forall>n. f n \<in> S" "Cauchy f"
  1961     and *: "\<forall>F\<le>principal S. F \<noteq> bot \<longrightarrow> cauchy_filter F \<longrightarrow> (\<exists>x\<in>S. F \<le> nhds x)"
  1962   then show "\<exists>l\<in>S. f \<longlonglongrightarrow> l"
  1963     unfolding filterlim_def using f
  1964     by (intro *[rule_format])
  1965        (auto simp: filtermap_sequentually_ne_bot le_principal eventually_filtermap Cauchy_uniform)
  1966 next
  1967   fix F :: "'a filter" assume "F \<le> principal S" "F \<noteq> bot" "cauchy_filter F"
  1968   assume seq: "\<forall>f. (\<forall>n. f n \<in> S) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>S. f \<longlonglongrightarrow> l)"
  1969 
  1970   from \<open>F \<le> principal S\<close> \<open>cauchy_filter F\<close> have FF_le: "F \<times>\<^sub>F F \<le> uniformity_on S"
  1971     by (simp add: cauchy_filter_def principal_prod_principal[symmetric] prod_filter_mono)
  1972 
  1973   let ?P = "\<lambda>P e. eventually P F \<and> (\<forall>x. P x \<longrightarrow> x \<in> S) \<and> (\<forall>x y. P x \<longrightarrow> P y \<longrightarrow> dist x y < e)"
  1974 
  1975   { fix \<epsilon> :: real assume "0 < \<epsilon>"
  1976     then have "eventually (\<lambda>(x, y). x \<in> S \<and> y \<in> S \<and> dist x y < \<epsilon>) (uniformity_on S)"
  1977       unfolding eventually_inf_principal eventually_uniformity_metric by auto
  1978     from filter_leD[OF FF_le this] have "\<exists>P. ?P P \<epsilon>"
  1979       unfolding eventually_prod_same by auto }
  1980   note P = this
  1981 
  1982   have "\<exists>P. \<forall>n. ?P (P n) (1 / Suc n) \<and> P (Suc n) \<le> P n"
  1983   proof (rule dependent_nat_choice)
  1984     show "\<exists>P. ?P P (1 / Suc 0)"
  1985       using P[of 1] by auto
  1986   next
  1987     fix P n assume "?P P (1/Suc n)"
  1988     moreover obtain Q where "?P Q (1 / Suc (Suc n))"
  1989       using P[of "1/Suc (Suc n)"] by auto
  1990     ultimately show "\<exists>Q. ?P Q (1 / Suc (Suc n)) \<and> Q \<le> P"
  1991       by (intro exI[of _ "\<lambda>x. P x \<and> Q x"]) (auto simp: eventually_conj_iff)
  1992   qed
  1993   then obtain P where P: "\<And>n. eventually (P n) F" "\<And>n x. P n x \<Longrightarrow> x \<in> S"
  1994     "\<And>n x y. P n x \<Longrightarrow> P n y \<Longrightarrow> dist x y < 1 / Suc n" "\<And>n. P (Suc n) \<le> P n"
  1995     by metis
  1996   have "antimono P"
  1997     using P(4) unfolding decseq_Suc_iff le_fun_def by blast
  1998 
  1999   obtain X where X: "\<And>n. P n (X n)"
  2000     using P(1)[THEN eventually_happens'[OF \<open>F \<noteq> bot\<close>]] by metis
  2001   have "Cauchy X"
  2002     unfolding metric_Cauchy_iff2 inverse_eq_divide
  2003   proof (intro exI allI impI)
  2004     fix j m n :: nat assume "j \<le> m" "j \<le> n"
  2005     with \<open>antimono P\<close> X have "P j (X m)" "P j (X n)"
  2006       by (auto simp: antimono_def)
  2007     then show "dist (X m) (X n) < 1 / Suc j"
  2008       by (rule P)
  2009   qed
  2010   moreover have "\<forall>n. X n \<in> S"
  2011     using P(2) X by auto
  2012   ultimately obtain x where "X \<longlonglongrightarrow> x" "x \<in> S"
  2013     using seq by blast
  2014 
  2015   show "\<exists>x\<in>S. F \<le> nhds x"
  2016   proof (rule bexI)
  2017     { fix e :: real assume "0 < e"
  2018       then have "(\<lambda>n. 1 / Suc n :: real) \<longlonglongrightarrow> 0 \<and> 0 < e / 2"
  2019         by (subst LIMSEQ_Suc_iff) (auto intro!: lim_1_over_n)
  2020       then have "\<forall>\<^sub>F n in sequentially. dist (X n) x < e / 2 \<and> 1 / Suc n < e / 2"
  2021         using \<open>X \<longlonglongrightarrow> x\<close> unfolding tendsto_iff order_tendsto_iff[where 'a=real] eventually_conj_iff by blast
  2022       then obtain n where "dist x (X n) < e / 2" "1 / Suc n < e / 2"
  2023         by (auto simp: eventually_sequentially dist_commute)
  2024       have "eventually (\<lambda>y. dist y x < e) F"
  2025         using \<open>eventually (P n) F\<close>
  2026       proof eventually_elim
  2027         fix y assume "P n y"
  2028         then have "dist y (X n) < 1 / Suc n"
  2029           by (intro X P)
  2030         also have "\<dots> < e / 2" by fact
  2031         finally show "dist y x < e"
  2032           by (rule dist_triangle_half_l) fact
  2033       qed }
  2034     then show "F \<le> nhds x"
  2035       unfolding nhds_metric le_INF_iff le_principal by auto
  2036   qed fact
  2037 qed
  2038 
  2039 lemma (in metric_space) totally_bounded_metric:
  2040   "totally_bounded S \<longleftrightarrow> (\<forall>e>0. \<exists>k. finite k \<and> S \<subseteq> (\<Union>x\<in>k. {y. dist x y < e}))"
  2041   unfolding totally_bounded_def eventually_uniformity_metric imp_ex
  2042   apply (subst all_comm)
  2043   apply (intro arg_cong[where f=All] ext)
  2044   apply safe
  2045   subgoal for e
  2046     apply (erule allE[of _ "\<lambda>(x, y). dist x y < e"])
  2047     apply auto
  2048     done
  2049   subgoal for e P k
  2050     apply (intro exI[of _ k])
  2051     apply (force simp: subset_eq)
  2052     done
  2053   done
  2054 
  2055 subsubsection \<open>Cauchy Sequences are Convergent\<close>
  2056 
  2057 (* TODO: update to uniform_space *)
  2058 class complete_space = metric_space +
  2059   assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
  2060 
  2061 lemma Cauchy_convergent_iff:
  2062   fixes X :: "nat \<Rightarrow> 'a::complete_space"
  2063   shows "Cauchy X = convergent X"
  2064 by (blast intro: Cauchy_convergent convergent_Cauchy)
  2065 
  2066 subsection \<open>The set of real numbers is a complete metric space\<close>
  2067 
  2068 text \<open>
  2069 Proof that Cauchy sequences converge based on the one from
  2070 @{url "http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html"}
  2071 \<close>
  2072 
  2073 text \<open>
  2074   If sequence @{term "X"} is Cauchy, then its limit is the lub of
  2075   @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
  2076 \<close>
  2077 
  2078 lemma increasing_LIMSEQ:
  2079   fixes f :: "nat \<Rightarrow> real"
  2080   assumes inc: "\<And>n. f n \<le> f (Suc n)"
  2081       and bdd: "\<And>n. f n \<le> l"
  2082       and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
  2083   shows "f \<longlonglongrightarrow> l"
  2084 proof (rule increasing_tendsto)
  2085   fix x assume "x < l"
  2086   with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
  2087     by auto
  2088   from en[OF \<open>0 < e\<close>] obtain n where "l - e \<le> f n"
  2089     by (auto simp: field_simps)
  2090   with \<open>e < l - x\<close> \<open>0 < e\<close> have "x < f n" by simp
  2091   with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
  2092     by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
  2093 qed (insert bdd, auto)
  2094 
  2095 lemma real_Cauchy_convergent:
  2096   fixes X :: "nat \<Rightarrow> real"
  2097   assumes X: "Cauchy X"
  2098   shows "convergent X"
  2099 proof -
  2100   define S :: "real set" where "S = {x. \<exists>N. \<forall>n\<ge>N. x < X n}"
  2101   then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto
  2102 
  2103   { fix N x assume N: "\<forall>n\<ge>N. X n < x"
  2104   fix y::real assume "y \<in> S"
  2105   hence "\<exists>M. \<forall>n\<ge>M. y < X n"
  2106     by (simp add: S_def)
  2107   then obtain M where "\<forall>n\<ge>M. y < X n" ..
  2108   hence "y < X (max M N)" by simp
  2109   also have "\<dots> < x" using N by simp
  2110   finally have "y \<le> x"
  2111     by (rule order_less_imp_le) }
  2112   note bound_isUb = this
  2113 
  2114   obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
  2115     using X[THEN metric_CauchyD, OF zero_less_one] by auto
  2116   hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
  2117   have [simp]: "S \<noteq> {}"
  2118   proof (intro exI ex_in_conv[THEN iffD1])
  2119     from N have "\<forall>n\<ge>N. X N - 1 < X n"
  2120       by (simp add: abs_diff_less_iff dist_real_def)
  2121     thus "X N - 1 \<in> S" by (rule mem_S)
  2122   qed
  2123   have [simp]: "bdd_above S"
  2124   proof
  2125     from N have "\<forall>n\<ge>N. X n < X N + 1"
  2126       by (simp add: abs_diff_less_iff dist_real_def)
  2127     thus "\<And>s. s \<in> S \<Longrightarrow>  s \<le> X N + 1"
  2128       by (rule bound_isUb)
  2129   qed
  2130   have "X \<longlonglongrightarrow> Sup S"
  2131   proof (rule metric_LIMSEQ_I)
  2132   fix r::real assume "0 < r"
  2133   hence r: "0 < r/2" by simp
  2134   obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
  2135     using metric_CauchyD [OF X r] by auto
  2136   hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
  2137   hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
  2138     by (simp only: dist_real_def abs_diff_less_iff)
  2139 
  2140   from N have "\<forall>n\<ge>N. X N - r/2 < X n" by blast
  2141   hence "X N - r/2 \<in> S" by (rule mem_S)
  2142   hence 1: "X N - r/2 \<le> Sup S" by (simp add: cSup_upper)
  2143 
  2144   from N have "\<forall>n\<ge>N. X n < X N + r/2" by blast
  2145   from bound_isUb[OF this]
  2146   have 2: "Sup S \<le> X N + r/2"
  2147     by (intro cSup_least) simp_all
  2148 
  2149   show "\<exists>N. \<forall>n\<ge>N. dist (X n) (Sup S) < r"
  2150   proof (intro exI allI impI)
  2151     fix n assume n: "N \<le> n"
  2152     from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
  2153     thus "dist (X n) (Sup S) < r" using 1 2
  2154       by (simp add: abs_diff_less_iff dist_real_def)
  2155   qed
  2156   qed
  2157   then show ?thesis unfolding convergent_def by auto
  2158 qed
  2159 
  2160 instance real :: complete_space
  2161   by intro_classes (rule real_Cauchy_convergent)
  2162 
  2163 class banach = real_normed_vector + complete_space
  2164 
  2165 instance real :: banach ..
  2166 
  2167 lemma tendsto_at_topI_sequentially:
  2168   fixes f :: "real \<Rightarrow> 'b::first_countable_topology"
  2169   assumes *: "\<And>X. filterlim X at_top sequentially \<Longrightarrow> (\<lambda>n. f (X n)) \<longlonglongrightarrow> y"
  2170   shows "(f \<longlongrightarrow> y) at_top"
  2171 proof -
  2172   from nhds_countable[of y] guess A . note A = this
  2173 
  2174   have "\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m"
  2175   proof (rule ccontr)
  2176     assume "\<not> (\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m)"
  2177     then obtain m where "\<And>k. \<exists>x\<ge>k. f x \<notin> A m"
  2178       by auto
  2179     then have "\<exists>X. \<forall>n. (f (X n) \<notin> A m) \<and> max n (X n) + 1 \<le> X (Suc n)"
  2180       by (intro dependent_nat_choice) (auto simp del: max.bounded_iff)
  2181     then obtain X where X: "\<And>n. f (X n) \<notin> A m" "\<And>n. max n (X n) + 1 \<le> X (Suc n)"
  2182       by auto
  2183     { fix n have "1 \<le> n \<longrightarrow> real n \<le> X n"
  2184         using X[of "n - 1"] by auto }
  2185     then have "filterlim X at_top sequentially"
  2186       by (force intro!: filterlim_at_top_mono[OF filterlim_real_sequentially]
  2187                 simp: eventually_sequentially)
  2188     from topological_tendstoD[OF *[OF this] A(2, 3), of m] X(1) show False
  2189       by auto
  2190   qed
  2191   then obtain k where "\<And>m x. k m \<le> x \<Longrightarrow> f x \<in> A m"
  2192     by metis
  2193   then show ?thesis
  2194     unfolding at_top_def A
  2195     by (intro filterlim_base[where i=k]) auto
  2196 qed
  2197 
  2198 lemma tendsto_at_topI_sequentially_real:
  2199   fixes f :: "real \<Rightarrow> real"
  2200   assumes mono: "mono f"
  2201   assumes limseq: "(\<lambda>n. f (real n)) \<longlonglongrightarrow> y"
  2202   shows "(f \<longlongrightarrow> y) at_top"
  2203 proof (rule tendstoI)
  2204   fix e :: real assume "0 < e"
  2205   with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e"
  2206     by (auto simp: lim_sequentially dist_real_def)
  2207   { fix x :: real
  2208     obtain n where "x \<le> real_of_nat n"
  2209       using real_arch_simple[of x] ..
  2210     note monoD[OF mono this]
  2211     also have "f (real_of_nat n) \<le> y"
  2212       by (rule LIMSEQ_le_const[OF limseq]) (auto intro!: exI[of _ n] monoD[OF mono])
  2213     finally have "f x \<le> y" . }
  2214   note le = this
  2215   have "eventually (\<lambda>x. real N \<le> x) at_top"
  2216     by (rule eventually_ge_at_top)
  2217   then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
  2218   proof eventually_elim
  2219     fix x assume N': "real N \<le> x"
  2220     with N[of N] le have "y - f (real N) < e" by auto
  2221     moreover note monoD[OF mono N']
  2222     ultimately show "dist (f x) y < e"
  2223       using le[of x] by (auto simp: dist_real_def field_simps)
  2224   qed
  2225 qed
  2226 
  2227 end