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