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