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