src/HOL/Real_Vector_Spaces.thy
author paulson <lp15@cam.ac.uk>
Mon Jul 27 16:52:57 2015 +0100 (2015-07-27)
changeset 60800 7d04351c795a
parent 60762 bf0c76ccee8d
child 61070 b72a990adfe2
permissions -rw-r--r--
New material for Cauchy's integral theorem
     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 real_vector_affinity_eq:
   232   fixes x :: "'a :: real_vector"
   233   assumes m0: "m \<noteq> 0"
   234   shows "m *\<^sub>R x + c = y \<longleftrightarrow> x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
   235 proof
   236   assume h: "m *\<^sub>R x + c = y"
   237   hence "m *\<^sub>R x = y - c" by (simp add: field_simps)
   238   hence "inverse m *\<^sub>R (m *\<^sub>R x) = inverse m *\<^sub>R (y - c)" by simp
   239   then show "x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
   240     using m0
   241   by (simp add: real_vector.scale_right_diff_distrib)
   242 next
   243   assume h: "x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
   244   show "m *\<^sub>R x + c = y" unfolding h
   245     using m0  by (simp add: real_vector.scale_right_diff_distrib)
   246 qed
   247 
   248 lemma real_vector_eq_affinity:
   249   fixes x :: "'a :: real_vector"
   250   shows "m \<noteq> 0 ==> (y = m *\<^sub>R x + c \<longleftrightarrow> inverse m *\<^sub>R y - (inverse m *\<^sub>R c) = x)"
   251   using real_vector_affinity_eq[where m=m and x=x and y=y and c=c]
   252   by metis
   253 
   254 
   255 subsection \<open>Embedding of the Reals into any @{text real_algebra_1}:
   256 @{term of_real}\<close>
   257 
   258 definition
   259   of_real :: "real \<Rightarrow> 'a::real_algebra_1" where
   260   "of_real r = scaleR r 1"
   261 
   262 lemma scaleR_conv_of_real: "scaleR r x = of_real r * x"
   263 by (simp add: of_real_def)
   264 
   265 lemma of_real_0 [simp]: "of_real 0 = 0"
   266 by (simp add: of_real_def)
   267 
   268 lemma of_real_1 [simp]: "of_real 1 = 1"
   269 by (simp add: of_real_def)
   270 
   271 lemma of_real_add [simp]: "of_real (x + y) = of_real x + of_real y"
   272 by (simp add: of_real_def scaleR_left_distrib)
   273 
   274 lemma of_real_minus [simp]: "of_real (- x) = - of_real x"
   275 by (simp add: of_real_def)
   276 
   277 lemma of_real_diff [simp]: "of_real (x - y) = of_real x - of_real y"
   278 by (simp add: of_real_def scaleR_left_diff_distrib)
   279 
   280 lemma of_real_mult [simp]: "of_real (x * y) = of_real x * of_real y"
   281 by (simp add: of_real_def mult.commute)
   282 
   283 lemma of_real_setsum[simp]: "of_real (setsum f s) = (\<Sum>x\<in>s. of_real (f x))"
   284   by (induct s rule: infinite_finite_induct) auto
   285 
   286 lemma of_real_setprod[simp]: "of_real (setprod f s) = (\<Prod>x\<in>s. of_real (f x))"
   287   by (induct s rule: infinite_finite_induct) auto
   288 
   289 lemma nonzero_of_real_inverse:
   290   "x \<noteq> 0 \<Longrightarrow> of_real (inverse x) =
   291    inverse (of_real x :: 'a::real_div_algebra)"
   292 by (simp add: of_real_def nonzero_inverse_scaleR_distrib)
   293 
   294 lemma of_real_inverse [simp]:
   295   "of_real (inverse x) =
   296    inverse (of_real x :: 'a::{real_div_algebra, division_ring})"
   297 by (simp add: of_real_def inverse_scaleR_distrib)
   298 
   299 lemma nonzero_of_real_divide:
   300   "y \<noteq> 0 \<Longrightarrow> of_real (x / y) =
   301    (of_real x / of_real y :: 'a::real_field)"
   302 by (simp add: divide_inverse nonzero_of_real_inverse)
   303 
   304 lemma of_real_divide [simp]:
   305   "of_real (x / y) =
   306    (of_real x / of_real y :: 'a::{real_field, field})"
   307 by (simp add: divide_inverse)
   308 
   309 lemma of_real_power [simp]:
   310   "of_real (x ^ n) = (of_real x :: 'a::{real_algebra_1}) ^ n"
   311 by (induct n) simp_all
   312 
   313 lemma of_real_eq_iff [simp]: "(of_real x = of_real y) = (x = y)"
   314 by (simp add: of_real_def)
   315 
   316 lemma inj_of_real:
   317   "inj of_real"
   318   by (auto intro: injI)
   319 
   320 lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
   321 
   322 lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
   323 proof
   324   fix r
   325   show "of_real r = id r"
   326     by (simp add: of_real_def)
   327 qed
   328 
   329 text\<open>Collapse nested embeddings\<close>
   330 lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
   331 by (induct n) auto
   332 
   333 lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
   334 by (cases z rule: int_diff_cases, simp)
   335 
   336 lemma of_real_real_of_nat_eq [simp]: "of_real (real n) = of_nat n"
   337   by (simp add: real_of_nat_def)
   338 
   339 lemma of_real_real_of_int_eq [simp]: "of_real (real z) = of_int z"
   340   by (simp add: real_of_int_def)
   341 
   342 lemma of_real_numeral [simp]: "of_real (numeral w) = numeral w"
   343 using of_real_of_int_eq [of "numeral w"] by simp
   344 
   345 lemma of_real_neg_numeral [simp]: "of_real (- numeral w) = - numeral w"
   346 using of_real_of_int_eq [of "- numeral w"] by simp
   347 
   348 text\<open>Every real algebra has characteristic zero\<close>
   349 
   350 instance real_algebra_1 < ring_char_0
   351 proof
   352   from inj_of_real inj_of_nat have "inj (of_real \<circ> of_nat)" by (rule inj_comp)
   353   then show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: comp_def)
   354 qed
   355 
   356 instance real_field < field_char_0 ..
   357 
   358 
   359 subsection \<open>The Set of Real Numbers\<close>
   360 
   361 definition Reals :: "'a::real_algebra_1 set" where
   362   "Reals = range of_real"
   363 
   364 notation (xsymbols)
   365   Reals  ("\<real>")
   366 
   367 lemma Reals_of_real [simp]: "of_real r \<in> Reals"
   368 by (simp add: Reals_def)
   369 
   370 lemma Reals_of_int [simp]: "of_int z \<in> Reals"
   371 by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
   372 
   373 lemma Reals_of_nat [simp]: "of_nat n \<in> Reals"
   374 by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
   375 
   376 lemma Reals_numeral [simp]: "numeral w \<in> Reals"
   377 by (subst of_real_numeral [symmetric], rule Reals_of_real)
   378 
   379 lemma Reals_0 [simp]: "0 \<in> Reals"
   380 apply (unfold Reals_def)
   381 apply (rule range_eqI)
   382 apply (rule of_real_0 [symmetric])
   383 done
   384 
   385 lemma Reals_1 [simp]: "1 \<in> Reals"
   386 apply (unfold Reals_def)
   387 apply (rule range_eqI)
   388 apply (rule of_real_1 [symmetric])
   389 done
   390 
   391 lemma Reals_add [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a + b \<in> Reals"
   392 apply (auto simp add: Reals_def)
   393 apply (rule range_eqI)
   394 apply (rule of_real_add [symmetric])
   395 done
   396 
   397 lemma Reals_minus [simp]: "a \<in> Reals \<Longrightarrow> - a \<in> Reals"
   398 apply (auto simp add: Reals_def)
   399 apply (rule range_eqI)
   400 apply (rule of_real_minus [symmetric])
   401 done
   402 
   403 lemma Reals_diff [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a - b \<in> Reals"
   404 apply (auto simp add: Reals_def)
   405 apply (rule range_eqI)
   406 apply (rule of_real_diff [symmetric])
   407 done
   408 
   409 lemma Reals_mult [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a * b \<in> Reals"
   410 apply (auto simp add: Reals_def)
   411 apply (rule range_eqI)
   412 apply (rule of_real_mult [symmetric])
   413 done
   414 
   415 lemma nonzero_Reals_inverse:
   416   fixes a :: "'a::real_div_algebra"
   417   shows "\<lbrakk>a \<in> Reals; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Reals"
   418 apply (auto simp add: Reals_def)
   419 apply (rule range_eqI)
   420 apply (erule nonzero_of_real_inverse [symmetric])
   421 done
   422 
   423 lemma Reals_inverse:
   424   fixes a :: "'a::{real_div_algebra, division_ring}"
   425   shows "a \<in> Reals \<Longrightarrow> inverse a \<in> Reals"
   426 apply (auto simp add: Reals_def)
   427 apply (rule range_eqI)
   428 apply (rule of_real_inverse [symmetric])
   429 done
   430 
   431 lemma Reals_inverse_iff [simp]:
   432   fixes x:: "'a :: {real_div_algebra, division_ring}"
   433   shows "inverse x \<in> \<real> \<longleftrightarrow> x \<in> \<real>"
   434 by (metis Reals_inverse inverse_inverse_eq)
   435 
   436 lemma nonzero_Reals_divide:
   437   fixes a b :: "'a::real_field"
   438   shows "\<lbrakk>a \<in> Reals; b \<in> Reals; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   439 apply (auto simp add: Reals_def)
   440 apply (rule range_eqI)
   441 apply (erule nonzero_of_real_divide [symmetric])
   442 done
   443 
   444 lemma Reals_divide [simp]:
   445   fixes a b :: "'a::{real_field, field}"
   446   shows "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   447 apply (auto simp add: Reals_def)
   448 apply (rule range_eqI)
   449 apply (rule of_real_divide [symmetric])
   450 done
   451 
   452 lemma Reals_power [simp]:
   453   fixes a :: "'a::{real_algebra_1}"
   454   shows "a \<in> Reals \<Longrightarrow> a ^ n \<in> Reals"
   455 apply (auto simp add: Reals_def)
   456 apply (rule range_eqI)
   457 apply (rule of_real_power [symmetric])
   458 done
   459 
   460 lemma Reals_cases [cases set: Reals]:
   461   assumes "q \<in> \<real>"
   462   obtains (of_real) r where "q = of_real r"
   463   unfolding Reals_def
   464 proof -
   465   from \<open>q \<in> \<real>\<close> have "q \<in> range of_real" unfolding Reals_def .
   466   then obtain r where "q = of_real r" ..
   467   then show thesis ..
   468 qed
   469 
   470 lemma setsum_in_Reals [intro,simp]:
   471   assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setsum f s \<in> \<real>"
   472 proof (cases "finite s")
   473   case True then show ?thesis using assms
   474     by (induct s rule: finite_induct) auto
   475 next
   476   case False then show ?thesis using assms
   477     by (metis Reals_0 setsum.infinite)
   478 qed
   479 
   480 lemma setprod_in_Reals [intro,simp]:
   481   assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setprod f s \<in> \<real>"
   482 proof (cases "finite s")
   483   case True then show ?thesis using assms
   484     by (induct s rule: finite_induct) auto
   485 next
   486   case False then show ?thesis using assms
   487     by (metis Reals_1 setprod.infinite)
   488 qed
   489 
   490 lemma Reals_induct [case_names of_real, induct set: Reals]:
   491   "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
   492   by (rule Reals_cases) auto
   493 
   494 subsection \<open>Ordered real vector spaces\<close>
   495 
   496 class ordered_real_vector = real_vector + ordered_ab_group_add +
   497   assumes scaleR_left_mono: "x \<le> y \<Longrightarrow> 0 \<le> a \<Longrightarrow> a *\<^sub>R x \<le> a *\<^sub>R y"
   498   assumes scaleR_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R x"
   499 begin
   500 
   501 lemma scaleR_mono:
   502   "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"
   503 apply (erule scaleR_right_mono [THEN order_trans], assumption)
   504 apply (erule scaleR_left_mono, assumption)
   505 done
   506 
   507 lemma scaleR_mono':
   508   "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"
   509   by (rule scaleR_mono) (auto intro: order.trans)
   510 
   511 lemma pos_le_divideRI:
   512   assumes "0 < c"
   513   assumes "c *\<^sub>R a \<le> b"
   514   shows "a \<le> b /\<^sub>R c"
   515 proof -
   516   from scaleR_left_mono[OF assms(2)] assms(1)
   517   have "c *\<^sub>R a /\<^sub>R c \<le> b /\<^sub>R c"
   518     by simp
   519   with assms show ?thesis
   520     by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
   521 qed
   522 
   523 lemma pos_le_divideR_eq:
   524   assumes "0 < c"
   525   shows "a \<le> b /\<^sub>R c \<longleftrightarrow> c *\<^sub>R a \<le> b"
   526 proof rule
   527   assume "a \<le> b /\<^sub>R c"
   528   from scaleR_left_mono[OF this] assms
   529   have "c *\<^sub>R a \<le> c *\<^sub>R (b /\<^sub>R c)"
   530     by simp
   531   with assms show "c *\<^sub>R a \<le> b"
   532     by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
   533 qed (rule pos_le_divideRI[OF assms])
   534 
   535 lemma scaleR_image_atLeastAtMost:
   536   "c > 0 \<Longrightarrow> scaleR c ` {x..y} = {c *\<^sub>R x..c *\<^sub>R y}"
   537   apply (auto intro!: scaleR_left_mono)
   538   apply (rule_tac x = "inverse c *\<^sub>R xa" in image_eqI)
   539   apply (simp_all add: pos_le_divideR_eq[symmetric] scaleR_scaleR scaleR_one)
   540   done
   541 
   542 end
   543 
   544 lemma neg_le_divideR_eq:
   545   fixes a :: "'a :: ordered_real_vector"
   546   assumes "c < 0"
   547   shows "a \<le> b /\<^sub>R c \<longleftrightarrow> b \<le> c *\<^sub>R a"
   548   using pos_le_divideR_eq [of "-c" a "-b"] assms
   549   by simp
   550 
   551 lemma scaleR_nonneg_nonneg: "0 \<le> a \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> 0 \<le> a *\<^sub>R x"
   552   using scaleR_left_mono [of 0 x a]
   553   by simp
   554 
   555 lemma scaleR_nonneg_nonpos: "0 \<le> a \<Longrightarrow> (x::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> a *\<^sub>R x \<le> 0"
   556   using scaleR_left_mono [of x 0 a] by simp
   557 
   558 lemma scaleR_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> a *\<^sub>R x \<le> 0"
   559   using scaleR_right_mono [of a 0 x] by simp
   560 
   561 lemma split_scaleR_neg_le: "(0 \<le> a & x \<le> 0) | (a \<le> 0 & 0 \<le> x) \<Longrightarrow>
   562   a *\<^sub>R (x::'a::ordered_real_vector) \<le> 0"
   563   by (auto simp add: scaleR_nonneg_nonpos scaleR_nonpos_nonneg)
   564 
   565 lemma le_add_iff1:
   566   fixes c d e::"'a::ordered_real_vector"
   567   shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> (a - b) *\<^sub>R e + c \<le> d"
   568   by (simp add: algebra_simps)
   569 
   570 lemma le_add_iff2:
   571   fixes c d e::"'a::ordered_real_vector"
   572   shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> c \<le> (b - a) *\<^sub>R e + d"
   573   by (simp add: algebra_simps)
   574 
   575 lemma scaleR_left_mono_neg:
   576   fixes a b::"'a::ordered_real_vector"
   577   shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b"
   578   apply (drule scaleR_left_mono [of _ _ "- c"])
   579   apply simp_all
   580   done
   581 
   582 lemma scaleR_right_mono_neg:
   583   fixes c::"'a::ordered_real_vector"
   584   shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R c"
   585   apply (drule scaleR_right_mono [of _ _ "- c"])
   586   apply simp_all
   587   done
   588 
   589 lemma scaleR_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> (b::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> 0 \<le> a *\<^sub>R b"
   590 using scaleR_right_mono_neg [of a 0 b] by simp
   591 
   592 lemma split_scaleR_pos_le:
   593   fixes b::"'a::ordered_real_vector"
   594   shows "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a *\<^sub>R b"
   595   by (auto simp add: scaleR_nonneg_nonneg scaleR_nonpos_nonpos)
   596 
   597 lemma zero_le_scaleR_iff:
   598   fixes b::"'a::ordered_real_vector"
   599   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")
   600 proof cases
   601   assume "a \<noteq> 0"
   602   show ?thesis
   603   proof
   604     assume lhs: ?lhs
   605     {
   606       assume "0 < a"
   607       with lhs have "inverse a *\<^sub>R 0 \<le> inverse a *\<^sub>R (a *\<^sub>R b)"
   608         by (intro scaleR_mono) auto
   609       hence ?rhs using \<open>0 < a\<close>
   610         by simp
   611     } moreover {
   612       assume "0 > a"
   613       with lhs have "- inverse a *\<^sub>R 0 \<le> - inverse a *\<^sub>R (a *\<^sub>R b)"
   614         by (intro scaleR_mono) auto
   615       hence ?rhs using \<open>0 > a\<close>
   616         by simp
   617     } ultimately show ?rhs using \<open>a \<noteq> 0\<close> by arith
   618   qed (auto simp: not_le \<open>a \<noteq> 0\<close> intro!: split_scaleR_pos_le)
   619 qed simp
   620 
   621 lemma scaleR_le_0_iff:
   622   fixes b::"'a::ordered_real_vector"
   623   shows "a *\<^sub>R b \<le> 0 \<longleftrightarrow> 0 < a \<and> b \<le> 0 \<or> a < 0 \<and> 0 \<le> b \<or> a = 0"
   624   by (insert zero_le_scaleR_iff [of "-a" b]) force
   625 
   626 lemma scaleR_le_cancel_left:
   627   fixes b::"'a::ordered_real_vector"
   628   shows "c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   629   by (auto simp add: neq_iff scaleR_left_mono scaleR_left_mono_neg
   630     dest: scaleR_left_mono[where a="inverse c"] scaleR_left_mono_neg[where c="inverse c"])
   631 
   632 lemma scaleR_le_cancel_left_pos:
   633   fixes b::"'a::ordered_real_vector"
   634   shows "0 < c \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> a \<le> b"
   635   by (auto simp: scaleR_le_cancel_left)
   636 
   637 lemma scaleR_le_cancel_left_neg:
   638   fixes b::"'a::ordered_real_vector"
   639   shows "c < 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> b \<le> a"
   640   by (auto simp: scaleR_le_cancel_left)
   641 
   642 lemma scaleR_left_le_one_le:
   643   fixes x::"'a::ordered_real_vector" and a::real
   644   shows "0 \<le> x \<Longrightarrow> a \<le> 1 \<Longrightarrow> a *\<^sub>R x \<le> x"
   645   using scaleR_right_mono[of a 1 x] by simp
   646 
   647 
   648 subsection \<open>Real normed vector spaces\<close>
   649 
   650 class dist =
   651   fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
   652 
   653 class norm =
   654   fixes norm :: "'a \<Rightarrow> real"
   655 
   656 class sgn_div_norm = scaleR + norm + sgn +
   657   assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
   658 
   659 class dist_norm = dist + norm + minus +
   660   assumes dist_norm: "dist x y = norm (x - y)"
   661 
   662 class open_dist = "open" + dist +
   663   assumes open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   664 
   665 class real_normed_vector = real_vector + sgn_div_norm + dist_norm + open_dist +
   666   assumes norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
   667   and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
   668   and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   669 begin
   670 
   671 lemma norm_ge_zero [simp]: "0 \<le> norm x"
   672 proof -
   673   have "0 = norm (x + -1 *\<^sub>R x)"
   674     using scaleR_add_left[of 1 "-1" x] norm_scaleR[of 0 x] by (simp add: scaleR_one)
   675   also have "\<dots> \<le> norm x + norm (-1 *\<^sub>R x)" by (rule norm_triangle_ineq)
   676   finally show ?thesis by simp
   677 qed
   678 
   679 end
   680 
   681 class real_normed_algebra = real_algebra + real_normed_vector +
   682   assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
   683 
   684 class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   685   assumes norm_one [simp]: "norm 1 = 1"
   686 
   687 class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   688   assumes norm_mult: "norm (x * y) = norm x * norm y"
   689 
   690 class real_normed_field = real_field + real_normed_div_algebra
   691 
   692 instance real_normed_div_algebra < real_normed_algebra_1
   693 proof
   694   fix x y :: 'a
   695   show "norm (x * y) \<le> norm x * norm y"
   696     by (simp add: norm_mult)
   697 next
   698   have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
   699     by (rule norm_mult)
   700   thus "norm (1::'a) = 1" by simp
   701 qed
   702 
   703 lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
   704 by simp
   705 
   706 lemma zero_less_norm_iff [simp]:
   707   fixes x :: "'a::real_normed_vector"
   708   shows "(0 < norm x) = (x \<noteq> 0)"
   709 by (simp add: order_less_le)
   710 
   711 lemma norm_not_less_zero [simp]:
   712   fixes x :: "'a::real_normed_vector"
   713   shows "\<not> norm x < 0"
   714 by (simp add: linorder_not_less)
   715 
   716 lemma norm_le_zero_iff [simp]:
   717   fixes x :: "'a::real_normed_vector"
   718   shows "(norm x \<le> 0) = (x = 0)"
   719 by (simp add: order_le_less)
   720 
   721 lemma norm_minus_cancel [simp]:
   722   fixes x :: "'a::real_normed_vector"
   723   shows "norm (- x) = norm x"
   724 proof -
   725   have "norm (- x) = norm (scaleR (- 1) x)"
   726     by (simp only: scaleR_minus_left scaleR_one)
   727   also have "\<dots> = \<bar>- 1\<bar> * norm x"
   728     by (rule norm_scaleR)
   729   finally show ?thesis by simp
   730 qed
   731 
   732 lemma norm_minus_commute:
   733   fixes a b :: "'a::real_normed_vector"
   734   shows "norm (a - b) = norm (b - a)"
   735 proof -
   736   have "norm (- (b - a)) = norm (b - a)"
   737     by (rule norm_minus_cancel)
   738   thus ?thesis by simp
   739 qed
   740 
   741 lemma norm_triangle_ineq2:
   742   fixes a b :: "'a::real_normed_vector"
   743   shows "norm a - norm b \<le> norm (a - b)"
   744 proof -
   745   have "norm (a - b + b) \<le> norm (a - b) + norm b"
   746     by (rule norm_triangle_ineq)
   747   thus ?thesis by simp
   748 qed
   749 
   750 lemma norm_triangle_ineq3:
   751   fixes a b :: "'a::real_normed_vector"
   752   shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
   753 apply (subst abs_le_iff)
   754 apply auto
   755 apply (rule norm_triangle_ineq2)
   756 apply (subst norm_minus_commute)
   757 apply (rule norm_triangle_ineq2)
   758 done
   759 
   760 lemma norm_triangle_ineq4:
   761   fixes a b :: "'a::real_normed_vector"
   762   shows "norm (a - b) \<le> norm a + norm b"
   763 proof -
   764   have "norm (a + - b) \<le> norm a + norm (- b)"
   765     by (rule norm_triangle_ineq)
   766   then show ?thesis by simp
   767 qed
   768 
   769 lemma norm_diff_ineq:
   770   fixes a b :: "'a::real_normed_vector"
   771   shows "norm a - norm b \<le> norm (a + b)"
   772 proof -
   773   have "norm a - norm (- b) \<le> norm (a - - b)"
   774     by (rule norm_triangle_ineq2)
   775   thus ?thesis by simp
   776 qed
   777 
   778 lemma norm_diff_triangle_ineq:
   779   fixes a b c d :: "'a::real_normed_vector"
   780   shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
   781 proof -
   782   have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
   783     by (simp add: algebra_simps)
   784   also have "\<dots> \<le> norm (a - c) + norm (b - d)"
   785     by (rule norm_triangle_ineq)
   786   finally show ?thesis .
   787 qed
   788 
   789 lemma norm_diff_triangle_le:
   790   fixes x y z :: "'a::real_normed_vector"
   791   assumes "norm (x - y) \<le> e1"  "norm (y - z) \<le> e2"
   792     shows "norm (x - z) \<le> e1 + e2"
   793   using norm_diff_triangle_ineq [of x y y z] assms by simp
   794 
   795 lemma norm_diff_triangle_less:
   796   fixes x y z :: "'a::real_normed_vector"
   797   assumes "norm (x - y) < e1"  "norm (y - z) < e2"
   798     shows "norm (x - z) < e1 + e2"
   799   using norm_diff_triangle_ineq [of x y y z] assms by simp
   800 
   801 lemma norm_triangle_mono:
   802   fixes a b :: "'a::real_normed_vector"
   803   shows "\<lbrakk>norm a \<le> r; norm b \<le> s\<rbrakk> \<Longrightarrow> norm (a + b) \<le> r + s"
   804 by (metis add_mono_thms_linordered_semiring(1) norm_triangle_ineq order.trans)
   805 
   806 lemma norm_setsum:
   807   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   808   shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
   809   by (induct A rule: infinite_finite_induct) (auto intro: norm_triangle_mono)
   810 
   811 lemma setsum_norm_le:
   812   fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   813   assumes fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
   814   shows "norm (setsum f S) \<le> setsum g S"
   815   by (rule order_trans [OF norm_setsum setsum_mono]) (simp add: fg)
   816 
   817 lemma abs_norm_cancel [simp]:
   818   fixes a :: "'a::real_normed_vector"
   819   shows "\<bar>norm a\<bar> = norm a"
   820 by (rule abs_of_nonneg [OF norm_ge_zero])
   821 
   822 lemma norm_add_less:
   823   fixes x y :: "'a::real_normed_vector"
   824   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
   825 by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
   826 
   827 lemma norm_mult_less:
   828   fixes x y :: "'a::real_normed_algebra"
   829   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
   830 apply (rule order_le_less_trans [OF norm_mult_ineq])
   831 apply (simp add: mult_strict_mono')
   832 done
   833 
   834 lemma norm_of_real [simp]:
   835   "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
   836 unfolding of_real_def by simp
   837 
   838 lemma norm_numeral [simp]:
   839   "norm (numeral w::'a::real_normed_algebra_1) = numeral w"
   840 by (subst of_real_numeral [symmetric], subst norm_of_real, simp)
   841 
   842 lemma norm_neg_numeral [simp]:
   843   "norm (- numeral w::'a::real_normed_algebra_1) = numeral w"
   844 by (subst of_real_neg_numeral [symmetric], subst norm_of_real, simp)
   845 
   846 lemma norm_of_int [simp]:
   847   "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
   848 by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
   849 
   850 lemma norm_of_nat [simp]:
   851   "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
   852 apply (subst of_real_of_nat_eq [symmetric])
   853 apply (subst norm_of_real, simp)
   854 done
   855 
   856 lemma nonzero_norm_inverse:
   857   fixes a :: "'a::real_normed_div_algebra"
   858   shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
   859 apply (rule inverse_unique [symmetric])
   860 apply (simp add: norm_mult [symmetric])
   861 done
   862 
   863 lemma norm_inverse:
   864   fixes a :: "'a::{real_normed_div_algebra, division_ring}"
   865   shows "norm (inverse a) = inverse (norm a)"
   866 apply (case_tac "a = 0", simp)
   867 apply (erule nonzero_norm_inverse)
   868 done
   869 
   870 lemma nonzero_norm_divide:
   871   fixes a b :: "'a::real_normed_field"
   872   shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
   873 by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
   874 
   875 lemma norm_divide:
   876   fixes a b :: "'a::{real_normed_field, field}"
   877   shows "norm (a / b) = norm a / norm b"
   878 by (simp add: divide_inverse norm_mult norm_inverse)
   879 
   880 lemma norm_power_ineq:
   881   fixes x :: "'a::{real_normed_algebra_1}"
   882   shows "norm (x ^ n) \<le> norm x ^ n"
   883 proof (induct n)
   884   case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
   885 next
   886   case (Suc n)
   887   have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
   888     by (rule norm_mult_ineq)
   889   also from Suc have "\<dots> \<le> norm x * norm x ^ n"
   890     using norm_ge_zero by (rule mult_left_mono)
   891   finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
   892     by simp
   893 qed
   894 
   895 lemma norm_power:
   896   fixes x :: "'a::{real_normed_div_algebra}"
   897   shows "norm (x ^ n) = norm x ^ n"
   898 by (induct n) (simp_all add: norm_mult)
   899 
   900 lemma norm_mult_numeral1 [simp]:
   901   fixes a b :: "'a::{real_normed_field, field}"
   902   shows "norm (numeral w * a) = numeral w * norm a"
   903 by (simp add: norm_mult)
   904 
   905 lemma norm_mult_numeral2 [simp]:
   906   fixes a b :: "'a::{real_normed_field, field}"
   907   shows "norm (a * numeral w) = norm a * numeral w"
   908 by (simp add: norm_mult)
   909 
   910 lemma norm_divide_numeral [simp]:
   911   fixes a b :: "'a::{real_normed_field, field}"
   912   shows "norm (a / numeral w) = norm a / numeral w"
   913 by (simp add: norm_divide)
   914 
   915 lemma norm_of_real_diff [simp]:
   916     "norm (of_real b - of_real a :: 'a::real_normed_algebra_1) \<le> \<bar>b - a\<bar>"
   917   by (metis norm_of_real of_real_diff order_refl)
   918 
   919 text\<open>Despite a superficial resemblance, @{text norm_eq_1} is not relevant.\<close>
   920 lemma square_norm_one:
   921   fixes x :: "'a::real_normed_div_algebra"
   922   assumes "x^2 = 1" shows "norm x = 1"
   923   by (metis assms norm_minus_cancel norm_one power2_eq_1_iff)
   924 
   925 lemma norm_less_p1:
   926   fixes x :: "'a::real_normed_algebra_1"
   927   shows "norm x < norm (of_real (norm x) + 1 :: 'a)"
   928 proof -
   929   have "norm x < norm (of_real (norm x + 1) :: 'a)"
   930     by (simp add: of_real_def)
   931   then show ?thesis
   932     by simp
   933 qed
   934 
   935 lemma setprod_norm:
   936   fixes f :: "'a \<Rightarrow> 'b::{comm_semiring_1,real_normed_div_algebra}"
   937   shows "setprod (\<lambda>x. norm(f x)) A = norm (setprod f A)"
   938   by (induct A rule: infinite_finite_induct) (auto simp: norm_mult)
   939 
   940 lemma norm_setprod_le:
   941   "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a :: 'a :: {real_normed_algebra_1, comm_monoid_mult}))"
   942 proof (induction A rule: infinite_finite_induct)
   943   case (insert a A)
   944   then have "norm (setprod f (insert a A)) \<le> norm (f a) * norm (setprod f A)"
   945     by (simp add: norm_mult_ineq)
   946   also have "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a))"
   947     by (rule insert)
   948   finally show ?case
   949     by (simp add: insert mult_left_mono)
   950 qed simp_all
   951 
   952 lemma norm_setprod_diff:
   953   fixes z w :: "'i \<Rightarrow> 'a::{real_normed_algebra_1, comm_monoid_mult}"
   954   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>
   955     norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) \<le> (\<Sum>i\<in>I. norm (z i - w i))"
   956 proof (induction I rule: infinite_finite_induct)
   957   case (insert i I)
   958   note insert.hyps[simp]
   959 
   960   have "norm ((\<Prod>i\<in>insert i I. z i) - (\<Prod>i\<in>insert i I. w i)) =
   961     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)"
   962     (is "_ = norm (?t1 + ?t2)")
   963     by (auto simp add: field_simps)
   964   also have "... \<le> norm ?t1 + norm ?t2"
   965     by (rule norm_triangle_ineq)
   966   also have "norm ?t1 \<le> norm (\<Prod>i\<in>I. z i) * norm (z i - w i)"
   967     by (rule norm_mult_ineq)
   968   also have "\<dots> \<le> (\<Prod>i\<in>I. norm (z i)) * norm(z i - w i)"
   969     by (rule mult_right_mono) (auto intro: norm_setprod_le)
   970   also have "(\<Prod>i\<in>I. norm (z i)) \<le> (\<Prod>i\<in>I. 1)"
   971     by (intro setprod_mono) (auto intro!: insert)
   972   also have "norm ?t2 \<le> norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) * norm (w i)"
   973     by (rule norm_mult_ineq)
   974   also have "norm (w i) \<le> 1"
   975     by (auto intro: insert)
   976   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))"
   977     using insert by auto
   978   finally show ?case
   979     by (auto simp add: ac_simps mult_right_mono mult_left_mono)
   980 qed simp_all
   981 
   982 lemma norm_power_diff:
   983   fixes z w :: "'a::{real_normed_algebra_1, comm_monoid_mult}"
   984   assumes "norm z \<le> 1" "norm w \<le> 1"
   985   shows "norm (z^m - w^m) \<le> m * norm (z - w)"
   986 proof -
   987   have "norm (z^m - w^m) = norm ((\<Prod> i < m. z) - (\<Prod> i < m. w))"
   988     by (simp add: setprod_constant)
   989   also have "\<dots> \<le> (\<Sum>i<m. norm (z - w))"
   990     by (intro norm_setprod_diff) (auto simp add: assms)
   991   also have "\<dots> = m * norm (z - w)"
   992     by (simp add: real_of_nat_def)
   993   finally show ?thesis .
   994 qed
   995 
   996 subsection \<open>Metric spaces\<close>
   997 
   998 class metric_space = open_dist +
   999   assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
  1000   assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
  1001 begin
  1002 
  1003 lemma dist_self [simp]: "dist x x = 0"
  1004 by simp
  1005 
  1006 lemma zero_le_dist [simp]: "0 \<le> dist x y"
  1007 using dist_triangle2 [of x x y] by simp
  1008 
  1009 lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
  1010 by (simp add: less_le)
  1011 
  1012 lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
  1013 by (simp add: not_less)
  1014 
  1015 lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
  1016 by (simp add: le_less)
  1017 
  1018 lemma dist_commute: "dist x y = dist y x"
  1019 proof (rule order_antisym)
  1020   show "dist x y \<le> dist y x"
  1021     using dist_triangle2 [of x y x] by simp
  1022   show "dist y x \<le> dist x y"
  1023     using dist_triangle2 [of y x y] by simp
  1024 qed
  1025 
  1026 lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
  1027 using dist_triangle2 [of x z y] by (simp add: dist_commute)
  1028 
  1029 lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
  1030 using dist_triangle2 [of x y a] by (simp add: dist_commute)
  1031 
  1032 lemma dist_triangle_alt:
  1033   shows "dist y z <= dist x y + dist x z"
  1034 by (rule dist_triangle3)
  1035 
  1036 lemma dist_pos_lt:
  1037   shows "x \<noteq> y ==> 0 < dist x y"
  1038 by (simp add: zero_less_dist_iff)
  1039 
  1040 lemma dist_nz:
  1041   shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
  1042 by (simp add: zero_less_dist_iff)
  1043 
  1044 lemma dist_triangle_le:
  1045   shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
  1046 by (rule order_trans [OF dist_triangle2])
  1047 
  1048 lemma dist_triangle_lt:
  1049   shows "dist x z + dist y z < e ==> dist x y < e"
  1050 by (rule le_less_trans [OF dist_triangle2])
  1051 
  1052 lemma dist_triangle_half_l:
  1053   shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
  1054 by (rule dist_triangle_lt [where z=y], simp)
  1055 
  1056 lemma dist_triangle_half_r:
  1057   shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
  1058 by (rule dist_triangle_half_l, simp_all add: dist_commute)
  1059 
  1060 subclass topological_space
  1061 proof
  1062   have "\<exists>e::real. 0 < e"
  1063     by (fast intro: zero_less_one)
  1064   then show "open UNIV"
  1065     unfolding open_dist by simp
  1066 next
  1067   fix S T assume "open S" "open T"
  1068   then show "open (S \<inter> T)"
  1069     unfolding open_dist
  1070     apply clarify
  1071     apply (drule (1) bspec)+
  1072     apply (clarify, rename_tac r s)
  1073     apply (rule_tac x="min r s" in exI, simp)
  1074     done
  1075 next
  1076   fix K assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
  1077     unfolding open_dist by fast
  1078 qed
  1079 
  1080 lemma open_ball: "open {y. dist x y < d}"
  1081 proof (unfold open_dist, intro ballI)
  1082   fix y assume *: "y \<in> {y. dist x y < d}"
  1083   then show "\<exists>e>0. \<forall>z. dist z y < e \<longrightarrow> z \<in> {y. dist x y < d}"
  1084     by (auto intro!: exI[of _ "d - dist x y"] simp: field_simps dist_triangle_lt)
  1085 qed
  1086 
  1087 subclass first_countable_topology
  1088 proof
  1089   fix x
  1090   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))"
  1091   proof (safe intro!: exI[of _ "\<lambda>n. {y. dist x y < inverse (Suc n)}"])
  1092     fix S assume "open S" "x \<in> S"
  1093     then obtain e where e: "0 < e" and "{y. dist x y < e} \<subseteq> S"
  1094       by (auto simp: open_dist subset_eq dist_commute)
  1095     moreover
  1096     from e obtain i where "inverse (Suc i) < e"
  1097       by (auto dest!: reals_Archimedean)
  1098     then have "{y. dist x y < inverse (Suc i)} \<subseteq> {y. dist x y < e}"
  1099       by auto
  1100     ultimately show "\<exists>i. {y. dist x y < inverse (Suc i)} \<subseteq> S"
  1101       by blast
  1102   qed (auto intro: open_ball)
  1103 qed
  1104 
  1105 end
  1106 
  1107 instance metric_space \<subseteq> t2_space
  1108 proof
  1109   fix x y :: "'a::metric_space"
  1110   assume xy: "x \<noteq> y"
  1111   let ?U = "{y'. dist x y' < dist x y / 2}"
  1112   let ?V = "{x'. dist y x' < dist x y / 2}"
  1113   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
  1114                \<Longrightarrow> \<not>(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
  1115   have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
  1116     using dist_pos_lt[OF xy] th0[of dist, OF dist_triangle dist_commute]
  1117     using open_ball[of _ "dist x y / 2"] by auto
  1118   then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
  1119     by blast
  1120 qed
  1121 
  1122 text \<open>Every normed vector space is a metric space.\<close>
  1123 
  1124 instance real_normed_vector < metric_space
  1125 proof
  1126   fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
  1127     unfolding dist_norm by simp
  1128 next
  1129   fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
  1130     unfolding dist_norm
  1131     using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
  1132 qed
  1133 
  1134 subsection \<open>Class instances for real numbers\<close>
  1135 
  1136 instantiation real :: real_normed_field
  1137 begin
  1138 
  1139 definition dist_real_def:
  1140   "dist x y = \<bar>x - y\<bar>"
  1141 
  1142 definition open_real_def [code del]:
  1143   "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
  1144 
  1145 definition real_norm_def [simp]:
  1146   "norm r = \<bar>r\<bar>"
  1147 
  1148 instance
  1149 apply (intro_classes, unfold real_norm_def real_scaleR_def)
  1150 apply (rule dist_real_def)
  1151 apply (rule open_real_def)
  1152 apply (simp add: sgn_real_def)
  1153 apply (rule abs_eq_0)
  1154 apply (rule abs_triangle_ineq)
  1155 apply (rule abs_mult)
  1156 apply (rule abs_mult)
  1157 done
  1158 
  1159 end
  1160 
  1161 lemma dist_of_real [simp]:
  1162   fixes a :: "'a::real_normed_div_algebra"
  1163   shows "dist (of_real x :: 'a) (of_real y) = dist x y"
  1164 by (metis dist_norm norm_of_real of_real_diff real_norm_def)
  1165 
  1166 declare [[code abort: "open :: real set \<Rightarrow> bool"]]
  1167 
  1168 instance real :: linorder_topology
  1169 proof
  1170   show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
  1171   proof (rule ext, safe)
  1172     fix S :: "real set" assume "open S"
  1173     then obtain f where "\<forall>x\<in>S. 0 < f x \<and> (\<forall>y. dist y x < f x \<longrightarrow> y \<in> S)"
  1174       unfolding open_real_def bchoice_iff ..
  1175     then have *: "S = (\<Union>x\<in>S. {x - f x <..} \<inter> {..< x + f x})"
  1176       by (fastforce simp: dist_real_def)
  1177     show "generate_topology (range lessThan \<union> range greaterThan) S"
  1178       apply (subst *)
  1179       apply (intro generate_topology_Union generate_topology.Int)
  1180       apply (auto intro: generate_topology.Basis)
  1181       done
  1182   next
  1183     fix S :: "real set" assume "generate_topology (range lessThan \<union> range greaterThan) S"
  1184     moreover have "\<And>a::real. open {..<a}"
  1185       unfolding open_real_def dist_real_def
  1186     proof clarify
  1187       fix x a :: real assume "x < a"
  1188       hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
  1189       thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
  1190     qed
  1191     moreover have "\<And>a::real. open {a <..}"
  1192       unfolding open_real_def dist_real_def
  1193     proof clarify
  1194       fix x a :: real assume "a < x"
  1195       hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
  1196       thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
  1197     qed
  1198     ultimately show "open S"
  1199       by induct auto
  1200   qed
  1201 qed
  1202 
  1203 instance real :: linear_continuum_topology ..
  1204 
  1205 lemmas open_real_greaterThan = open_greaterThan[where 'a=real]
  1206 lemmas open_real_lessThan = open_lessThan[where 'a=real]
  1207 lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]
  1208 lemmas closed_real_atMost = closed_atMost[where 'a=real]
  1209 lemmas closed_real_atLeast = closed_atLeast[where 'a=real]
  1210 lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]
  1211 
  1212 subsection \<open>Extra type constraints\<close>
  1213 
  1214 text \<open>Only allow @{term "open"} in class @{text topological_space}.\<close>
  1215 
  1216 setup \<open>Sign.add_const_constraint
  1217   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"})\<close>
  1218 
  1219 text \<open>Only allow @{term dist} in class @{text metric_space}.\<close>
  1220 
  1221 setup \<open>Sign.add_const_constraint
  1222   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"})\<close>
  1223 
  1224 text \<open>Only allow @{term norm} in class @{text real_normed_vector}.\<close>
  1225 
  1226 setup \<open>Sign.add_const_constraint
  1227   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"})\<close>
  1228 
  1229 subsection \<open>Sign function\<close>
  1230 
  1231 lemma norm_sgn:
  1232   "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
  1233 by (simp add: sgn_div_norm)
  1234 
  1235 lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
  1236 by (simp add: sgn_div_norm)
  1237 
  1238 lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
  1239 by (simp add: sgn_div_norm)
  1240 
  1241 lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
  1242 by (simp add: sgn_div_norm)
  1243 
  1244 lemma sgn_scaleR:
  1245   "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
  1246 by (simp add: sgn_div_norm ac_simps)
  1247 
  1248 lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
  1249 by (simp add: sgn_div_norm)
  1250 
  1251 lemma sgn_of_real:
  1252   "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
  1253 unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
  1254 
  1255 lemma sgn_mult:
  1256   fixes x y :: "'a::real_normed_div_algebra"
  1257   shows "sgn (x * y) = sgn x * sgn y"
  1258 by (simp add: sgn_div_norm norm_mult mult.commute)
  1259 
  1260 lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
  1261 by (simp add: sgn_div_norm divide_inverse)
  1262 
  1263 lemma real_sgn_pos: "0 < (x::real) \<Longrightarrow> sgn x = 1"
  1264 unfolding real_sgn_eq by simp
  1265 
  1266 lemma real_sgn_neg: "(x::real) < 0 \<Longrightarrow> sgn x = -1"
  1267 unfolding real_sgn_eq by simp
  1268 
  1269 lemma zero_le_sgn_iff [simp]: "0 \<le> sgn x \<longleftrightarrow> 0 \<le> (x::real)"
  1270   by (cases "0::real" x rule: linorder_cases) simp_all
  1271 
  1272 lemma zero_less_sgn_iff [simp]: "0 < sgn x \<longleftrightarrow> 0 < (x::real)"
  1273   by (cases "0::real" x rule: linorder_cases) simp_all
  1274 
  1275 lemma sgn_le_0_iff [simp]: "sgn x \<le> 0 \<longleftrightarrow> (x::real) \<le> 0"
  1276   by (cases "0::real" x rule: linorder_cases) simp_all
  1277 
  1278 lemma sgn_less_0_iff [simp]: "sgn x < 0 \<longleftrightarrow> (x::real) < 0"
  1279   by (cases "0::real" x rule: linorder_cases) simp_all
  1280 
  1281 lemma norm_conv_dist: "norm x = dist x 0"
  1282   unfolding dist_norm by simp
  1283 
  1284 lemma dist_diff [simp]: "dist a (a - b) = norm b"  "dist (a - b) a = norm b"
  1285   by (simp_all add: dist_norm)
  1286   
  1287 subsection \<open>Bounded Linear and Bilinear Operators\<close>
  1288 
  1289 locale linear = additive f for f :: "'a::real_vector \<Rightarrow> 'b::real_vector" +
  1290   assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
  1291 
  1292 lemma linear_imp_scaleR:
  1293   assumes "linear D" obtains d where "D = (\<lambda>x. x *\<^sub>R d)"
  1294   by (metis assms linear.scaleR mult.commute mult.left_neutral real_scaleR_def)
  1295 
  1296 lemma linearI:
  1297   assumes "\<And>x y. f (x + y) = f x + f y"
  1298   assumes "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
  1299   shows "linear f"
  1300   by default (rule assms)+
  1301 
  1302 locale bounded_linear = linear f for f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" +
  1303   assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
  1304 begin
  1305 
  1306 lemma pos_bounded:
  1307   "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
  1308 proof -
  1309   obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
  1310     using bounded by fast
  1311   show ?thesis
  1312   proof (intro exI impI conjI allI)
  1313     show "0 < max 1 K"
  1314       by (rule order_less_le_trans [OF zero_less_one max.cobounded1])
  1315   next
  1316     fix x
  1317     have "norm (f x) \<le> norm x * K" using K .
  1318     also have "\<dots> \<le> norm x * max 1 K"
  1319       by (rule mult_left_mono [OF max.cobounded2 norm_ge_zero])
  1320     finally show "norm (f x) \<le> norm x * max 1 K" .
  1321   qed
  1322 qed
  1323 
  1324 lemma nonneg_bounded:
  1325   "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
  1326 proof -
  1327   from pos_bounded
  1328   show ?thesis by (auto intro: order_less_imp_le)
  1329 qed
  1330 
  1331 lemma linear: "linear f" ..
  1332 
  1333 end
  1334 
  1335 lemma bounded_linear_intro:
  1336   assumes "\<And>x y. f (x + y) = f x + f y"
  1337   assumes "\<And>r x. f (scaleR r x) = scaleR r (f x)"
  1338   assumes "\<And>x. norm (f x) \<le> norm x * K"
  1339   shows "bounded_linear f"
  1340   by default (fast intro: assms)+
  1341 
  1342 locale bounded_bilinear =
  1343   fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
  1344                  \<Rightarrow> 'c::real_normed_vector"
  1345     (infixl "**" 70)
  1346   assumes add_left: "prod (a + a') b = prod a b + prod a' b"
  1347   assumes add_right: "prod a (b + b') = prod a b + prod a b'"
  1348   assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
  1349   assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
  1350   assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
  1351 begin
  1352 
  1353 lemma pos_bounded:
  1354   "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1355 apply (cut_tac bounded, erule exE)
  1356 apply (rule_tac x="max 1 K" in exI, safe)
  1357 apply (rule order_less_le_trans [OF zero_less_one max.cobounded1])
  1358 apply (drule spec, drule spec, erule order_trans)
  1359 apply (rule mult_left_mono [OF max.cobounded2])
  1360 apply (intro mult_nonneg_nonneg norm_ge_zero)
  1361 done
  1362 
  1363 lemma nonneg_bounded:
  1364   "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1365 proof -
  1366   from pos_bounded
  1367   show ?thesis by (auto intro: order_less_imp_le)
  1368 qed
  1369 
  1370 lemma additive_right: "additive (\<lambda>b. prod a b)"
  1371 by (rule additive.intro, rule add_right)
  1372 
  1373 lemma additive_left: "additive (\<lambda>a. prod a b)"
  1374 by (rule additive.intro, rule add_left)
  1375 
  1376 lemma zero_left: "prod 0 b = 0"
  1377 by (rule additive.zero [OF additive_left])
  1378 
  1379 lemma zero_right: "prod a 0 = 0"
  1380 by (rule additive.zero [OF additive_right])
  1381 
  1382 lemma minus_left: "prod (- a) b = - prod a b"
  1383 by (rule additive.minus [OF additive_left])
  1384 
  1385 lemma minus_right: "prod a (- b) = - prod a b"
  1386 by (rule additive.minus [OF additive_right])
  1387 
  1388 lemma diff_left:
  1389   "prod (a - a') b = prod a b - prod a' b"
  1390 by (rule additive.diff [OF additive_left])
  1391 
  1392 lemma diff_right:
  1393   "prod a (b - b') = prod a b - prod a b'"
  1394 by (rule additive.diff [OF additive_right])
  1395 
  1396 lemma bounded_linear_left:
  1397   "bounded_linear (\<lambda>a. a ** b)"
  1398 apply (cut_tac bounded, safe)
  1399 apply (rule_tac K="norm b * K" in bounded_linear_intro)
  1400 apply (rule add_left)
  1401 apply (rule scaleR_left)
  1402 apply (simp add: ac_simps)
  1403 done
  1404 
  1405 lemma bounded_linear_right:
  1406   "bounded_linear (\<lambda>b. a ** b)"
  1407 apply (cut_tac bounded, safe)
  1408 apply (rule_tac K="norm a * K" in bounded_linear_intro)
  1409 apply (rule add_right)
  1410 apply (rule scaleR_right)
  1411 apply (simp add: ac_simps)
  1412 done
  1413 
  1414 lemma prod_diff_prod:
  1415   "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
  1416 by (simp add: diff_left diff_right)
  1417 
  1418 end
  1419 
  1420 lemma bounded_linear_ident[simp]: "bounded_linear (\<lambda>x. x)"
  1421   by default (auto intro!: exI[of _ 1])
  1422 
  1423 lemma bounded_linear_zero[simp]: "bounded_linear (\<lambda>x. 0)"
  1424   by default (auto intro!: exI[of _ 1])
  1425 
  1426 lemma bounded_linear_add:
  1427   assumes "bounded_linear f"
  1428   assumes "bounded_linear g"
  1429   shows "bounded_linear (\<lambda>x. f x + g x)"
  1430 proof -
  1431   interpret f: bounded_linear f by fact
  1432   interpret g: bounded_linear g by fact
  1433   show ?thesis
  1434   proof
  1435     from f.bounded obtain Kf where Kf: "\<And>x. norm (f x) \<le> norm x * Kf" by blast
  1436     from g.bounded obtain Kg where Kg: "\<And>x. norm (g x) \<le> norm x * Kg" by blast
  1437     show "\<exists>K. \<forall>x. norm (f x + g x) \<le> norm x * K"
  1438       using add_mono[OF Kf Kg]
  1439       by (intro exI[of _ "Kf + Kg"]) (auto simp: field_simps intro: norm_triangle_ineq order_trans)
  1440   qed (simp_all add: f.add g.add f.scaleR g.scaleR scaleR_right_distrib)
  1441 qed
  1442 
  1443 lemma bounded_linear_minus:
  1444   assumes "bounded_linear f"
  1445   shows "bounded_linear (\<lambda>x. - f x)"
  1446 proof -
  1447   interpret f: bounded_linear f by fact
  1448   show ?thesis apply (unfold_locales)
  1449     apply (simp add: f.add)
  1450     apply (simp add: f.scaleR)
  1451     apply (simp add: f.bounded)
  1452     done
  1453 qed
  1454 
  1455 lemma bounded_linear_compose:
  1456   assumes "bounded_linear f"
  1457   assumes "bounded_linear g"
  1458   shows "bounded_linear (\<lambda>x. f (g x))"
  1459 proof -
  1460   interpret f: bounded_linear f by fact
  1461   interpret g: bounded_linear g by fact
  1462   show ?thesis proof (unfold_locales)
  1463     fix x y show "f (g (x + y)) = f (g x) + f (g y)"
  1464       by (simp only: f.add g.add)
  1465   next
  1466     fix r x show "f (g (scaleR r x)) = scaleR r (f (g x))"
  1467       by (simp only: f.scaleR g.scaleR)
  1468   next
  1469     from f.pos_bounded
  1470     obtain Kf where f: "\<And>x. norm (f x) \<le> norm x * Kf" and Kf: "0 < Kf" by fast
  1471     from g.pos_bounded
  1472     obtain Kg where g: "\<And>x. norm (g x) \<le> norm x * Kg" by fast
  1473     show "\<exists>K. \<forall>x. norm (f (g x)) \<le> norm x * K"
  1474     proof (intro exI allI)
  1475       fix x
  1476       have "norm (f (g x)) \<le> norm (g x) * Kf"
  1477         using f .
  1478       also have "\<dots> \<le> (norm x * Kg) * Kf"
  1479         using g Kf [THEN order_less_imp_le] by (rule mult_right_mono)
  1480       also have "(norm x * Kg) * Kf = norm x * (Kg * Kf)"
  1481         by (rule mult.assoc)
  1482       finally show "norm (f (g x)) \<le> norm x * (Kg * Kf)" .
  1483     qed
  1484   qed
  1485 qed
  1486 
  1487 lemma bounded_bilinear_mult:
  1488   "bounded_bilinear (op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra)"
  1489 apply (rule bounded_bilinear.intro)
  1490 apply (rule distrib_right)
  1491 apply (rule distrib_left)
  1492 apply (rule mult_scaleR_left)
  1493 apply (rule mult_scaleR_right)
  1494 apply (rule_tac x="1" in exI)
  1495 apply (simp add: norm_mult_ineq)
  1496 done
  1497 
  1498 lemma bounded_linear_mult_left:
  1499   "bounded_linear (\<lambda>x::'a::real_normed_algebra. x * y)"
  1500   using bounded_bilinear_mult
  1501   by (rule bounded_bilinear.bounded_linear_left)
  1502 
  1503 lemma bounded_linear_mult_right:
  1504   "bounded_linear (\<lambda>y::'a::real_normed_algebra. x * y)"
  1505   using bounded_bilinear_mult
  1506   by (rule bounded_bilinear.bounded_linear_right)
  1507 
  1508 lemmas bounded_linear_mult_const =
  1509   bounded_linear_mult_left [THEN bounded_linear_compose]
  1510 
  1511 lemmas bounded_linear_const_mult =
  1512   bounded_linear_mult_right [THEN bounded_linear_compose]
  1513 
  1514 lemma bounded_linear_divide:
  1515   "bounded_linear (\<lambda>x::'a::real_normed_field. x / y)"
  1516   unfolding divide_inverse by (rule bounded_linear_mult_left)
  1517 
  1518 lemma bounded_bilinear_scaleR: "bounded_bilinear scaleR"
  1519 apply (rule bounded_bilinear.intro)
  1520 apply (rule scaleR_left_distrib)
  1521 apply (rule scaleR_right_distrib)
  1522 apply simp
  1523 apply (rule scaleR_left_commute)
  1524 apply (rule_tac x="1" in exI, simp)
  1525 done
  1526 
  1527 lemma bounded_linear_scaleR_left: "bounded_linear (\<lambda>r. scaleR r x)"
  1528   using bounded_bilinear_scaleR
  1529   by (rule bounded_bilinear.bounded_linear_left)
  1530 
  1531 lemma bounded_linear_scaleR_right: "bounded_linear (\<lambda>x. scaleR r x)"
  1532   using bounded_bilinear_scaleR
  1533   by (rule bounded_bilinear.bounded_linear_right)
  1534 
  1535 lemma bounded_linear_of_real: "bounded_linear (\<lambda>r. of_real r)"
  1536   unfolding of_real_def by (rule bounded_linear_scaleR_left)
  1537 
  1538 lemma real_bounded_linear:
  1539   fixes f :: "real \<Rightarrow> real"
  1540   shows "bounded_linear f \<longleftrightarrow> (\<exists>c::real. f = (\<lambda>x. x * c))"
  1541 proof -
  1542   { fix x assume "bounded_linear f"
  1543     then interpret bounded_linear f .
  1544     from scaleR[of x 1] have "f x = x * f 1"
  1545       by simp }
  1546   then show ?thesis
  1547     by (auto intro: exI[of _ "f 1"] bounded_linear_mult_left)
  1548 qed
  1549 
  1550 lemma bij_linear_imp_inv_linear:
  1551   assumes "linear f" "bij f" shows "linear (inv f)"
  1552   using assms unfolding linear_def linear_axioms_def additive_def
  1553   by (auto simp: bij_is_surj bij_is_inj surj_f_inv_f intro!:  Hilbert_Choice.inv_f_eq)
  1554     
  1555 instance real_normed_algebra_1 \<subseteq> perfect_space
  1556 proof
  1557   fix x::'a
  1558   show "\<not> open {x}"
  1559     unfolding open_dist dist_norm
  1560     by (clarsimp, rule_tac x="x + of_real (e/2)" in exI, simp)
  1561 qed
  1562 
  1563 subsection \<open>Filters and Limits on Metric Space\<close>
  1564 
  1565 lemma (in metric_space) nhds_metric: "nhds x = (INF e:{0 <..}. principal {y. dist y x < e})"
  1566   unfolding nhds_def
  1567 proof (safe intro!: INF_eq)
  1568   fix S assume "open S" "x \<in> S"
  1569   then obtain e where "{y. dist y x < e} \<subseteq> S" "0 < e"
  1570     by (auto simp: open_dist subset_eq)
  1571   then show "\<exists>e\<in>{0<..}. principal {y. dist y x < e} \<le> principal S"
  1572     by auto
  1573 qed (auto intro!: exI[of _ "{y. dist x y < e}" for e] open_ball simp: dist_commute)
  1574 
  1575 lemma (in metric_space) tendsto_iff:
  1576   "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
  1577   unfolding nhds_metric filterlim_INF filterlim_principal by auto
  1578 
  1579 lemma (in metric_space) tendstoI: "(\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F) \<Longrightarrow> (f ---> l) F"
  1580   by (auto simp: tendsto_iff)
  1581 
  1582 lemma (in metric_space) tendstoD: "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
  1583   by (auto simp: tendsto_iff)
  1584 
  1585 lemma (in metric_space) eventually_nhds_metric:
  1586   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
  1587   unfolding nhds_metric
  1588   by (subst eventually_INF_base)
  1589      (auto simp: eventually_principal Bex_def subset_eq intro: exI[of _ "min a b" for a b])
  1590 
  1591 lemma eventually_at:
  1592   fixes a :: "'a :: metric_space"
  1593   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)"
  1594   unfolding eventually_at_filter eventually_nhds_metric by (auto simp: dist_nz)
  1595 
  1596 lemma eventually_at_le:
  1597   fixes a :: "'a::metric_space"
  1598   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)"
  1599   unfolding eventually_at_filter eventually_nhds_metric
  1600   apply auto
  1601   apply (rule_tac x="d / 2" in exI)
  1602   apply auto
  1603   done
  1604 
  1605 lemma metric_tendsto_imp_tendsto:
  1606   fixes a :: "'a :: metric_space" and b :: "'b :: metric_space"
  1607   assumes f: "(f ---> a) F"
  1608   assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
  1609   shows "(g ---> b) F"
  1610 proof (rule tendstoI)
  1611   fix e :: real assume "0 < e"
  1612   with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
  1613   with le show "eventually (\<lambda>x. dist (g x) b < e) F"
  1614     using le_less_trans by (rule eventually_elim2)
  1615 qed
  1616 
  1617 lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
  1618   unfolding filterlim_at_top
  1619   apply (intro allI)
  1620   apply (rule_tac c="nat(ceiling (Z + 1))" in eventually_sequentiallyI)
  1621   by linarith
  1622 
  1623 subsubsection \<open>Limits of Sequences\<close>
  1624 
  1625 lemma lim_sequentially: "X ----> (L::'a::metric_space) \<longleftrightarrow> (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
  1626   unfolding tendsto_iff eventually_sequentially ..
  1627 
  1628 lemmas LIMSEQ_def = lim_sequentially  (*legacy binding*)
  1629 
  1630 lemma LIMSEQ_iff_nz: "X ----> (L::'a::metric_space) = (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
  1631   unfolding lim_sequentially by (metis Suc_leD zero_less_Suc)
  1632 
  1633 lemma metric_LIMSEQ_I:
  1634   "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X ----> (L::'a::metric_space)"
  1635 by (simp add: lim_sequentially)
  1636 
  1637 lemma metric_LIMSEQ_D:
  1638   "\<lbrakk>X ----> (L::'a::metric_space); 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
  1639 by (simp add: lim_sequentially)
  1640 
  1641 
  1642 subsubsection \<open>Limits of Functions\<close>
  1643 
  1644 lemma LIM_def: "f -- (a::'a::metric_space) --> (L::'b::metric_space) =
  1645      (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & dist x a < s
  1646         --> dist (f x) L < r)"
  1647   unfolding tendsto_iff eventually_at by simp
  1648 
  1649 lemma metric_LIM_I:
  1650   "(\<And>r. 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r)
  1651     \<Longrightarrow> f -- (a::'a::metric_space) --> (L::'b::metric_space)"
  1652 by (simp add: LIM_def)
  1653 
  1654 lemma metric_LIM_D:
  1655   "\<lbrakk>f -- (a::'a::metric_space) --> (L::'b::metric_space); 0 < r\<rbrakk>
  1656     \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
  1657 by (simp add: LIM_def)
  1658 
  1659 lemma metric_LIM_imp_LIM:
  1660   assumes f: "f -- a --> (l::'a::metric_space)"
  1661   assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
  1662   shows "g -- a --> (m::'b::metric_space)"
  1663   by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
  1664 
  1665 lemma metric_LIM_equal2:
  1666   assumes 1: "0 < R"
  1667   assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; dist x a < R\<rbrakk> \<Longrightarrow> f x = g x"
  1668   shows "g -- a --> l \<Longrightarrow> f -- (a::'a::metric_space) --> l"
  1669 apply (rule topological_tendstoI)
  1670 apply (drule (2) topological_tendstoD)
  1671 apply (simp add: eventually_at, safe)
  1672 apply (rule_tac x="min d R" in exI, safe)
  1673 apply (simp add: 1)
  1674 apply (simp add: 2)
  1675 done
  1676 
  1677 lemma metric_LIM_compose2:
  1678   assumes f: "f -- (a::'a::metric_space) --> b"
  1679   assumes g: "g -- b --> c"
  1680   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
  1681   shows "(\<lambda>x. g (f x)) -- a --> c"
  1682   using inj
  1683   by (intro tendsto_compose_eventually[OF g f]) (auto simp: eventually_at)
  1684 
  1685 lemma metric_isCont_LIM_compose2:
  1686   fixes f :: "'a :: metric_space \<Rightarrow> _"
  1687   assumes f [unfolded isCont_def]: "isCont f a"
  1688   assumes g: "g -- f a --> l"
  1689   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
  1690   shows "(\<lambda>x. g (f x)) -- a --> l"
  1691 by (rule metric_LIM_compose2 [OF f g inj])
  1692 
  1693 subsection \<open>Complete metric spaces\<close>
  1694 
  1695 subsection \<open>Cauchy sequences\<close>
  1696 
  1697 definition (in metric_space) Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
  1698   "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < e)"
  1699 
  1700 subsection \<open>Cauchy Sequences\<close>
  1701 
  1702 lemma metric_CauchyI:
  1703   "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
  1704   by (simp add: Cauchy_def)
  1705 
  1706 lemma metric_CauchyD:
  1707   "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
  1708   by (simp add: Cauchy_def)
  1709 
  1710 lemma metric_Cauchy_iff2:
  1711   "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"
  1712 apply (simp add: Cauchy_def, auto)
  1713 apply (drule reals_Archimedean, safe)
  1714 apply (drule_tac x = n in spec, auto)
  1715 apply (rule_tac x = M in exI, auto)
  1716 apply (drule_tac x = m in spec, simp)
  1717 apply (drule_tac x = na in spec, auto)
  1718 done
  1719 
  1720 lemma Cauchy_iff2:
  1721   "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse(real (Suc j))))"
  1722   unfolding metric_Cauchy_iff2 dist_real_def ..
  1723 
  1724 lemma Cauchy_subseq_Cauchy:
  1725   "\<lbrakk> Cauchy X; subseq f \<rbrakk> \<Longrightarrow> Cauchy (X o f)"
  1726 apply (auto simp add: Cauchy_def)
  1727 apply (drule_tac x=e in spec, clarify)
  1728 apply (rule_tac x=M in exI, clarify)
  1729 apply (blast intro: le_trans [OF _ seq_suble] dest!: spec)
  1730 done
  1731 
  1732 theorem LIMSEQ_imp_Cauchy:
  1733   assumes X: "X ----> a" shows "Cauchy X"
  1734 proof (rule metric_CauchyI)
  1735   fix e::real assume "0 < e"
  1736   hence "0 < e/2" by simp
  1737   with X have "\<exists>N. \<forall>n\<ge>N. dist (X n) a < e/2" by (rule metric_LIMSEQ_D)
  1738   then obtain N where N: "\<forall>n\<ge>N. dist (X n) a < e/2" ..
  1739   show "\<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < e"
  1740   proof (intro exI allI impI)
  1741     fix m assume "N \<le> m"
  1742     hence m: "dist (X m) a < e/2" using N by fast
  1743     fix n assume "N \<le> n"
  1744     hence n: "dist (X n) a < e/2" using N by fast
  1745     have "dist (X m) (X n) \<le> dist (X m) a + dist (X n) a"
  1746       by (rule dist_triangle2)
  1747     also from m n have "\<dots> < e" by simp
  1748     finally show "dist (X m) (X n) < e" .
  1749   qed
  1750 qed
  1751 
  1752 lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
  1753 unfolding convergent_def
  1754 by (erule exE, erule LIMSEQ_imp_Cauchy)
  1755 
  1756 subsubsection \<open>Cauchy Sequences are Convergent\<close>
  1757 
  1758 class complete_space = metric_space +
  1759   assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
  1760 
  1761 lemma Cauchy_convergent_iff:
  1762   fixes X :: "nat \<Rightarrow> 'a::complete_space"
  1763   shows "Cauchy X = convergent X"
  1764 by (fast intro: Cauchy_convergent convergent_Cauchy)
  1765 
  1766 subsection \<open>The set of real numbers is a complete metric space\<close>
  1767 
  1768 text \<open>
  1769 Proof that Cauchy sequences converge based on the one from
  1770 @{url "http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html"}
  1771 \<close>
  1772 
  1773 text \<open>
  1774   If sequence @{term "X"} is Cauchy, then its limit is the lub of
  1775   @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
  1776 \<close>
  1777 
  1778 lemma increasing_LIMSEQ:
  1779   fixes f :: "nat \<Rightarrow> real"
  1780   assumes inc: "\<And>n. f n \<le> f (Suc n)"
  1781       and bdd: "\<And>n. f n \<le> l"
  1782       and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
  1783   shows "f ----> l"
  1784 proof (rule increasing_tendsto)
  1785   fix x assume "x < l"
  1786   with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
  1787     by auto
  1788   from en[OF \<open>0 < e\<close>] obtain n where "l - e \<le> f n"
  1789     by (auto simp: field_simps)
  1790   with \<open>e < l - x\<close> \<open>0 < e\<close> have "x < f n" by simp
  1791   with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
  1792     by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
  1793 qed (insert bdd, auto)
  1794 
  1795 lemma real_Cauchy_convergent:
  1796   fixes X :: "nat \<Rightarrow> real"
  1797   assumes X: "Cauchy X"
  1798   shows "convergent X"
  1799 proof -
  1800   def S \<equiv> "{x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"
  1801   then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto
  1802 
  1803   { fix N x assume N: "\<forall>n\<ge>N. X n < x"
  1804   fix y::real assume "y \<in> S"
  1805   hence "\<exists>M. \<forall>n\<ge>M. y < X n"
  1806     by (simp add: S_def)
  1807   then obtain M where "\<forall>n\<ge>M. y < X n" ..
  1808   hence "y < X (max M N)" by simp
  1809   also have "\<dots> < x" using N by simp
  1810   finally have "y \<le> x"
  1811     by (rule order_less_imp_le) }
  1812   note bound_isUb = this
  1813 
  1814   obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
  1815     using X[THEN metric_CauchyD, OF zero_less_one] by auto
  1816   hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
  1817   have [simp]: "S \<noteq> {}"
  1818   proof (intro exI ex_in_conv[THEN iffD1])
  1819     from N have "\<forall>n\<ge>N. X N - 1 < X n"
  1820       by (simp add: abs_diff_less_iff dist_real_def)
  1821     thus "X N - 1 \<in> S" by (rule mem_S)
  1822   qed
  1823   have [simp]: "bdd_above S"
  1824   proof
  1825     from N have "\<forall>n\<ge>N. X n < X N + 1"
  1826       by (simp add: abs_diff_less_iff dist_real_def)
  1827     thus "\<And>s. s \<in> S \<Longrightarrow>  s \<le> X N + 1"
  1828       by (rule bound_isUb)
  1829   qed
  1830   have "X ----> Sup S"
  1831   proof (rule metric_LIMSEQ_I)
  1832   fix r::real assume "0 < r"
  1833   hence r: "0 < r/2" by simp
  1834   obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
  1835     using metric_CauchyD [OF X r] by auto
  1836   hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
  1837   hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
  1838     by (simp only: dist_real_def abs_diff_less_iff)
  1839 
  1840   from N have "\<forall>n\<ge>N. X N - r/2 < X n" by fast
  1841   hence "X N - r/2 \<in> S" by (rule mem_S)
  1842   hence 1: "X N - r/2 \<le> Sup S" by (simp add: cSup_upper)
  1843 
  1844   from N have "\<forall>n\<ge>N. X n < X N + r/2" by fast
  1845   from bound_isUb[OF this]
  1846   have 2: "Sup S \<le> X N + r/2"
  1847     by (intro cSup_least) simp_all
  1848 
  1849   show "\<exists>N. \<forall>n\<ge>N. dist (X n) (Sup S) < r"
  1850   proof (intro exI allI impI)
  1851     fix n assume n: "N \<le> n"
  1852     from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
  1853     thus "dist (X n) (Sup S) < r" using 1 2
  1854       by (simp add: abs_diff_less_iff dist_real_def)
  1855   qed
  1856   qed
  1857   then show ?thesis unfolding convergent_def by auto
  1858 qed
  1859 
  1860 instance real :: complete_space
  1861   by intro_classes (rule real_Cauchy_convergent)
  1862 
  1863 class banach = real_normed_vector + complete_space
  1864 
  1865 instance real :: banach by default
  1866 
  1867 lemma tendsto_at_topI_sequentially:
  1868   fixes f :: "real \<Rightarrow> 'b::first_countable_topology"
  1869   assumes *: "\<And>X. filterlim X at_top sequentially \<Longrightarrow> (\<lambda>n. f (X n)) ----> y"
  1870   shows "(f ---> y) at_top"
  1871 proof -
  1872   from nhds_countable[of y] guess A . note A = this
  1873 
  1874   have "\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m"
  1875   proof (rule ccontr)
  1876     assume "\<not> (\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m)"
  1877     then obtain m where "\<And>k. \<exists>x\<ge>k. f x \<notin> A m"
  1878       by auto
  1879     then have "\<exists>X. \<forall>n. (f (X n) \<notin> A m) \<and> max n (X n) + 1 \<le> X (Suc n)"
  1880       by (intro dependent_nat_choice) (auto simp del: max.bounded_iff)
  1881     then obtain X where X: "\<And>n. f (X n) \<notin> A m" "\<And>n. max n (X n) + 1 \<le> X (Suc n)"
  1882       by auto
  1883     { fix n have "1 \<le> n \<longrightarrow> real n \<le> X n"
  1884         using X[of "n - 1"] by auto }
  1885     then have "filterlim X at_top sequentially"
  1886       by (force intro!: filterlim_at_top_mono[OF filterlim_real_sequentially]
  1887                 simp: eventually_sequentially)
  1888     from topological_tendstoD[OF *[OF this] A(2, 3), of m] X(1) show False
  1889       by auto
  1890   qed
  1891   then obtain k where "\<And>m x. k m \<le> x \<Longrightarrow> f x \<in> A m"
  1892     by metis
  1893   then show ?thesis
  1894     unfolding at_top_def A
  1895     by (intro filterlim_base[where i=k]) auto
  1896 qed
  1897 
  1898 lemma tendsto_at_topI_sequentially_real:
  1899   fixes f :: "real \<Rightarrow> real"
  1900   assumes mono: "mono f"
  1901   assumes limseq: "(\<lambda>n. f (real n)) ----> y"
  1902   shows "(f ---> y) at_top"
  1903 proof (rule tendstoI)
  1904   fix e :: real assume "0 < e"
  1905   with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e"
  1906     by (auto simp: lim_sequentially dist_real_def)
  1907   { fix x :: real
  1908     obtain n where "x \<le> real_of_nat n"
  1909       using ex_le_of_nat[of x] ..
  1910     note monoD[OF mono this]
  1911     also have "f (real_of_nat n) \<le> y"
  1912       by (rule LIMSEQ_le_const[OF limseq])
  1913          (auto intro: exI[of _ n] monoD[OF mono] simp: real_eq_of_nat[symmetric])
  1914     finally have "f x \<le> y" . }
  1915   note le = this
  1916   have "eventually (\<lambda>x. real N \<le> x) at_top"
  1917     by (rule eventually_ge_at_top)
  1918   then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
  1919   proof eventually_elim
  1920     fix x assume N': "real N \<le> x"
  1921     with N[of N] le have "y - f (real N) < e" by auto
  1922     moreover note monoD[OF mono N']
  1923     ultimately show "dist (f x) y < e"
  1924       using le[of x] by (auto simp: dist_real_def field_simps)
  1925   qed
  1926 qed
  1927 
  1928 end
  1929