src/HOL/RealVector.thy
author hoelzl
Thu Sep 02 10:14:32 2010 +0200 (2010-09-02)
changeset 39072 1030b1a166ef
parent 38621 d6cb7e625d75
child 41969 1cf3e4107a2a
permissions -rw-r--r--
Add lessThan_Suc_eq_insert_0
     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 RComplete
     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: add minus diff_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_ring_inverse_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_ring_inverse_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, field_inverse_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)
   272 
   273 lemma inj_of_real:
   274   "inj of_real"
   275   by (auto intro: injI)
   276 
   277 lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
   278 
   279 lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
   280 proof
   281   fix r
   282   show "of_real r = id r"
   283     by (simp add: of_real_def)
   284 qed
   285 
   286 text{*Collapse nested embeddings*}
   287 lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
   288 by (induct n) auto
   289 
   290 lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
   291 by (cases z rule: int_diff_cases, simp)
   292 
   293 lemma of_real_number_of_eq:
   294   "of_real (number_of w) = (number_of w :: 'a::{number_ring,real_algebra_1})"
   295 by (simp add: number_of_eq)
   296 
   297 text{*Every real algebra has characteristic zero*}
   298 
   299 instance real_algebra_1 < ring_char_0
   300 proof
   301   from inj_of_real inj_of_nat have "inj (of_real \<circ> of_nat)" by (rule inj_comp)
   302   then show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: comp_def)
   303 qed
   304 
   305 instance real_field < field_char_0 ..
   306 
   307 
   308 subsection {* The Set of Real Numbers *}
   309 
   310 definition Reals :: "'a::real_algebra_1 set" where
   311   "Reals = range of_real"
   312 
   313 notation (xsymbols)
   314   Reals  ("\<real>")
   315 
   316 lemma Reals_of_real [simp]: "of_real r \<in> Reals"
   317 by (simp add: Reals_def)
   318 
   319 lemma Reals_of_int [simp]: "of_int z \<in> Reals"
   320 by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
   321 
   322 lemma Reals_of_nat [simp]: "of_nat n \<in> Reals"
   323 by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
   324 
   325 lemma Reals_number_of [simp]:
   326   "(number_of w::'a::{number_ring,real_algebra_1}) \<in> Reals"
   327 by (subst of_real_number_of_eq [symmetric], rule Reals_of_real)
   328 
   329 lemma Reals_0 [simp]: "0 \<in> Reals"
   330 apply (unfold Reals_def)
   331 apply (rule range_eqI)
   332 apply (rule of_real_0 [symmetric])
   333 done
   334 
   335 lemma Reals_1 [simp]: "1 \<in> Reals"
   336 apply (unfold Reals_def)
   337 apply (rule range_eqI)
   338 apply (rule of_real_1 [symmetric])
   339 done
   340 
   341 lemma Reals_add [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a + b \<in> Reals"
   342 apply (auto simp add: Reals_def)
   343 apply (rule range_eqI)
   344 apply (rule of_real_add [symmetric])
   345 done
   346 
   347 lemma Reals_minus [simp]: "a \<in> Reals \<Longrightarrow> - a \<in> Reals"
   348 apply (auto simp add: Reals_def)
   349 apply (rule range_eqI)
   350 apply (rule of_real_minus [symmetric])
   351 done
   352 
   353 lemma Reals_diff [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a - b \<in> Reals"
   354 apply (auto simp add: Reals_def)
   355 apply (rule range_eqI)
   356 apply (rule of_real_diff [symmetric])
   357 done
   358 
   359 lemma Reals_mult [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a * b \<in> Reals"
   360 apply (auto simp add: Reals_def)
   361 apply (rule range_eqI)
   362 apply (rule of_real_mult [symmetric])
   363 done
   364 
   365 lemma nonzero_Reals_inverse:
   366   fixes a :: "'a::real_div_algebra"
   367   shows "\<lbrakk>a \<in> Reals; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Reals"
   368 apply (auto simp add: Reals_def)
   369 apply (rule range_eqI)
   370 apply (erule nonzero_of_real_inverse [symmetric])
   371 done
   372 
   373 lemma Reals_inverse [simp]:
   374   fixes a :: "'a::{real_div_algebra, division_ring_inverse_zero}"
   375   shows "a \<in> Reals \<Longrightarrow> inverse a \<in> Reals"
   376 apply (auto simp add: Reals_def)
   377 apply (rule range_eqI)
   378 apply (rule of_real_inverse [symmetric])
   379 done
   380 
   381 lemma nonzero_Reals_divide:
   382   fixes a b :: "'a::real_field"
   383   shows "\<lbrakk>a \<in> Reals; b \<in> Reals; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   384 apply (auto simp add: Reals_def)
   385 apply (rule range_eqI)
   386 apply (erule nonzero_of_real_divide [symmetric])
   387 done
   388 
   389 lemma Reals_divide [simp]:
   390   fixes a b :: "'a::{real_field, field_inverse_zero}"
   391   shows "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   392 apply (auto simp add: Reals_def)
   393 apply (rule range_eqI)
   394 apply (rule of_real_divide [symmetric])
   395 done
   396 
   397 lemma Reals_power [simp]:
   398   fixes a :: "'a::{real_algebra_1}"
   399   shows "a \<in> Reals \<Longrightarrow> a ^ n \<in> Reals"
   400 apply (auto simp add: Reals_def)
   401 apply (rule range_eqI)
   402 apply (rule of_real_power [symmetric])
   403 done
   404 
   405 lemma Reals_cases [cases set: Reals]:
   406   assumes "q \<in> \<real>"
   407   obtains (of_real) r where "q = of_real r"
   408   unfolding Reals_def
   409 proof -
   410   from `q \<in> \<real>` have "q \<in> range of_real" unfolding Reals_def .
   411   then obtain r where "q = of_real r" ..
   412   then show thesis ..
   413 qed
   414 
   415 lemma Reals_induct [case_names of_real, induct set: Reals]:
   416   "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
   417   by (rule Reals_cases) auto
   418 
   419 
   420 subsection {* Topological spaces *}
   421 
   422 class "open" =
   423   fixes "open" :: "'a set \<Rightarrow> bool"
   424 
   425 class topological_space = "open" +
   426   assumes open_UNIV [simp, intro]: "open UNIV"
   427   assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
   428   assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union> K)"
   429 begin
   430 
   431 definition
   432   closed :: "'a set \<Rightarrow> bool" where
   433   "closed S \<longleftrightarrow> open (- S)"
   434 
   435 lemma open_empty [intro, simp]: "open {}"
   436   using open_Union [of "{}"] by simp
   437 
   438 lemma open_Un [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
   439   using open_Union [of "{S, T}"] by simp
   440 
   441 lemma open_UN [intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
   442   unfolding UN_eq by (rule open_Union) auto
   443 
   444 lemma open_INT [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
   445   by (induct set: finite) auto
   446 
   447 lemma open_Inter [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
   448   unfolding Inter_def by (rule open_INT)
   449 
   450 lemma closed_empty [intro, simp]:  "closed {}"
   451   unfolding closed_def by simp
   452 
   453 lemma closed_Un [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
   454   unfolding closed_def by auto
   455 
   456 lemma closed_Inter [intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter> K)"
   457   unfolding closed_def Inter_def by auto
   458 
   459 lemma closed_UNIV [intro, simp]: "closed UNIV"
   460   unfolding closed_def by simp
   461 
   462 lemma closed_Int [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
   463   unfolding closed_def by auto
   464 
   465 lemma closed_INT [intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
   466   unfolding closed_def by auto
   467 
   468 lemma closed_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
   469   by (induct set: finite) auto
   470 
   471 lemma closed_Union [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
   472   unfolding Union_def by (rule closed_UN)
   473 
   474 lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
   475   unfolding closed_def by simp
   476 
   477 lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
   478   unfolding closed_def by simp
   479 
   480 lemma open_Diff [intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
   481   unfolding closed_open Diff_eq by (rule open_Int)
   482 
   483 lemma closed_Diff [intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
   484   unfolding open_closed Diff_eq by (rule closed_Int)
   485 
   486 lemma open_Compl [intro]: "closed S \<Longrightarrow> open (- S)"
   487   unfolding closed_open .
   488 
   489 lemma closed_Compl [intro]: "open S \<Longrightarrow> closed (- S)"
   490   unfolding open_closed .
   491 
   492 end
   493 
   494 
   495 subsection {* Metric spaces *}
   496 
   497 class dist =
   498   fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
   499 
   500 class open_dist = "open" + dist +
   501   assumes open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   502 
   503 class metric_space = open_dist +
   504   assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
   505   assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
   506 begin
   507 
   508 lemma dist_self [simp]: "dist x x = 0"
   509 by simp
   510 
   511 lemma zero_le_dist [simp]: "0 \<le> dist x y"
   512 using dist_triangle2 [of x x y] by simp
   513 
   514 lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
   515 by (simp add: less_le)
   516 
   517 lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
   518 by (simp add: not_less)
   519 
   520 lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
   521 by (simp add: le_less)
   522 
   523 lemma dist_commute: "dist x y = dist y x"
   524 proof (rule order_antisym)
   525   show "dist x y \<le> dist y x"
   526     using dist_triangle2 [of x y x] by simp
   527   show "dist y x \<le> dist x y"
   528     using dist_triangle2 [of y x y] by simp
   529 qed
   530 
   531 lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
   532 using dist_triangle2 [of x z y] by (simp add: dist_commute)
   533 
   534 lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
   535 using dist_triangle2 [of x y a] by (simp add: dist_commute)
   536 
   537 subclass topological_space
   538 proof
   539   have "\<exists>e::real. 0 < e"
   540     by (fast intro: zero_less_one)
   541   then show "open UNIV"
   542     unfolding open_dist by simp
   543 next
   544   fix S T assume "open S" "open T"
   545   then show "open (S \<inter> T)"
   546     unfolding open_dist
   547     apply clarify
   548     apply (drule (1) bspec)+
   549     apply (clarify, rename_tac r s)
   550     apply (rule_tac x="min r s" in exI, simp)
   551     done
   552 next
   553   fix K assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
   554     unfolding open_dist by fast
   555 qed
   556 
   557 end
   558 
   559 
   560 subsection {* Real normed vector spaces *}
   561 
   562 class norm =
   563   fixes norm :: "'a \<Rightarrow> real"
   564 
   565 class sgn_div_norm = scaleR + norm + sgn +
   566   assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
   567 
   568 class dist_norm = dist + norm + minus +
   569   assumes dist_norm: "dist x y = norm (x - y)"
   570 
   571 class real_normed_vector = real_vector + sgn_div_norm + dist_norm + open_dist +
   572   assumes norm_ge_zero [simp]: "0 \<le> norm x"
   573   and norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
   574   and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
   575   and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   576 
   577 class real_normed_algebra = real_algebra + real_normed_vector +
   578   assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
   579 
   580 class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   581   assumes norm_one [simp]: "norm 1 = 1"
   582 
   583 class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   584   assumes norm_mult: "norm (x * y) = norm x * norm y"
   585 
   586 class real_normed_field = real_field + real_normed_div_algebra
   587 
   588 instance real_normed_div_algebra < real_normed_algebra_1
   589 proof
   590   fix x y :: 'a
   591   show "norm (x * y) \<le> norm x * norm y"
   592     by (simp add: norm_mult)
   593 next
   594   have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
   595     by (rule norm_mult)
   596   thus "norm (1::'a) = 1" by simp
   597 qed
   598 
   599 lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
   600 by simp
   601 
   602 lemma zero_less_norm_iff [simp]:
   603   fixes x :: "'a::real_normed_vector"
   604   shows "(0 < norm x) = (x \<noteq> 0)"
   605 by (simp add: order_less_le)
   606 
   607 lemma norm_not_less_zero [simp]:
   608   fixes x :: "'a::real_normed_vector"
   609   shows "\<not> norm x < 0"
   610 by (simp add: linorder_not_less)
   611 
   612 lemma norm_le_zero_iff [simp]:
   613   fixes x :: "'a::real_normed_vector"
   614   shows "(norm x \<le> 0) = (x = 0)"
   615 by (simp add: order_le_less)
   616 
   617 lemma norm_minus_cancel [simp]:
   618   fixes x :: "'a::real_normed_vector"
   619   shows "norm (- x) = norm x"
   620 proof -
   621   have "norm (- x) = norm (scaleR (- 1) x)"
   622     by (simp only: scaleR_minus_left scaleR_one)
   623   also have "\<dots> = \<bar>- 1\<bar> * norm x"
   624     by (rule norm_scaleR)
   625   finally show ?thesis by simp
   626 qed
   627 
   628 lemma norm_minus_commute:
   629   fixes a b :: "'a::real_normed_vector"
   630   shows "norm (a - b) = norm (b - a)"
   631 proof -
   632   have "norm (- (b - a)) = norm (b - a)"
   633     by (rule norm_minus_cancel)
   634   thus ?thesis by simp
   635 qed
   636 
   637 lemma norm_triangle_ineq2:
   638   fixes a b :: "'a::real_normed_vector"
   639   shows "norm a - norm b \<le> norm (a - b)"
   640 proof -
   641   have "norm (a - b + b) \<le> norm (a - b) + norm b"
   642     by (rule norm_triangle_ineq)
   643   thus ?thesis by simp
   644 qed
   645 
   646 lemma norm_triangle_ineq3:
   647   fixes a b :: "'a::real_normed_vector"
   648   shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
   649 apply (subst abs_le_iff)
   650 apply auto
   651 apply (rule norm_triangle_ineq2)
   652 apply (subst norm_minus_commute)
   653 apply (rule norm_triangle_ineq2)
   654 done
   655 
   656 lemma norm_triangle_ineq4:
   657   fixes a b :: "'a::real_normed_vector"
   658   shows "norm (a - b) \<le> norm a + norm b"
   659 proof -
   660   have "norm (a + - b) \<le> norm a + norm (- b)"
   661     by (rule norm_triangle_ineq)
   662   thus ?thesis
   663     by (simp only: diff_minus norm_minus_cancel)
   664 qed
   665 
   666 lemma norm_diff_ineq:
   667   fixes a b :: "'a::real_normed_vector"
   668   shows "norm a - norm b \<le> norm (a + b)"
   669 proof -
   670   have "norm a - norm (- b) \<le> norm (a - - b)"
   671     by (rule norm_triangle_ineq2)
   672   thus ?thesis by simp
   673 qed
   674 
   675 lemma norm_diff_triangle_ineq:
   676   fixes a b c d :: "'a::real_normed_vector"
   677   shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
   678 proof -
   679   have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
   680     by (simp add: diff_minus add_ac)
   681   also have "\<dots> \<le> norm (a - c) + norm (b - d)"
   682     by (rule norm_triangle_ineq)
   683   finally show ?thesis .
   684 qed
   685 
   686 lemma abs_norm_cancel [simp]:
   687   fixes a :: "'a::real_normed_vector"
   688   shows "\<bar>norm a\<bar> = norm a"
   689 by (rule abs_of_nonneg [OF norm_ge_zero])
   690 
   691 lemma norm_add_less:
   692   fixes x y :: "'a::real_normed_vector"
   693   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
   694 by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
   695 
   696 lemma norm_mult_less:
   697   fixes x y :: "'a::real_normed_algebra"
   698   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
   699 apply (rule order_le_less_trans [OF norm_mult_ineq])
   700 apply (simp add: mult_strict_mono')
   701 done
   702 
   703 lemma norm_of_real [simp]:
   704   "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
   705 unfolding of_real_def by simp
   706 
   707 lemma norm_number_of [simp]:
   708   "norm (number_of w::'a::{number_ring,real_normed_algebra_1})
   709     = \<bar>number_of w\<bar>"
   710 by (subst of_real_number_of_eq [symmetric], rule norm_of_real)
   711 
   712 lemma norm_of_int [simp]:
   713   "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
   714 by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
   715 
   716 lemma norm_of_nat [simp]:
   717   "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
   718 apply (subst of_real_of_nat_eq [symmetric])
   719 apply (subst norm_of_real, simp)
   720 done
   721 
   722 lemma nonzero_norm_inverse:
   723   fixes a :: "'a::real_normed_div_algebra"
   724   shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
   725 apply (rule inverse_unique [symmetric])
   726 apply (simp add: norm_mult [symmetric])
   727 done
   728 
   729 lemma norm_inverse:
   730   fixes a :: "'a::{real_normed_div_algebra, division_ring_inverse_zero}"
   731   shows "norm (inverse a) = inverse (norm a)"
   732 apply (case_tac "a = 0", simp)
   733 apply (erule nonzero_norm_inverse)
   734 done
   735 
   736 lemma nonzero_norm_divide:
   737   fixes a b :: "'a::real_normed_field"
   738   shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
   739 by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
   740 
   741 lemma norm_divide:
   742   fixes a b :: "'a::{real_normed_field, field_inverse_zero}"
   743   shows "norm (a / b) = norm a / norm b"
   744 by (simp add: divide_inverse norm_mult norm_inverse)
   745 
   746 lemma norm_power_ineq:
   747   fixes x :: "'a::{real_normed_algebra_1}"
   748   shows "norm (x ^ n) \<le> norm x ^ n"
   749 proof (induct n)
   750   case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
   751 next
   752   case (Suc n)
   753   have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
   754     by (rule norm_mult_ineq)
   755   also from Suc have "\<dots> \<le> norm x * norm x ^ n"
   756     using norm_ge_zero by (rule mult_left_mono)
   757   finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
   758     by simp
   759 qed
   760 
   761 lemma norm_power:
   762   fixes x :: "'a::{real_normed_div_algebra}"
   763   shows "norm (x ^ n) = norm x ^ n"
   764 by (induct n) (simp_all add: norm_mult)
   765 
   766 text {* Every normed vector space is a metric space. *}
   767 
   768 instance real_normed_vector < metric_space
   769 proof
   770   fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
   771     unfolding dist_norm by simp
   772 next
   773   fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
   774     unfolding dist_norm
   775     using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
   776 qed
   777 
   778 
   779 subsection {* Class instances for real numbers *}
   780 
   781 instantiation real :: real_normed_field
   782 begin
   783 
   784 definition real_norm_def [simp]:
   785   "norm r = \<bar>r\<bar>"
   786 
   787 definition dist_real_def:
   788   "dist x y = \<bar>x - y\<bar>"
   789 
   790 definition open_real_def:
   791   "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   792 
   793 instance
   794 apply (intro_classes, unfold real_norm_def real_scaleR_def)
   795 apply (rule dist_real_def)
   796 apply (rule open_real_def)
   797 apply (simp add: sgn_real_def)
   798 apply (rule abs_ge_zero)
   799 apply (rule abs_eq_0)
   800 apply (rule abs_triangle_ineq)
   801 apply (rule abs_mult)
   802 apply (rule abs_mult)
   803 done
   804 
   805 end
   806 
   807 lemma open_real_lessThan [simp]:
   808   fixes a :: real shows "open {..<a}"
   809 unfolding open_real_def dist_real_def
   810 proof (clarify)
   811   fix x assume "x < a"
   812   hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
   813   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
   814 qed
   815 
   816 lemma open_real_greaterThan [simp]:
   817   fixes a :: real shows "open {a<..}"
   818 unfolding open_real_def dist_real_def
   819 proof (clarify)
   820   fix x assume "a < x"
   821   hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
   822   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
   823 qed
   824 
   825 lemma open_real_greaterThanLessThan [simp]:
   826   fixes a b :: real shows "open {a<..<b}"
   827 proof -
   828   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   829   thus "open {a<..<b}" by (simp add: open_Int)
   830 qed
   831 
   832 lemma closed_real_atMost [simp]: 
   833   fixes a :: real shows "closed {..a}"
   834 unfolding closed_open by simp
   835 
   836 lemma closed_real_atLeast [simp]:
   837   fixes a :: real shows "closed {a..}"
   838 unfolding closed_open by simp
   839 
   840 lemma closed_real_atLeastAtMost [simp]:
   841   fixes a b :: real shows "closed {a..b}"
   842 proof -
   843   have "{a..b} = {a..} \<inter> {..b}" by auto
   844   thus "closed {a..b}" by (simp add: closed_Int)
   845 qed
   846 
   847 
   848 subsection {* Extra type constraints *}
   849 
   850 text {* Only allow @{term "open"} in class @{text topological_space}. *}
   851 
   852 setup {* Sign.add_const_constraint
   853   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"}) *}
   854 
   855 text {* Only allow @{term dist} in class @{text metric_space}. *}
   856 
   857 setup {* Sign.add_const_constraint
   858   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"}) *}
   859 
   860 text {* Only allow @{term norm} in class @{text real_normed_vector}. *}
   861 
   862 setup {* Sign.add_const_constraint
   863   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"}) *}
   864 
   865 
   866 subsection {* Sign function *}
   867 
   868 lemma norm_sgn:
   869   "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
   870 by (simp add: sgn_div_norm)
   871 
   872 lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
   873 by (simp add: sgn_div_norm)
   874 
   875 lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
   876 by (simp add: sgn_div_norm)
   877 
   878 lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
   879 by (simp add: sgn_div_norm)
   880 
   881 lemma sgn_scaleR:
   882   "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
   883 by (simp add: sgn_div_norm mult_ac)
   884 
   885 lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
   886 by (simp add: sgn_div_norm)
   887 
   888 lemma sgn_of_real:
   889   "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
   890 unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
   891 
   892 lemma sgn_mult:
   893   fixes x y :: "'a::real_normed_div_algebra"
   894   shows "sgn (x * y) = sgn x * sgn y"
   895 by (simp add: sgn_div_norm norm_mult mult_commute)
   896 
   897 lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
   898 by (simp add: sgn_div_norm divide_inverse)
   899 
   900 lemma real_sgn_pos: "0 < (x::real) \<Longrightarrow> sgn x = 1"
   901 unfolding real_sgn_eq by simp
   902 
   903 lemma real_sgn_neg: "(x::real) < 0 \<Longrightarrow> sgn x = -1"
   904 unfolding real_sgn_eq by simp
   905 
   906 
   907 subsection {* Bounded Linear and Bilinear Operators *}
   908 
   909 locale bounded_linear = additive +
   910   constrains f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
   911   assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
   912   assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
   913 begin
   914 
   915 lemma pos_bounded:
   916   "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
   917 proof -
   918   obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
   919     using bounded by fast
   920   show ?thesis
   921   proof (intro exI impI conjI allI)
   922     show "0 < max 1 K"
   923       by (rule order_less_le_trans [OF zero_less_one le_maxI1])
   924   next
   925     fix x
   926     have "norm (f x) \<le> norm x * K" using K .
   927     also have "\<dots> \<le> norm x * max 1 K"
   928       by (rule mult_left_mono [OF le_maxI2 norm_ge_zero])
   929     finally show "norm (f x) \<le> norm x * max 1 K" .
   930   qed
   931 qed
   932 
   933 lemma nonneg_bounded:
   934   "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
   935 proof -
   936   from pos_bounded
   937   show ?thesis by (auto intro: order_less_imp_le)
   938 qed
   939 
   940 end
   941 
   942 locale bounded_bilinear =
   943   fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
   944                  \<Rightarrow> 'c::real_normed_vector"
   945     (infixl "**" 70)
   946   assumes add_left: "prod (a + a') b = prod a b + prod a' b"
   947   assumes add_right: "prod a (b + b') = prod a b + prod a b'"
   948   assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
   949   assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
   950   assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
   951 begin
   952 
   953 lemma pos_bounded:
   954   "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
   955 apply (cut_tac bounded, erule exE)
   956 apply (rule_tac x="max 1 K" in exI, safe)
   957 apply (rule order_less_le_trans [OF zero_less_one le_maxI1])
   958 apply (drule spec, drule spec, erule order_trans)
   959 apply (rule mult_left_mono [OF le_maxI2])
   960 apply (intro mult_nonneg_nonneg norm_ge_zero)
   961 done
   962 
   963 lemma nonneg_bounded:
   964   "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
   965 proof -
   966   from pos_bounded
   967   show ?thesis by (auto intro: order_less_imp_le)
   968 qed
   969 
   970 lemma additive_right: "additive (\<lambda>b. prod a b)"
   971 by (rule additive.intro, rule add_right)
   972 
   973 lemma additive_left: "additive (\<lambda>a. prod a b)"
   974 by (rule additive.intro, rule add_left)
   975 
   976 lemma zero_left: "prod 0 b = 0"
   977 by (rule additive.zero [OF additive_left])
   978 
   979 lemma zero_right: "prod a 0 = 0"
   980 by (rule additive.zero [OF additive_right])
   981 
   982 lemma minus_left: "prod (- a) b = - prod a b"
   983 by (rule additive.minus [OF additive_left])
   984 
   985 lemma minus_right: "prod a (- b) = - prod a b"
   986 by (rule additive.minus [OF additive_right])
   987 
   988 lemma diff_left:
   989   "prod (a - a') b = prod a b - prod a' b"
   990 by (rule additive.diff [OF additive_left])
   991 
   992 lemma diff_right:
   993   "prod a (b - b') = prod a b - prod a b'"
   994 by (rule additive.diff [OF additive_right])
   995 
   996 lemma bounded_linear_left:
   997   "bounded_linear (\<lambda>a. a ** b)"
   998 apply (unfold_locales)
   999 apply (rule add_left)
  1000 apply (rule scaleR_left)
  1001 apply (cut_tac bounded, safe)
  1002 apply (rule_tac x="norm b * K" in exI)
  1003 apply (simp add: mult_ac)
  1004 done
  1005 
  1006 lemma bounded_linear_right:
  1007   "bounded_linear (\<lambda>b. a ** b)"
  1008 apply (unfold_locales)
  1009 apply (rule add_right)
  1010 apply (rule scaleR_right)
  1011 apply (cut_tac bounded, safe)
  1012 apply (rule_tac x="norm a * K" in exI)
  1013 apply (simp add: mult_ac)
  1014 done
  1015 
  1016 lemma prod_diff_prod:
  1017   "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
  1018 by (simp add: diff_left diff_right)
  1019 
  1020 end
  1021 
  1022 interpretation mult:
  1023   bounded_bilinear "op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra"
  1024 apply (rule bounded_bilinear.intro)
  1025 apply (rule left_distrib)
  1026 apply (rule right_distrib)
  1027 apply (rule mult_scaleR_left)
  1028 apply (rule mult_scaleR_right)
  1029 apply (rule_tac x="1" in exI)
  1030 apply (simp add: norm_mult_ineq)
  1031 done
  1032 
  1033 interpretation mult_left:
  1034   bounded_linear "(\<lambda>x::'a::real_normed_algebra. x * y)"
  1035 by (rule mult.bounded_linear_left)
  1036 
  1037 interpretation mult_right:
  1038   bounded_linear "(\<lambda>y::'a::real_normed_algebra. x * y)"
  1039 by (rule mult.bounded_linear_right)
  1040 
  1041 interpretation divide:
  1042   bounded_linear "(\<lambda>x::'a::real_normed_field. x / y)"
  1043 unfolding divide_inverse by (rule mult.bounded_linear_left)
  1044 
  1045 interpretation scaleR: bounded_bilinear "scaleR"
  1046 apply (rule bounded_bilinear.intro)
  1047 apply (rule scaleR_left_distrib)
  1048 apply (rule scaleR_right_distrib)
  1049 apply simp
  1050 apply (rule scaleR_left_commute)
  1051 apply (rule_tac x="1" in exI, simp)
  1052 done
  1053 
  1054 interpretation scaleR_left: bounded_linear "\<lambda>r. scaleR r x"
  1055 by (rule scaleR.bounded_linear_left)
  1056 
  1057 interpretation scaleR_right: bounded_linear "\<lambda>x. scaleR r x"
  1058 by (rule scaleR.bounded_linear_right)
  1059 
  1060 interpretation of_real: bounded_linear "\<lambda>r. of_real r"
  1061 unfolding of_real_def by (rule scaleR.bounded_linear_left)
  1062 
  1063 end