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