src/HOL/Real_Vector_Spaces.thy
author eberlm
Thu Oct 29 15:40:52 2015 +0100 (2015-10-29)
changeset 61524 f2e51e704a96
parent 61169 4de9ff3ea29a
child 61531 ab2e862263e7
permissions -rw-r--r--
added many small lemmas about setsum/setprod/powr/...
     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"  ("\<real>")
   362   where "\<real> = range of_real"
   363 
   364 lemma Reals_of_real [simp]: "of_real r \<in> \<real>"
   365 by (simp add: Reals_def)
   366 
   367 lemma Reals_of_int [simp]: "of_int z \<in> \<real>"
   368 by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
   369 
   370 lemma Reals_of_nat [simp]: "of_nat n \<in> \<real>"
   371 by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
   372 
   373 lemma Reals_numeral [simp]: "numeral w \<in> \<real>"
   374 by (subst of_real_numeral [symmetric], rule Reals_of_real)
   375 
   376 lemma Reals_0 [simp]: "0 \<in> \<real>"
   377 apply (unfold Reals_def)
   378 apply (rule range_eqI)
   379 apply (rule of_real_0 [symmetric])
   380 done
   381 
   382 lemma Reals_1 [simp]: "1 \<in> \<real>"
   383 apply (unfold Reals_def)
   384 apply (rule range_eqI)
   385 apply (rule of_real_1 [symmetric])
   386 done
   387 
   388 lemma Reals_add [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a + b \<in> \<real>"
   389 apply (auto simp add: Reals_def)
   390 apply (rule range_eqI)
   391 apply (rule of_real_add [symmetric])
   392 done
   393 
   394 lemma Reals_minus [simp]: "a \<in> \<real> \<Longrightarrow> - a \<in> \<real>"
   395 apply (auto simp add: Reals_def)
   396 apply (rule range_eqI)
   397 apply (rule of_real_minus [symmetric])
   398 done
   399 
   400 lemma Reals_diff [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a - b \<in> \<real>"
   401 apply (auto simp add: Reals_def)
   402 apply (rule range_eqI)
   403 apply (rule of_real_diff [symmetric])
   404 done
   405 
   406 lemma Reals_mult [simp]: "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a * b \<in> \<real>"
   407 apply (auto simp add: Reals_def)
   408 apply (rule range_eqI)
   409 apply (rule of_real_mult [symmetric])
   410 done
   411 
   412 lemma nonzero_Reals_inverse:
   413   fixes a :: "'a::real_div_algebra"
   414   shows "\<lbrakk>a \<in> \<real>; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> \<real>"
   415 apply (auto simp add: Reals_def)
   416 apply (rule range_eqI)
   417 apply (erule nonzero_of_real_inverse [symmetric])
   418 done
   419 
   420 lemma Reals_inverse:
   421   fixes a :: "'a::{real_div_algebra, division_ring}"
   422   shows "a \<in> \<real> \<Longrightarrow> inverse a \<in> \<real>"
   423 apply (auto simp add: Reals_def)
   424 apply (rule range_eqI)
   425 apply (rule of_real_inverse [symmetric])
   426 done
   427 
   428 lemma Reals_inverse_iff [simp]:
   429   fixes x:: "'a :: {real_div_algebra, division_ring}"
   430   shows "inverse x \<in> \<real> \<longleftrightarrow> x \<in> \<real>"
   431 by (metis Reals_inverse inverse_inverse_eq)
   432 
   433 lemma nonzero_Reals_divide:
   434   fixes a b :: "'a::real_field"
   435   shows "\<lbrakk>a \<in> \<real>; b \<in> \<real>; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> \<real>"
   436 apply (auto simp add: Reals_def)
   437 apply (rule range_eqI)
   438 apply (erule nonzero_of_real_divide [symmetric])
   439 done
   440 
   441 lemma Reals_divide [simp]:
   442   fixes a b :: "'a::{real_field, field}"
   443   shows "\<lbrakk>a \<in> \<real>; b \<in> \<real>\<rbrakk> \<Longrightarrow> a / b \<in> \<real>"
   444 apply (auto simp add: Reals_def)
   445 apply (rule range_eqI)
   446 apply (rule of_real_divide [symmetric])
   447 done
   448 
   449 lemma Reals_power [simp]:
   450   fixes a :: "'a::{real_algebra_1}"
   451   shows "a \<in> \<real> \<Longrightarrow> a ^ n \<in> \<real>"
   452 apply (auto simp add: Reals_def)
   453 apply (rule range_eqI)
   454 apply (rule of_real_power [symmetric])
   455 done
   456 
   457 lemma Reals_cases [cases set: Reals]:
   458   assumes "q \<in> \<real>"
   459   obtains (of_real) r where "q = of_real r"
   460   unfolding Reals_def
   461 proof -
   462   from \<open>q \<in> \<real>\<close> have "q \<in> range of_real" unfolding Reals_def .
   463   then obtain r where "q = of_real r" ..
   464   then show thesis ..
   465 qed
   466 
   467 lemma setsum_in_Reals [intro,simp]:
   468   assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setsum f s \<in> \<real>"
   469 proof (cases "finite s")
   470   case True then show ?thesis using assms
   471     by (induct s rule: finite_induct) auto
   472 next
   473   case False then show ?thesis using assms
   474     by (metis Reals_0 setsum.infinite)
   475 qed
   476 
   477 lemma setprod_in_Reals [intro,simp]:
   478   assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setprod f s \<in> \<real>"
   479 proof (cases "finite s")
   480   case True then show ?thesis using assms
   481     by (induct s rule: finite_induct) auto
   482 next
   483   case False then show ?thesis using assms
   484     by (metis Reals_1 setprod.infinite)
   485 qed
   486 
   487 lemma Reals_induct [case_names of_real, induct set: Reals]:
   488   "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
   489   by (rule Reals_cases) auto
   490 
   491 subsection \<open>Ordered real vector spaces\<close>
   492 
   493 class ordered_real_vector = real_vector + ordered_ab_group_add +
   494   assumes scaleR_left_mono: "x \<le> y \<Longrightarrow> 0 \<le> a \<Longrightarrow> a *\<^sub>R x \<le> a *\<^sub>R y"
   495   assumes scaleR_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R x"
   496 begin
   497 
   498 lemma scaleR_mono:
   499   "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"
   500 apply (erule scaleR_right_mono [THEN order_trans], assumption)
   501 apply (erule scaleR_left_mono, assumption)
   502 done
   503 
   504 lemma scaleR_mono':
   505   "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"
   506   by (rule scaleR_mono) (auto intro: order.trans)
   507 
   508 lemma pos_le_divideRI:
   509   assumes "0 < c"
   510   assumes "c *\<^sub>R a \<le> b"
   511   shows "a \<le> b /\<^sub>R c"
   512 proof -
   513   from scaleR_left_mono[OF assms(2)] assms(1)
   514   have "c *\<^sub>R a /\<^sub>R c \<le> b /\<^sub>R c"
   515     by simp
   516   with assms show ?thesis
   517     by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
   518 qed
   519 
   520 lemma pos_le_divideR_eq:
   521   assumes "0 < c"
   522   shows "a \<le> b /\<^sub>R c \<longleftrightarrow> c *\<^sub>R a \<le> b"
   523 proof rule
   524   assume "a \<le> b /\<^sub>R c"
   525   from scaleR_left_mono[OF this] assms
   526   have "c *\<^sub>R a \<le> c *\<^sub>R (b /\<^sub>R c)"
   527     by simp
   528   with assms show "c *\<^sub>R a \<le> b"
   529     by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
   530 qed (rule pos_le_divideRI[OF assms])
   531 
   532 lemma scaleR_image_atLeastAtMost:
   533   "c > 0 \<Longrightarrow> scaleR c ` {x..y} = {c *\<^sub>R x..c *\<^sub>R y}"
   534   apply (auto intro!: scaleR_left_mono)
   535   apply (rule_tac x = "inverse c *\<^sub>R xa" in image_eqI)
   536   apply (simp_all add: pos_le_divideR_eq[symmetric] scaleR_scaleR scaleR_one)
   537   done
   538 
   539 end
   540 
   541 lemma neg_le_divideR_eq:
   542   fixes a :: "'a :: ordered_real_vector"
   543   assumes "c < 0"
   544   shows "a \<le> b /\<^sub>R c \<longleftrightarrow> b \<le> c *\<^sub>R a"
   545   using pos_le_divideR_eq [of "-c" a "-b"] assms
   546   by simp
   547 
   548 lemma scaleR_nonneg_nonneg: "0 \<le> a \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> 0 \<le> a *\<^sub>R x"
   549   using scaleR_left_mono [of 0 x a]
   550   by simp
   551 
   552 lemma scaleR_nonneg_nonpos: "0 \<le> a \<Longrightarrow> (x::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> a *\<^sub>R x \<le> 0"
   553   using scaleR_left_mono [of x 0 a] by simp
   554 
   555 lemma scaleR_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> a *\<^sub>R x \<le> 0"
   556   using scaleR_right_mono [of a 0 x] by simp
   557 
   558 lemma split_scaleR_neg_le: "(0 \<le> a & x \<le> 0) | (a \<le> 0 & 0 \<le> x) \<Longrightarrow>
   559   a *\<^sub>R (x::'a::ordered_real_vector) \<le> 0"
   560   by (auto simp add: scaleR_nonneg_nonpos scaleR_nonpos_nonneg)
   561 
   562 lemma le_add_iff1:
   563   fixes c d e::"'a::ordered_real_vector"
   564   shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> (a - b) *\<^sub>R e + c \<le> d"
   565   by (simp add: algebra_simps)
   566 
   567 lemma le_add_iff2:
   568   fixes c d e::"'a::ordered_real_vector"
   569   shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> c \<le> (b - a) *\<^sub>R e + d"
   570   by (simp add: algebra_simps)
   571 
   572 lemma scaleR_left_mono_neg:
   573   fixes a b::"'a::ordered_real_vector"
   574   shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b"
   575   apply (drule scaleR_left_mono [of _ _ "- c"])
   576   apply simp_all
   577   done
   578 
   579 lemma scaleR_right_mono_neg:
   580   fixes c::"'a::ordered_real_vector"
   581   shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R c"
   582   apply (drule scaleR_right_mono [of _ _ "- c"])
   583   apply simp_all
   584   done
   585 
   586 lemma scaleR_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> (b::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> 0 \<le> a *\<^sub>R b"
   587 using scaleR_right_mono_neg [of a 0 b] by simp
   588 
   589 lemma split_scaleR_pos_le:
   590   fixes b::"'a::ordered_real_vector"
   591   shows "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a *\<^sub>R b"
   592   by (auto simp add: scaleR_nonneg_nonneg scaleR_nonpos_nonpos)
   593 
   594 lemma zero_le_scaleR_iff:
   595   fixes b::"'a::ordered_real_vector"
   596   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")
   597 proof cases
   598   assume "a \<noteq> 0"
   599   show ?thesis
   600   proof
   601     assume lhs: ?lhs
   602     {
   603       assume "0 < a"
   604       with lhs have "inverse a *\<^sub>R 0 \<le> inverse a *\<^sub>R (a *\<^sub>R b)"
   605         by (intro scaleR_mono) auto
   606       hence ?rhs using \<open>0 < a\<close>
   607         by simp
   608     } moreover {
   609       assume "0 > a"
   610       with lhs have "- inverse a *\<^sub>R 0 \<le> - inverse a *\<^sub>R (a *\<^sub>R b)"
   611         by (intro scaleR_mono) auto
   612       hence ?rhs using \<open>0 > a\<close>
   613         by simp
   614     } ultimately show ?rhs using \<open>a \<noteq> 0\<close> by arith
   615   qed (auto simp: not_le \<open>a \<noteq> 0\<close> intro!: split_scaleR_pos_le)
   616 qed simp
   617 
   618 lemma scaleR_le_0_iff:
   619   fixes b::"'a::ordered_real_vector"
   620   shows "a *\<^sub>R b \<le> 0 \<longleftrightarrow> 0 < a \<and> b \<le> 0 \<or> a < 0 \<and> 0 \<le> b \<or> a = 0"
   621   by (insert zero_le_scaleR_iff [of "-a" b]) force
   622 
   623 lemma scaleR_le_cancel_left:
   624   fixes b::"'a::ordered_real_vector"
   625   shows "c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
   626   by (auto simp add: neq_iff scaleR_left_mono scaleR_left_mono_neg
   627     dest: scaleR_left_mono[where a="inverse c"] scaleR_left_mono_neg[where c="inverse c"])
   628 
   629 lemma scaleR_le_cancel_left_pos:
   630   fixes b::"'a::ordered_real_vector"
   631   shows "0 < c \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> a \<le> b"
   632   by (auto simp: scaleR_le_cancel_left)
   633 
   634 lemma scaleR_le_cancel_left_neg:
   635   fixes b::"'a::ordered_real_vector"
   636   shows "c < 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> b \<le> a"
   637   by (auto simp: scaleR_le_cancel_left)
   638 
   639 lemma scaleR_left_le_one_le:
   640   fixes x::"'a::ordered_real_vector" and a::real
   641   shows "0 \<le> x \<Longrightarrow> a \<le> 1 \<Longrightarrow> a *\<^sub>R x \<le> x"
   642   using scaleR_right_mono[of a 1 x] by simp
   643 
   644 
   645 subsection \<open>Real normed vector spaces\<close>
   646 
   647 class dist =
   648   fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
   649 
   650 class norm =
   651   fixes norm :: "'a \<Rightarrow> real"
   652 
   653 class sgn_div_norm = scaleR + norm + sgn +
   654   assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
   655 
   656 class dist_norm = dist + norm + minus +
   657   assumes dist_norm: "dist x y = norm (x - y)"
   658 
   659 class open_dist = "open" + dist +
   660   assumes open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   661 
   662 class real_normed_vector = real_vector + sgn_div_norm + dist_norm + open_dist +
   663   assumes norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
   664   and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
   665   and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   666 begin
   667 
   668 lemma norm_ge_zero [simp]: "0 \<le> norm x"
   669 proof -
   670   have "0 = norm (x + -1 *\<^sub>R x)"
   671     using scaleR_add_left[of 1 "-1" x] norm_scaleR[of 0 x] by (simp add: scaleR_one)
   672   also have "\<dots> \<le> norm x + norm (-1 *\<^sub>R x)" by (rule norm_triangle_ineq)
   673   finally show ?thesis by simp
   674 qed
   675 
   676 end
   677 
   678 class real_normed_algebra = real_algebra + real_normed_vector +
   679   assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
   680 
   681 class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   682   assumes norm_one [simp]: "norm 1 = 1"
   683 
   684 class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   685   assumes norm_mult: "norm (x * y) = norm x * norm y"
   686 
   687 class real_normed_field = real_field + real_normed_div_algebra
   688 
   689 instance real_normed_div_algebra < real_normed_algebra_1
   690 proof
   691   fix x y :: 'a
   692   show "norm (x * y) \<le> norm x * norm y"
   693     by (simp add: norm_mult)
   694 next
   695   have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
   696     by (rule norm_mult)
   697   thus "norm (1::'a) = 1" by simp
   698 qed
   699 
   700 lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
   701 by simp
   702 
   703 lemma zero_less_norm_iff [simp]:
   704   fixes x :: "'a::real_normed_vector"
   705   shows "(0 < norm x) = (x \<noteq> 0)"
   706 by (simp add: order_less_le)
   707 
   708 lemma norm_not_less_zero [simp]:
   709   fixes x :: "'a::real_normed_vector"
   710   shows "\<not> norm x < 0"
   711 by (simp add: linorder_not_less)
   712 
   713 lemma norm_le_zero_iff [simp]:
   714   fixes x :: "'a::real_normed_vector"
   715   shows "(norm x \<le> 0) = (x = 0)"
   716 by (simp add: order_le_less)
   717 
   718 lemma norm_minus_cancel [simp]:
   719   fixes x :: "'a::real_normed_vector"
   720   shows "norm (- x) = norm x"
   721 proof -
   722   have "norm (- x) = norm (scaleR (- 1) x)"
   723     by (simp only: scaleR_minus_left scaleR_one)
   724   also have "\<dots> = \<bar>- 1\<bar> * norm x"
   725     by (rule norm_scaleR)
   726   finally show ?thesis by simp
   727 qed
   728 
   729 lemma norm_minus_commute:
   730   fixes a b :: "'a::real_normed_vector"
   731   shows "norm (a - b) = norm (b - a)"
   732 proof -
   733   have "norm (- (b - a)) = norm (b - a)"
   734     by (rule norm_minus_cancel)
   735   thus ?thesis by simp
   736 qed
   737 
   738 lemma norm_uminus_minus: "norm (-x - y :: 'a :: real_normed_vector) = norm (x + y)"
   739   by (subst (2) norm_minus_cancel[symmetric], subst minus_add_distrib) simp
   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 lemma dist_of_int: "dist (of_int m) (of_int n :: 'a :: real_normed_algebra_1) = of_int \<bar>m - n\<bar>"
  1288 proof -
  1289   have "dist (of_int m) (of_int n :: 'a) = dist (of_int m :: 'a) (of_int m - (of_int (m - n)))"
  1290     by simp
  1291   also have "\<dots> = of_int \<bar>m - n\<bar>" by (subst dist_diff, subst norm_of_int) simp
  1292   finally show ?thesis .
  1293 qed
  1294 
  1295 lemma dist_of_nat: 
  1296   "dist (of_nat m) (of_nat n :: 'a :: real_normed_algebra_1) = of_int \<bar>int m - int n\<bar>"
  1297   by (subst (1 2) of_int_of_nat_eq [symmetric]) (rule dist_of_int)
  1298   
  1299 subsection \<open>Bounded Linear and Bilinear Operators\<close>
  1300 
  1301 locale linear = additive f for f :: "'a::real_vector \<Rightarrow> 'b::real_vector" +
  1302   assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
  1303 
  1304 lemma linear_imp_scaleR:
  1305   assumes "linear D" obtains d where "D = (\<lambda>x. x *\<^sub>R d)"
  1306   by (metis assms linear.scaleR mult.commute mult.left_neutral real_scaleR_def)
  1307 
  1308 lemma linearI:
  1309   assumes "\<And>x y. f (x + y) = f x + f y"
  1310   assumes "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
  1311   shows "linear f"
  1312   by standard (rule assms)+
  1313 
  1314 locale bounded_linear = linear f for f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" +
  1315   assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
  1316 begin
  1317 
  1318 lemma pos_bounded:
  1319   "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
  1320 proof -
  1321   obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
  1322     using bounded by fast
  1323   show ?thesis
  1324   proof (intro exI impI conjI allI)
  1325     show "0 < max 1 K"
  1326       by (rule order_less_le_trans [OF zero_less_one max.cobounded1])
  1327   next
  1328     fix x
  1329     have "norm (f x) \<le> norm x * K" using K .
  1330     also have "\<dots> \<le> norm x * max 1 K"
  1331       by (rule mult_left_mono [OF max.cobounded2 norm_ge_zero])
  1332     finally show "norm (f x) \<le> norm x * max 1 K" .
  1333   qed
  1334 qed
  1335 
  1336 lemma nonneg_bounded:
  1337   "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
  1338 proof -
  1339   from pos_bounded
  1340   show ?thesis by (auto intro: order_less_imp_le)
  1341 qed
  1342 
  1343 lemma linear: "linear f" ..
  1344 
  1345 end
  1346 
  1347 lemma bounded_linear_intro:
  1348   assumes "\<And>x y. f (x + y) = f x + f y"
  1349   assumes "\<And>r x. f (scaleR r x) = scaleR r (f x)"
  1350   assumes "\<And>x. norm (f x) \<le> norm x * K"
  1351   shows "bounded_linear f"
  1352   by standard (fast intro: assms)+
  1353 
  1354 locale bounded_bilinear =
  1355   fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
  1356                  \<Rightarrow> 'c::real_normed_vector"
  1357     (infixl "**" 70)
  1358   assumes add_left: "prod (a + a') b = prod a b + prod a' b"
  1359   assumes add_right: "prod a (b + b') = prod a b + prod a b'"
  1360   assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
  1361   assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
  1362   assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
  1363 begin
  1364 
  1365 lemma pos_bounded:
  1366   "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1367 apply (cut_tac bounded, erule exE)
  1368 apply (rule_tac x="max 1 K" in exI, safe)
  1369 apply (rule order_less_le_trans [OF zero_less_one max.cobounded1])
  1370 apply (drule spec, drule spec, erule order_trans)
  1371 apply (rule mult_left_mono [OF max.cobounded2])
  1372 apply (intro mult_nonneg_nonneg norm_ge_zero)
  1373 done
  1374 
  1375 lemma nonneg_bounded:
  1376   "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1377 proof -
  1378   from pos_bounded
  1379   show ?thesis by (auto intro: order_less_imp_le)
  1380 qed
  1381 
  1382 lemma additive_right: "additive (\<lambda>b. prod a b)"
  1383 by (rule additive.intro, rule add_right)
  1384 
  1385 lemma additive_left: "additive (\<lambda>a. prod a b)"
  1386 by (rule additive.intro, rule add_left)
  1387 
  1388 lemma zero_left: "prod 0 b = 0"
  1389 by (rule additive.zero [OF additive_left])
  1390 
  1391 lemma zero_right: "prod a 0 = 0"
  1392 by (rule additive.zero [OF additive_right])
  1393 
  1394 lemma minus_left: "prod (- a) b = - prod a b"
  1395 by (rule additive.minus [OF additive_left])
  1396 
  1397 lemma minus_right: "prod a (- b) = - prod a b"
  1398 by (rule additive.minus [OF additive_right])
  1399 
  1400 lemma diff_left:
  1401   "prod (a - a') b = prod a b - prod a' b"
  1402 by (rule additive.diff [OF additive_left])
  1403 
  1404 lemma diff_right:
  1405   "prod a (b - b') = prod a b - prod a b'"
  1406 by (rule additive.diff [OF additive_right])
  1407 
  1408 lemma bounded_linear_left:
  1409   "bounded_linear (\<lambda>a. a ** b)"
  1410 apply (cut_tac bounded, safe)
  1411 apply (rule_tac K="norm b * K" in bounded_linear_intro)
  1412 apply (rule add_left)
  1413 apply (rule scaleR_left)
  1414 apply (simp add: ac_simps)
  1415 done
  1416 
  1417 lemma bounded_linear_right:
  1418   "bounded_linear (\<lambda>b. a ** b)"
  1419 apply (cut_tac bounded, safe)
  1420 apply (rule_tac K="norm a * K" in bounded_linear_intro)
  1421 apply (rule add_right)
  1422 apply (rule scaleR_right)
  1423 apply (simp add: ac_simps)
  1424 done
  1425 
  1426 lemma prod_diff_prod:
  1427   "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
  1428 by (simp add: diff_left diff_right)
  1429 
  1430 end
  1431 
  1432 lemma bounded_linear_ident[simp]: "bounded_linear (\<lambda>x. x)"
  1433   by standard (auto intro!: exI[of _ 1])
  1434 
  1435 lemma bounded_linear_zero[simp]: "bounded_linear (\<lambda>x. 0)"
  1436   by standard (auto intro!: exI[of _ 1])
  1437 
  1438 lemma bounded_linear_add:
  1439   assumes "bounded_linear f"
  1440   assumes "bounded_linear g"
  1441   shows "bounded_linear (\<lambda>x. f x + g x)"
  1442 proof -
  1443   interpret f: bounded_linear f by fact
  1444   interpret g: bounded_linear g by fact
  1445   show ?thesis
  1446   proof
  1447     from f.bounded obtain Kf where Kf: "\<And>x. norm (f x) \<le> norm x * Kf" by blast
  1448     from g.bounded obtain Kg where Kg: "\<And>x. norm (g x) \<le> norm x * Kg" by blast
  1449     show "\<exists>K. \<forall>x. norm (f x + g x) \<le> norm x * K"
  1450       using add_mono[OF Kf Kg]
  1451       by (intro exI[of _ "Kf + Kg"]) (auto simp: field_simps intro: norm_triangle_ineq order_trans)
  1452   qed (simp_all add: f.add g.add f.scaleR g.scaleR scaleR_right_distrib)
  1453 qed
  1454 
  1455 lemma bounded_linear_minus:
  1456   assumes "bounded_linear f"
  1457   shows "bounded_linear (\<lambda>x. - f x)"
  1458 proof -
  1459   interpret f: bounded_linear f by fact
  1460   show ?thesis apply (unfold_locales)
  1461     apply (simp add: f.add)
  1462     apply (simp add: f.scaleR)
  1463     apply (simp add: f.bounded)
  1464     done
  1465 qed
  1466 
  1467 lemma bounded_linear_compose:
  1468   assumes "bounded_linear f"
  1469   assumes "bounded_linear g"
  1470   shows "bounded_linear (\<lambda>x. f (g x))"
  1471 proof -
  1472   interpret f: bounded_linear f by fact
  1473   interpret g: bounded_linear g by fact
  1474   show ?thesis proof (unfold_locales)
  1475     fix x y show "f (g (x + y)) = f (g x) + f (g y)"
  1476       by (simp only: f.add g.add)
  1477   next
  1478     fix r x show "f (g (scaleR r x)) = scaleR r (f (g x))"
  1479       by (simp only: f.scaleR g.scaleR)
  1480   next
  1481     from f.pos_bounded
  1482     obtain Kf where f: "\<And>x. norm (f x) \<le> norm x * Kf" and Kf: "0 < Kf" by fast
  1483     from g.pos_bounded
  1484     obtain Kg where g: "\<And>x. norm (g x) \<le> norm x * Kg" by fast
  1485     show "\<exists>K. \<forall>x. norm (f (g x)) \<le> norm x * K"
  1486     proof (intro exI allI)
  1487       fix x
  1488       have "norm (f (g x)) \<le> norm (g x) * Kf"
  1489         using f .
  1490       also have "\<dots> \<le> (norm x * Kg) * Kf"
  1491         using g Kf [THEN order_less_imp_le] by (rule mult_right_mono)
  1492       also have "(norm x * Kg) * Kf = norm x * (Kg * Kf)"
  1493         by (rule mult.assoc)
  1494       finally show "norm (f (g x)) \<le> norm x * (Kg * Kf)" .
  1495     qed
  1496   qed
  1497 qed
  1498 
  1499 lemma bounded_bilinear_mult:
  1500   "bounded_bilinear (op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra)"
  1501 apply (rule bounded_bilinear.intro)
  1502 apply (rule distrib_right)
  1503 apply (rule distrib_left)
  1504 apply (rule mult_scaleR_left)
  1505 apply (rule mult_scaleR_right)
  1506 apply (rule_tac x="1" in exI)
  1507 apply (simp add: norm_mult_ineq)
  1508 done
  1509 
  1510 lemma bounded_linear_mult_left:
  1511   "bounded_linear (\<lambda>x::'a::real_normed_algebra. x * y)"
  1512   using bounded_bilinear_mult
  1513   by (rule bounded_bilinear.bounded_linear_left)
  1514 
  1515 lemma bounded_linear_mult_right:
  1516   "bounded_linear (\<lambda>y::'a::real_normed_algebra. x * y)"
  1517   using bounded_bilinear_mult
  1518   by (rule bounded_bilinear.bounded_linear_right)
  1519 
  1520 lemmas bounded_linear_mult_const =
  1521   bounded_linear_mult_left [THEN bounded_linear_compose]
  1522 
  1523 lemmas bounded_linear_const_mult =
  1524   bounded_linear_mult_right [THEN bounded_linear_compose]
  1525 
  1526 lemma bounded_linear_divide:
  1527   "bounded_linear (\<lambda>x::'a::real_normed_field. x / y)"
  1528   unfolding divide_inverse by (rule bounded_linear_mult_left)
  1529 
  1530 lemma bounded_bilinear_scaleR: "bounded_bilinear scaleR"
  1531 apply (rule bounded_bilinear.intro)
  1532 apply (rule scaleR_left_distrib)
  1533 apply (rule scaleR_right_distrib)
  1534 apply simp
  1535 apply (rule scaleR_left_commute)
  1536 apply (rule_tac x="1" in exI, simp)
  1537 done
  1538 
  1539 lemma bounded_linear_scaleR_left: "bounded_linear (\<lambda>r. scaleR r x)"
  1540   using bounded_bilinear_scaleR
  1541   by (rule bounded_bilinear.bounded_linear_left)
  1542 
  1543 lemma bounded_linear_scaleR_right: "bounded_linear (\<lambda>x. scaleR r x)"
  1544   using bounded_bilinear_scaleR
  1545   by (rule bounded_bilinear.bounded_linear_right)
  1546 
  1547 lemma bounded_linear_of_real: "bounded_linear (\<lambda>r. of_real r)"
  1548   unfolding of_real_def by (rule bounded_linear_scaleR_left)
  1549 
  1550 lemma real_bounded_linear:
  1551   fixes f :: "real \<Rightarrow> real"
  1552   shows "bounded_linear f \<longleftrightarrow> (\<exists>c::real. f = (\<lambda>x. x * c))"
  1553 proof -
  1554   { fix x assume "bounded_linear f"
  1555     then interpret bounded_linear f .
  1556     from scaleR[of x 1] have "f x = x * f 1"
  1557       by simp }
  1558   then show ?thesis
  1559     by (auto intro: exI[of _ "f 1"] bounded_linear_mult_left)
  1560 qed
  1561 
  1562 lemma bij_linear_imp_inv_linear:
  1563   assumes "linear f" "bij f" shows "linear (inv f)"
  1564   using assms unfolding linear_def linear_axioms_def additive_def
  1565   by (auto simp: bij_is_surj bij_is_inj surj_f_inv_f intro!:  Hilbert_Choice.inv_f_eq)
  1566     
  1567 instance real_normed_algebra_1 \<subseteq> perfect_space
  1568 proof
  1569   fix x::'a
  1570   show "\<not> open {x}"
  1571     unfolding open_dist dist_norm
  1572     by (clarsimp, rule_tac x="x + of_real (e/2)" in exI, simp)
  1573 qed
  1574 
  1575 subsection \<open>Filters and Limits on Metric Space\<close>
  1576 
  1577 lemma (in metric_space) nhds_metric: "nhds x = (INF e:{0 <..}. principal {y. dist y x < e})"
  1578   unfolding nhds_def
  1579 proof (safe intro!: INF_eq)
  1580   fix S assume "open S" "x \<in> S"
  1581   then obtain e where "{y. dist y x < e} \<subseteq> S" "0 < e"
  1582     by (auto simp: open_dist subset_eq)
  1583   then show "\<exists>e\<in>{0<..}. principal {y. dist y x < e} \<le> principal S"
  1584     by auto
  1585 qed (auto intro!: exI[of _ "{y. dist x y < e}" for e] open_ball simp: dist_commute)
  1586 
  1587 lemma (in metric_space) tendsto_iff:
  1588   "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
  1589   unfolding nhds_metric filterlim_INF filterlim_principal by auto
  1590 
  1591 lemma (in metric_space) tendstoI: "(\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F) \<Longrightarrow> (f ---> l) F"
  1592   by (auto simp: tendsto_iff)
  1593 
  1594 lemma (in metric_space) tendstoD: "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
  1595   by (auto simp: tendsto_iff)
  1596 
  1597 lemma (in metric_space) eventually_nhds_metric:
  1598   "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
  1599   unfolding nhds_metric
  1600   by (subst eventually_INF_base)
  1601      (auto simp: eventually_principal Bex_def subset_eq intro: exI[of _ "min a b" for a b])
  1602 
  1603 lemma eventually_at:
  1604   fixes a :: "'a :: metric_space"
  1605   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)"
  1606   unfolding eventually_at_filter eventually_nhds_metric by (auto simp: dist_nz)
  1607 
  1608 lemma eventually_at_le:
  1609   fixes a :: "'a::metric_space"
  1610   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)"
  1611   unfolding eventually_at_filter eventually_nhds_metric
  1612   apply auto
  1613   apply (rule_tac x="d / 2" in exI)
  1614   apply auto
  1615   done
  1616 
  1617 lemma metric_tendsto_imp_tendsto:
  1618   fixes a :: "'a :: metric_space" and b :: "'b :: metric_space"
  1619   assumes f: "(f ---> a) F"
  1620   assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
  1621   shows "(g ---> b) F"
  1622 proof (rule tendstoI)
  1623   fix e :: real assume "0 < e"
  1624   with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
  1625   with le show "eventually (\<lambda>x. dist (g x) b < e) F"
  1626     using le_less_trans by (rule eventually_elim2)
  1627 qed
  1628 
  1629 lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
  1630   unfolding filterlim_at_top
  1631   apply (intro allI)
  1632   apply (rule_tac c="nat(ceiling (Z + 1))" in eventually_sequentiallyI)
  1633   by linarith
  1634 
  1635 subsubsection \<open>Limits of Sequences\<close>
  1636 
  1637 lemma lim_sequentially: "X ----> (L::'a::metric_space) \<longleftrightarrow> (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
  1638   unfolding tendsto_iff eventually_sequentially ..
  1639 
  1640 lemmas LIMSEQ_def = lim_sequentially  (*legacy binding*)
  1641 
  1642 lemma LIMSEQ_iff_nz: "X ----> (L::'a::metric_space) = (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
  1643   unfolding lim_sequentially by (metis Suc_leD zero_less_Suc)
  1644 
  1645 lemma metric_LIMSEQ_I:
  1646   "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X ----> (L::'a::metric_space)"
  1647 by (simp add: lim_sequentially)
  1648 
  1649 lemma metric_LIMSEQ_D:
  1650   "\<lbrakk>X ----> (L::'a::metric_space); 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
  1651 by (simp add: lim_sequentially)
  1652 
  1653 
  1654 subsubsection \<open>Limits of Functions\<close>
  1655 
  1656 lemma LIM_def: "f -- (a::'a::metric_space) --> (L::'b::metric_space) =
  1657      (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & dist x a < s
  1658         --> dist (f x) L < r)"
  1659   unfolding tendsto_iff eventually_at by simp
  1660 
  1661 lemma metric_LIM_I:
  1662   "(\<And>r. 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r)
  1663     \<Longrightarrow> f -- (a::'a::metric_space) --> (L::'b::metric_space)"
  1664 by (simp add: LIM_def)
  1665 
  1666 lemma metric_LIM_D:
  1667   "\<lbrakk>f -- (a::'a::metric_space) --> (L::'b::metric_space); 0 < r\<rbrakk>
  1668     \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
  1669 by (simp add: LIM_def)
  1670 
  1671 lemma metric_LIM_imp_LIM:
  1672   assumes f: "f -- a --> (l::'a::metric_space)"
  1673   assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
  1674   shows "g -- a --> (m::'b::metric_space)"
  1675   by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
  1676 
  1677 lemma metric_LIM_equal2:
  1678   assumes 1: "0 < R"
  1679   assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; dist x a < R\<rbrakk> \<Longrightarrow> f x = g x"
  1680   shows "g -- a --> l \<Longrightarrow> f -- (a::'a::metric_space) --> l"
  1681 apply (rule topological_tendstoI)
  1682 apply (drule (2) topological_tendstoD)
  1683 apply (simp add: eventually_at, safe)
  1684 apply (rule_tac x="min d R" in exI, safe)
  1685 apply (simp add: 1)
  1686 apply (simp add: 2)
  1687 done
  1688 
  1689 lemma metric_LIM_compose2:
  1690   assumes f: "f -- (a::'a::metric_space) --> b"
  1691   assumes g: "g -- b --> c"
  1692   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
  1693   shows "(\<lambda>x. g (f x)) -- a --> c"
  1694   using inj
  1695   by (intro tendsto_compose_eventually[OF g f]) (auto simp: eventually_at)
  1696 
  1697 lemma metric_isCont_LIM_compose2:
  1698   fixes f :: "'a :: metric_space \<Rightarrow> _"
  1699   assumes f [unfolded isCont_def]: "isCont f a"
  1700   assumes g: "g -- f a --> l"
  1701   assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
  1702   shows "(\<lambda>x. g (f x)) -- a --> l"
  1703 by (rule metric_LIM_compose2 [OF f g inj])
  1704 
  1705 subsection \<open>Complete metric spaces\<close>
  1706 
  1707 subsection \<open>Cauchy sequences\<close>
  1708 
  1709 definition (in metric_space) Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
  1710   "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < e)"
  1711 
  1712 subsection \<open>Cauchy Sequences\<close>
  1713 
  1714 lemma metric_CauchyI:
  1715   "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
  1716   by (simp add: Cauchy_def)
  1717 
  1718 lemma metric_CauchyD:
  1719   "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
  1720   by (simp add: Cauchy_def)
  1721 
  1722 lemma metric_Cauchy_iff2:
  1723   "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"
  1724 apply (simp add: Cauchy_def, auto)
  1725 apply (drule reals_Archimedean, safe)
  1726 apply (drule_tac x = n in spec, auto)
  1727 apply (rule_tac x = M in exI, auto)
  1728 apply (drule_tac x = m in spec, simp)
  1729 apply (drule_tac x = na in spec, auto)
  1730 done
  1731 
  1732 lemma Cauchy_iff2:
  1733   "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse(real (Suc j))))"
  1734   unfolding metric_Cauchy_iff2 dist_real_def ..
  1735 
  1736 lemma Cauchy_subseq_Cauchy:
  1737   "\<lbrakk> Cauchy X; subseq f \<rbrakk> \<Longrightarrow> Cauchy (X o f)"
  1738 apply (auto simp add: Cauchy_def)
  1739 apply (drule_tac x=e in spec, clarify)
  1740 apply (rule_tac x=M in exI, clarify)
  1741 apply (blast intro: le_trans [OF _ seq_suble] dest!: spec)
  1742 done
  1743 
  1744 theorem LIMSEQ_imp_Cauchy:
  1745   assumes X: "X ----> a" shows "Cauchy X"
  1746 proof (rule metric_CauchyI)
  1747   fix e::real assume "0 < e"
  1748   hence "0 < e/2" by simp
  1749   with X have "\<exists>N. \<forall>n\<ge>N. dist (X n) a < e/2" by (rule metric_LIMSEQ_D)
  1750   then obtain N where N: "\<forall>n\<ge>N. dist (X n) a < e/2" ..
  1751   show "\<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < e"
  1752   proof (intro exI allI impI)
  1753     fix m assume "N \<le> m"
  1754     hence m: "dist (X m) a < e/2" using N by fast
  1755     fix n assume "N \<le> n"
  1756     hence n: "dist (X n) a < e/2" using N by fast
  1757     have "dist (X m) (X n) \<le> dist (X m) a + dist (X n) a"
  1758       by (rule dist_triangle2)
  1759     also from m n have "\<dots> < e" by simp
  1760     finally show "dist (X m) (X n) < e" .
  1761   qed
  1762 qed
  1763 
  1764 lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
  1765 unfolding convergent_def
  1766 by (erule exE, erule LIMSEQ_imp_Cauchy)
  1767 
  1768 subsubsection \<open>Cauchy Sequences are Convergent\<close>
  1769 
  1770 class complete_space = metric_space +
  1771   assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
  1772 
  1773 lemma Cauchy_convergent_iff:
  1774   fixes X :: "nat \<Rightarrow> 'a::complete_space"
  1775   shows "Cauchy X = convergent X"
  1776 by (fast intro: Cauchy_convergent convergent_Cauchy)
  1777 
  1778 subsection \<open>The set of real numbers is a complete metric space\<close>
  1779 
  1780 text \<open>
  1781 Proof that Cauchy sequences converge based on the one from
  1782 @{url "http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html"}
  1783 \<close>
  1784 
  1785 text \<open>
  1786   If sequence @{term "X"} is Cauchy, then its limit is the lub of
  1787   @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
  1788 \<close>
  1789 
  1790 lemma increasing_LIMSEQ:
  1791   fixes f :: "nat \<Rightarrow> real"
  1792   assumes inc: "\<And>n. f n \<le> f (Suc n)"
  1793       and bdd: "\<And>n. f n \<le> l"
  1794       and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
  1795   shows "f ----> l"
  1796 proof (rule increasing_tendsto)
  1797   fix x assume "x < l"
  1798   with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
  1799     by auto
  1800   from en[OF \<open>0 < e\<close>] obtain n where "l - e \<le> f n"
  1801     by (auto simp: field_simps)
  1802   with \<open>e < l - x\<close> \<open>0 < e\<close> have "x < f n" by simp
  1803   with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
  1804     by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
  1805 qed (insert bdd, auto)
  1806 
  1807 lemma real_Cauchy_convergent:
  1808   fixes X :: "nat \<Rightarrow> real"
  1809   assumes X: "Cauchy X"
  1810   shows "convergent X"
  1811 proof -
  1812   def S \<equiv> "{x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"
  1813   then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto
  1814 
  1815   { fix N x assume N: "\<forall>n\<ge>N. X n < x"
  1816   fix y::real assume "y \<in> S"
  1817   hence "\<exists>M. \<forall>n\<ge>M. y < X n"
  1818     by (simp add: S_def)
  1819   then obtain M where "\<forall>n\<ge>M. y < X n" ..
  1820   hence "y < X (max M N)" by simp
  1821   also have "\<dots> < x" using N by simp
  1822   finally have "y \<le> x"
  1823     by (rule order_less_imp_le) }
  1824   note bound_isUb = this
  1825 
  1826   obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
  1827     using X[THEN metric_CauchyD, OF zero_less_one] by auto
  1828   hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
  1829   have [simp]: "S \<noteq> {}"
  1830   proof (intro exI ex_in_conv[THEN iffD1])
  1831     from N have "\<forall>n\<ge>N. X N - 1 < X n"
  1832       by (simp add: abs_diff_less_iff dist_real_def)
  1833     thus "X N - 1 \<in> S" by (rule mem_S)
  1834   qed
  1835   have [simp]: "bdd_above S"
  1836   proof
  1837     from N have "\<forall>n\<ge>N. X n < X N + 1"
  1838       by (simp add: abs_diff_less_iff dist_real_def)
  1839     thus "\<And>s. s \<in> S \<Longrightarrow>  s \<le> X N + 1"
  1840       by (rule bound_isUb)
  1841   qed
  1842   have "X ----> Sup S"
  1843   proof (rule metric_LIMSEQ_I)
  1844   fix r::real assume "0 < r"
  1845   hence r: "0 < r/2" by simp
  1846   obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
  1847     using metric_CauchyD [OF X r] by auto
  1848   hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
  1849   hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
  1850     by (simp only: dist_real_def abs_diff_less_iff)
  1851 
  1852   from N have "\<forall>n\<ge>N. X N - r/2 < X n" by fast
  1853   hence "X N - r/2 \<in> S" by (rule mem_S)
  1854   hence 1: "X N - r/2 \<le> Sup S" by (simp add: cSup_upper)
  1855 
  1856   from N have "\<forall>n\<ge>N. X n < X N + r/2" by fast
  1857   from bound_isUb[OF this]
  1858   have 2: "Sup S \<le> X N + r/2"
  1859     by (intro cSup_least) simp_all
  1860 
  1861   show "\<exists>N. \<forall>n\<ge>N. dist (X n) (Sup S) < r"
  1862   proof (intro exI allI impI)
  1863     fix n assume n: "N \<le> n"
  1864     from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
  1865     thus "dist (X n) (Sup S) < r" using 1 2
  1866       by (simp add: abs_diff_less_iff dist_real_def)
  1867   qed
  1868   qed
  1869   then show ?thesis unfolding convergent_def by auto
  1870 qed
  1871 
  1872 instance real :: complete_space
  1873   by intro_classes (rule real_Cauchy_convergent)
  1874 
  1875 class banach = real_normed_vector + complete_space
  1876 
  1877 instance real :: banach ..
  1878 
  1879 lemma tendsto_at_topI_sequentially:
  1880   fixes f :: "real \<Rightarrow> 'b::first_countable_topology"
  1881   assumes *: "\<And>X. filterlim X at_top sequentially \<Longrightarrow> (\<lambda>n. f (X n)) ----> y"
  1882   shows "(f ---> y) at_top"
  1883 proof -
  1884   from nhds_countable[of y] guess A . note A = this
  1885 
  1886   have "\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m"
  1887   proof (rule ccontr)
  1888     assume "\<not> (\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m)"
  1889     then obtain m where "\<And>k. \<exists>x\<ge>k. f x \<notin> A m"
  1890       by auto
  1891     then have "\<exists>X. \<forall>n. (f (X n) \<notin> A m) \<and> max n (X n) + 1 \<le> X (Suc n)"
  1892       by (intro dependent_nat_choice) (auto simp del: max.bounded_iff)
  1893     then obtain X where X: "\<And>n. f (X n) \<notin> A m" "\<And>n. max n (X n) + 1 \<le> X (Suc n)"
  1894       by auto
  1895     { fix n have "1 \<le> n \<longrightarrow> real n \<le> X n"
  1896         using X[of "n - 1"] by auto }
  1897     then have "filterlim X at_top sequentially"
  1898       by (force intro!: filterlim_at_top_mono[OF filterlim_real_sequentially]
  1899                 simp: eventually_sequentially)
  1900     from topological_tendstoD[OF *[OF this] A(2, 3), of m] X(1) show False
  1901       by auto
  1902   qed
  1903   then obtain k where "\<And>m x. k m \<le> x \<Longrightarrow> f x \<in> A m"
  1904     by metis
  1905   then show ?thesis
  1906     unfolding at_top_def A
  1907     by (intro filterlim_base[where i=k]) auto
  1908 qed
  1909 
  1910 lemma tendsto_at_topI_sequentially_real:
  1911   fixes f :: "real \<Rightarrow> real"
  1912   assumes mono: "mono f"
  1913   assumes limseq: "(\<lambda>n. f (real n)) ----> y"
  1914   shows "(f ---> y) at_top"
  1915 proof (rule tendstoI)
  1916   fix e :: real assume "0 < e"
  1917   with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e"
  1918     by (auto simp: lim_sequentially dist_real_def)
  1919   { fix x :: real
  1920     obtain n where "x \<le> real_of_nat n"
  1921       using ex_le_of_nat[of x] ..
  1922     note monoD[OF mono this]
  1923     also have "f (real_of_nat n) \<le> y"
  1924       by (rule LIMSEQ_le_const[OF limseq])
  1925          (auto intro: exI[of _ n] monoD[OF mono] simp: real_eq_of_nat[symmetric])
  1926     finally have "f x \<le> y" . }
  1927   note le = this
  1928   have "eventually (\<lambda>x. real N \<le> x) at_top"
  1929     by (rule eventually_ge_at_top)
  1930   then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
  1931   proof eventually_elim
  1932     fix x assume N': "real N \<le> x"
  1933     with N[of N] le have "y - f (real N) < e" by auto
  1934     moreover note monoD[OF mono N']
  1935     ultimately show "dist (f x) y < e"
  1936       using le[of x] by (auto simp: dist_real_def field_simps)
  1937   qed
  1938 qed
  1939 
  1940 end
  1941