src/HOL/RealVector.thy
author huffman
Thu Jun 11 10:37:02 2009 -0700 (2009-06-11)
changeset 31564 d2abf6f6f619
parent 31494 1ba61c7b129f
child 31565 da5a5589418e
permissions -rw-r--r--
subsection for real instances; new lemmas for open sets of reals
     1 (*  Title:      HOL/RealVector.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 header {* Vector Spaces and Algebras over the Reals *}
     6 
     7 theory RealVector
     8 imports RealPow
     9 begin
    10 
    11 subsection {* Locale for additive functions *}
    12 
    13 locale additive =
    14   fixes f :: "'a::ab_group_add \<Rightarrow> 'b::ab_group_add"
    15   assumes add: "f (x + y) = f x + f y"
    16 begin
    17 
    18 lemma zero: "f 0 = 0"
    19 proof -
    20   have "f 0 = f (0 + 0)" by simp
    21   also have "\<dots> = f 0 + f 0" by (rule add)
    22   finally show "f 0 = 0" by simp
    23 qed
    24 
    25 lemma minus: "f (- x) = - f x"
    26 proof -
    27   have "f (- x) + f x = f (- x + x)" by (rule add [symmetric])
    28   also have "\<dots> = - f x + f x" by (simp add: zero)
    29   finally show "f (- x) = - f x" by (rule add_right_imp_eq)
    30 qed
    31 
    32 lemma diff: "f (x - y) = f x - f y"
    33 by (simp add: diff_def add minus)
    34 
    35 lemma setsum: "f (setsum g A) = (\<Sum>x\<in>A. f (g x))"
    36 apply (cases "finite A")
    37 apply (induct set: finite)
    38 apply (simp add: zero)
    39 apply (simp add: add)
    40 apply (simp add: zero)
    41 done
    42 
    43 end
    44 
    45 subsection {* Vector spaces *}
    46 
    47 locale vector_space =
    48   fixes scale :: "'a::field \<Rightarrow> 'b::ab_group_add \<Rightarrow> 'b"
    49   assumes scale_right_distrib [algebra_simps]:
    50     "scale a (x + y) = scale a x + scale a y"
    51   and scale_left_distrib [algebra_simps]:
    52     "scale (a + b) x = scale a x + scale b x"
    53   and scale_scale [simp]: "scale a (scale b x) = scale (a * b) x"
    54   and scale_one [simp]: "scale 1 x = x"
    55 begin
    56 
    57 lemma scale_left_commute:
    58   "scale a (scale b x) = scale b (scale a x)"
    59 by (simp add: mult_commute)
    60 
    61 lemma scale_zero_left [simp]: "scale 0 x = 0"
    62   and scale_minus_left [simp]: "scale (- a) x = - (scale a x)"
    63   and scale_left_diff_distrib [algebra_simps]:
    64         "scale (a - b) x = scale a x - scale b x"
    65 proof -
    66   interpret s: additive "\<lambda>a. scale a x"
    67     proof qed (rule scale_left_distrib)
    68   show "scale 0 x = 0" by (rule s.zero)
    69   show "scale (- a) x = - (scale a x)" by (rule s.minus)
    70   show "scale (a - b) x = scale a x - scale b x" by (rule s.diff)
    71 qed
    72 
    73 lemma scale_zero_right [simp]: "scale a 0 = 0"
    74   and scale_minus_right [simp]: "scale a (- x) = - (scale a x)"
    75   and scale_right_diff_distrib [algebra_simps]:
    76         "scale a (x - y) = scale a x - scale a y"
    77 proof -
    78   interpret s: additive "\<lambda>x. scale a x"
    79     proof qed (rule scale_right_distrib)
    80   show "scale a 0 = 0" by (rule s.zero)
    81   show "scale a (- x) = - (scale a x)" by (rule s.minus)
    82   show "scale a (x - y) = scale a x - scale a y" by (rule s.diff)
    83 qed
    84 
    85 lemma scale_eq_0_iff [simp]:
    86   "scale a x = 0 \<longleftrightarrow> a = 0 \<or> x = 0"
    87 proof cases
    88   assume "a = 0" thus ?thesis by simp
    89 next
    90   assume anz [simp]: "a \<noteq> 0"
    91   { assume "scale a x = 0"
    92     hence "scale (inverse a) (scale a x) = 0" by simp
    93     hence "x = 0" by simp }
    94   thus ?thesis by force
    95 qed
    96 
    97 lemma scale_left_imp_eq:
    98   "\<lbrakk>a \<noteq> 0; scale a x = scale a y\<rbrakk> \<Longrightarrow> x = y"
    99 proof -
   100   assume nonzero: "a \<noteq> 0"
   101   assume "scale a x = scale a y"
   102   hence "scale a (x - y) = 0"
   103      by (simp add: scale_right_diff_distrib)
   104   hence "x - y = 0" by (simp add: nonzero)
   105   thus "x = y" by (simp only: right_minus_eq)
   106 qed
   107 
   108 lemma scale_right_imp_eq:
   109   "\<lbrakk>x \<noteq> 0; scale a x = scale b x\<rbrakk> \<Longrightarrow> a = b"
   110 proof -
   111   assume nonzero: "x \<noteq> 0"
   112   assume "scale a x = scale b x"
   113   hence "scale (a - b) x = 0"
   114      by (simp add: scale_left_diff_distrib)
   115   hence "a - b = 0" by (simp add: nonzero)
   116   thus "a = b" by (simp only: right_minus_eq)
   117 qed
   118 
   119 lemma scale_cancel_left:
   120   "scale a x = scale a y \<longleftrightarrow> x = y \<or> a = 0"
   121 by (auto intro: scale_left_imp_eq)
   122 
   123 lemma scale_cancel_right:
   124   "scale a x = scale b x \<longleftrightarrow> a = b \<or> x = 0"
   125 by (auto intro: scale_right_imp_eq)
   126 
   127 end
   128 
   129 subsection {* Real vector spaces *}
   130 
   131 class scaleR =
   132   fixes scaleR :: "real \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "*\<^sub>R" 75)
   133 begin
   134 
   135 abbreviation
   136   divideR :: "'a \<Rightarrow> real \<Rightarrow> 'a" (infixl "'/\<^sub>R" 70)
   137 where
   138   "x /\<^sub>R r == scaleR (inverse r) x"
   139 
   140 end
   141 
   142 class real_vector = scaleR + ab_group_add +
   143   assumes scaleR_right_distrib: "scaleR a (x + y) = scaleR a x + scaleR a y"
   144   and scaleR_left_distrib: "scaleR (a + b) x = scaleR a x + scaleR b x"
   145   and scaleR_scaleR: "scaleR a (scaleR b x) = scaleR (a * b) x"
   146   and scaleR_one: "scaleR 1 x = x"
   147 
   148 interpretation real_vector:
   149   vector_space "scaleR :: real \<Rightarrow> 'a \<Rightarrow> 'a::real_vector"
   150 apply unfold_locales
   151 apply (rule scaleR_right_distrib)
   152 apply (rule scaleR_left_distrib)
   153 apply (rule scaleR_scaleR)
   154 apply (rule scaleR_one)
   155 done
   156 
   157 text {* Recover original theorem names *}
   158 
   159 lemmas scaleR_left_commute = real_vector.scale_left_commute
   160 lemmas scaleR_zero_left = real_vector.scale_zero_left
   161 lemmas scaleR_minus_left = real_vector.scale_minus_left
   162 lemmas scaleR_left_diff_distrib = real_vector.scale_left_diff_distrib
   163 lemmas scaleR_zero_right = real_vector.scale_zero_right
   164 lemmas scaleR_minus_right = real_vector.scale_minus_right
   165 lemmas scaleR_right_diff_distrib = real_vector.scale_right_diff_distrib
   166 lemmas scaleR_eq_0_iff = real_vector.scale_eq_0_iff
   167 lemmas scaleR_left_imp_eq = real_vector.scale_left_imp_eq
   168 lemmas scaleR_right_imp_eq = real_vector.scale_right_imp_eq
   169 lemmas scaleR_cancel_left = real_vector.scale_cancel_left
   170 lemmas scaleR_cancel_right = real_vector.scale_cancel_right
   171 
   172 lemma scaleR_minus1_left [simp]:
   173   fixes x :: "'a::real_vector"
   174   shows "scaleR (-1) x = - x"
   175   using scaleR_minus_left [of 1 x] by simp
   176 
   177 class real_algebra = real_vector + ring +
   178   assumes mult_scaleR_left [simp]: "scaleR a x * y = scaleR a (x * y)"
   179   and mult_scaleR_right [simp]: "x * scaleR a y = scaleR a (x * y)"
   180 
   181 class real_algebra_1 = real_algebra + ring_1
   182 
   183 class real_div_algebra = real_algebra_1 + division_ring
   184 
   185 class real_field = real_div_algebra + field
   186 
   187 instantiation real :: real_field
   188 begin
   189 
   190 definition
   191   real_scaleR_def [simp]: "scaleR a x = a * x"
   192 
   193 instance proof
   194 qed (simp_all add: algebra_simps)
   195 
   196 end
   197 
   198 interpretation scaleR_left: additive "(\<lambda>a. scaleR a x::'a::real_vector)"
   199 proof qed (rule scaleR_left_distrib)
   200 
   201 interpretation scaleR_right: additive "(\<lambda>x. scaleR a x::'a::real_vector)"
   202 proof qed (rule scaleR_right_distrib)
   203 
   204 lemma nonzero_inverse_scaleR_distrib:
   205   fixes x :: "'a::real_div_algebra" shows
   206   "\<lbrakk>a \<noteq> 0; x \<noteq> 0\<rbrakk> \<Longrightarrow> inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
   207 by (rule inverse_unique, simp)
   208 
   209 lemma inverse_scaleR_distrib:
   210   fixes x :: "'a::{real_div_algebra,division_by_zero}"
   211   shows "inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
   212 apply (case_tac "a = 0", simp)
   213 apply (case_tac "x = 0", simp)
   214 apply (erule (1) nonzero_inverse_scaleR_distrib)
   215 done
   216 
   217 
   218 subsection {* Embedding of the Reals into any @{text real_algebra_1}:
   219 @{term of_real} *}
   220 
   221 definition
   222   of_real :: "real \<Rightarrow> 'a::real_algebra_1" where
   223   "of_real r = scaleR r 1"
   224 
   225 lemma scaleR_conv_of_real: "scaleR r x = of_real r * x"
   226 by (simp add: of_real_def)
   227 
   228 lemma of_real_0 [simp]: "of_real 0 = 0"
   229 by (simp add: of_real_def)
   230 
   231 lemma of_real_1 [simp]: "of_real 1 = 1"
   232 by (simp add: of_real_def)
   233 
   234 lemma of_real_add [simp]: "of_real (x + y) = of_real x + of_real y"
   235 by (simp add: of_real_def scaleR_left_distrib)
   236 
   237 lemma of_real_minus [simp]: "of_real (- x) = - of_real x"
   238 by (simp add: of_real_def)
   239 
   240 lemma of_real_diff [simp]: "of_real (x - y) = of_real x - of_real y"
   241 by (simp add: of_real_def scaleR_left_diff_distrib)
   242 
   243 lemma of_real_mult [simp]: "of_real (x * y) = of_real x * of_real y"
   244 by (simp add: of_real_def mult_commute)
   245 
   246 lemma nonzero_of_real_inverse:
   247   "x \<noteq> 0 \<Longrightarrow> of_real (inverse x) =
   248    inverse (of_real x :: 'a::real_div_algebra)"
   249 by (simp add: of_real_def nonzero_inverse_scaleR_distrib)
   250 
   251 lemma of_real_inverse [simp]:
   252   "of_real (inverse x) =
   253    inverse (of_real x :: 'a::{real_div_algebra,division_by_zero})"
   254 by (simp add: of_real_def inverse_scaleR_distrib)
   255 
   256 lemma nonzero_of_real_divide:
   257   "y \<noteq> 0 \<Longrightarrow> of_real (x / y) =
   258    (of_real x / of_real y :: 'a::real_field)"
   259 by (simp add: divide_inverse nonzero_of_real_inverse)
   260 
   261 lemma of_real_divide [simp]:
   262   "of_real (x / y) =
   263    (of_real x / of_real y :: 'a::{real_field,division_by_zero})"
   264 by (simp add: divide_inverse)
   265 
   266 lemma of_real_power [simp]:
   267   "of_real (x ^ n) = (of_real x :: 'a::{real_algebra_1}) ^ n"
   268 by (induct n) simp_all
   269 
   270 lemma of_real_eq_iff [simp]: "(of_real x = of_real y) = (x = y)"
   271 by (simp add: of_real_def scaleR_cancel_right)
   272 
   273 lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
   274 
   275 lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
   276 proof
   277   fix r
   278   show "of_real r = id r"
   279     by (simp add: of_real_def)
   280 qed
   281 
   282 text{*Collapse nested embeddings*}
   283 lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
   284 by (induct n) auto
   285 
   286 lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
   287 by (cases z rule: int_diff_cases, simp)
   288 
   289 lemma of_real_number_of_eq:
   290   "of_real (number_of w) = (number_of w :: 'a::{number_ring,real_algebra_1})"
   291 by (simp add: number_of_eq)
   292 
   293 text{*Every real algebra has characteristic zero*}
   294 instance real_algebra_1 < ring_char_0
   295 proof
   296   fix m n :: nat
   297   have "(of_real (of_nat m) = (of_real (of_nat n)::'a)) = (m = n)"
   298     by (simp only: of_real_eq_iff of_nat_eq_iff)
   299   thus "(of_nat m = (of_nat n::'a)) = (m = n)"
   300     by (simp only: of_real_of_nat_eq)
   301 qed
   302 
   303 instance real_field < field_char_0 ..
   304 
   305 
   306 subsection {* The Set of Real Numbers *}
   307 
   308 definition
   309   Reals :: "'a::real_algebra_1 set" where
   310   [code del]: "Reals = range of_real"
   311 
   312 notation (xsymbols)
   313   Reals  ("\<real>")
   314 
   315 lemma Reals_of_real [simp]: "of_real r \<in> Reals"
   316 by (simp add: Reals_def)
   317 
   318 lemma Reals_of_int [simp]: "of_int z \<in> Reals"
   319 by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
   320 
   321 lemma Reals_of_nat [simp]: "of_nat n \<in> Reals"
   322 by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
   323 
   324 lemma Reals_number_of [simp]:
   325   "(number_of w::'a::{number_ring,real_algebra_1}) \<in> Reals"
   326 by (subst of_real_number_of_eq [symmetric], rule Reals_of_real)
   327 
   328 lemma Reals_0 [simp]: "0 \<in> Reals"
   329 apply (unfold Reals_def)
   330 apply (rule range_eqI)
   331 apply (rule of_real_0 [symmetric])
   332 done
   333 
   334 lemma Reals_1 [simp]: "1 \<in> Reals"
   335 apply (unfold Reals_def)
   336 apply (rule range_eqI)
   337 apply (rule of_real_1 [symmetric])
   338 done
   339 
   340 lemma Reals_add [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a + b \<in> Reals"
   341 apply (auto simp add: Reals_def)
   342 apply (rule range_eqI)
   343 apply (rule of_real_add [symmetric])
   344 done
   345 
   346 lemma Reals_minus [simp]: "a \<in> Reals \<Longrightarrow> - a \<in> Reals"
   347 apply (auto simp add: Reals_def)
   348 apply (rule range_eqI)
   349 apply (rule of_real_minus [symmetric])
   350 done
   351 
   352 lemma Reals_diff [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a - b \<in> Reals"
   353 apply (auto simp add: Reals_def)
   354 apply (rule range_eqI)
   355 apply (rule of_real_diff [symmetric])
   356 done
   357 
   358 lemma Reals_mult [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a * b \<in> Reals"
   359 apply (auto simp add: Reals_def)
   360 apply (rule range_eqI)
   361 apply (rule of_real_mult [symmetric])
   362 done
   363 
   364 lemma nonzero_Reals_inverse:
   365   fixes a :: "'a::real_div_algebra"
   366   shows "\<lbrakk>a \<in> Reals; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Reals"
   367 apply (auto simp add: Reals_def)
   368 apply (rule range_eqI)
   369 apply (erule nonzero_of_real_inverse [symmetric])
   370 done
   371 
   372 lemma Reals_inverse [simp]:
   373   fixes a :: "'a::{real_div_algebra,division_by_zero}"
   374   shows "a \<in> Reals \<Longrightarrow> inverse a \<in> Reals"
   375 apply (auto simp add: Reals_def)
   376 apply (rule range_eqI)
   377 apply (rule of_real_inverse [symmetric])
   378 done
   379 
   380 lemma nonzero_Reals_divide:
   381   fixes a b :: "'a::real_field"
   382   shows "\<lbrakk>a \<in> Reals; b \<in> Reals; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   383 apply (auto simp add: Reals_def)
   384 apply (rule range_eqI)
   385 apply (erule nonzero_of_real_divide [symmetric])
   386 done
   387 
   388 lemma Reals_divide [simp]:
   389   fixes a b :: "'a::{real_field,division_by_zero}"
   390   shows "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   391 apply (auto simp add: Reals_def)
   392 apply (rule range_eqI)
   393 apply (rule of_real_divide [symmetric])
   394 done
   395 
   396 lemma Reals_power [simp]:
   397   fixes a :: "'a::{real_algebra_1}"
   398   shows "a \<in> Reals \<Longrightarrow> a ^ n \<in> Reals"
   399 apply (auto simp add: Reals_def)
   400 apply (rule range_eqI)
   401 apply (rule of_real_power [symmetric])
   402 done
   403 
   404 lemma Reals_cases [cases set: Reals]:
   405   assumes "q \<in> \<real>"
   406   obtains (of_real) r where "q = of_real r"
   407   unfolding Reals_def
   408 proof -
   409   from `q \<in> \<real>` have "q \<in> range of_real" unfolding Reals_def .
   410   then obtain r where "q = of_real r" ..
   411   then show thesis ..
   412 qed
   413 
   414 lemma Reals_induct [case_names of_real, induct set: Reals]:
   415   "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
   416   by (rule Reals_cases) auto
   417 
   418 
   419 subsection {* Topological spaces *}
   420 
   421 class "open" =
   422   fixes "open" :: "'a set \<Rightarrow> bool"
   423 
   424 class topological_space = "open" +
   425   assumes open_UNIV [simp, intro]: "open UNIV"
   426   assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
   427   assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union> K)"
   428 begin
   429 
   430 definition
   431   closed :: "'a set \<Rightarrow> bool" where
   432   "closed S \<longleftrightarrow> open (- S)"
   433 
   434 lemma open_empty [intro, simp]: "open {}"
   435   using open_Union [of "{}"] by simp
   436 
   437 lemma open_Un [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
   438   using open_Union [of "{S, T}"] by simp
   439 
   440 lemma open_UN [intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
   441   unfolding UN_eq by (rule open_Union) auto
   442 
   443 lemma open_INT [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
   444   by (induct set: finite) auto
   445 
   446 lemma open_Inter [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
   447   unfolding Inter_def by (rule open_INT)
   448 
   449 lemma closed_empty [intro, simp]:  "closed {}"
   450   unfolding closed_def by simp
   451 
   452 lemma closed_Un [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
   453   unfolding closed_def by auto
   454 
   455 lemma closed_Inter [intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter> K)"
   456   unfolding closed_def Inter_def by auto
   457 
   458 lemma closed_UNIV [intro, simp]: "closed UNIV"
   459   unfolding closed_def by simp
   460 
   461 lemma closed_Int [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
   462   unfolding closed_def by auto
   463 
   464 lemma closed_INT [intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
   465   unfolding closed_def by auto
   466 
   467 lemma closed_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
   468   by (induct set: finite) auto
   469 
   470 lemma closed_Union [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
   471   unfolding Union_def by (rule closed_UN)
   472 
   473 lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
   474   unfolding closed_def by simp
   475 
   476 lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
   477   unfolding closed_def by simp
   478 
   479 lemma open_Diff [intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
   480   unfolding closed_open Diff_eq by (rule open_Int)
   481 
   482 lemma closed_Diff [intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
   483   unfolding open_closed Diff_eq by (rule closed_Int)
   484 
   485 lemma open_Compl [intro]: "closed S \<Longrightarrow> open (- S)"
   486   unfolding closed_open .
   487 
   488 lemma closed_Compl [intro]: "open S \<Longrightarrow> closed (- S)"
   489   unfolding open_closed .
   490 
   491 end
   492 
   493 
   494 subsection {* Metric spaces *}
   495 
   496 class dist =
   497   fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
   498 
   499 class open_dist = "open" + dist +
   500   assumes open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   501 
   502 class metric_space = open_dist +
   503   assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
   504   assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
   505 begin
   506 
   507 lemma dist_self [simp]: "dist x x = 0"
   508 by simp
   509 
   510 lemma zero_le_dist [simp]: "0 \<le> dist x y"
   511 using dist_triangle2 [of x x y] by simp
   512 
   513 lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
   514 by (simp add: less_le)
   515 
   516 lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
   517 by (simp add: not_less)
   518 
   519 lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
   520 by (simp add: le_less)
   521 
   522 lemma dist_commute: "dist x y = dist y x"
   523 proof (rule order_antisym)
   524   show "dist x y \<le> dist y x"
   525     using dist_triangle2 [of x y x] by simp
   526   show "dist y x \<le> dist x y"
   527     using dist_triangle2 [of y x y] by simp
   528 qed
   529 
   530 lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
   531 using dist_triangle2 [of x z y] by (simp add: dist_commute)
   532 
   533 subclass topological_space
   534 proof
   535   have "\<exists>e::real. 0 < e"
   536     by (fast intro: zero_less_one)
   537   then show "open UNIV"
   538     unfolding open_dist by simp
   539 next
   540   fix S T assume "open S" "open T"
   541   then show "open (S \<inter> T)"
   542     unfolding open_dist
   543     apply clarify
   544     apply (drule (1) bspec)+
   545     apply (clarify, rename_tac r s)
   546     apply (rule_tac x="min r s" in exI, simp)
   547     done
   548 next
   549   fix K assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
   550     unfolding open_dist by fast
   551 qed
   552 
   553 end
   554 
   555 
   556 subsection {* Real normed vector spaces *}
   557 
   558 class norm =
   559   fixes norm :: "'a \<Rightarrow> real"
   560 
   561 class sgn_div_norm = scaleR + norm + sgn +
   562   assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
   563 
   564 class dist_norm = dist + norm + minus +
   565   assumes dist_norm: "dist x y = norm (x - y)"
   566 
   567 class real_normed_vector = real_vector + sgn_div_norm + dist_norm + open_dist +
   568   assumes norm_ge_zero [simp]: "0 \<le> norm x"
   569   and norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
   570   and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
   571   and norm_scaleR: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   572 
   573 class real_normed_algebra = real_algebra + real_normed_vector +
   574   assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
   575 
   576 class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   577   assumes norm_one [simp]: "norm 1 = 1"
   578 
   579 class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   580   assumes norm_mult: "norm (x * y) = norm x * norm y"
   581 
   582 class real_normed_field = real_field + real_normed_div_algebra
   583 
   584 instance real_normed_div_algebra < real_normed_algebra_1
   585 proof
   586   fix x y :: 'a
   587   show "norm (x * y) \<le> norm x * norm y"
   588     by (simp add: norm_mult)
   589 next
   590   have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
   591     by (rule norm_mult)
   592   thus "norm (1::'a) = 1" by simp
   593 qed
   594 
   595 lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
   596 by simp
   597 
   598 lemma zero_less_norm_iff [simp]:
   599   fixes x :: "'a::real_normed_vector"
   600   shows "(0 < norm x) = (x \<noteq> 0)"
   601 by (simp add: order_less_le)
   602 
   603 lemma norm_not_less_zero [simp]:
   604   fixes x :: "'a::real_normed_vector"
   605   shows "\<not> norm x < 0"
   606 by (simp add: linorder_not_less)
   607 
   608 lemma norm_le_zero_iff [simp]:
   609   fixes x :: "'a::real_normed_vector"
   610   shows "(norm x \<le> 0) = (x = 0)"
   611 by (simp add: order_le_less)
   612 
   613 lemma norm_minus_cancel [simp]:
   614   fixes x :: "'a::real_normed_vector"
   615   shows "norm (- x) = norm x"
   616 proof -
   617   have "norm (- x) = norm (scaleR (- 1) x)"
   618     by (simp only: scaleR_minus_left scaleR_one)
   619   also have "\<dots> = \<bar>- 1\<bar> * norm x"
   620     by (rule norm_scaleR)
   621   finally show ?thesis by simp
   622 qed
   623 
   624 lemma norm_minus_commute:
   625   fixes a b :: "'a::real_normed_vector"
   626   shows "norm (a - b) = norm (b - a)"
   627 proof -
   628   have "norm (- (b - a)) = norm (b - a)"
   629     by (rule norm_minus_cancel)
   630   thus ?thesis by simp
   631 qed
   632 
   633 lemma norm_triangle_ineq2:
   634   fixes a b :: "'a::real_normed_vector"
   635   shows "norm a - norm b \<le> norm (a - b)"
   636 proof -
   637   have "norm (a - b + b) \<le> norm (a - b) + norm b"
   638     by (rule norm_triangle_ineq)
   639   thus ?thesis by simp
   640 qed
   641 
   642 lemma norm_triangle_ineq3:
   643   fixes a b :: "'a::real_normed_vector"
   644   shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
   645 apply (subst abs_le_iff)
   646 apply auto
   647 apply (rule norm_triangle_ineq2)
   648 apply (subst norm_minus_commute)
   649 apply (rule norm_triangle_ineq2)
   650 done
   651 
   652 lemma norm_triangle_ineq4:
   653   fixes a b :: "'a::real_normed_vector"
   654   shows "norm (a - b) \<le> norm a + norm b"
   655 proof -
   656   have "norm (a + - b) \<le> norm a + norm (- b)"
   657     by (rule norm_triangle_ineq)
   658   thus ?thesis
   659     by (simp only: diff_minus norm_minus_cancel)
   660 qed
   661 
   662 lemma norm_diff_ineq:
   663   fixes a b :: "'a::real_normed_vector"
   664   shows "norm a - norm b \<le> norm (a + b)"
   665 proof -
   666   have "norm a - norm (- b) \<le> norm (a - - b)"
   667     by (rule norm_triangle_ineq2)
   668   thus ?thesis by simp
   669 qed
   670 
   671 lemma norm_diff_triangle_ineq:
   672   fixes a b c d :: "'a::real_normed_vector"
   673   shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
   674 proof -
   675   have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
   676     by (simp add: diff_minus add_ac)
   677   also have "\<dots> \<le> norm (a - c) + norm (b - d)"
   678     by (rule norm_triangle_ineq)
   679   finally show ?thesis .
   680 qed
   681 
   682 lemma abs_norm_cancel [simp]:
   683   fixes a :: "'a::real_normed_vector"
   684   shows "\<bar>norm a\<bar> = norm a"
   685 by (rule abs_of_nonneg [OF norm_ge_zero])
   686 
   687 lemma norm_add_less:
   688   fixes x y :: "'a::real_normed_vector"
   689   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
   690 by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
   691 
   692 lemma norm_mult_less:
   693   fixes x y :: "'a::real_normed_algebra"
   694   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
   695 apply (rule order_le_less_trans [OF norm_mult_ineq])
   696 apply (simp add: mult_strict_mono')
   697 done
   698 
   699 lemma norm_of_real [simp]:
   700   "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
   701 unfolding of_real_def by (simp add: norm_scaleR)
   702 
   703 lemma norm_number_of [simp]:
   704   "norm (number_of w::'a::{number_ring,real_normed_algebra_1})
   705     = \<bar>number_of w\<bar>"
   706 by (subst of_real_number_of_eq [symmetric], rule norm_of_real)
   707 
   708 lemma norm_of_int [simp]:
   709   "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
   710 by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
   711 
   712 lemma norm_of_nat [simp]:
   713   "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
   714 apply (subst of_real_of_nat_eq [symmetric])
   715 apply (subst norm_of_real, simp)
   716 done
   717 
   718 lemma nonzero_norm_inverse:
   719   fixes a :: "'a::real_normed_div_algebra"
   720   shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
   721 apply (rule inverse_unique [symmetric])
   722 apply (simp add: norm_mult [symmetric])
   723 done
   724 
   725 lemma norm_inverse:
   726   fixes a :: "'a::{real_normed_div_algebra,division_by_zero}"
   727   shows "norm (inverse a) = inverse (norm a)"
   728 apply (case_tac "a = 0", simp)
   729 apply (erule nonzero_norm_inverse)
   730 done
   731 
   732 lemma nonzero_norm_divide:
   733   fixes a b :: "'a::real_normed_field"
   734   shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
   735 by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
   736 
   737 lemma norm_divide:
   738   fixes a b :: "'a::{real_normed_field,division_by_zero}"
   739   shows "norm (a / b) = norm a / norm b"
   740 by (simp add: divide_inverse norm_mult norm_inverse)
   741 
   742 lemma norm_power_ineq:
   743   fixes x :: "'a::{real_normed_algebra_1}"
   744   shows "norm (x ^ n) \<le> norm x ^ n"
   745 proof (induct n)
   746   case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
   747 next
   748   case (Suc n)
   749   have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
   750     by (rule norm_mult_ineq)
   751   also from Suc have "\<dots> \<le> norm x * norm x ^ n"
   752     using norm_ge_zero by (rule mult_left_mono)
   753   finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
   754     by simp
   755 qed
   756 
   757 lemma norm_power:
   758   fixes x :: "'a::{real_normed_div_algebra}"
   759   shows "norm (x ^ n) = norm x ^ n"
   760 by (induct n) (simp_all add: norm_mult)
   761 
   762 text {* Every normed vector space is a metric space. *}
   763 
   764 instance real_normed_vector < metric_space
   765 proof
   766   fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
   767     unfolding dist_norm by simp
   768 next
   769   fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
   770     unfolding dist_norm
   771     using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
   772 qed
   773 
   774 
   775 subsection {* Class instances for real numbers *}
   776 
   777 instantiation real :: real_normed_field
   778 begin
   779 
   780 definition real_norm_def [simp]:
   781   "norm r = \<bar>r\<bar>"
   782 
   783 definition dist_real_def:
   784   "dist x y = \<bar>x - y\<bar>"
   785 
   786 definition open_real_def [code del]:
   787   "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   788 
   789 instance
   790 apply (intro_classes, unfold real_norm_def real_scaleR_def)
   791 apply (rule dist_real_def)
   792 apply (rule open_real_def)
   793 apply (simp add: real_sgn_def)
   794 apply (rule abs_ge_zero)
   795 apply (rule abs_eq_0)
   796 apply (rule abs_triangle_ineq)
   797 apply (rule abs_mult)
   798 apply (rule abs_mult)
   799 done
   800 
   801 end
   802 
   803 lemma open_real_lessThan [simp]:
   804   fixes a :: real shows "open {..<a}"
   805 unfolding open_real_def dist_real_def
   806 proof (clarify)
   807   fix x assume "x < a"
   808   hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
   809   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
   810 qed
   811 
   812 lemma open_real_greaterThan [simp]:
   813   fixes a :: real shows "open {a<..}"
   814 unfolding open_real_def dist_real_def
   815 proof (clarify)
   816   fix x assume "a < x"
   817   hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
   818   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
   819 qed
   820 
   821 lemma open_real_greaterThanLessThan [simp]:
   822   fixes a b :: real shows "open {a<..<b}"
   823 proof -
   824   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   825   thus "open {a<..<b}" by (simp add: open_Int)
   826 qed
   827 
   828 
   829 subsection {* Extra type constraints *}
   830 
   831 text {* Only allow @{term "open"} in class @{text topological_space}. *}
   832 
   833 setup {* Sign.add_const_constraint
   834   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"}) *}
   835 
   836 text {* Only allow @{term dist} in class @{text metric_space}. *}
   837 
   838 setup {* Sign.add_const_constraint
   839   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"}) *}
   840 
   841 text {* Only allow @{term norm} in class @{text real_normed_vector}. *}
   842 
   843 setup {* Sign.add_const_constraint
   844   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"}) *}
   845 
   846 
   847 subsection {* Sign function *}
   848 
   849 lemma norm_sgn:
   850   "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
   851 by (simp add: sgn_div_norm norm_scaleR)
   852 
   853 lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
   854 by (simp add: sgn_div_norm)
   855 
   856 lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
   857 by (simp add: sgn_div_norm)
   858 
   859 lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
   860 by (simp add: sgn_div_norm)
   861 
   862 lemma sgn_scaleR:
   863   "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
   864 by (simp add: sgn_div_norm norm_scaleR mult_ac)
   865 
   866 lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
   867 by (simp add: sgn_div_norm)
   868 
   869 lemma sgn_of_real:
   870   "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
   871 unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
   872 
   873 lemma sgn_mult:
   874   fixes x y :: "'a::real_normed_div_algebra"
   875   shows "sgn (x * y) = sgn x * sgn y"
   876 by (simp add: sgn_div_norm norm_mult mult_commute)
   877 
   878 lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
   879 by (simp add: sgn_div_norm divide_inverse)
   880 
   881 lemma real_sgn_pos: "0 < (x::real) \<Longrightarrow> sgn x = 1"
   882 unfolding real_sgn_eq by simp
   883 
   884 lemma real_sgn_neg: "(x::real) < 0 \<Longrightarrow> sgn x = -1"
   885 unfolding real_sgn_eq by simp
   886 
   887 
   888 subsection {* Bounded Linear and Bilinear Operators *}
   889 
   890 locale bounded_linear = additive +
   891   constrains f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
   892   assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
   893   assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
   894 begin
   895 
   896 lemma pos_bounded:
   897   "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
   898 proof -
   899   obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
   900     using bounded by fast
   901   show ?thesis
   902   proof (intro exI impI conjI allI)
   903     show "0 < max 1 K"
   904       by (rule order_less_le_trans [OF zero_less_one le_maxI1])
   905   next
   906     fix x
   907     have "norm (f x) \<le> norm x * K" using K .
   908     also have "\<dots> \<le> norm x * max 1 K"
   909       by (rule mult_left_mono [OF le_maxI2 norm_ge_zero])
   910     finally show "norm (f x) \<le> norm x * max 1 K" .
   911   qed
   912 qed
   913 
   914 lemma nonneg_bounded:
   915   "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
   916 proof -
   917   from pos_bounded
   918   show ?thesis by (auto intro: order_less_imp_le)
   919 qed
   920 
   921 end
   922 
   923 locale bounded_bilinear =
   924   fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
   925                  \<Rightarrow> 'c::real_normed_vector"
   926     (infixl "**" 70)
   927   assumes add_left: "prod (a + a') b = prod a b + prod a' b"
   928   assumes add_right: "prod a (b + b') = prod a b + prod a b'"
   929   assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
   930   assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
   931   assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
   932 begin
   933 
   934 lemma pos_bounded:
   935   "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
   936 apply (cut_tac bounded, erule exE)
   937 apply (rule_tac x="max 1 K" in exI, safe)
   938 apply (rule order_less_le_trans [OF zero_less_one le_maxI1])
   939 apply (drule spec, drule spec, erule order_trans)
   940 apply (rule mult_left_mono [OF le_maxI2])
   941 apply (intro mult_nonneg_nonneg norm_ge_zero)
   942 done
   943 
   944 lemma nonneg_bounded:
   945   "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
   946 proof -
   947   from pos_bounded
   948   show ?thesis by (auto intro: order_less_imp_le)
   949 qed
   950 
   951 lemma additive_right: "additive (\<lambda>b. prod a b)"
   952 by (rule additive.intro, rule add_right)
   953 
   954 lemma additive_left: "additive (\<lambda>a. prod a b)"
   955 by (rule additive.intro, rule add_left)
   956 
   957 lemma zero_left: "prod 0 b = 0"
   958 by (rule additive.zero [OF additive_left])
   959 
   960 lemma zero_right: "prod a 0 = 0"
   961 by (rule additive.zero [OF additive_right])
   962 
   963 lemma minus_left: "prod (- a) b = - prod a b"
   964 by (rule additive.minus [OF additive_left])
   965 
   966 lemma minus_right: "prod a (- b) = - prod a b"
   967 by (rule additive.minus [OF additive_right])
   968 
   969 lemma diff_left:
   970   "prod (a - a') b = prod a b - prod a' b"
   971 by (rule additive.diff [OF additive_left])
   972 
   973 lemma diff_right:
   974   "prod a (b - b') = prod a b - prod a b'"
   975 by (rule additive.diff [OF additive_right])
   976 
   977 lemma bounded_linear_left:
   978   "bounded_linear (\<lambda>a. a ** b)"
   979 apply (unfold_locales)
   980 apply (rule add_left)
   981 apply (rule scaleR_left)
   982 apply (cut_tac bounded, safe)
   983 apply (rule_tac x="norm b * K" in exI)
   984 apply (simp add: mult_ac)
   985 done
   986 
   987 lemma bounded_linear_right:
   988   "bounded_linear (\<lambda>b. a ** b)"
   989 apply (unfold_locales)
   990 apply (rule add_right)
   991 apply (rule scaleR_right)
   992 apply (cut_tac bounded, safe)
   993 apply (rule_tac x="norm a * K" in exI)
   994 apply (simp add: mult_ac)
   995 done
   996 
   997 lemma prod_diff_prod:
   998   "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
   999 by (simp add: diff_left diff_right)
  1000 
  1001 end
  1002 
  1003 interpretation mult:
  1004   bounded_bilinear "op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra"
  1005 apply (rule bounded_bilinear.intro)
  1006 apply (rule left_distrib)
  1007 apply (rule right_distrib)
  1008 apply (rule mult_scaleR_left)
  1009 apply (rule mult_scaleR_right)
  1010 apply (rule_tac x="1" in exI)
  1011 apply (simp add: norm_mult_ineq)
  1012 done
  1013 
  1014 interpretation mult_left:
  1015   bounded_linear "(\<lambda>x::'a::real_normed_algebra. x * y)"
  1016 by (rule mult.bounded_linear_left)
  1017 
  1018 interpretation mult_right:
  1019   bounded_linear "(\<lambda>y::'a::real_normed_algebra. x * y)"
  1020 by (rule mult.bounded_linear_right)
  1021 
  1022 interpretation divide:
  1023   bounded_linear "(\<lambda>x::'a::real_normed_field. x / y)"
  1024 unfolding divide_inverse by (rule mult.bounded_linear_left)
  1025 
  1026 interpretation scaleR: bounded_bilinear "scaleR"
  1027 apply (rule bounded_bilinear.intro)
  1028 apply (rule scaleR_left_distrib)
  1029 apply (rule scaleR_right_distrib)
  1030 apply simp
  1031 apply (rule scaleR_left_commute)
  1032 apply (rule_tac x="1" in exI)
  1033 apply (simp add: norm_scaleR)
  1034 done
  1035 
  1036 interpretation scaleR_left: bounded_linear "\<lambda>r. scaleR r x"
  1037 by (rule scaleR.bounded_linear_left)
  1038 
  1039 interpretation scaleR_right: bounded_linear "\<lambda>x. scaleR r x"
  1040 by (rule scaleR.bounded_linear_right)
  1041 
  1042 interpretation of_real: bounded_linear "\<lambda>r. of_real r"
  1043 unfolding of_real_def by (rule scaleR.bounded_linear_left)
  1044 
  1045 end