src/HOL/RealVector.thy
author wenzelm
Thu May 24 17:25:53 2012 +0200 (2012-05-24)
changeset 47988 e4b69e10b990
parent 47108 2a1953f0d20d
child 49962 a8cc904a6820
permissions -rw-r--r--
tuned proofs;
     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   and scale_setsum_left: "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)"
    66 proof -
    67   interpret s: additive "\<lambda>a. scale a x"
    68     proof qed (rule scale_left_distrib)
    69   show "scale 0 x = 0" by (rule s.zero)
    70   show "scale (- a) x = - (scale a x)" by (rule s.minus)
    71   show "scale (a - b) x = scale a x - scale b x" by (rule s.diff)
    72   show "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)" by (rule s.setsum)
    73 qed
    74 
    75 lemma scale_zero_right [simp]: "scale a 0 = 0"
    76   and scale_minus_right [simp]: "scale a (- x) = - (scale a x)"
    77   and scale_right_diff_distrib [algebra_simps]:
    78         "scale a (x - y) = scale a x - scale a y"
    79   and scale_setsum_right: "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))"
    80 proof -
    81   interpret s: additive "\<lambda>x. scale a x"
    82     proof qed (rule scale_right_distrib)
    83   show "scale a 0 = 0" by (rule s.zero)
    84   show "scale a (- x) = - (scale a x)" by (rule s.minus)
    85   show "scale a (x - y) = scale a x - scale a y" by (rule s.diff)
    86   show "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))" by (rule s.setsum)
    87 qed
    88 
    89 lemma scale_eq_0_iff [simp]:
    90   "scale a x = 0 \<longleftrightarrow> a = 0 \<or> x = 0"
    91 proof cases
    92   assume "a = 0" thus ?thesis by simp
    93 next
    94   assume anz [simp]: "a \<noteq> 0"
    95   { assume "scale a x = 0"
    96     hence "scale (inverse a) (scale a x) = 0" by simp
    97     hence "x = 0" by simp }
    98   thus ?thesis by force
    99 qed
   100 
   101 lemma scale_left_imp_eq:
   102   "\<lbrakk>a \<noteq> 0; scale a x = scale a y\<rbrakk> \<Longrightarrow> x = y"
   103 proof -
   104   assume nonzero: "a \<noteq> 0"
   105   assume "scale a x = scale a y"
   106   hence "scale a (x - y) = 0"
   107      by (simp add: scale_right_diff_distrib)
   108   hence "x - y = 0" by (simp add: nonzero)
   109   thus "x = y" by (simp only: right_minus_eq)
   110 qed
   111 
   112 lemma scale_right_imp_eq:
   113   "\<lbrakk>x \<noteq> 0; scale a x = scale b x\<rbrakk> \<Longrightarrow> a = b"
   114 proof -
   115   assume nonzero: "x \<noteq> 0"
   116   assume "scale a x = scale b x"
   117   hence "scale (a - b) x = 0"
   118      by (simp add: scale_left_diff_distrib)
   119   hence "a - b = 0" by (simp add: nonzero)
   120   thus "a = b" by (simp only: right_minus_eq)
   121 qed
   122 
   123 lemma scale_cancel_left [simp]:
   124   "scale a x = scale a y \<longleftrightarrow> x = y \<or> a = 0"
   125 by (auto intro: scale_left_imp_eq)
   126 
   127 lemma scale_cancel_right [simp]:
   128   "scale a x = scale b x \<longleftrightarrow> a = b \<or> x = 0"
   129 by (auto intro: scale_right_imp_eq)
   130 
   131 end
   132 
   133 subsection {* Real vector spaces *}
   134 
   135 class scaleR =
   136   fixes scaleR :: "real \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "*\<^sub>R" 75)
   137 begin
   138 
   139 abbreviation
   140   divideR :: "'a \<Rightarrow> real \<Rightarrow> 'a" (infixl "'/\<^sub>R" 70)
   141 where
   142   "x /\<^sub>R r == scaleR (inverse r) x"
   143 
   144 end
   145 
   146 class real_vector = scaleR + ab_group_add +
   147   assumes scaleR_add_right: "scaleR a (x + y) = scaleR a x + scaleR a y"
   148   and scaleR_add_left: "scaleR (a + b) x = scaleR a x + scaleR b x"
   149   and scaleR_scaleR: "scaleR a (scaleR b x) = scaleR (a * b) x"
   150   and scaleR_one: "scaleR 1 x = x"
   151 
   152 interpretation real_vector:
   153   vector_space "scaleR :: real \<Rightarrow> 'a \<Rightarrow> 'a::real_vector"
   154 apply unfold_locales
   155 apply (rule scaleR_add_right)
   156 apply (rule scaleR_add_left)
   157 apply (rule scaleR_scaleR)
   158 apply (rule scaleR_one)
   159 done
   160 
   161 text {* Recover original theorem names *}
   162 
   163 lemmas scaleR_left_commute = real_vector.scale_left_commute
   164 lemmas scaleR_zero_left = real_vector.scale_zero_left
   165 lemmas scaleR_minus_left = real_vector.scale_minus_left
   166 lemmas scaleR_diff_left = real_vector.scale_left_diff_distrib
   167 lemmas scaleR_setsum_left = real_vector.scale_setsum_left
   168 lemmas scaleR_zero_right = real_vector.scale_zero_right
   169 lemmas scaleR_minus_right = real_vector.scale_minus_right
   170 lemmas scaleR_diff_right = real_vector.scale_right_diff_distrib
   171 lemmas scaleR_setsum_right = real_vector.scale_setsum_right
   172 lemmas scaleR_eq_0_iff = real_vector.scale_eq_0_iff
   173 lemmas scaleR_left_imp_eq = real_vector.scale_left_imp_eq
   174 lemmas scaleR_right_imp_eq = real_vector.scale_right_imp_eq
   175 lemmas scaleR_cancel_left = real_vector.scale_cancel_left
   176 lemmas scaleR_cancel_right = real_vector.scale_cancel_right
   177 
   178 text {* Legacy names *}
   179 
   180 lemmas scaleR_left_distrib = scaleR_add_left
   181 lemmas scaleR_right_distrib = scaleR_add_right
   182 lemmas scaleR_left_diff_distrib = scaleR_diff_left
   183 lemmas scaleR_right_diff_distrib = scaleR_diff_right
   184 
   185 lemma scaleR_minus1_left [simp]:
   186   fixes x :: "'a::real_vector"
   187   shows "scaleR (-1) x = - x"
   188   using scaleR_minus_left [of 1 x] by simp
   189 
   190 class real_algebra = real_vector + ring +
   191   assumes mult_scaleR_left [simp]: "scaleR a x * y = scaleR a (x * y)"
   192   and mult_scaleR_right [simp]: "x * scaleR a y = scaleR a (x * y)"
   193 
   194 class real_algebra_1 = real_algebra + ring_1
   195 
   196 class real_div_algebra = real_algebra_1 + division_ring
   197 
   198 class real_field = real_div_algebra + field
   199 
   200 instantiation real :: real_field
   201 begin
   202 
   203 definition
   204   real_scaleR_def [simp]: "scaleR a x = a * x"
   205 
   206 instance proof
   207 qed (simp_all add: algebra_simps)
   208 
   209 end
   210 
   211 interpretation scaleR_left: additive "(\<lambda>a. scaleR a x::'a::real_vector)"
   212 proof qed (rule scaleR_left_distrib)
   213 
   214 interpretation scaleR_right: additive "(\<lambda>x. scaleR a x::'a::real_vector)"
   215 proof qed (rule scaleR_right_distrib)
   216 
   217 lemma nonzero_inverse_scaleR_distrib:
   218   fixes x :: "'a::real_div_algebra" shows
   219   "\<lbrakk>a \<noteq> 0; x \<noteq> 0\<rbrakk> \<Longrightarrow> inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
   220 by (rule inverse_unique, simp)
   221 
   222 lemma inverse_scaleR_distrib:
   223   fixes x :: "'a::{real_div_algebra, division_ring_inverse_zero}"
   224   shows "inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
   225 apply (case_tac "a = 0", simp)
   226 apply (case_tac "x = 0", simp)
   227 apply (erule (1) nonzero_inverse_scaleR_distrib)
   228 done
   229 
   230 
   231 subsection {* Embedding of the Reals into any @{text real_algebra_1}:
   232 @{term of_real} *}
   233 
   234 definition
   235   of_real :: "real \<Rightarrow> 'a::real_algebra_1" where
   236   "of_real r = scaleR r 1"
   237 
   238 lemma scaleR_conv_of_real: "scaleR r x = of_real r * x"
   239 by (simp add: of_real_def)
   240 
   241 lemma of_real_0 [simp]: "of_real 0 = 0"
   242 by (simp add: of_real_def)
   243 
   244 lemma of_real_1 [simp]: "of_real 1 = 1"
   245 by (simp add: of_real_def)
   246 
   247 lemma of_real_add [simp]: "of_real (x + y) = of_real x + of_real y"
   248 by (simp add: of_real_def scaleR_left_distrib)
   249 
   250 lemma of_real_minus [simp]: "of_real (- x) = - of_real x"
   251 by (simp add: of_real_def)
   252 
   253 lemma of_real_diff [simp]: "of_real (x - y) = of_real x - of_real y"
   254 by (simp add: of_real_def scaleR_left_diff_distrib)
   255 
   256 lemma of_real_mult [simp]: "of_real (x * y) = of_real x * of_real y"
   257 by (simp add: of_real_def mult_commute)
   258 
   259 lemma nonzero_of_real_inverse:
   260   "x \<noteq> 0 \<Longrightarrow> of_real (inverse x) =
   261    inverse (of_real x :: 'a::real_div_algebra)"
   262 by (simp add: of_real_def nonzero_inverse_scaleR_distrib)
   263 
   264 lemma of_real_inverse [simp]:
   265   "of_real (inverse x) =
   266    inverse (of_real x :: 'a::{real_div_algebra, division_ring_inverse_zero})"
   267 by (simp add: of_real_def inverse_scaleR_distrib)
   268 
   269 lemma nonzero_of_real_divide:
   270   "y \<noteq> 0 \<Longrightarrow> of_real (x / y) =
   271    (of_real x / of_real y :: 'a::real_field)"
   272 by (simp add: divide_inverse nonzero_of_real_inverse)
   273 
   274 lemma of_real_divide [simp]:
   275   "of_real (x / y) =
   276    (of_real x / of_real y :: 'a::{real_field, field_inverse_zero})"
   277 by (simp add: divide_inverse)
   278 
   279 lemma of_real_power [simp]:
   280   "of_real (x ^ n) = (of_real x :: 'a::{real_algebra_1}) ^ n"
   281 by (induct n) simp_all
   282 
   283 lemma of_real_eq_iff [simp]: "(of_real x = of_real y) = (x = y)"
   284 by (simp add: of_real_def)
   285 
   286 lemma inj_of_real:
   287   "inj of_real"
   288   by (auto intro: injI)
   289 
   290 lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
   291 
   292 lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
   293 proof
   294   fix r
   295   show "of_real r = id r"
   296     by (simp add: of_real_def)
   297 qed
   298 
   299 text{*Collapse nested embeddings*}
   300 lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
   301 by (induct n) auto
   302 
   303 lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
   304 by (cases z rule: int_diff_cases, simp)
   305 
   306 lemma of_real_numeral: "of_real (numeral w) = numeral w"
   307 using of_real_of_int_eq [of "numeral w"] by simp
   308 
   309 lemma of_real_neg_numeral: "of_real (neg_numeral w) = neg_numeral w"
   310 using of_real_of_int_eq [of "neg_numeral w"] by simp
   311 
   312 text{*Every real algebra has characteristic zero*}
   313 
   314 instance real_algebra_1 < ring_char_0
   315 proof
   316   from inj_of_real inj_of_nat have "inj (of_real \<circ> of_nat)" by (rule inj_comp)
   317   then show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: comp_def)
   318 qed
   319 
   320 instance real_field < field_char_0 ..
   321 
   322 
   323 subsection {* The Set of Real Numbers *}
   324 
   325 definition Reals :: "'a::real_algebra_1 set" where
   326   "Reals = range of_real"
   327 
   328 notation (xsymbols)
   329   Reals  ("\<real>")
   330 
   331 lemma Reals_of_real [simp]: "of_real r \<in> Reals"
   332 by (simp add: Reals_def)
   333 
   334 lemma Reals_of_int [simp]: "of_int z \<in> Reals"
   335 by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
   336 
   337 lemma Reals_of_nat [simp]: "of_nat n \<in> Reals"
   338 by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
   339 
   340 lemma Reals_numeral [simp]: "numeral w \<in> Reals"
   341 by (subst of_real_numeral [symmetric], rule Reals_of_real)
   342 
   343 lemma Reals_neg_numeral [simp]: "neg_numeral w \<in> Reals"
   344 by (subst of_real_neg_numeral [symmetric], rule Reals_of_real)
   345 
   346 lemma Reals_0 [simp]: "0 \<in> Reals"
   347 apply (unfold Reals_def)
   348 apply (rule range_eqI)
   349 apply (rule of_real_0 [symmetric])
   350 done
   351 
   352 lemma Reals_1 [simp]: "1 \<in> Reals"
   353 apply (unfold Reals_def)
   354 apply (rule range_eqI)
   355 apply (rule of_real_1 [symmetric])
   356 done
   357 
   358 lemma Reals_add [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_add [symmetric])
   362 done
   363 
   364 lemma Reals_minus [simp]: "a \<in> Reals \<Longrightarrow> - a \<in> Reals"
   365 apply (auto simp add: Reals_def)
   366 apply (rule range_eqI)
   367 apply (rule of_real_minus [symmetric])
   368 done
   369 
   370 lemma Reals_diff [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a - b \<in> Reals"
   371 apply (auto simp add: Reals_def)
   372 apply (rule range_eqI)
   373 apply (rule of_real_diff [symmetric])
   374 done
   375 
   376 lemma Reals_mult [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a * b \<in> Reals"
   377 apply (auto simp add: Reals_def)
   378 apply (rule range_eqI)
   379 apply (rule of_real_mult [symmetric])
   380 done
   381 
   382 lemma nonzero_Reals_inverse:
   383   fixes a :: "'a::real_div_algebra"
   384   shows "\<lbrakk>a \<in> Reals; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Reals"
   385 apply (auto simp add: Reals_def)
   386 apply (rule range_eqI)
   387 apply (erule nonzero_of_real_inverse [symmetric])
   388 done
   389 
   390 lemma Reals_inverse [simp]:
   391   fixes a :: "'a::{real_div_algebra, division_ring_inverse_zero}"
   392   shows "a \<in> Reals \<Longrightarrow> inverse a \<in> Reals"
   393 apply (auto simp add: Reals_def)
   394 apply (rule range_eqI)
   395 apply (rule of_real_inverse [symmetric])
   396 done
   397 
   398 lemma nonzero_Reals_divide:
   399   fixes a b :: "'a::real_field"
   400   shows "\<lbrakk>a \<in> Reals; b \<in> Reals; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   401 apply (auto simp add: Reals_def)
   402 apply (rule range_eqI)
   403 apply (erule nonzero_of_real_divide [symmetric])
   404 done
   405 
   406 lemma Reals_divide [simp]:
   407   fixes a b :: "'a::{real_field, field_inverse_zero}"
   408   shows "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
   409 apply (auto simp add: Reals_def)
   410 apply (rule range_eqI)
   411 apply (rule of_real_divide [symmetric])
   412 done
   413 
   414 lemma Reals_power [simp]:
   415   fixes a :: "'a::{real_algebra_1}"
   416   shows "a \<in> Reals \<Longrightarrow> a ^ n \<in> Reals"
   417 apply (auto simp add: Reals_def)
   418 apply (rule range_eqI)
   419 apply (rule of_real_power [symmetric])
   420 done
   421 
   422 lemma Reals_cases [cases set: Reals]:
   423   assumes "q \<in> \<real>"
   424   obtains (of_real) r where "q = of_real r"
   425   unfolding Reals_def
   426 proof -
   427   from `q \<in> \<real>` have "q \<in> range of_real" unfolding Reals_def .
   428   then obtain r where "q = of_real r" ..
   429   then show thesis ..
   430 qed
   431 
   432 lemma Reals_induct [case_names of_real, induct set: Reals]:
   433   "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
   434   by (rule Reals_cases) auto
   435 
   436 
   437 subsection {* Topological spaces *}
   438 
   439 class "open" =
   440   fixes "open" :: "'a set \<Rightarrow> bool"
   441 
   442 class topological_space = "open" +
   443   assumes open_UNIV [simp, intro]: "open UNIV"
   444   assumes open_Int [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<inter> T)"
   445   assumes open_Union [intro]: "\<forall>S\<in>K. open S \<Longrightarrow> open (\<Union> K)"
   446 begin
   447 
   448 definition
   449   closed :: "'a set \<Rightarrow> bool" where
   450   "closed S \<longleftrightarrow> open (- S)"
   451 
   452 lemma open_empty [intro, simp]: "open {}"
   453   using open_Union [of "{}"] by simp
   454 
   455 lemma open_Un [intro]: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<union> T)"
   456   using open_Union [of "{S, T}"] by simp
   457 
   458 lemma open_UN [intro]: "\<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Union>x\<in>A. B x)"
   459   unfolding SUP_def by (rule open_Union) auto
   460 
   461 lemma open_Inter [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. open T \<Longrightarrow> open (\<Inter>S)"
   462   by (induct set: finite) auto
   463 
   464 lemma open_INT [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. open (B x) \<Longrightarrow> open (\<Inter>x\<in>A. B x)"
   465   unfolding INF_def by (rule open_Inter) auto
   466 
   467 lemma closed_empty [intro, simp]:  "closed {}"
   468   unfolding closed_def by simp
   469 
   470 lemma closed_Un [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<union> T)"
   471   unfolding closed_def by auto
   472 
   473 lemma closed_UNIV [intro, simp]: "closed UNIV"
   474   unfolding closed_def by simp
   475 
   476 lemma closed_Int [intro]: "closed S \<Longrightarrow> closed T \<Longrightarrow> closed (S \<inter> T)"
   477   unfolding closed_def by auto
   478 
   479 lemma closed_INT [intro]: "\<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Inter>x\<in>A. B x)"
   480   unfolding closed_def by auto
   481 
   482 lemma closed_Inter [intro]: "\<forall>S\<in>K. closed S \<Longrightarrow> closed (\<Inter> K)"
   483   unfolding closed_def uminus_Inf by auto
   484 
   485 lemma closed_Union [intro]: "finite S \<Longrightarrow> \<forall>T\<in>S. closed T \<Longrightarrow> closed (\<Union>S)"
   486   by (induct set: finite) auto
   487 
   488 lemma closed_UN [intro]: "finite A \<Longrightarrow> \<forall>x\<in>A. closed (B x) \<Longrightarrow> closed (\<Union>x\<in>A. B x)"
   489   unfolding SUP_def by (rule closed_Union) auto
   490 
   491 lemma open_closed: "open S \<longleftrightarrow> closed (- S)"
   492   unfolding closed_def by simp
   493 
   494 lemma closed_open: "closed S \<longleftrightarrow> open (- S)"
   495   unfolding closed_def by simp
   496 
   497 lemma open_Diff [intro]: "open S \<Longrightarrow> closed T \<Longrightarrow> open (S - T)"
   498   unfolding closed_open Diff_eq by (rule open_Int)
   499 
   500 lemma closed_Diff [intro]: "closed S \<Longrightarrow> open T \<Longrightarrow> closed (S - T)"
   501   unfolding open_closed Diff_eq by (rule closed_Int)
   502 
   503 lemma open_Compl [intro]: "closed S \<Longrightarrow> open (- S)"
   504   unfolding closed_open .
   505 
   506 lemma closed_Compl [intro]: "open S \<Longrightarrow> closed (- S)"
   507   unfolding open_closed .
   508 
   509 end
   510 
   511 
   512 subsection {* Metric spaces *}
   513 
   514 class dist =
   515   fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
   516 
   517 class open_dist = "open" + dist +
   518   assumes open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   519 
   520 class metric_space = open_dist +
   521   assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
   522   assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
   523 begin
   524 
   525 lemma dist_self [simp]: "dist x x = 0"
   526 by simp
   527 
   528 lemma zero_le_dist [simp]: "0 \<le> dist x y"
   529 using dist_triangle2 [of x x y] by simp
   530 
   531 lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
   532 by (simp add: less_le)
   533 
   534 lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
   535 by (simp add: not_less)
   536 
   537 lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
   538 by (simp add: le_less)
   539 
   540 lemma dist_commute: "dist x y = dist y x"
   541 proof (rule order_antisym)
   542   show "dist x y \<le> dist y x"
   543     using dist_triangle2 [of x y x] by simp
   544   show "dist y x \<le> dist x y"
   545     using dist_triangle2 [of y x y] by simp
   546 qed
   547 
   548 lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
   549 using dist_triangle2 [of x z y] by (simp add: dist_commute)
   550 
   551 lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
   552 using dist_triangle2 [of x y a] by (simp add: dist_commute)
   553 
   554 lemma dist_triangle_alt:
   555   shows "dist y z <= dist x y + dist x z"
   556 by (rule dist_triangle3)
   557 
   558 lemma dist_pos_lt:
   559   shows "x \<noteq> y ==> 0 < dist x y"
   560 by (simp add: zero_less_dist_iff)
   561 
   562 lemma dist_nz:
   563   shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
   564 by (simp add: zero_less_dist_iff)
   565 
   566 lemma dist_triangle_le:
   567   shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
   568 by (rule order_trans [OF dist_triangle2])
   569 
   570 lemma dist_triangle_lt:
   571   shows "dist x z + dist y z < e ==> dist x y < e"
   572 by (rule le_less_trans [OF dist_triangle2])
   573 
   574 lemma dist_triangle_half_l:
   575   shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
   576 by (rule dist_triangle_lt [where z=y], simp)
   577 
   578 lemma dist_triangle_half_r:
   579   shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
   580 by (rule dist_triangle_half_l, simp_all add: dist_commute)
   581 
   582 subclass topological_space
   583 proof
   584   have "\<exists>e::real. 0 < e"
   585     by (fast intro: zero_less_one)
   586   then show "open UNIV"
   587     unfolding open_dist by simp
   588 next
   589   fix S T assume "open S" "open T"
   590   then show "open (S \<inter> T)"
   591     unfolding open_dist
   592     apply clarify
   593     apply (drule (1) bspec)+
   594     apply (clarify, rename_tac r s)
   595     apply (rule_tac x="min r s" in exI, simp)
   596     done
   597 next
   598   fix K assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
   599     unfolding open_dist by fast
   600 qed
   601 
   602 lemma (in metric_space) open_ball: "open {y. dist x y < d}"
   603 proof (unfold open_dist, intro ballI)
   604   fix y assume *: "y \<in> {y. dist x y < d}"
   605   then show "\<exists>e>0. \<forall>z. dist z y < e \<longrightarrow> z \<in> {y. dist x y < d}"
   606     by (auto intro!: exI[of _ "d - dist x y"] simp: field_simps dist_triangle_lt)
   607 qed
   608 
   609 end
   610 
   611 
   612 subsection {* Real normed vector spaces *}
   613 
   614 class norm =
   615   fixes norm :: "'a \<Rightarrow> real"
   616 
   617 class sgn_div_norm = scaleR + norm + sgn +
   618   assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
   619 
   620 class dist_norm = dist + norm + minus +
   621   assumes dist_norm: "dist x y = norm (x - y)"
   622 
   623 class real_normed_vector = real_vector + sgn_div_norm + dist_norm + open_dist +
   624   assumes norm_ge_zero [simp]: "0 \<le> norm x"
   625   and norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
   626   and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
   627   and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   628 
   629 class real_normed_algebra = real_algebra + real_normed_vector +
   630   assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
   631 
   632 class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
   633   assumes norm_one [simp]: "norm 1 = 1"
   634 
   635 class real_normed_div_algebra = real_div_algebra + real_normed_vector +
   636   assumes norm_mult: "norm (x * y) = norm x * norm y"
   637 
   638 class real_normed_field = real_field + real_normed_div_algebra
   639 
   640 instance real_normed_div_algebra < real_normed_algebra_1
   641 proof
   642   fix x y :: 'a
   643   show "norm (x * y) \<le> norm x * norm y"
   644     by (simp add: norm_mult)
   645 next
   646   have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
   647     by (rule norm_mult)
   648   thus "norm (1::'a) = 1" by simp
   649 qed
   650 
   651 lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
   652 by simp
   653 
   654 lemma zero_less_norm_iff [simp]:
   655   fixes x :: "'a::real_normed_vector"
   656   shows "(0 < norm x) = (x \<noteq> 0)"
   657 by (simp add: order_less_le)
   658 
   659 lemma norm_not_less_zero [simp]:
   660   fixes x :: "'a::real_normed_vector"
   661   shows "\<not> norm x < 0"
   662 by (simp add: linorder_not_less)
   663 
   664 lemma norm_le_zero_iff [simp]:
   665   fixes x :: "'a::real_normed_vector"
   666   shows "(norm x \<le> 0) = (x = 0)"
   667 by (simp add: order_le_less)
   668 
   669 lemma norm_minus_cancel [simp]:
   670   fixes x :: "'a::real_normed_vector"
   671   shows "norm (- x) = norm x"
   672 proof -
   673   have "norm (- x) = norm (scaleR (- 1) x)"
   674     by (simp only: scaleR_minus_left scaleR_one)
   675   also have "\<dots> = \<bar>- 1\<bar> * norm x"
   676     by (rule norm_scaleR)
   677   finally show ?thesis by simp
   678 qed
   679 
   680 lemma norm_minus_commute:
   681   fixes a b :: "'a::real_normed_vector"
   682   shows "norm (a - b) = norm (b - a)"
   683 proof -
   684   have "norm (- (b - a)) = norm (b - a)"
   685     by (rule norm_minus_cancel)
   686   thus ?thesis by simp
   687 qed
   688 
   689 lemma norm_triangle_ineq2:
   690   fixes a b :: "'a::real_normed_vector"
   691   shows "norm a - norm b \<le> norm (a - b)"
   692 proof -
   693   have "norm (a - b + b) \<le> norm (a - b) + norm b"
   694     by (rule norm_triangle_ineq)
   695   thus ?thesis by simp
   696 qed
   697 
   698 lemma norm_triangle_ineq3:
   699   fixes a b :: "'a::real_normed_vector"
   700   shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
   701 apply (subst abs_le_iff)
   702 apply auto
   703 apply (rule norm_triangle_ineq2)
   704 apply (subst norm_minus_commute)
   705 apply (rule norm_triangle_ineq2)
   706 done
   707 
   708 lemma norm_triangle_ineq4:
   709   fixes a b :: "'a::real_normed_vector"
   710   shows "norm (a - b) \<le> norm a + norm b"
   711 proof -
   712   have "norm (a + - b) \<le> norm a + norm (- b)"
   713     by (rule norm_triangle_ineq)
   714   thus ?thesis
   715     by (simp only: diff_minus norm_minus_cancel)
   716 qed
   717 
   718 lemma norm_diff_ineq:
   719   fixes a b :: "'a::real_normed_vector"
   720   shows "norm a - norm b \<le> norm (a + b)"
   721 proof -
   722   have "norm a - norm (- b) \<le> norm (a - - b)"
   723     by (rule norm_triangle_ineq2)
   724   thus ?thesis by simp
   725 qed
   726 
   727 lemma norm_diff_triangle_ineq:
   728   fixes a b c d :: "'a::real_normed_vector"
   729   shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
   730 proof -
   731   have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
   732     by (simp add: diff_minus add_ac)
   733   also have "\<dots> \<le> norm (a - c) + norm (b - d)"
   734     by (rule norm_triangle_ineq)
   735   finally show ?thesis .
   736 qed
   737 
   738 lemma abs_norm_cancel [simp]:
   739   fixes a :: "'a::real_normed_vector"
   740   shows "\<bar>norm a\<bar> = norm a"
   741 by (rule abs_of_nonneg [OF norm_ge_zero])
   742 
   743 lemma norm_add_less:
   744   fixes x y :: "'a::real_normed_vector"
   745   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
   746 by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
   747 
   748 lemma norm_mult_less:
   749   fixes x y :: "'a::real_normed_algebra"
   750   shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
   751 apply (rule order_le_less_trans [OF norm_mult_ineq])
   752 apply (simp add: mult_strict_mono')
   753 done
   754 
   755 lemma norm_of_real [simp]:
   756   "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
   757 unfolding of_real_def by simp
   758 
   759 lemma norm_numeral [simp]:
   760   "norm (numeral w::'a::real_normed_algebra_1) = numeral w"
   761 by (subst of_real_numeral [symmetric], subst norm_of_real, simp)
   762 
   763 lemma norm_neg_numeral [simp]:
   764   "norm (neg_numeral w::'a::real_normed_algebra_1) = numeral w"
   765 by (subst of_real_neg_numeral [symmetric], subst norm_of_real, simp)
   766 
   767 lemma norm_of_int [simp]:
   768   "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
   769 by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
   770 
   771 lemma norm_of_nat [simp]:
   772   "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
   773 apply (subst of_real_of_nat_eq [symmetric])
   774 apply (subst norm_of_real, simp)
   775 done
   776 
   777 lemma nonzero_norm_inverse:
   778   fixes a :: "'a::real_normed_div_algebra"
   779   shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
   780 apply (rule inverse_unique [symmetric])
   781 apply (simp add: norm_mult [symmetric])
   782 done
   783 
   784 lemma norm_inverse:
   785   fixes a :: "'a::{real_normed_div_algebra, division_ring_inverse_zero}"
   786   shows "norm (inverse a) = inverse (norm a)"
   787 apply (case_tac "a = 0", simp)
   788 apply (erule nonzero_norm_inverse)
   789 done
   790 
   791 lemma nonzero_norm_divide:
   792   fixes a b :: "'a::real_normed_field"
   793   shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
   794 by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
   795 
   796 lemma norm_divide:
   797   fixes a b :: "'a::{real_normed_field, field_inverse_zero}"
   798   shows "norm (a / b) = norm a / norm b"
   799 by (simp add: divide_inverse norm_mult norm_inverse)
   800 
   801 lemma norm_power_ineq:
   802   fixes x :: "'a::{real_normed_algebra_1}"
   803   shows "norm (x ^ n) \<le> norm x ^ n"
   804 proof (induct n)
   805   case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
   806 next
   807   case (Suc n)
   808   have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
   809     by (rule norm_mult_ineq)
   810   also from Suc have "\<dots> \<le> norm x * norm x ^ n"
   811     using norm_ge_zero by (rule mult_left_mono)
   812   finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
   813     by simp
   814 qed
   815 
   816 lemma norm_power:
   817   fixes x :: "'a::{real_normed_div_algebra}"
   818   shows "norm (x ^ n) = norm x ^ n"
   819 by (induct n) (simp_all add: norm_mult)
   820 
   821 text {* Every normed vector space is a metric space. *}
   822 
   823 instance real_normed_vector < metric_space
   824 proof
   825   fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
   826     unfolding dist_norm by simp
   827 next
   828   fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
   829     unfolding dist_norm
   830     using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
   831 qed
   832 
   833 
   834 subsection {* Class instances for real numbers *}
   835 
   836 instantiation real :: real_normed_field
   837 begin
   838 
   839 definition real_norm_def [simp]:
   840   "norm r = \<bar>r\<bar>"
   841 
   842 definition dist_real_def:
   843   "dist x y = \<bar>x - y\<bar>"
   844 
   845 definition open_real_def:
   846   "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   847 
   848 instance
   849 apply (intro_classes, unfold real_norm_def real_scaleR_def)
   850 apply (rule dist_real_def)
   851 apply (rule open_real_def)
   852 apply (simp add: sgn_real_def)
   853 apply (rule abs_ge_zero)
   854 apply (rule abs_eq_0)
   855 apply (rule abs_triangle_ineq)
   856 apply (rule abs_mult)
   857 apply (rule abs_mult)
   858 done
   859 
   860 end
   861 
   862 lemma open_real_lessThan [simp]:
   863   fixes a :: real shows "open {..<a}"
   864 unfolding open_real_def dist_real_def
   865 proof (clarify)
   866   fix x assume "x < a"
   867   hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
   868   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
   869 qed
   870 
   871 lemma open_real_greaterThan [simp]:
   872   fixes a :: real shows "open {a<..}"
   873 unfolding open_real_def dist_real_def
   874 proof (clarify)
   875   fix x assume "a < x"
   876   hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
   877   thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
   878 qed
   879 
   880 lemma open_real_greaterThanLessThan [simp]:
   881   fixes a b :: real shows "open {a<..<b}"
   882 proof -
   883   have "{a<..<b} = {a<..} \<inter> {..<b}" by auto
   884   thus "open {a<..<b}" by (simp add: open_Int)
   885 qed
   886 
   887 lemma closed_real_atMost [simp]: 
   888   fixes a :: real shows "closed {..a}"
   889 unfolding closed_open by simp
   890 
   891 lemma closed_real_atLeast [simp]:
   892   fixes a :: real shows "closed {a..}"
   893 unfolding closed_open by simp
   894 
   895 lemma closed_real_atLeastAtMost [simp]:
   896   fixes a b :: real shows "closed {a..b}"
   897 proof -
   898   have "{a..b} = {a..} \<inter> {..b}" by auto
   899   thus "closed {a..b}" by (simp add: closed_Int)
   900 qed
   901 
   902 
   903 subsection {* Extra type constraints *}
   904 
   905 text {* Only allow @{term "open"} in class @{text topological_space}. *}
   906 
   907 setup {* Sign.add_const_constraint
   908   (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"}) *}
   909 
   910 text {* Only allow @{term dist} in class @{text metric_space}. *}
   911 
   912 setup {* Sign.add_const_constraint
   913   (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"}) *}
   914 
   915 text {* Only allow @{term norm} in class @{text real_normed_vector}. *}
   916 
   917 setup {* Sign.add_const_constraint
   918   (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"}) *}
   919 
   920 
   921 subsection {* Sign function *}
   922 
   923 lemma norm_sgn:
   924   "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
   925 by (simp add: sgn_div_norm)
   926 
   927 lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
   928 by (simp add: sgn_div_norm)
   929 
   930 lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
   931 by (simp add: sgn_div_norm)
   932 
   933 lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
   934 by (simp add: sgn_div_norm)
   935 
   936 lemma sgn_scaleR:
   937   "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
   938 by (simp add: sgn_div_norm mult_ac)
   939 
   940 lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
   941 by (simp add: sgn_div_norm)
   942 
   943 lemma sgn_of_real:
   944   "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
   945 unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
   946 
   947 lemma sgn_mult:
   948   fixes x y :: "'a::real_normed_div_algebra"
   949   shows "sgn (x * y) = sgn x * sgn y"
   950 by (simp add: sgn_div_norm norm_mult mult_commute)
   951 
   952 lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
   953 by (simp add: sgn_div_norm divide_inverse)
   954 
   955 lemma real_sgn_pos: "0 < (x::real) \<Longrightarrow> sgn x = 1"
   956 unfolding real_sgn_eq by simp
   957 
   958 lemma real_sgn_neg: "(x::real) < 0 \<Longrightarrow> sgn x = -1"
   959 unfolding real_sgn_eq by simp
   960 
   961 
   962 subsection {* Bounded Linear and Bilinear Operators *}
   963 
   964 locale bounded_linear = additive f for f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" +
   965   assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
   966   assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
   967 begin
   968 
   969 lemma pos_bounded:
   970   "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
   971 proof -
   972   obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
   973     using bounded by fast
   974   show ?thesis
   975   proof (intro exI impI conjI allI)
   976     show "0 < max 1 K"
   977       by (rule order_less_le_trans [OF zero_less_one le_maxI1])
   978   next
   979     fix x
   980     have "norm (f x) \<le> norm x * K" using K .
   981     also have "\<dots> \<le> norm x * max 1 K"
   982       by (rule mult_left_mono [OF le_maxI2 norm_ge_zero])
   983     finally show "norm (f x) \<le> norm x * max 1 K" .
   984   qed
   985 qed
   986 
   987 lemma nonneg_bounded:
   988   "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
   989 proof -
   990   from pos_bounded
   991   show ?thesis by (auto intro: order_less_imp_le)
   992 qed
   993 
   994 end
   995 
   996 lemma bounded_linear_intro:
   997   assumes "\<And>x y. f (x + y) = f x + f y"
   998   assumes "\<And>r x. f (scaleR r x) = scaleR r (f x)"
   999   assumes "\<And>x. norm (f x) \<le> norm x * K"
  1000   shows "bounded_linear f"
  1001   by default (fast intro: assms)+
  1002 
  1003 locale bounded_bilinear =
  1004   fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
  1005                  \<Rightarrow> 'c::real_normed_vector"
  1006     (infixl "**" 70)
  1007   assumes add_left: "prod (a + a') b = prod a b + prod a' b"
  1008   assumes add_right: "prod a (b + b') = prod a b + prod a b'"
  1009   assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
  1010   assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
  1011   assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
  1012 begin
  1013 
  1014 lemma pos_bounded:
  1015   "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1016 apply (cut_tac bounded, erule exE)
  1017 apply (rule_tac x="max 1 K" in exI, safe)
  1018 apply (rule order_less_le_trans [OF zero_less_one le_maxI1])
  1019 apply (drule spec, drule spec, erule order_trans)
  1020 apply (rule mult_left_mono [OF le_maxI2])
  1021 apply (intro mult_nonneg_nonneg norm_ge_zero)
  1022 done
  1023 
  1024 lemma nonneg_bounded:
  1025   "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
  1026 proof -
  1027   from pos_bounded
  1028   show ?thesis by (auto intro: order_less_imp_le)
  1029 qed
  1030 
  1031 lemma additive_right: "additive (\<lambda>b. prod a b)"
  1032 by (rule additive.intro, rule add_right)
  1033 
  1034 lemma additive_left: "additive (\<lambda>a. prod a b)"
  1035 by (rule additive.intro, rule add_left)
  1036 
  1037 lemma zero_left: "prod 0 b = 0"
  1038 by (rule additive.zero [OF additive_left])
  1039 
  1040 lemma zero_right: "prod a 0 = 0"
  1041 by (rule additive.zero [OF additive_right])
  1042 
  1043 lemma minus_left: "prod (- a) b = - prod a b"
  1044 by (rule additive.minus [OF additive_left])
  1045 
  1046 lemma minus_right: "prod a (- b) = - prod a b"
  1047 by (rule additive.minus [OF additive_right])
  1048 
  1049 lemma diff_left:
  1050   "prod (a - a') b = prod a b - prod a' b"
  1051 by (rule additive.diff [OF additive_left])
  1052 
  1053 lemma diff_right:
  1054   "prod a (b - b') = prod a b - prod a b'"
  1055 by (rule additive.diff [OF additive_right])
  1056 
  1057 lemma bounded_linear_left:
  1058   "bounded_linear (\<lambda>a. a ** b)"
  1059 apply (cut_tac bounded, safe)
  1060 apply (rule_tac K="norm b * K" in bounded_linear_intro)
  1061 apply (rule add_left)
  1062 apply (rule scaleR_left)
  1063 apply (simp add: mult_ac)
  1064 done
  1065 
  1066 lemma bounded_linear_right:
  1067   "bounded_linear (\<lambda>b. a ** b)"
  1068 apply (cut_tac bounded, safe)
  1069 apply (rule_tac K="norm a * K" in bounded_linear_intro)
  1070 apply (rule add_right)
  1071 apply (rule scaleR_right)
  1072 apply (simp add: mult_ac)
  1073 done
  1074 
  1075 lemma prod_diff_prod:
  1076   "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
  1077 by (simp add: diff_left diff_right)
  1078 
  1079 end
  1080 
  1081 lemma bounded_bilinear_mult:
  1082   "bounded_bilinear (op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra)"
  1083 apply (rule bounded_bilinear.intro)
  1084 apply (rule left_distrib)
  1085 apply (rule right_distrib)
  1086 apply (rule mult_scaleR_left)
  1087 apply (rule mult_scaleR_right)
  1088 apply (rule_tac x="1" in exI)
  1089 apply (simp add: norm_mult_ineq)
  1090 done
  1091 
  1092 lemma bounded_linear_mult_left:
  1093   "bounded_linear (\<lambda>x::'a::real_normed_algebra. x * y)"
  1094   using bounded_bilinear_mult
  1095   by (rule bounded_bilinear.bounded_linear_left)
  1096 
  1097 lemma bounded_linear_mult_right:
  1098   "bounded_linear (\<lambda>y::'a::real_normed_algebra. x * y)"
  1099   using bounded_bilinear_mult
  1100   by (rule bounded_bilinear.bounded_linear_right)
  1101 
  1102 lemma bounded_linear_divide:
  1103   "bounded_linear (\<lambda>x::'a::real_normed_field. x / y)"
  1104   unfolding divide_inverse by (rule bounded_linear_mult_left)
  1105 
  1106 lemma bounded_bilinear_scaleR: "bounded_bilinear scaleR"
  1107 apply (rule bounded_bilinear.intro)
  1108 apply (rule scaleR_left_distrib)
  1109 apply (rule scaleR_right_distrib)
  1110 apply simp
  1111 apply (rule scaleR_left_commute)
  1112 apply (rule_tac x="1" in exI, simp)
  1113 done
  1114 
  1115 lemma bounded_linear_scaleR_left: "bounded_linear (\<lambda>r. scaleR r x)"
  1116   using bounded_bilinear_scaleR
  1117   by (rule bounded_bilinear.bounded_linear_left)
  1118 
  1119 lemma bounded_linear_scaleR_right: "bounded_linear (\<lambda>x. scaleR r x)"
  1120   using bounded_bilinear_scaleR
  1121   by (rule bounded_bilinear.bounded_linear_right)
  1122 
  1123 lemma bounded_linear_of_real: "bounded_linear (\<lambda>r. of_real r)"
  1124   unfolding of_real_def by (rule bounded_linear_scaleR_left)
  1125 
  1126 subsection{* Hausdorff and other separation properties *}
  1127 
  1128 class t0_space = topological_space +
  1129   assumes t0_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> \<not> (x \<in> U \<longleftrightarrow> y \<in> U)"
  1130 
  1131 class t1_space = topological_space +
  1132   assumes t1_space: "x \<noteq> y \<Longrightarrow> \<exists>U. open U \<and> x \<in> U \<and> y \<notin> U"
  1133 
  1134 instance t1_space \<subseteq> t0_space
  1135 proof qed (fast dest: t1_space)
  1136 
  1137 lemma separation_t1:
  1138   fixes x y :: "'a::t1_space"
  1139   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> x \<in> U \<and> y \<notin> U)"
  1140   using t1_space[of x y] by blast
  1141 
  1142 lemma closed_singleton:
  1143   fixes a :: "'a::t1_space"
  1144   shows "closed {a}"
  1145 proof -
  1146   let ?T = "\<Union>{S. open S \<and> a \<notin> S}"
  1147   have "open ?T" by (simp add: open_Union)
  1148   also have "?T = - {a}"
  1149     by (simp add: set_eq_iff separation_t1, auto)
  1150   finally show "closed {a}" unfolding closed_def .
  1151 qed
  1152 
  1153 lemma closed_insert [simp]:
  1154   fixes a :: "'a::t1_space"
  1155   assumes "closed S" shows "closed (insert a S)"
  1156 proof -
  1157   from closed_singleton assms
  1158   have "closed ({a} \<union> S)" by (rule closed_Un)
  1159   thus "closed (insert a S)" by simp
  1160 qed
  1161 
  1162 lemma finite_imp_closed:
  1163   fixes S :: "'a::t1_space set"
  1164   shows "finite S \<Longrightarrow> closed S"
  1165 by (induct set: finite, simp_all)
  1166 
  1167 text {* T2 spaces are also known as Hausdorff spaces. *}
  1168 
  1169 class t2_space = topological_space +
  1170   assumes hausdorff: "x \<noteq> y \<Longrightarrow> \<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
  1171 
  1172 instance t2_space \<subseteq> t1_space
  1173 proof qed (fast dest: hausdorff)
  1174 
  1175 instance metric_space \<subseteq> t2_space
  1176 proof
  1177   fix x y :: "'a::metric_space"
  1178   assume xy: "x \<noteq> y"
  1179   let ?U = "{y'. dist x y' < dist x y / 2}"
  1180   let ?V = "{x'. dist y x' < dist x y / 2}"
  1181   have th0: "\<And>d x y z. (d x z :: real) \<le> d x y + d y z \<Longrightarrow> d y z = d z y
  1182                \<Longrightarrow> \<not>(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
  1183   have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
  1184     using dist_pos_lt[OF xy] th0[of dist, OF dist_triangle dist_commute]
  1185     using open_ball[of _ "dist x y / 2"] by auto
  1186   then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
  1187     by blast
  1188 qed
  1189 
  1190 lemma separation_t2:
  1191   fixes x y :: "'a::t2_space"
  1192   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {})"
  1193   using hausdorff[of x y] by blast
  1194 
  1195 lemma separation_t0:
  1196   fixes x y :: "'a::t0_space"
  1197   shows "x \<noteq> y \<longleftrightarrow> (\<exists>U. open U \<and> ~(x\<in>U \<longleftrightarrow> y\<in>U))"
  1198   using t0_space[of x y] by blast
  1199 
  1200 text {* A perfect space is a topological space with no isolated points. *}
  1201 
  1202 class perfect_space = topological_space +
  1203   assumes not_open_singleton: "\<not> open {x}"
  1204 
  1205 instance real_normed_algebra_1 \<subseteq> perfect_space
  1206 proof
  1207   fix x::'a
  1208   show "\<not> open {x}"
  1209     unfolding open_dist dist_norm
  1210     by (clarsimp, rule_tac x="x + of_real (e/2)" in exI, simp)
  1211 qed
  1212 
  1213 end