src/HOL/RealVector.thy
author huffman
Fri Jun 12 11:39:23 2009 -0700 (2009-06-12)
changeset 31586 d4707b99e631
parent 31567 0fb78b3a9145
child 35216 7641e8d831d2
permissions -rw-r--r--
declare norm_scaleR [simp]; declare scaleR_cancel lemmas [simp]
     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 [simp]:
   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 [simp]:
   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 lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
   534 using dist_triangle2 [of x y a] by (simp add: dist_commute)
   535 
   536 subclass topological_space
   537 proof
   538   have "\<exists>e::real. 0 < e"
   539     by (fast intro: zero_less_one)
   540   then show "open UNIV"
   541     unfolding open_dist by simp
   542 next
   543   fix S T assume "open S" "open T"
   544   then show "open (S \<inter> T)"
   545     unfolding open_dist
   546     apply clarify
   547     apply (drule (1) bspec)+
   548     apply (clarify, rename_tac r s)
   549     apply (rule_tac x="min r s" in exI, simp)
   550     done
   551 next
   552   fix K assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
   553     unfolding open_dist by fast
   554 qed
   555 
   556 end
   557 
   558 
   559 subsection {* Real normed vector spaces *}
   560 
   561 class norm =
   562   fixes norm :: "'a \<Rightarrow> real"
   563 
   564 class sgn_div_norm = scaleR + norm + sgn +
   565   assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
   566 
   567 class dist_norm = dist + norm + minus +
   568   assumes dist_norm: "dist x y = norm (x - y)"
   569 
   570 class real_normed_vector = real_vector + sgn_div_norm + dist_norm + open_dist +
   571   assumes norm_ge_zero [simp]: "0 \<le> norm x"
   572   and norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
   573   and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
   574   and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   575 
   576 class real_normed_algebra = real_algebra + real_normed_vector +
   577   assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
   578 
   579 class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   580   assumes norm_one [simp]: "norm 1 = 1"
   581 
   582 class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   583   assumes norm_mult: "norm (x * y) = norm x * norm y"
   584 
   585 class real_normed_field = real_field + real_normed_div_algebra
   586 
   587 instance real_normed_div_algebra < real_normed_algebra_1
   588 proof
   589   fix x y :: 'a
   590   show "norm (x * y) \<le> norm x * norm y"
   591     by (simp add: norm_mult)
   592 next
   593   have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
   594     by (rule norm_mult)
   595   thus "norm (1::'a) = 1" by simp
   596 qed
   597 
   598 lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
   599 by simp
   600 
   601 lemma zero_less_norm_iff [simp]:
   602   fixes x :: "'a::real_normed_vector"
   603   shows "(0 < norm x) = (x \<noteq> 0)"
   604 by (simp add: order_less_le)
   605 
   606 lemma norm_not_less_zero [simp]:
   607   fixes x :: "'a::real_normed_vector"
   608   shows "\<not> norm x < 0"
   609 by (simp add: linorder_not_less)
   610 
   611 lemma norm_le_zero_iff [simp]:
   612   fixes x :: "'a::real_normed_vector"
   613   shows "(norm x \<le> 0) = (x = 0)"
   614 by (simp add: order_le_less)
   615 
   616 lemma norm_minus_cancel [simp]:
   617   fixes x :: "'a::real_normed_vector"
   618   shows "norm (- x) = norm x"
   619 proof -
   620   have "norm (- x) = norm (scaleR (- 1) x)"
   621     by (simp only: scaleR_minus_left scaleR_one)
   622   also have "\<dots> = \<bar>- 1\<bar> * norm x"
   623     by (rule norm_scaleR)
   624   finally show ?thesis by simp
   625 qed
   626 
   627 lemma norm_minus_commute:
   628   fixes a b :: "'a::real_normed_vector"
   629   shows "norm (a - b) = norm (b - a)"
   630 proof -
   631   have "norm (- (b - a)) = norm (b - a)"
   632     by (rule norm_minus_cancel)
   633   thus ?thesis by simp
   634 qed
   635 
   636 lemma norm_triangle_ineq2:
   637   fixes a b :: "'a::real_normed_vector"
   638   shows "norm a - norm b \<le> norm (a - b)"
   639 proof -
   640   have "norm (a - b + b) \<le> norm (a - b) + norm b"
   641     by (rule norm_triangle_ineq)
   642   thus ?thesis by simp
   643 qed
   644 
   645 lemma norm_triangle_ineq3:
   646   fixes a b :: "'a::real_normed_vector"
   647   shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
   648 apply (subst abs_le_iff)
   649 apply auto
   650 apply (rule norm_triangle_ineq2)
   651 apply (subst norm_minus_commute)
   652 apply (rule norm_triangle_ineq2)
   653 done
   654 
   655 lemma norm_triangle_ineq4:
   656   fixes a b :: "'a::real_normed_vector"
   657   shows "norm (a - b) \<le> norm a + norm b"
   658 proof -
   659   have "norm (a + - b) \<le> norm a + norm (- b)"
   660     by (rule norm_triangle_ineq)
   661   thus ?thesis
   662     by (simp only: diff_minus norm_minus_cancel)
   663 qed
   664 
   665 lemma norm_diff_ineq:
   666   fixes a b :: "'a::real_normed_vector"
   667   shows "norm a - norm b \<le> norm (a + b)"
   668 proof -
   669   have "norm a - norm (- b) \<le> norm (a - - b)"
   670     by (rule norm_triangle_ineq2)
   671   thus ?thesis by simp
   672 qed
   673 
   674 lemma norm_diff_triangle_ineq:
   675   fixes a b c d :: "'a::real_normed_vector"
   676   shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
   677 proof -
   678   have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
   679     by (simp add: diff_minus add_ac)
   680   also have "\<dots> \<le> norm (a - c) + norm (b - d)"
   681     by (rule norm_triangle_ineq)
   682   finally show ?thesis .
   683 qed
   684 
   685 lemma abs_norm_cancel [simp]:
   686   fixes a :: "'a::real_normed_vector"
   687   shows "\<bar>norm a\<bar> = norm a"
   688 by (rule abs_of_nonneg [OF norm_ge_zero])
   689 
   690 lemma norm_add_less:
   691   fixes x y :: "'a::real_normed_vector"
   692   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
   693 by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
   694 
   695 lemma norm_mult_less:
   696   fixes x y :: "'a::real_normed_algebra"
   697   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
   698 apply (rule order_le_less_trans [OF norm_mult_ineq])
   699 apply (simp add: mult_strict_mono')
   700 done
   701 
   702 lemma norm_of_real [simp]:
   703   "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
   704 unfolding of_real_def by simp
   705 
   706 lemma norm_number_of [simp]:
   707   "norm (number_of w::'a::{number_ring,real_normed_algebra_1})
   708     = \<bar>number_of w\<bar>"
   709 by (subst of_real_number_of_eq [symmetric], rule norm_of_real)
   710 
   711 lemma norm_of_int [simp]:
   712   "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
   713 by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
   714 
   715 lemma norm_of_nat [simp]:
   716   "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
   717 apply (subst of_real_of_nat_eq [symmetric])
   718 apply (subst norm_of_real, simp)
   719 done
   720 
   721 lemma nonzero_norm_inverse:
   722   fixes a :: "'a::real_normed_div_algebra"
   723   shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
   724 apply (rule inverse_unique [symmetric])
   725 apply (simp add: norm_mult [symmetric])
   726 done
   727 
   728 lemma norm_inverse:
   729   fixes a :: "'a::{real_normed_div_algebra,division_by_zero}"
   730   shows "norm (inverse a) = inverse (norm a)"
   731 apply (case_tac "a = 0", simp)
   732 apply (erule nonzero_norm_inverse)
   733 done
   734 
   735 lemma nonzero_norm_divide:
   736   fixes a b :: "'a::real_normed_field"
   737   shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
   738 by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
   739 
   740 lemma norm_divide:
   741   fixes a b :: "'a::{real_normed_field,division_by_zero}"
   742   shows "norm (a / b) = norm a / norm b"
   743 by (simp add: divide_inverse norm_mult norm_inverse)
   744 
   745 lemma norm_power_ineq:
   746   fixes x :: "'a::{real_normed_algebra_1}"
   747   shows "norm (x ^ n) \<le> norm x ^ n"
   748 proof (induct n)
   749   case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
   750 next
   751   case (Suc n)
   752   have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
   753     by (rule norm_mult_ineq)
   754   also from Suc have "\<dots> \<le> norm x * norm x ^ n"
   755     using norm_ge_zero by (rule mult_left_mono)
   756   finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
   757     by simp
   758 qed
   759 
   760 lemma norm_power:
   761   fixes x :: "'a::{real_normed_div_algebra}"
   762   shows "norm (x ^ n) = norm x ^ n"
   763 by (induct n) (simp_all add: norm_mult)
   764 
   765 text {* Every normed vector space is a metric space. *}
   766 
   767 instance real_normed_vector < metric_space
   768 proof
   769   fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
   770     unfolding dist_norm by simp
   771 next
   772   fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
   773     unfolding dist_norm
   774     using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
   775 qed
   776 
   777 
   778 subsection {* Class instances for real numbers *}
   779 
   780 instantiation real :: real_normed_field
   781 begin
   782 
   783 definition real_norm_def [simp]:
   784   "norm r = \<bar>r\<bar>"
   785 
   786 definition dist_real_def:
   787   "dist x y = \<bar>x - y\<bar>"
   788 
   789 definition open_real_def [code del]:
   790   "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   791 
   792 instance
   793 apply (intro_classes, unfold real_norm_def real_scaleR_def)
   794 apply (rule dist_real_def)
   795 apply (rule open_real_def)
   796 apply (simp add: real_sgn_def)
   797 apply (rule abs_ge_zero)
   798 apply (rule abs_eq_0)
   799 apply (rule abs_triangle_ineq)
   800 apply (rule abs_mult)
   801 apply (rule abs_mult)
   802 done
   803 
   804 end
   805 
   806 lemma open_real_lessThan [simp]:
   807   fixes a :: real shows "open {..<a}"
   808 unfolding open_real_def dist_real_def
   809 proof (clarify)
   810   fix x assume "x < a"
   811   hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
   812   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
   813 qed
   814 
   815 lemma open_real_greaterThan [simp]:
   816   fixes a :: real shows "open {a<..}"
   817 unfolding open_real_def dist_real_def
   818 proof (clarify)
   819   fix x assume "a < x"
   820   hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
   821   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
   822 qed
   823 
   824 lemma open_real_greaterThanLessThan [simp]:
   825   fixes a b :: real shows "open {a<..<b}"
   826 proof -
   827   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   828   thus "open {a<..<b}" by (simp add: open_Int)
   829 qed
   830 
   831 lemma closed_real_atMost [simp]: 
   832   fixes a :: real shows "closed {..a}"
   833 unfolding closed_open by simp
   834 
   835 lemma closed_real_atLeast [simp]:
   836   fixes a :: real shows "closed {a..}"
   837 unfolding closed_open by simp
   838 
   839 lemma closed_real_atLeastAtMost [simp]:
   840   fixes a b :: real shows "closed {a..b}"
   841 proof -
   842   have "{a..b} = {a..} \<inter> {..b}" by auto
   843   thus "closed {a..b}" by (simp add: closed_Int)
   844 qed
   845 
   846 
   847 subsection {* Extra type constraints *}
   848 
   849 text {* Only allow @{term "open"} in class @{text topological_space}. *}
   850 
   851 setup {* Sign.add_const_constraint
   852   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"}) *}
   853 
   854 text {* Only allow @{term dist} in class @{text metric_space}. *}
   855 
   856 setup {* Sign.add_const_constraint
   857   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"}) *}
   858 
   859 text {* Only allow @{term norm} in class @{text real_normed_vector}. *}
   860 
   861 setup {* Sign.add_const_constraint
   862   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"}) *}
   863 
   864 
   865 subsection {* Sign function *}
   866 
   867 lemma norm_sgn:
   868   "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
   869 by (simp add: sgn_div_norm)
   870 
   871 lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
   872 by (simp add: sgn_div_norm)
   873 
   874 lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
   875 by (simp add: sgn_div_norm)
   876 
   877 lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
   878 by (simp add: sgn_div_norm)
   879 
   880 lemma sgn_scaleR:
   881   "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
   882 by (simp add: sgn_div_norm mult_ac)
   883 
   884 lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
   885 by (simp add: sgn_div_norm)
   886 
   887 lemma sgn_of_real:
   888   "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
   889 unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
   890 
   891 lemma sgn_mult:
   892   fixes x y :: "'a::real_normed_div_algebra"
   893   shows "sgn (x * y) = sgn x * sgn y"
   894 by (simp add: sgn_div_norm norm_mult mult_commute)
   895 
   896 lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
   897 by (simp add: sgn_div_norm divide_inverse)
   898 
   899 lemma real_sgn_pos: "0 < (x::real) \<Longrightarrow> sgn x = 1"
   900 unfolding real_sgn_eq by simp
   901 
   902 lemma real_sgn_neg: "(x::real) < 0 \<Longrightarrow> sgn x = -1"
   903 unfolding real_sgn_eq by simp
   904 
   905 
   906 subsection {* Bounded Linear and Bilinear Operators *}
   907 
   908 locale bounded_linear = additive +
   909   constrains f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
   910   assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
   911   assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
   912 begin
   913 
   914 lemma pos_bounded:
   915   "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
   916 proof -
   917   obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
   918     using bounded by fast
   919   show ?thesis
   920   proof (intro exI impI conjI allI)
   921     show "0 < max 1 K"
   922       by (rule order_less_le_trans [OF zero_less_one le_maxI1])
   923   next
   924     fix x
   925     have "norm (f x) \<le> norm x * K" using K .
   926     also have "\<dots> \<le> norm x * max 1 K"
   927       by (rule mult_left_mono [OF le_maxI2 norm_ge_zero])
   928     finally show "norm (f x) \<le> norm x * max 1 K" .
   929   qed
   930 qed
   931 
   932 lemma nonneg_bounded:
   933   "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
   934 proof -
   935   from pos_bounded
   936   show ?thesis by (auto intro: order_less_imp_le)
   937 qed
   938 
   939 end
   940 
   941 locale bounded_bilinear =
   942   fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
   943                  \<Rightarrow> 'c::real_normed_vector"
   944     (infixl "**" 70)
   945   assumes add_left: "prod (a + a') b = prod a b + prod a' b"
   946   assumes add_right: "prod a (b + b') = prod a b + prod a b'"
   947   assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
   948   assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
   949   assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
   950 begin
   951 
   952 lemma pos_bounded:
   953   "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
   954 apply (cut_tac bounded, erule exE)
   955 apply (rule_tac x="max 1 K" in exI, safe)
   956 apply (rule order_less_le_trans [OF zero_less_one le_maxI1])
   957 apply (drule spec, drule spec, erule order_trans)
   958 apply (rule mult_left_mono [OF le_maxI2])
   959 apply (intro mult_nonneg_nonneg norm_ge_zero)
   960 done
   961 
   962 lemma nonneg_bounded:
   963   "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
   964 proof -
   965   from pos_bounded
   966   show ?thesis by (auto intro: order_less_imp_le)
   967 qed
   968 
   969 lemma additive_right: "additive (\<lambda>b. prod a b)"
   970 by (rule additive.intro, rule add_right)
   971 
   972 lemma additive_left: "additive (\<lambda>a. prod a b)"
   973 by (rule additive.intro, rule add_left)
   974 
   975 lemma zero_left: "prod 0 b = 0"
   976 by (rule additive.zero [OF additive_left])
   977 
   978 lemma zero_right: "prod a 0 = 0"
   979 by (rule additive.zero [OF additive_right])
   980 
   981 lemma minus_left: "prod (- a) b = - prod a b"
   982 by (rule additive.minus [OF additive_left])
   983 
   984 lemma minus_right: "prod a (- b) = - prod a b"
   985 by (rule additive.minus [OF additive_right])
   986 
   987 lemma diff_left:
   988   "prod (a - a') b = prod a b - prod a' b"
   989 by (rule additive.diff [OF additive_left])
   990 
   991 lemma diff_right:
   992   "prod a (b - b') = prod a b - prod a b'"
   993 by (rule additive.diff [OF additive_right])
   994 
   995 lemma bounded_linear_left:
   996   "bounded_linear (\<lambda>a. a ** b)"
   997 apply (unfold_locales)
   998 apply (rule add_left)
   999 apply (rule scaleR_left)
  1000 apply (cut_tac bounded, safe)
  1001 apply (rule_tac x="norm b * K" in exI)
  1002 apply (simp add: mult_ac)
  1003 done
  1004 
  1005 lemma bounded_linear_right:
  1006   "bounded_linear (\<lambda>b. a ** b)"
  1007 apply (unfold_locales)
  1008 apply (rule add_right)
  1009 apply (rule scaleR_right)
  1010 apply (cut_tac bounded, safe)
  1011 apply (rule_tac x="norm a * K" in exI)
  1012 apply (simp add: mult_ac)
  1013 done
  1014 
  1015 lemma prod_diff_prod:
  1016   "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
  1017 by (simp add: diff_left diff_right)
  1018 
  1019 end
  1020 
  1021 interpretation mult:
  1022   bounded_bilinear "op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra"
  1023 apply (rule bounded_bilinear.intro)
  1024 apply (rule left_distrib)
  1025 apply (rule right_distrib)
  1026 apply (rule mult_scaleR_left)
  1027 apply (rule mult_scaleR_right)
  1028 apply (rule_tac x="1" in exI)
  1029 apply (simp add: norm_mult_ineq)
  1030 done
  1031 
  1032 interpretation mult_left:
  1033   bounded_linear "(\<lambda>x::'a::real_normed_algebra. x * y)"
  1034 by (rule mult.bounded_linear_left)
  1035 
  1036 interpretation mult_right:
  1037   bounded_linear "(\<lambda>y::'a::real_normed_algebra. x * y)"
  1038 by (rule mult.bounded_linear_right)
  1039 
  1040 interpretation divide:
  1041   bounded_linear "(\<lambda>x::'a::real_normed_field. x / y)"
  1042 unfolding divide_inverse by (rule mult.bounded_linear_left)
  1043 
  1044 interpretation scaleR: bounded_bilinear "scaleR"
  1045 apply (rule bounded_bilinear.intro)
  1046 apply (rule scaleR_left_distrib)
  1047 apply (rule scaleR_right_distrib)
  1048 apply simp
  1049 apply (rule scaleR_left_commute)
  1050 apply (rule_tac x="1" in exI, simp)
  1051 done
  1052 
  1053 interpretation scaleR_left: bounded_linear "\<lambda>r. scaleR r x"
  1054 by (rule scaleR.bounded_linear_left)
  1055 
  1056 interpretation scaleR_right: bounded_linear "\<lambda>x. scaleR r x"
  1057 by (rule scaleR.bounded_linear_right)
  1058 
  1059 interpretation of_real: bounded_linear "\<lambda>r. of_real r"
  1060 unfolding of_real_def by (rule scaleR.bounded_linear_left)
  1061 
  1062 end