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