src/HOL/Real_Vector_Spaces.thy
author paulson <lp15@cam.ac.uk>
Thu Apr 03 23:51:52 2014 +0100 (2014-04-03)
changeset 56409 36489d77c484
parent 56369 2704ca85be98
child 56479 91958d4b30f7
permissions -rw-r--r--
removing simprule status for divide_minus_left and divide_minus_right
hoelzl@51524
     1
(*  Title:      HOL/Real_Vector_Spaces.thy
haftmann@27552
     2
    Author:     Brian Huffman
hoelzl@51531
     3
    Author:     Johannes Hölzl
huffman@20504
     4
*)
huffman@20504
     5
huffman@20504
     6
header {* Vector Spaces and Algebras over the Reals *}
huffman@20504
     7
hoelzl@51524
     8
theory Real_Vector_Spaces
hoelzl@51531
     9
imports Real Topological_Spaces
huffman@20504
    10
begin
huffman@20504
    11
huffman@20504
    12
subsection {* Locale for additive functions *}
huffman@20504
    13
huffman@20504
    14
locale additive =
huffman@20504
    15
  fixes f :: "'a::ab_group_add \<Rightarrow> 'b::ab_group_add"
huffman@20504
    16
  assumes add: "f (x + y) = f x + f y"
huffman@27443
    17
begin
huffman@20504
    18
huffman@27443
    19
lemma zero: "f 0 = 0"
huffman@20504
    20
proof -
huffman@20504
    21
  have "f 0 = f (0 + 0)" by simp
huffman@20504
    22
  also have "\<dots> = f 0 + f 0" by (rule add)
huffman@20504
    23
  finally show "f 0 = 0" by simp
huffman@20504
    24
qed
huffman@20504
    25
huffman@27443
    26
lemma minus: "f (- x) = - f x"
huffman@20504
    27
proof -
huffman@20504
    28
  have "f (- x) + f x = f (- x + x)" by (rule add [symmetric])
huffman@20504
    29
  also have "\<dots> = - f x + f x" by (simp add: zero)
huffman@20504
    30
  finally show "f (- x) = - f x" by (rule add_right_imp_eq)
huffman@20504
    31
qed
huffman@20504
    32
huffman@27443
    33
lemma diff: "f (x - y) = f x - f y"
haftmann@54230
    34
  using add [of x "- y"] by (simp add: minus)
huffman@20504
    35
huffman@27443
    36
lemma setsum: "f (setsum g A) = (\<Sum>x\<in>A. f (g x))"
huffman@22942
    37
apply (cases "finite A")
huffman@22942
    38
apply (induct set: finite)
huffman@22942
    39
apply (simp add: zero)
huffman@22942
    40
apply (simp add: add)
huffman@22942
    41
apply (simp add: zero)
huffman@22942
    42
done
huffman@22942
    43
huffman@27443
    44
end
huffman@20504
    45
huffman@28029
    46
subsection {* Vector spaces *}
huffman@28029
    47
huffman@28029
    48
locale vector_space =
huffman@28029
    49
  fixes scale :: "'a::field \<Rightarrow> 'b::ab_group_add \<Rightarrow> 'b"
huffman@30070
    50
  assumes scale_right_distrib [algebra_simps]:
huffman@30070
    51
    "scale a (x + y) = scale a x + scale a y"
huffman@30070
    52
  and scale_left_distrib [algebra_simps]:
huffman@30070
    53
    "scale (a + b) x = scale a x + scale b x"
huffman@28029
    54
  and scale_scale [simp]: "scale a (scale b x) = scale (a * b) x"
huffman@28029
    55
  and scale_one [simp]: "scale 1 x = x"
huffman@28029
    56
begin
huffman@28029
    57
huffman@28029
    58
lemma scale_left_commute:
huffman@28029
    59
  "scale a (scale b x) = scale b (scale a x)"
huffman@28029
    60
by (simp add: mult_commute)
huffman@28029
    61
huffman@28029
    62
lemma scale_zero_left [simp]: "scale 0 x = 0"
huffman@28029
    63
  and scale_minus_left [simp]: "scale (- a) x = - (scale a x)"
huffman@30070
    64
  and scale_left_diff_distrib [algebra_simps]:
huffman@30070
    65
        "scale (a - b) x = scale a x - scale b x"
huffman@44282
    66
  and scale_setsum_left: "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)"
huffman@28029
    67
proof -
ballarin@29229
    68
  interpret s: additive "\<lambda>a. scale a x"
haftmann@28823
    69
    proof qed (rule scale_left_distrib)
huffman@28029
    70
  show "scale 0 x = 0" by (rule s.zero)
huffman@28029
    71
  show "scale (- a) x = - (scale a x)" by (rule s.minus)
huffman@28029
    72
  show "scale (a - b) x = scale a x - scale b x" by (rule s.diff)
huffman@44282
    73
  show "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)" by (rule s.setsum)
huffman@28029
    74
qed
huffman@28029
    75
huffman@28029
    76
lemma scale_zero_right [simp]: "scale a 0 = 0"
huffman@28029
    77
  and scale_minus_right [simp]: "scale a (- x) = - (scale a x)"
huffman@30070
    78
  and scale_right_diff_distrib [algebra_simps]:
huffman@30070
    79
        "scale a (x - y) = scale a x - scale a y"
huffman@44282
    80
  and scale_setsum_right: "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))"
huffman@28029
    81
proof -
ballarin@29229
    82
  interpret s: additive "\<lambda>x. scale a x"
haftmann@28823
    83
    proof qed (rule scale_right_distrib)
huffman@28029
    84
  show "scale a 0 = 0" by (rule s.zero)
huffman@28029
    85
  show "scale a (- x) = - (scale a x)" by (rule s.minus)
huffman@28029
    86
  show "scale a (x - y) = scale a x - scale a y" by (rule s.diff)
huffman@44282
    87
  show "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))" by (rule s.setsum)
huffman@28029
    88
qed
huffman@28029
    89
huffman@28029
    90
lemma scale_eq_0_iff [simp]:
huffman@28029
    91
  "scale a x = 0 \<longleftrightarrow> a = 0 \<or> x = 0"
huffman@28029
    92
proof cases
huffman@28029
    93
  assume "a = 0" thus ?thesis by simp
huffman@28029
    94
next
huffman@28029
    95
  assume anz [simp]: "a \<noteq> 0"
huffman@28029
    96
  { assume "scale a x = 0"
huffman@28029
    97
    hence "scale (inverse a) (scale a x) = 0" by simp
huffman@28029
    98
    hence "x = 0" by simp }
huffman@28029
    99
  thus ?thesis by force
huffman@28029
   100
qed
huffman@28029
   101
huffman@28029
   102
lemma scale_left_imp_eq:
huffman@28029
   103
  "\<lbrakk>a \<noteq> 0; scale a x = scale a y\<rbrakk> \<Longrightarrow> x = y"
huffman@28029
   104
proof -
huffman@28029
   105
  assume nonzero: "a \<noteq> 0"
huffman@28029
   106
  assume "scale a x = scale a y"
huffman@28029
   107
  hence "scale a (x - y) = 0"
huffman@28029
   108
     by (simp add: scale_right_diff_distrib)
huffman@28029
   109
  hence "x - y = 0" by (simp add: nonzero)
huffman@28029
   110
  thus "x = y" by (simp only: right_minus_eq)
huffman@28029
   111
qed
huffman@28029
   112
huffman@28029
   113
lemma scale_right_imp_eq:
huffman@28029
   114
  "\<lbrakk>x \<noteq> 0; scale a x = scale b x\<rbrakk> \<Longrightarrow> a = b"
huffman@28029
   115
proof -
huffman@28029
   116
  assume nonzero: "x \<noteq> 0"
huffman@28029
   117
  assume "scale a x = scale b x"
huffman@28029
   118
  hence "scale (a - b) x = 0"
huffman@28029
   119
     by (simp add: scale_left_diff_distrib)
huffman@28029
   120
  hence "a - b = 0" by (simp add: nonzero)
huffman@28029
   121
  thus "a = b" by (simp only: right_minus_eq)
huffman@28029
   122
qed
huffman@28029
   123
huffman@31586
   124
lemma scale_cancel_left [simp]:
huffman@28029
   125
  "scale a x = scale a y \<longleftrightarrow> x = y \<or> a = 0"
huffman@28029
   126
by (auto intro: scale_left_imp_eq)
huffman@28029
   127
huffman@31586
   128
lemma scale_cancel_right [simp]:
huffman@28029
   129
  "scale a x = scale b x \<longleftrightarrow> a = b \<or> x = 0"
huffman@28029
   130
by (auto intro: scale_right_imp_eq)
huffman@28029
   131
huffman@28029
   132
end
huffman@28029
   133
huffman@20504
   134
subsection {* Real vector spaces *}
huffman@20504
   135
haftmann@29608
   136
class scaleR =
haftmann@25062
   137
  fixes scaleR :: "real \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "*\<^sub>R" 75)
haftmann@24748
   138
begin
huffman@20504
   139
huffman@20763
   140
abbreviation
haftmann@25062
   141
  divideR :: "'a \<Rightarrow> real \<Rightarrow> 'a" (infixl "'/\<^sub>R" 70)
haftmann@24748
   142
where
haftmann@25062
   143
  "x /\<^sub>R r == scaleR (inverse r) x"
haftmann@24748
   144
haftmann@24748
   145
end
haftmann@24748
   146
haftmann@24588
   147
class real_vector = scaleR + ab_group_add +
huffman@44282
   148
  assumes scaleR_add_right: "scaleR a (x + y) = scaleR a x + scaleR a y"
huffman@44282
   149
  and scaleR_add_left: "scaleR (a + b) x = scaleR a x + scaleR b x"
huffman@30070
   150
  and scaleR_scaleR: "scaleR a (scaleR b x) = scaleR (a * b) x"
huffman@30070
   151
  and scaleR_one: "scaleR 1 x = x"
huffman@20504
   152
wenzelm@30729
   153
interpretation real_vector:
ballarin@29229
   154
  vector_space "scaleR :: real \<Rightarrow> 'a \<Rightarrow> 'a::real_vector"
huffman@28009
   155
apply unfold_locales
huffman@44282
   156
apply (rule scaleR_add_right)
huffman@44282
   157
apply (rule scaleR_add_left)
huffman@28009
   158
apply (rule scaleR_scaleR)
huffman@28009
   159
apply (rule scaleR_one)
huffman@28009
   160
done
huffman@28009
   161
huffman@28009
   162
text {* Recover original theorem names *}
huffman@28009
   163
huffman@28009
   164
lemmas scaleR_left_commute = real_vector.scale_left_commute
huffman@28009
   165
lemmas scaleR_zero_left = real_vector.scale_zero_left
huffman@28009
   166
lemmas scaleR_minus_left = real_vector.scale_minus_left
huffman@44282
   167
lemmas scaleR_diff_left = real_vector.scale_left_diff_distrib
huffman@44282
   168
lemmas scaleR_setsum_left = real_vector.scale_setsum_left
huffman@28009
   169
lemmas scaleR_zero_right = real_vector.scale_zero_right
huffman@28009
   170
lemmas scaleR_minus_right = real_vector.scale_minus_right
huffman@44282
   171
lemmas scaleR_diff_right = real_vector.scale_right_diff_distrib
huffman@44282
   172
lemmas scaleR_setsum_right = real_vector.scale_setsum_right
huffman@28009
   173
lemmas scaleR_eq_0_iff = real_vector.scale_eq_0_iff
huffman@28009
   174
lemmas scaleR_left_imp_eq = real_vector.scale_left_imp_eq
huffman@28009
   175
lemmas scaleR_right_imp_eq = real_vector.scale_right_imp_eq
huffman@28009
   176
lemmas scaleR_cancel_left = real_vector.scale_cancel_left
huffman@28009
   177
lemmas scaleR_cancel_right = real_vector.scale_cancel_right
huffman@28009
   178
huffman@44282
   179
text {* Legacy names *}
huffman@44282
   180
huffman@44282
   181
lemmas scaleR_left_distrib = scaleR_add_left
huffman@44282
   182
lemmas scaleR_right_distrib = scaleR_add_right
huffman@44282
   183
lemmas scaleR_left_diff_distrib = scaleR_diff_left
huffman@44282
   184
lemmas scaleR_right_diff_distrib = scaleR_diff_right
huffman@44282
   185
huffman@31285
   186
lemma scaleR_minus1_left [simp]:
huffman@31285
   187
  fixes x :: "'a::real_vector"
huffman@31285
   188
  shows "scaleR (-1) x = - x"
huffman@31285
   189
  using scaleR_minus_left [of 1 x] by simp
huffman@31285
   190
haftmann@24588
   191
class real_algebra = real_vector + ring +
haftmann@25062
   192
  assumes mult_scaleR_left [simp]: "scaleR a x * y = scaleR a (x * y)"
haftmann@25062
   193
  and mult_scaleR_right [simp]: "x * scaleR a y = scaleR a (x * y)"
huffman@20504
   194
haftmann@24588
   195
class real_algebra_1 = real_algebra + ring_1
huffman@20554
   196
haftmann@24588
   197
class real_div_algebra = real_algebra_1 + division_ring
huffman@20584
   198
haftmann@24588
   199
class real_field = real_div_algebra + field
huffman@20584
   200
huffman@30069
   201
instantiation real :: real_field
huffman@30069
   202
begin
huffman@30069
   203
huffman@30069
   204
definition
huffman@30069
   205
  real_scaleR_def [simp]: "scaleR a x = a * x"
huffman@30069
   206
huffman@30070
   207
instance proof
huffman@30070
   208
qed (simp_all add: algebra_simps)
huffman@20554
   209
huffman@30069
   210
end
huffman@30069
   211
wenzelm@30729
   212
interpretation scaleR_left: additive "(\<lambda>a. scaleR a x::'a::real_vector)"
haftmann@28823
   213
proof qed (rule scaleR_left_distrib)
huffman@20504
   214
wenzelm@30729
   215
interpretation scaleR_right: additive "(\<lambda>x. scaleR a x::'a::real_vector)"
haftmann@28823
   216
proof qed (rule scaleR_right_distrib)
huffman@20504
   217
huffman@20584
   218
lemma nonzero_inverse_scaleR_distrib:
huffman@21809
   219
  fixes x :: "'a::real_div_algebra" shows
huffman@21809
   220
  "\<lbrakk>a \<noteq> 0; x \<noteq> 0\<rbrakk> \<Longrightarrow> inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
huffman@20763
   221
by (rule inverse_unique, simp)
huffman@20584
   222
huffman@20584
   223
lemma inverse_scaleR_distrib:
haftmann@36409
   224
  fixes x :: "'a::{real_div_algebra, division_ring_inverse_zero}"
huffman@21809
   225
  shows "inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
huffman@20584
   226
apply (case_tac "a = 0", simp)
huffman@20584
   227
apply (case_tac "x = 0", simp)
huffman@20584
   228
apply (erule (1) nonzero_inverse_scaleR_distrib)
huffman@20584
   229
done
huffman@20584
   230
huffman@20554
   231
huffman@20554
   232
subsection {* Embedding of the Reals into any @{text real_algebra_1}:
huffman@20554
   233
@{term of_real} *}
huffman@20554
   234
huffman@20554
   235
definition
wenzelm@21404
   236
  of_real :: "real \<Rightarrow> 'a::real_algebra_1" where
huffman@21809
   237
  "of_real r = scaleR r 1"
huffman@20554
   238
huffman@21809
   239
lemma scaleR_conv_of_real: "scaleR r x = of_real r * x"
huffman@20763
   240
by (simp add: of_real_def)
huffman@20763
   241
huffman@20554
   242
lemma of_real_0 [simp]: "of_real 0 = 0"
huffman@20554
   243
by (simp add: of_real_def)
huffman@20554
   244
huffman@20554
   245
lemma of_real_1 [simp]: "of_real 1 = 1"
huffman@20554
   246
by (simp add: of_real_def)
huffman@20554
   247
huffman@20554
   248
lemma of_real_add [simp]: "of_real (x + y) = of_real x + of_real y"
huffman@20554
   249
by (simp add: of_real_def scaleR_left_distrib)
huffman@20554
   250
huffman@20554
   251
lemma of_real_minus [simp]: "of_real (- x) = - of_real x"
huffman@20554
   252
by (simp add: of_real_def)
huffman@20554
   253
huffman@20554
   254
lemma of_real_diff [simp]: "of_real (x - y) = of_real x - of_real y"
huffman@20554
   255
by (simp add: of_real_def scaleR_left_diff_distrib)
huffman@20554
   256
huffman@20554
   257
lemma of_real_mult [simp]: "of_real (x * y) = of_real x * of_real y"
huffman@20763
   258
by (simp add: of_real_def mult_commute)
huffman@20554
   259
huffman@20584
   260
lemma nonzero_of_real_inverse:
huffman@20584
   261
  "x \<noteq> 0 \<Longrightarrow> of_real (inverse x) =
huffman@20584
   262
   inverse (of_real x :: 'a::real_div_algebra)"
huffman@20584
   263
by (simp add: of_real_def nonzero_inverse_scaleR_distrib)
huffman@20584
   264
huffman@20584
   265
lemma of_real_inverse [simp]:
huffman@20584
   266
  "of_real (inverse x) =
haftmann@36409
   267
   inverse (of_real x :: 'a::{real_div_algebra, division_ring_inverse_zero})"
huffman@20584
   268
by (simp add: of_real_def inverse_scaleR_distrib)
huffman@20584
   269
huffman@20584
   270
lemma nonzero_of_real_divide:
huffman@20584
   271
  "y \<noteq> 0 \<Longrightarrow> of_real (x / y) =
huffman@20584
   272
   (of_real x / of_real y :: 'a::real_field)"
huffman@20584
   273
by (simp add: divide_inverse nonzero_of_real_inverse)
huffman@20722
   274
huffman@20722
   275
lemma of_real_divide [simp]:
huffman@20584
   276
  "of_real (x / y) =
haftmann@36409
   277
   (of_real x / of_real y :: 'a::{real_field, field_inverse_zero})"
huffman@20584
   278
by (simp add: divide_inverse)
huffman@20584
   279
huffman@20722
   280
lemma of_real_power [simp]:
haftmann@31017
   281
  "of_real (x ^ n) = (of_real x :: 'a::{real_algebra_1}) ^ n"
huffman@30273
   282
by (induct n) simp_all
huffman@20722
   283
huffman@20554
   284
lemma of_real_eq_iff [simp]: "(of_real x = of_real y) = (x = y)"
huffman@35216
   285
by (simp add: of_real_def)
huffman@20554
   286
haftmann@38621
   287
lemma inj_of_real:
haftmann@38621
   288
  "inj of_real"
haftmann@38621
   289
  by (auto intro: injI)
haftmann@38621
   290
huffman@20584
   291
lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
huffman@20554
   292
huffman@20554
   293
lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
huffman@20554
   294
proof
huffman@20554
   295
  fix r
huffman@20554
   296
  show "of_real r = id r"
huffman@22973
   297
    by (simp add: of_real_def)
huffman@20554
   298
qed
huffman@20554
   299
huffman@20554
   300
text{*Collapse nested embeddings*}
huffman@20554
   301
lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
wenzelm@20772
   302
by (induct n) auto
huffman@20554
   303
huffman@20554
   304
lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
huffman@20554
   305
by (cases z rule: int_diff_cases, simp)
huffman@20554
   306
huffman@47108
   307
lemma of_real_numeral: "of_real (numeral w) = numeral w"
huffman@47108
   308
using of_real_of_int_eq [of "numeral w"] by simp
huffman@47108
   309
haftmann@54489
   310
lemma of_real_neg_numeral: "of_real (- numeral w) = - numeral w"
haftmann@54489
   311
using of_real_of_int_eq [of "- numeral w"] by simp
huffman@20554
   312
huffman@22912
   313
text{*Every real algebra has characteristic zero*}
haftmann@38621
   314
huffman@22912
   315
instance real_algebra_1 < ring_char_0
huffman@22912
   316
proof
haftmann@38621
   317
  from inj_of_real inj_of_nat have "inj (of_real \<circ> of_nat)" by (rule inj_comp)
haftmann@38621
   318
  then show "inj (of_nat :: nat \<Rightarrow> 'a)" by (simp add: comp_def)
huffman@22912
   319
qed
huffman@22912
   320
huffman@27553
   321
instance real_field < field_char_0 ..
huffman@27553
   322
huffman@20554
   323
huffman@20554
   324
subsection {* The Set of Real Numbers *}
huffman@20554
   325
haftmann@37767
   326
definition Reals :: "'a::real_algebra_1 set" where
haftmann@37767
   327
  "Reals = range of_real"
huffman@20554
   328
wenzelm@21210
   329
notation (xsymbols)
huffman@20554
   330
  Reals  ("\<real>")
huffman@20554
   331
huffman@21809
   332
lemma Reals_of_real [simp]: "of_real r \<in> Reals"
huffman@20554
   333
by (simp add: Reals_def)
huffman@20554
   334
huffman@21809
   335
lemma Reals_of_int [simp]: "of_int z \<in> Reals"
huffman@21809
   336
by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
huffman@20718
   337
huffman@21809
   338
lemma Reals_of_nat [simp]: "of_nat n \<in> Reals"
huffman@21809
   339
by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
huffman@21809
   340
huffman@47108
   341
lemma Reals_numeral [simp]: "numeral w \<in> Reals"
huffman@47108
   342
by (subst of_real_numeral [symmetric], rule Reals_of_real)
huffman@47108
   343
huffman@20554
   344
lemma Reals_0 [simp]: "0 \<in> Reals"
huffman@20554
   345
apply (unfold Reals_def)
huffman@20554
   346
apply (rule range_eqI)
huffman@20554
   347
apply (rule of_real_0 [symmetric])
huffman@20554
   348
done
huffman@20554
   349
huffman@20554
   350
lemma Reals_1 [simp]: "1 \<in> Reals"
huffman@20554
   351
apply (unfold Reals_def)
huffman@20554
   352
apply (rule range_eqI)
huffman@20554
   353
apply (rule of_real_1 [symmetric])
huffman@20554
   354
done
huffman@20554
   355
huffman@20584
   356
lemma Reals_add [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a + b \<in> Reals"
huffman@20554
   357
apply (auto simp add: Reals_def)
huffman@20554
   358
apply (rule range_eqI)
huffman@20554
   359
apply (rule of_real_add [symmetric])
huffman@20554
   360
done
huffman@20554
   361
huffman@20584
   362
lemma Reals_minus [simp]: "a \<in> Reals \<Longrightarrow> - a \<in> Reals"
huffman@20584
   363
apply (auto simp add: Reals_def)
huffman@20584
   364
apply (rule range_eqI)
huffman@20584
   365
apply (rule of_real_minus [symmetric])
huffman@20584
   366
done
huffman@20584
   367
huffman@20584
   368
lemma Reals_diff [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a - b \<in> Reals"
huffman@20584
   369
apply (auto simp add: Reals_def)
huffman@20584
   370
apply (rule range_eqI)
huffman@20584
   371
apply (rule of_real_diff [symmetric])
huffman@20584
   372
done
huffman@20584
   373
huffman@20584
   374
lemma Reals_mult [simp]: "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a * b \<in> Reals"
huffman@20554
   375
apply (auto simp add: Reals_def)
huffman@20554
   376
apply (rule range_eqI)
huffman@20554
   377
apply (rule of_real_mult [symmetric])
huffman@20554
   378
done
huffman@20554
   379
huffman@20584
   380
lemma nonzero_Reals_inverse:
huffman@20584
   381
  fixes a :: "'a::real_div_algebra"
huffman@20584
   382
  shows "\<lbrakk>a \<in> Reals; a \<noteq> 0\<rbrakk> \<Longrightarrow> inverse a \<in> Reals"
huffman@20584
   383
apply (auto simp add: Reals_def)
huffman@20584
   384
apply (rule range_eqI)
huffman@20584
   385
apply (erule nonzero_of_real_inverse [symmetric])
huffman@20584
   386
done
huffman@20584
   387
lp15@55719
   388
lemma Reals_inverse:
haftmann@36409
   389
  fixes a :: "'a::{real_div_algebra, division_ring_inverse_zero}"
huffman@20584
   390
  shows "a \<in> Reals \<Longrightarrow> inverse a \<in> Reals"
huffman@20584
   391
apply (auto simp add: Reals_def)
huffman@20584
   392
apply (rule range_eqI)
huffman@20584
   393
apply (rule of_real_inverse [symmetric])
huffman@20584
   394
done
huffman@20584
   395
lp15@55719
   396
lemma Reals_inverse_iff [simp]: 
lp15@55719
   397
  fixes x:: "'a :: {real_div_algebra, division_ring_inverse_zero}"
lp15@55719
   398
  shows "inverse x \<in> \<real> \<longleftrightarrow> x \<in> \<real>"
lp15@55719
   399
by (metis Reals_inverse inverse_inverse_eq)
lp15@55719
   400
huffman@20584
   401
lemma nonzero_Reals_divide:
huffman@20584
   402
  fixes a b :: "'a::real_field"
huffman@20584
   403
  shows "\<lbrakk>a \<in> Reals; b \<in> Reals; b \<noteq> 0\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
huffman@20584
   404
apply (auto simp add: Reals_def)
huffman@20584
   405
apply (rule range_eqI)
huffman@20584
   406
apply (erule nonzero_of_real_divide [symmetric])
huffman@20584
   407
done
huffman@20584
   408
huffman@20584
   409
lemma Reals_divide [simp]:
haftmann@36409
   410
  fixes a b :: "'a::{real_field, field_inverse_zero}"
huffman@20584
   411
  shows "\<lbrakk>a \<in> Reals; b \<in> Reals\<rbrakk> \<Longrightarrow> a / b \<in> Reals"
huffman@20584
   412
apply (auto simp add: Reals_def)
huffman@20584
   413
apply (rule range_eqI)
huffman@20584
   414
apply (rule of_real_divide [symmetric])
huffman@20584
   415
done
huffman@20584
   416
huffman@20722
   417
lemma Reals_power [simp]:
haftmann@31017
   418
  fixes a :: "'a::{real_algebra_1}"
huffman@20722
   419
  shows "a \<in> Reals \<Longrightarrow> a ^ n \<in> Reals"
huffman@20722
   420
apply (auto simp add: Reals_def)
huffman@20722
   421
apply (rule range_eqI)
huffman@20722
   422
apply (rule of_real_power [symmetric])
huffman@20722
   423
done
huffman@20722
   424
huffman@20554
   425
lemma Reals_cases [cases set: Reals]:
huffman@20554
   426
  assumes "q \<in> \<real>"
huffman@20554
   427
  obtains (of_real) r where "q = of_real r"
huffman@20554
   428
  unfolding Reals_def
huffman@20554
   429
proof -
huffman@20554
   430
  from `q \<in> \<real>` have "q \<in> range of_real" unfolding Reals_def .
huffman@20554
   431
  then obtain r where "q = of_real r" ..
huffman@20554
   432
  then show thesis ..
huffman@20554
   433
qed
huffman@20554
   434
lp15@55719
   435
lemma setsum_in_Reals: assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setsum f s \<in> \<real>"
lp15@55719
   436
proof (cases "finite s")
lp15@55719
   437
  case True then show ?thesis using assms
lp15@55719
   438
    by (induct s rule: finite_induct) auto
lp15@55719
   439
next
lp15@55719
   440
  case False then show ?thesis using assms
lp15@55719
   441
    by (metis Reals_0 setsum_infinite)
lp15@55719
   442
qed
lp15@55719
   443
lp15@55719
   444
lemma setprod_in_Reals: assumes "\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>" shows "setprod f s \<in> \<real>"
lp15@55719
   445
proof (cases "finite s")
lp15@55719
   446
  case True then show ?thesis using assms
lp15@55719
   447
    by (induct s rule: finite_induct) auto
lp15@55719
   448
next
lp15@55719
   449
  case False then show ?thesis using assms
lp15@55719
   450
    by (metis Reals_1 setprod_infinite)
lp15@55719
   451
qed
lp15@55719
   452
huffman@20554
   453
lemma Reals_induct [case_names of_real, induct set: Reals]:
huffman@20554
   454
  "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
huffman@20554
   455
  by (rule Reals_cases) auto
huffman@20554
   456
immler@54778
   457
subsection {* Ordered real vector spaces *}
immler@54778
   458
immler@54778
   459
class ordered_real_vector = real_vector + ordered_ab_group_add +
immler@54778
   460
  assumes scaleR_left_mono: "x \<le> y \<Longrightarrow> 0 \<le> a \<Longrightarrow> a *\<^sub>R x \<le> a *\<^sub>R y"
immler@54778
   461
  assumes scaleR_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R x"
immler@54778
   462
begin
immler@54778
   463
immler@54778
   464
lemma scaleR_mono:
immler@54778
   465
  "a \<le> b \<Longrightarrow> x \<le> y \<Longrightarrow> 0 \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R y"
immler@54778
   466
apply (erule scaleR_right_mono [THEN order_trans], assumption)
immler@54778
   467
apply (erule scaleR_left_mono, assumption)
immler@54778
   468
done
immler@54778
   469
immler@54778
   470
lemma scaleR_mono':
immler@54778
   471
  "a \<le> b \<Longrightarrow> c \<le> d \<Longrightarrow> 0 \<le> a \<Longrightarrow> 0 \<le> c \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R d"
immler@54778
   472
  by (rule scaleR_mono) (auto intro: order.trans)
immler@54778
   473
immler@54785
   474
lemma pos_le_divideRI:
immler@54785
   475
  assumes "0 < c"
immler@54785
   476
  assumes "c *\<^sub>R a \<le> b"
immler@54785
   477
  shows "a \<le> b /\<^sub>R c"
immler@54785
   478
proof -
immler@54785
   479
  from scaleR_left_mono[OF assms(2)] assms(1)
immler@54785
   480
  have "c *\<^sub>R a /\<^sub>R c \<le> b /\<^sub>R c"
immler@54785
   481
    by simp
immler@54785
   482
  with assms show ?thesis
immler@54785
   483
    by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
immler@54785
   484
qed
immler@54785
   485
immler@54785
   486
lemma pos_le_divideR_eq:
immler@54785
   487
  assumes "0 < c"
immler@54785
   488
  shows "a \<le> b /\<^sub>R c \<longleftrightarrow> c *\<^sub>R a \<le> b"
immler@54785
   489
proof rule
immler@54785
   490
  assume "a \<le> b /\<^sub>R c"
immler@54785
   491
  from scaleR_left_mono[OF this] assms
immler@54785
   492
  have "c *\<^sub>R a \<le> c *\<^sub>R (b /\<^sub>R c)"
immler@54785
   493
    by simp
immler@54785
   494
  with assms show "c *\<^sub>R a \<le> b"
immler@54785
   495
    by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
immler@54785
   496
qed (rule pos_le_divideRI[OF assms])
immler@54785
   497
immler@54785
   498
lemma scaleR_image_atLeastAtMost:
immler@54785
   499
  "c > 0 \<Longrightarrow> scaleR c ` {x..y} = {c *\<^sub>R x..c *\<^sub>R y}"
immler@54785
   500
  apply (auto intro!: scaleR_left_mono)
immler@54785
   501
  apply (rule_tac x = "inverse c *\<^sub>R xa" in image_eqI)
immler@54785
   502
  apply (simp_all add: pos_le_divideR_eq[symmetric] scaleR_scaleR scaleR_one)
immler@54785
   503
  done
immler@54785
   504
immler@54778
   505
end
immler@54778
   506
immler@54778
   507
lemma scaleR_nonneg_nonneg: "0 \<le> a \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> 0 \<le> a *\<^sub>R x"
immler@54778
   508
  using scaleR_left_mono [of 0 x a]
immler@54778
   509
  by simp
immler@54778
   510
immler@54778
   511
lemma scaleR_nonneg_nonpos: "0 \<le> a \<Longrightarrow> (x::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> a *\<^sub>R x \<le> 0"
immler@54778
   512
  using scaleR_left_mono [of x 0 a] by simp
immler@54778
   513
immler@54778
   514
lemma scaleR_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> (x::'a::ordered_real_vector) \<Longrightarrow> a *\<^sub>R x \<le> 0"
immler@54778
   515
  using scaleR_right_mono [of a 0 x] by simp
immler@54778
   516
immler@54778
   517
lemma split_scaleR_neg_le: "(0 \<le> a & x \<le> 0) | (a \<le> 0 & 0 \<le> x) \<Longrightarrow>
immler@54778
   518
  a *\<^sub>R (x::'a::ordered_real_vector) \<le> 0"
immler@54778
   519
  by (auto simp add: scaleR_nonneg_nonpos scaleR_nonpos_nonneg)
immler@54778
   520
immler@54778
   521
lemma le_add_iff1:
immler@54778
   522
  fixes c d e::"'a::ordered_real_vector"
immler@54778
   523
  shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> (a - b) *\<^sub>R e + c \<le> d"
immler@54778
   524
  by (simp add: algebra_simps)
immler@54778
   525
immler@54778
   526
lemma le_add_iff2:
immler@54778
   527
  fixes c d e::"'a::ordered_real_vector"
immler@54778
   528
  shows "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> c \<le> (b - a) *\<^sub>R e + d"
immler@54778
   529
  by (simp add: algebra_simps)
immler@54778
   530
immler@54778
   531
lemma scaleR_left_mono_neg:
immler@54778
   532
  fixes a b::"'a::ordered_real_vector"
immler@54778
   533
  shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b"
immler@54778
   534
  apply (drule scaleR_left_mono [of _ _ "- c"])
immler@54778
   535
  apply simp_all
immler@54778
   536
  done
immler@54778
   537
immler@54778
   538
lemma scaleR_right_mono_neg:
immler@54778
   539
  fixes c::"'a::ordered_real_vector"
immler@54778
   540
  shows "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R c"
immler@54778
   541
  apply (drule scaleR_right_mono [of _ _ "- c"])
immler@54778
   542
  apply simp_all
immler@54778
   543
  done
immler@54778
   544
immler@54778
   545
lemma scaleR_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> (b::'a::ordered_real_vector) \<le> 0 \<Longrightarrow> 0 \<le> a *\<^sub>R b"
immler@54778
   546
using scaleR_right_mono_neg [of a 0 b] by simp
immler@54778
   547
immler@54778
   548
lemma split_scaleR_pos_le:
immler@54778
   549
  fixes b::"'a::ordered_real_vector"
immler@54778
   550
  shows "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a *\<^sub>R b"
immler@54778
   551
  by (auto simp add: scaleR_nonneg_nonneg scaleR_nonpos_nonpos)
immler@54778
   552
immler@54778
   553
lemma zero_le_scaleR_iff:
immler@54778
   554
  fixes b::"'a::ordered_real_vector"
immler@54778
   555
  shows "0 \<le> a *\<^sub>R b \<longleftrightarrow> 0 < a \<and> 0 \<le> b \<or> a < 0 \<and> b \<le> 0 \<or> a = 0" (is "?lhs = ?rhs")
immler@54778
   556
proof cases
immler@54778
   557
  assume "a \<noteq> 0"
immler@54778
   558
  show ?thesis
immler@54778
   559
  proof
immler@54778
   560
    assume lhs: ?lhs
immler@54778
   561
    {
immler@54778
   562
      assume "0 < a"
immler@54778
   563
      with lhs have "inverse a *\<^sub>R 0 \<le> inverse a *\<^sub>R (a *\<^sub>R b)"
immler@54778
   564
        by (intro scaleR_mono) auto
immler@54778
   565
      hence ?rhs using `0 < a`
immler@54778
   566
        by simp
immler@54778
   567
    } moreover {
immler@54778
   568
      assume "0 > a"
immler@54778
   569
      with lhs have "- inverse a *\<^sub>R 0 \<le> - inverse a *\<^sub>R (a *\<^sub>R b)"
immler@54778
   570
        by (intro scaleR_mono) auto
immler@54778
   571
      hence ?rhs using `0 > a`
immler@54778
   572
        by simp
immler@54778
   573
    } ultimately show ?rhs using `a \<noteq> 0` by arith
immler@54778
   574
  qed (auto simp: not_le `a \<noteq> 0` intro!: split_scaleR_pos_le)
immler@54778
   575
qed simp
immler@54778
   576
immler@54778
   577
lemma scaleR_le_0_iff:
immler@54778
   578
  fixes b::"'a::ordered_real_vector"
immler@54778
   579
  shows "a *\<^sub>R b \<le> 0 \<longleftrightarrow> 0 < a \<and> b \<le> 0 \<or> a < 0 \<and> 0 \<le> b \<or> a = 0"
immler@54778
   580
  by (insert zero_le_scaleR_iff [of "-a" b]) force
immler@54778
   581
immler@54778
   582
lemma scaleR_le_cancel_left:
immler@54778
   583
  fixes b::"'a::ordered_real_vector"
immler@54778
   584
  shows "c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
immler@54778
   585
  by (auto simp add: neq_iff scaleR_left_mono scaleR_left_mono_neg
immler@54778
   586
    dest: scaleR_left_mono[where a="inverse c"] scaleR_left_mono_neg[where c="inverse c"])
immler@54778
   587
immler@54778
   588
lemma scaleR_le_cancel_left_pos:
immler@54778
   589
  fixes b::"'a::ordered_real_vector"
immler@54778
   590
  shows "0 < c \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> a \<le> b"
immler@54778
   591
  by (auto simp: scaleR_le_cancel_left)
immler@54778
   592
immler@54778
   593
lemma scaleR_le_cancel_left_neg:
immler@54778
   594
  fixes b::"'a::ordered_real_vector"
immler@54778
   595
  shows "c < 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> b \<le> a"
immler@54778
   596
  by (auto simp: scaleR_le_cancel_left)
immler@54778
   597
immler@54778
   598
lemma scaleR_left_le_one_le:
immler@54778
   599
  fixes x::"'a::ordered_real_vector" and a::real
immler@54778
   600
  shows "0 \<le> x \<Longrightarrow> a \<le> 1 \<Longrightarrow> a *\<^sub>R x \<le> x"
immler@54778
   601
  using scaleR_right_mono[of a 1 x] by simp
immler@54778
   602
huffman@20504
   603
huffman@20504
   604
subsection {* Real normed vector spaces *}
huffman@20504
   605
hoelzl@51531
   606
class dist =
hoelzl@51531
   607
  fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
hoelzl@51531
   608
haftmann@29608
   609
class norm =
huffman@22636
   610
  fixes norm :: "'a \<Rightarrow> real"
huffman@20504
   611
huffman@24520
   612
class sgn_div_norm = scaleR + norm + sgn +
haftmann@25062
   613
  assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
nipkow@24506
   614
huffman@31289
   615
class dist_norm = dist + norm + minus +
huffman@31289
   616
  assumes dist_norm: "dist x y = norm (x - y)"
huffman@31289
   617
hoelzl@51531
   618
class open_dist = "open" + dist +
hoelzl@51531
   619
  assumes open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
hoelzl@51531
   620
huffman@31492
   621
class real_normed_vector = real_vector + sgn_div_norm + dist_norm + open_dist +
hoelzl@51002
   622
  assumes norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
haftmann@25062
   623
  and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
huffman@31586
   624
  and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
hoelzl@51002
   625
begin
hoelzl@51002
   626
hoelzl@51002
   627
lemma norm_ge_zero [simp]: "0 \<le> norm x"
hoelzl@51002
   628
proof -
hoelzl@51002
   629
  have "0 = norm (x + -1 *\<^sub>R x)" 
hoelzl@51002
   630
    using scaleR_add_left[of 1 "-1" x] norm_scaleR[of 0 x] by (simp add: scaleR_one)
hoelzl@51002
   631
  also have "\<dots> \<le> norm x + norm (-1 *\<^sub>R x)" by (rule norm_triangle_ineq)
hoelzl@51002
   632
  finally show ?thesis by simp
hoelzl@51002
   633
qed
hoelzl@51002
   634
hoelzl@51002
   635
end
huffman@20504
   636
haftmann@24588
   637
class real_normed_algebra = real_algebra + real_normed_vector +
haftmann@25062
   638
  assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
huffman@20504
   639
haftmann@24588
   640
class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
haftmann@25062
   641
  assumes norm_one [simp]: "norm 1 = 1"
huffman@22852
   642
haftmann@24588
   643
class real_normed_div_algebra = real_div_algebra + real_normed_vector +
haftmann@25062
   644
  assumes norm_mult: "norm (x * y) = norm x * norm y"
huffman@20504
   645
haftmann@24588
   646
class real_normed_field = real_field + real_normed_div_algebra
huffman@20584
   647
huffman@22852
   648
instance real_normed_div_algebra < real_normed_algebra_1
huffman@20554
   649
proof
huffman@20554
   650
  fix x y :: 'a
huffman@20554
   651
  show "norm (x * y) \<le> norm x * norm y"
huffman@20554
   652
    by (simp add: norm_mult)
huffman@22852
   653
next
huffman@22852
   654
  have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
huffman@22852
   655
    by (rule norm_mult)
huffman@22852
   656
  thus "norm (1::'a) = 1" by simp
huffman@20554
   657
qed
huffman@20554
   658
huffman@22852
   659
lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
huffman@20504
   660
by simp
huffman@20504
   661
huffman@22852
   662
lemma zero_less_norm_iff [simp]:
huffman@22852
   663
  fixes x :: "'a::real_normed_vector"
huffman@22852
   664
  shows "(0 < norm x) = (x \<noteq> 0)"
huffman@20504
   665
by (simp add: order_less_le)
huffman@20504
   666
huffman@22852
   667
lemma norm_not_less_zero [simp]:
huffman@22852
   668
  fixes x :: "'a::real_normed_vector"
huffman@22852
   669
  shows "\<not> norm x < 0"
huffman@20828
   670
by (simp add: linorder_not_less)
huffman@20828
   671
huffman@22852
   672
lemma norm_le_zero_iff [simp]:
huffman@22852
   673
  fixes x :: "'a::real_normed_vector"
huffman@22852
   674
  shows "(norm x \<le> 0) = (x = 0)"
huffman@20828
   675
by (simp add: order_le_less)
huffman@20828
   676
huffman@20504
   677
lemma norm_minus_cancel [simp]:
huffman@20584
   678
  fixes x :: "'a::real_normed_vector"
huffman@20584
   679
  shows "norm (- x) = norm x"
huffman@20504
   680
proof -
huffman@21809
   681
  have "norm (- x) = norm (scaleR (- 1) x)"
huffman@20504
   682
    by (simp only: scaleR_minus_left scaleR_one)
huffman@20533
   683
  also have "\<dots> = \<bar>- 1\<bar> * norm x"
huffman@20504
   684
    by (rule norm_scaleR)
huffman@20504
   685
  finally show ?thesis by simp
huffman@20504
   686
qed
huffman@20504
   687
huffman@20504
   688
lemma norm_minus_commute:
huffman@20584
   689
  fixes a b :: "'a::real_normed_vector"
huffman@20584
   690
  shows "norm (a - b) = norm (b - a)"
huffman@20504
   691
proof -
huffman@22898
   692
  have "norm (- (b - a)) = norm (b - a)"
huffman@22898
   693
    by (rule norm_minus_cancel)
huffman@22898
   694
  thus ?thesis by simp
huffman@20504
   695
qed
huffman@20504
   696
huffman@20504
   697
lemma norm_triangle_ineq2:
huffman@20584
   698
  fixes a b :: "'a::real_normed_vector"
huffman@20533
   699
  shows "norm a - norm b \<le> norm (a - b)"
huffman@20504
   700
proof -
huffman@20533
   701
  have "norm (a - b + b) \<le> norm (a - b) + norm b"
huffman@20504
   702
    by (rule norm_triangle_ineq)
huffman@22898
   703
  thus ?thesis by simp
huffman@20504
   704
qed
huffman@20504
   705
huffman@20584
   706
lemma norm_triangle_ineq3:
huffman@20584
   707
  fixes a b :: "'a::real_normed_vector"
huffman@20584
   708
  shows "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
huffman@20584
   709
apply (subst abs_le_iff)
huffman@20584
   710
apply auto
huffman@20584
   711
apply (rule norm_triangle_ineq2)
huffman@20584
   712
apply (subst norm_minus_commute)
huffman@20584
   713
apply (rule norm_triangle_ineq2)
huffman@20584
   714
done
huffman@20584
   715
huffman@20504
   716
lemma norm_triangle_ineq4:
huffman@20584
   717
  fixes a b :: "'a::real_normed_vector"
huffman@20533
   718
  shows "norm (a - b) \<le> norm a + norm b"
huffman@20504
   719
proof -
huffman@22898
   720
  have "norm (a + - b) \<le> norm a + norm (- b)"
huffman@20504
   721
    by (rule norm_triangle_ineq)
haftmann@54230
   722
  then show ?thesis by simp
huffman@22898
   723
qed
huffman@22898
   724
huffman@22898
   725
lemma norm_diff_ineq:
huffman@22898
   726
  fixes a b :: "'a::real_normed_vector"
huffman@22898
   727
  shows "norm a - norm b \<le> norm (a + b)"
huffman@22898
   728
proof -
huffman@22898
   729
  have "norm a - norm (- b) \<le> norm (a - - b)"
huffman@22898
   730
    by (rule norm_triangle_ineq2)
huffman@22898
   731
  thus ?thesis by simp
huffman@20504
   732
qed
huffman@20504
   733
huffman@20551
   734
lemma norm_diff_triangle_ineq:
huffman@20551
   735
  fixes a b c d :: "'a::real_normed_vector"
huffman@20551
   736
  shows "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
huffman@20551
   737
proof -
huffman@20551
   738
  have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
haftmann@54230
   739
    by (simp add: algebra_simps)
huffman@20551
   740
  also have "\<dots> \<le> norm (a - c) + norm (b - d)"
huffman@20551
   741
    by (rule norm_triangle_ineq)
huffman@20551
   742
  finally show ?thesis .
huffman@20551
   743
qed
huffman@20551
   744
lp15@55719
   745
lemma norm_triangle_mono: 
lp15@55719
   746
  fixes a b :: "'a::real_normed_vector"
lp15@55719
   747
  shows "\<lbrakk>norm a \<le> r; norm b \<le> s\<rbrakk> \<Longrightarrow> norm (a + b) \<le> r + s"
lp15@55719
   748
by (metis add_mono_thms_linordered_semiring(1) norm_triangle_ineq order.trans)
lp15@55719
   749
hoelzl@56194
   750
lemma norm_setsum:
hoelzl@56194
   751
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56194
   752
  shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
hoelzl@56194
   753
  by (induct A rule: infinite_finite_induct) (auto intro: norm_triangle_mono)
hoelzl@56194
   754
hoelzl@56369
   755
lemma setsum_norm_le:
hoelzl@56369
   756
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56369
   757
  assumes fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
hoelzl@56369
   758
  shows "norm (setsum f S) \<le> setsum g S"
hoelzl@56369
   759
  by (rule order_trans [OF norm_setsum setsum_mono]) (simp add: fg)
hoelzl@56369
   760
huffman@22857
   761
lemma abs_norm_cancel [simp]:
huffman@22857
   762
  fixes a :: "'a::real_normed_vector"
huffman@22857
   763
  shows "\<bar>norm a\<bar> = norm a"
huffman@22857
   764
by (rule abs_of_nonneg [OF norm_ge_zero])
huffman@22857
   765
huffman@22880
   766
lemma norm_add_less:
huffman@22880
   767
  fixes x y :: "'a::real_normed_vector"
huffman@22880
   768
  shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x + y) < r + s"
huffman@22880
   769
by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
huffman@22880
   770
huffman@22880
   771
lemma norm_mult_less:
huffman@22880
   772
  fixes x y :: "'a::real_normed_algebra"
huffman@22880
   773
  shows "\<lbrakk>norm x < r; norm y < s\<rbrakk> \<Longrightarrow> norm (x * y) < r * s"
huffman@22880
   774
apply (rule order_le_less_trans [OF norm_mult_ineq])
huffman@22880
   775
apply (simp add: mult_strict_mono')
huffman@22880
   776
done
huffman@22880
   777
huffman@22857
   778
lemma norm_of_real [simp]:
huffman@22857
   779
  "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
huffman@31586
   780
unfolding of_real_def by simp
huffman@20560
   781
huffman@47108
   782
lemma norm_numeral [simp]:
huffman@47108
   783
  "norm (numeral w::'a::real_normed_algebra_1) = numeral w"
huffman@47108
   784
by (subst of_real_numeral [symmetric], subst norm_of_real, simp)
huffman@47108
   785
huffman@47108
   786
lemma norm_neg_numeral [simp]:
haftmann@54489
   787
  "norm (- numeral w::'a::real_normed_algebra_1) = numeral w"
huffman@47108
   788
by (subst of_real_neg_numeral [symmetric], subst norm_of_real, simp)
huffman@22876
   789
huffman@22876
   790
lemma norm_of_int [simp]:
huffman@22876
   791
  "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
huffman@22876
   792
by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
huffman@22876
   793
huffman@22876
   794
lemma norm_of_nat [simp]:
huffman@22876
   795
  "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
huffman@22876
   796
apply (subst of_real_of_nat_eq [symmetric])
huffman@22876
   797
apply (subst norm_of_real, simp)
huffman@22876
   798
done
huffman@22876
   799
huffman@20504
   800
lemma nonzero_norm_inverse:
huffman@20504
   801
  fixes a :: "'a::real_normed_div_algebra"
huffman@20533
   802
  shows "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
huffman@20504
   803
apply (rule inverse_unique [symmetric])
huffman@20504
   804
apply (simp add: norm_mult [symmetric])
huffman@20504
   805
done
huffman@20504
   806
huffman@20504
   807
lemma norm_inverse:
haftmann@36409
   808
  fixes a :: "'a::{real_normed_div_algebra, division_ring_inverse_zero}"
huffman@20533
   809
  shows "norm (inverse a) = inverse (norm a)"
huffman@20504
   810
apply (case_tac "a = 0", simp)
huffman@20504
   811
apply (erule nonzero_norm_inverse)
huffman@20504
   812
done
huffman@20504
   813
huffman@20584
   814
lemma nonzero_norm_divide:
huffman@20584
   815
  fixes a b :: "'a::real_normed_field"
huffman@20584
   816
  shows "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
huffman@20584
   817
by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
huffman@20584
   818
huffman@20584
   819
lemma norm_divide:
haftmann@36409
   820
  fixes a b :: "'a::{real_normed_field, field_inverse_zero}"
huffman@20584
   821
  shows "norm (a / b) = norm a / norm b"
huffman@20584
   822
by (simp add: divide_inverse norm_mult norm_inverse)
huffman@20584
   823
huffman@22852
   824
lemma norm_power_ineq:
haftmann@31017
   825
  fixes x :: "'a::{real_normed_algebra_1}"
huffman@22852
   826
  shows "norm (x ^ n) \<le> norm x ^ n"
huffman@22852
   827
proof (induct n)
huffman@22852
   828
  case 0 show "norm (x ^ 0) \<le> norm x ^ 0" by simp
huffman@22852
   829
next
huffman@22852
   830
  case (Suc n)
huffman@22852
   831
  have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
huffman@22852
   832
    by (rule norm_mult_ineq)
huffman@22852
   833
  also from Suc have "\<dots> \<le> norm x * norm x ^ n"
huffman@22852
   834
    using norm_ge_zero by (rule mult_left_mono)
huffman@22852
   835
  finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
huffman@30273
   836
    by simp
huffman@22852
   837
qed
huffman@22852
   838
huffman@20684
   839
lemma norm_power:
haftmann@31017
   840
  fixes x :: "'a::{real_normed_div_algebra}"
huffman@20684
   841
  shows "norm (x ^ n) = norm x ^ n"
huffman@30273
   842
by (induct n) (simp_all add: norm_mult)
huffman@20684
   843
lp15@55719
   844
lemma setprod_norm:
lp15@55719
   845
  fixes f :: "'a \<Rightarrow> 'b::{comm_semiring_1,real_normed_div_algebra}"
lp15@55719
   846
  shows "setprod (\<lambda>x. norm(f x)) A = norm (setprod f A)"
lp15@55719
   847
proof (cases "finite A")
lp15@55719
   848
  case True then show ?thesis 
lp15@55719
   849
    by (induct A rule: finite_induct) (auto simp: norm_mult)
lp15@55719
   850
next
lp15@55719
   851
  case False then show ?thesis
lp15@55719
   852
    by (metis norm_one setprod.infinite) 
lp15@55719
   853
qed
lp15@55719
   854
hoelzl@51531
   855
hoelzl@51531
   856
subsection {* Metric spaces *}
hoelzl@51531
   857
hoelzl@51531
   858
class metric_space = open_dist +
hoelzl@51531
   859
  assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
hoelzl@51531
   860
  assumes dist_triangle2: "dist x y \<le> dist x z + dist y z"
hoelzl@51531
   861
begin
hoelzl@51531
   862
hoelzl@51531
   863
lemma dist_self [simp]: "dist x x = 0"
hoelzl@51531
   864
by simp
hoelzl@51531
   865
hoelzl@51531
   866
lemma zero_le_dist [simp]: "0 \<le> dist x y"
hoelzl@51531
   867
using dist_triangle2 [of x x y] by simp
hoelzl@51531
   868
hoelzl@51531
   869
lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
hoelzl@51531
   870
by (simp add: less_le)
hoelzl@51531
   871
hoelzl@51531
   872
lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
hoelzl@51531
   873
by (simp add: not_less)
hoelzl@51531
   874
hoelzl@51531
   875
lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
hoelzl@51531
   876
by (simp add: le_less)
hoelzl@51531
   877
hoelzl@51531
   878
lemma dist_commute: "dist x y = dist y x"
hoelzl@51531
   879
proof (rule order_antisym)
hoelzl@51531
   880
  show "dist x y \<le> dist y x"
hoelzl@51531
   881
    using dist_triangle2 [of x y x] by simp
hoelzl@51531
   882
  show "dist y x \<le> dist x y"
hoelzl@51531
   883
    using dist_triangle2 [of y x y] by simp
hoelzl@51531
   884
qed
hoelzl@51531
   885
hoelzl@51531
   886
lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
hoelzl@51531
   887
using dist_triangle2 [of x z y] by (simp add: dist_commute)
hoelzl@51531
   888
hoelzl@51531
   889
lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
hoelzl@51531
   890
using dist_triangle2 [of x y a] by (simp add: dist_commute)
hoelzl@51531
   891
hoelzl@51531
   892
lemma dist_triangle_alt:
hoelzl@51531
   893
  shows "dist y z <= dist x y + dist x z"
hoelzl@51531
   894
by (rule dist_triangle3)
hoelzl@51531
   895
hoelzl@51531
   896
lemma dist_pos_lt:
hoelzl@51531
   897
  shows "x \<noteq> y ==> 0 < dist x y"
hoelzl@51531
   898
by (simp add: zero_less_dist_iff)
hoelzl@51531
   899
hoelzl@51531
   900
lemma dist_nz:
hoelzl@51531
   901
  shows "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
hoelzl@51531
   902
by (simp add: zero_less_dist_iff)
hoelzl@51531
   903
hoelzl@51531
   904
lemma dist_triangle_le:
hoelzl@51531
   905
  shows "dist x z + dist y z <= e \<Longrightarrow> dist x y <= e"
hoelzl@51531
   906
by (rule order_trans [OF dist_triangle2])
hoelzl@51531
   907
hoelzl@51531
   908
lemma dist_triangle_lt:
hoelzl@51531
   909
  shows "dist x z + dist y z < e ==> dist x y < e"
hoelzl@51531
   910
by (rule le_less_trans [OF dist_triangle2])
hoelzl@51531
   911
hoelzl@51531
   912
lemma dist_triangle_half_l:
hoelzl@51531
   913
  shows "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
hoelzl@51531
   914
by (rule dist_triangle_lt [where z=y], simp)
hoelzl@51531
   915
hoelzl@51531
   916
lemma dist_triangle_half_r:
hoelzl@51531
   917
  shows "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
hoelzl@51531
   918
by (rule dist_triangle_half_l, simp_all add: dist_commute)
hoelzl@51531
   919
hoelzl@51531
   920
subclass topological_space
hoelzl@51531
   921
proof
hoelzl@51531
   922
  have "\<exists>e::real. 0 < e"
hoelzl@51531
   923
    by (fast intro: zero_less_one)
hoelzl@51531
   924
  then show "open UNIV"
hoelzl@51531
   925
    unfolding open_dist by simp
hoelzl@51531
   926
next
hoelzl@51531
   927
  fix S T assume "open S" "open T"
hoelzl@51531
   928
  then show "open (S \<inter> T)"
hoelzl@51531
   929
    unfolding open_dist
hoelzl@51531
   930
    apply clarify
hoelzl@51531
   931
    apply (drule (1) bspec)+
hoelzl@51531
   932
    apply (clarify, rename_tac r s)
hoelzl@51531
   933
    apply (rule_tac x="min r s" in exI, simp)
hoelzl@51531
   934
    done
hoelzl@51531
   935
next
hoelzl@51531
   936
  fix K assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
hoelzl@51531
   937
    unfolding open_dist by fast
hoelzl@51531
   938
qed
hoelzl@51531
   939
hoelzl@51531
   940
lemma open_ball: "open {y. dist x y < d}"
hoelzl@51531
   941
proof (unfold open_dist, intro ballI)
hoelzl@51531
   942
  fix y assume *: "y \<in> {y. dist x y < d}"
hoelzl@51531
   943
  then show "\<exists>e>0. \<forall>z. dist z y < e \<longrightarrow> z \<in> {y. dist x y < d}"
hoelzl@51531
   944
    by (auto intro!: exI[of _ "d - dist x y"] simp: field_simps dist_triangle_lt)
hoelzl@51531
   945
qed
hoelzl@51531
   946
hoelzl@51531
   947
subclass first_countable_topology
hoelzl@51531
   948
proof
hoelzl@51531
   949
  fix x 
hoelzl@51531
   950
  show "\<exists>A::nat \<Rightarrow> 'a set. (\<forall>i. x \<in> A i \<and> open (A i)) \<and> (\<forall>S. open S \<and> x \<in> S \<longrightarrow> (\<exists>i. A i \<subseteq> S))"
hoelzl@51531
   951
  proof (safe intro!: exI[of _ "\<lambda>n. {y. dist x y < inverse (Suc n)}"])
hoelzl@51531
   952
    fix S assume "open S" "x \<in> S"
wenzelm@53374
   953
    then obtain e where e: "0 < e" and "{y. dist x y < e} \<subseteq> S"
hoelzl@51531
   954
      by (auto simp: open_dist subset_eq dist_commute)
hoelzl@51531
   955
    moreover
wenzelm@53374
   956
    from e obtain i where "inverse (Suc i) < e"
hoelzl@51531
   957
      by (auto dest!: reals_Archimedean)
hoelzl@51531
   958
    then have "{y. dist x y < inverse (Suc i)} \<subseteq> {y. dist x y < e}"
hoelzl@51531
   959
      by auto
hoelzl@51531
   960
    ultimately show "\<exists>i. {y. dist x y < inverse (Suc i)} \<subseteq> S"
hoelzl@51531
   961
      by blast
hoelzl@51531
   962
  qed (auto intro: open_ball)
hoelzl@51531
   963
qed
hoelzl@51531
   964
hoelzl@51531
   965
end
hoelzl@51531
   966
hoelzl@51531
   967
instance metric_space \<subseteq> t2_space
hoelzl@51531
   968
proof
hoelzl@51531
   969
  fix x y :: "'a::metric_space"
hoelzl@51531
   970
  assume xy: "x \<noteq> y"
hoelzl@51531
   971
  let ?U = "{y'. dist x y' < dist x y / 2}"
hoelzl@51531
   972
  let ?V = "{x'. dist y x' < dist x y / 2}"
hoelzl@51531
   973
  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
hoelzl@51531
   974
               \<Longrightarrow> \<not>(d x y * 2 < d x z \<and> d z y * 2 < d x z)" by arith
hoelzl@51531
   975
  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
hoelzl@51531
   976
    using dist_pos_lt[OF xy] th0[of dist, OF dist_triangle dist_commute]
hoelzl@51531
   977
    using open_ball[of _ "dist x y / 2"] by auto
hoelzl@51531
   978
  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
hoelzl@51531
   979
    by blast
hoelzl@51531
   980
qed
hoelzl@51531
   981
huffman@31289
   982
text {* Every normed vector space is a metric space. *}
huffman@31285
   983
huffman@31289
   984
instance real_normed_vector < metric_space
huffman@31289
   985
proof
huffman@31289
   986
  fix x y :: 'a show "dist x y = 0 \<longleftrightarrow> x = y"
huffman@31289
   987
    unfolding dist_norm by simp
huffman@31289
   988
next
huffman@31289
   989
  fix x y z :: 'a show "dist x y \<le> dist x z + dist y z"
huffman@31289
   990
    unfolding dist_norm
huffman@31289
   991
    using norm_triangle_ineq4 [of "x - z" "y - z"] by simp
huffman@31289
   992
qed
huffman@31285
   993
huffman@31564
   994
subsection {* Class instances for real numbers *}
huffman@31564
   995
huffman@31564
   996
instantiation real :: real_normed_field
huffman@31564
   997
begin
huffman@31564
   998
hoelzl@51531
   999
definition dist_real_def:
hoelzl@51531
  1000
  "dist x y = \<bar>x - y\<bar>"
hoelzl@51531
  1001
haftmann@52381
  1002
definition open_real_def [code del]:
hoelzl@51531
  1003
  "open (S :: real set) \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
hoelzl@51531
  1004
huffman@31564
  1005
definition real_norm_def [simp]:
huffman@31564
  1006
  "norm r = \<bar>r\<bar>"
huffman@31564
  1007
huffman@31564
  1008
instance
huffman@31564
  1009
apply (intro_classes, unfold real_norm_def real_scaleR_def)
huffman@31564
  1010
apply (rule dist_real_def)
hoelzl@51531
  1011
apply (rule open_real_def)
huffman@36795
  1012
apply (simp add: sgn_real_def)
huffman@31564
  1013
apply (rule abs_eq_0)
huffman@31564
  1014
apply (rule abs_triangle_ineq)
huffman@31564
  1015
apply (rule abs_mult)
huffman@31564
  1016
apply (rule abs_mult)
huffman@31564
  1017
done
huffman@31564
  1018
huffman@31564
  1019
end
huffman@31564
  1020
haftmann@54890
  1021
declare [[code abort: "open :: real set \<Rightarrow> bool"]]
haftmann@52381
  1022
hoelzl@51531
  1023
instance real :: linorder_topology
hoelzl@51531
  1024
proof
hoelzl@51531
  1025
  show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
hoelzl@51531
  1026
  proof (rule ext, safe)
hoelzl@51531
  1027
    fix S :: "real set" assume "open S"
wenzelm@53381
  1028
    then obtain f where "\<forall>x\<in>S. 0 < f x \<and> (\<forall>y. dist y x < f x \<longrightarrow> y \<in> S)"
wenzelm@53381
  1029
      unfolding open_real_def bchoice_iff ..
hoelzl@51531
  1030
    then have *: "S = (\<Union>x\<in>S. {x - f x <..} \<inter> {..< x + f x})"
hoelzl@51531
  1031
      by (fastforce simp: dist_real_def)
hoelzl@51531
  1032
    show "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51531
  1033
      apply (subst *)
hoelzl@51531
  1034
      apply (intro generate_topology_Union generate_topology.Int)
hoelzl@51531
  1035
      apply (auto intro: generate_topology.Basis)
hoelzl@51531
  1036
      done
hoelzl@51531
  1037
  next
hoelzl@51531
  1038
    fix S :: "real set" assume "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51531
  1039
    moreover have "\<And>a::real. open {..<a}"
hoelzl@51531
  1040
      unfolding open_real_def dist_real_def
hoelzl@51531
  1041
    proof clarify
hoelzl@51531
  1042
      fix x a :: real assume "x < a"
hoelzl@51531
  1043
      hence "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
hoelzl@51531
  1044
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
hoelzl@51531
  1045
    qed
hoelzl@51531
  1046
    moreover have "\<And>a::real. open {a <..}"
hoelzl@51531
  1047
      unfolding open_real_def dist_real_def
hoelzl@51531
  1048
    proof clarify
hoelzl@51531
  1049
      fix x a :: real assume "a < x"
hoelzl@51531
  1050
      hence "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
hoelzl@51531
  1051
      thus "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
hoelzl@51531
  1052
    qed
hoelzl@51531
  1053
    ultimately show "open S"
hoelzl@51531
  1054
      by induct auto
hoelzl@51531
  1055
  qed
hoelzl@51531
  1056
qed
hoelzl@51531
  1057
hoelzl@51775
  1058
instance real :: linear_continuum_topology ..
hoelzl@51518
  1059
hoelzl@51531
  1060
lemmas open_real_greaterThan = open_greaterThan[where 'a=real]
hoelzl@51531
  1061
lemmas open_real_lessThan = open_lessThan[where 'a=real]
hoelzl@51531
  1062
lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]
hoelzl@51531
  1063
lemmas closed_real_atMost = closed_atMost[where 'a=real]
hoelzl@51531
  1064
lemmas closed_real_atLeast = closed_atLeast[where 'a=real]
hoelzl@51531
  1065
lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]
hoelzl@51531
  1066
huffman@31446
  1067
subsection {* Extra type constraints *}
huffman@31446
  1068
huffman@31492
  1069
text {* Only allow @{term "open"} in class @{text topological_space}. *}
huffman@31492
  1070
huffman@31492
  1071
setup {* Sign.add_const_constraint
huffman@31492
  1072
  (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"}) *}
huffman@31492
  1073
huffman@31446
  1074
text {* Only allow @{term dist} in class @{text metric_space}. *}
huffman@31446
  1075
huffman@31446
  1076
setup {* Sign.add_const_constraint
huffman@31446
  1077
  (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"}) *}
huffman@31446
  1078
huffman@31446
  1079
text {* Only allow @{term norm} in class @{text real_normed_vector}. *}
huffman@31446
  1080
huffman@31446
  1081
setup {* Sign.add_const_constraint
huffman@31446
  1082
  (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"}) *}
huffman@31446
  1083
huffman@22972
  1084
subsection {* Sign function *}
huffman@22972
  1085
nipkow@24506
  1086
lemma norm_sgn:
nipkow@24506
  1087
  "norm (sgn(x::'a::real_normed_vector)) = (if x = 0 then 0 else 1)"
huffman@31586
  1088
by (simp add: sgn_div_norm)
huffman@22972
  1089
nipkow@24506
  1090
lemma sgn_zero [simp]: "sgn(0::'a::real_normed_vector) = 0"
nipkow@24506
  1091
by (simp add: sgn_div_norm)
huffman@22972
  1092
nipkow@24506
  1093
lemma sgn_zero_iff: "(sgn(x::'a::real_normed_vector) = 0) = (x = 0)"
nipkow@24506
  1094
by (simp add: sgn_div_norm)
huffman@22972
  1095
nipkow@24506
  1096
lemma sgn_minus: "sgn (- x) = - sgn(x::'a::real_normed_vector)"
nipkow@24506
  1097
by (simp add: sgn_div_norm)
huffman@22972
  1098
nipkow@24506
  1099
lemma sgn_scaleR:
nipkow@24506
  1100
  "sgn (scaleR r x) = scaleR (sgn r) (sgn(x::'a::real_normed_vector))"
huffman@31586
  1101
by (simp add: sgn_div_norm mult_ac)
huffman@22973
  1102
huffman@22972
  1103
lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
nipkow@24506
  1104
by (simp add: sgn_div_norm)
huffman@22972
  1105
huffman@22972
  1106
lemma sgn_of_real:
huffman@22972
  1107
  "sgn (of_real r::'a::real_normed_algebra_1) = of_real (sgn r)"
huffman@22972
  1108
unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
huffman@22972
  1109
huffman@22973
  1110
lemma sgn_mult:
huffman@22973
  1111
  fixes x y :: "'a::real_normed_div_algebra"
huffman@22973
  1112
  shows "sgn (x * y) = sgn x * sgn y"
nipkow@24506
  1113
by (simp add: sgn_div_norm norm_mult mult_commute)
huffman@22973
  1114
huffman@22972
  1115
lemma real_sgn_eq: "sgn (x::real) = x / \<bar>x\<bar>"
nipkow@24506
  1116
by (simp add: sgn_div_norm divide_inverse)
huffman@22972
  1117
huffman@22972
  1118
lemma real_sgn_pos: "0 < (x::real) \<Longrightarrow> sgn x = 1"
lp15@56409
  1119
  by (rule sgn_pos)
huffman@22972
  1120
huffman@22972
  1121
lemma real_sgn_neg: "(x::real) < 0 \<Longrightarrow> sgn x = -1"
lp15@56409
  1122
  by (rule sgn_neg)
huffman@22972
  1123
hoelzl@51474
  1124
lemma norm_conv_dist: "norm x = dist x 0"
hoelzl@51474
  1125
  unfolding dist_norm by simp
huffman@22972
  1126
huffman@22442
  1127
subsection {* Bounded Linear and Bilinear Operators *}
huffman@22442
  1128
huffman@53600
  1129
locale linear = additive f for f :: "'a::real_vector \<Rightarrow> 'b::real_vector" +
huffman@22442
  1130
  assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
huffman@53600
  1131
huffman@53600
  1132
lemma linearI:
huffman@53600
  1133
  assumes "\<And>x y. f (x + y) = f x + f y"
huffman@53600
  1134
  assumes "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
huffman@53600
  1135
  shows "linear f"
huffman@53600
  1136
  by default (rule assms)+
huffman@53600
  1137
huffman@53600
  1138
locale bounded_linear = linear f for f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" +
huffman@22442
  1139
  assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
huffman@27443
  1140
begin
huffman@22442
  1141
huffman@27443
  1142
lemma pos_bounded:
huffman@22442
  1143
  "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
huffman@22442
  1144
proof -
huffman@22442
  1145
  obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
huffman@22442
  1146
    using bounded by fast
huffman@22442
  1147
  show ?thesis
huffman@22442
  1148
  proof (intro exI impI conjI allI)
huffman@22442
  1149
    show "0 < max 1 K"
haftmann@54863
  1150
      by (rule order_less_le_trans [OF zero_less_one max.cobounded1])
huffman@22442
  1151
  next
huffman@22442
  1152
    fix x
huffman@22442
  1153
    have "norm (f x) \<le> norm x * K" using K .
huffman@22442
  1154
    also have "\<dots> \<le> norm x * max 1 K"
haftmann@54863
  1155
      by (rule mult_left_mono [OF max.cobounded2 norm_ge_zero])
huffman@22442
  1156
    finally show "norm (f x) \<le> norm x * max 1 K" .
huffman@22442
  1157
  qed
huffman@22442
  1158
qed
huffman@22442
  1159
huffman@27443
  1160
lemma nonneg_bounded:
huffman@22442
  1161
  "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
huffman@22442
  1162
proof -
huffman@22442
  1163
  from pos_bounded
huffman@22442
  1164
  show ?thesis by (auto intro: order_less_imp_le)
huffman@22442
  1165
qed
huffman@22442
  1166
hoelzl@56369
  1167
lemma linear: "linear f" ..
hoelzl@56369
  1168
huffman@27443
  1169
end
huffman@27443
  1170
huffman@44127
  1171
lemma bounded_linear_intro:
huffman@44127
  1172
  assumes "\<And>x y. f (x + y) = f x + f y"
huffman@44127
  1173
  assumes "\<And>r x. f (scaleR r x) = scaleR r (f x)"
huffman@44127
  1174
  assumes "\<And>x. norm (f x) \<le> norm x * K"
huffman@44127
  1175
  shows "bounded_linear f"
huffman@44127
  1176
  by default (fast intro: assms)+
huffman@44127
  1177
huffman@22442
  1178
locale bounded_bilinear =
huffman@22442
  1179
  fixes prod :: "['a::real_normed_vector, 'b::real_normed_vector]
huffman@22442
  1180
                 \<Rightarrow> 'c::real_normed_vector"
huffman@22442
  1181
    (infixl "**" 70)
huffman@22442
  1182
  assumes add_left: "prod (a + a') b = prod a b + prod a' b"
huffman@22442
  1183
  assumes add_right: "prod a (b + b') = prod a b + prod a b'"
huffman@22442
  1184
  assumes scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
huffman@22442
  1185
  assumes scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
huffman@22442
  1186
  assumes bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
huffman@27443
  1187
begin
huffman@22442
  1188
huffman@27443
  1189
lemma pos_bounded:
huffman@22442
  1190
  "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
huffman@22442
  1191
apply (cut_tac bounded, erule exE)
huffman@22442
  1192
apply (rule_tac x="max 1 K" in exI, safe)
haftmann@54863
  1193
apply (rule order_less_le_trans [OF zero_less_one max.cobounded1])
huffman@22442
  1194
apply (drule spec, drule spec, erule order_trans)
haftmann@54863
  1195
apply (rule mult_left_mono [OF max.cobounded2])
huffman@22442
  1196
apply (intro mult_nonneg_nonneg norm_ge_zero)
huffman@22442
  1197
done
huffman@22442
  1198
huffman@27443
  1199
lemma nonneg_bounded:
huffman@22442
  1200
  "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
huffman@22442
  1201
proof -
huffman@22442
  1202
  from pos_bounded
huffman@22442
  1203
  show ?thesis by (auto intro: order_less_imp_le)
huffman@22442
  1204
qed
huffman@22442
  1205
huffman@27443
  1206
lemma additive_right: "additive (\<lambda>b. prod a b)"
huffman@22442
  1207
by (rule additive.intro, rule add_right)
huffman@22442
  1208
huffman@27443
  1209
lemma additive_left: "additive (\<lambda>a. prod a b)"
huffman@22442
  1210
by (rule additive.intro, rule add_left)
huffman@22442
  1211
huffman@27443
  1212
lemma zero_left: "prod 0 b = 0"
huffman@22442
  1213
by (rule additive.zero [OF additive_left])
huffman@22442
  1214
huffman@27443
  1215
lemma zero_right: "prod a 0 = 0"
huffman@22442
  1216
by (rule additive.zero [OF additive_right])
huffman@22442
  1217
huffman@27443
  1218
lemma minus_left: "prod (- a) b = - prod a b"
huffman@22442
  1219
by (rule additive.minus [OF additive_left])
huffman@22442
  1220
huffman@27443
  1221
lemma minus_right: "prod a (- b) = - prod a b"
huffman@22442
  1222
by (rule additive.minus [OF additive_right])
huffman@22442
  1223
huffman@27443
  1224
lemma diff_left:
huffman@22442
  1225
  "prod (a - a') b = prod a b - prod a' b"
huffman@22442
  1226
by (rule additive.diff [OF additive_left])
huffman@22442
  1227
huffman@27443
  1228
lemma diff_right:
huffman@22442
  1229
  "prod a (b - b') = prod a b - prod a b'"
huffman@22442
  1230
by (rule additive.diff [OF additive_right])
huffman@22442
  1231
huffman@27443
  1232
lemma bounded_linear_left:
huffman@22442
  1233
  "bounded_linear (\<lambda>a. a ** b)"
huffman@44127
  1234
apply (cut_tac bounded, safe)
huffman@44127
  1235
apply (rule_tac K="norm b * K" in bounded_linear_intro)
huffman@22442
  1236
apply (rule add_left)
huffman@22442
  1237
apply (rule scaleR_left)
huffman@22442
  1238
apply (simp add: mult_ac)
huffman@22442
  1239
done
huffman@22442
  1240
huffman@27443
  1241
lemma bounded_linear_right:
huffman@22442
  1242
  "bounded_linear (\<lambda>b. a ** b)"
huffman@44127
  1243
apply (cut_tac bounded, safe)
huffman@44127
  1244
apply (rule_tac K="norm a * K" in bounded_linear_intro)
huffman@22442
  1245
apply (rule add_right)
huffman@22442
  1246
apply (rule scaleR_right)
huffman@22442
  1247
apply (simp add: mult_ac)
huffman@22442
  1248
done
huffman@22442
  1249
huffman@27443
  1250
lemma prod_diff_prod:
huffman@22442
  1251
  "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
huffman@22442
  1252
by (simp add: diff_left diff_right)
huffman@22442
  1253
huffman@27443
  1254
end
huffman@27443
  1255
hoelzl@51642
  1256
lemma bounded_linear_ident[simp]: "bounded_linear (\<lambda>x. x)"
hoelzl@51642
  1257
  by default (auto intro!: exI[of _ 1])
hoelzl@51642
  1258
hoelzl@51642
  1259
lemma bounded_linear_zero[simp]: "bounded_linear (\<lambda>x. 0)"
hoelzl@51642
  1260
  by default (auto intro!: exI[of _ 1])
hoelzl@51642
  1261
hoelzl@51642
  1262
lemma bounded_linear_add:
hoelzl@51642
  1263
  assumes "bounded_linear f"
hoelzl@51642
  1264
  assumes "bounded_linear g"
hoelzl@51642
  1265
  shows "bounded_linear (\<lambda>x. f x + g x)"
hoelzl@51642
  1266
proof -
hoelzl@51642
  1267
  interpret f: bounded_linear f by fact
hoelzl@51642
  1268
  interpret g: bounded_linear g by fact
hoelzl@51642
  1269
  show ?thesis
hoelzl@51642
  1270
  proof
hoelzl@51642
  1271
    from f.bounded obtain Kf where Kf: "\<And>x. norm (f x) \<le> norm x * Kf" by blast
hoelzl@51642
  1272
    from g.bounded obtain Kg where Kg: "\<And>x. norm (g x) \<le> norm x * Kg" by blast
hoelzl@51642
  1273
    show "\<exists>K. \<forall>x. norm (f x + g x) \<le> norm x * K"
hoelzl@51642
  1274
      using add_mono[OF Kf Kg]
hoelzl@51642
  1275
      by (intro exI[of _ "Kf + Kg"]) (auto simp: field_simps intro: norm_triangle_ineq order_trans)
hoelzl@51642
  1276
  qed (simp_all add: f.add g.add f.scaleR g.scaleR scaleR_right_distrib)
hoelzl@51642
  1277
qed
hoelzl@51642
  1278
hoelzl@51642
  1279
lemma bounded_linear_minus:
hoelzl@51642
  1280
  assumes "bounded_linear f"
hoelzl@51642
  1281
  shows "bounded_linear (\<lambda>x. - f x)"
hoelzl@51642
  1282
proof -
hoelzl@51642
  1283
  interpret f: bounded_linear f by fact
hoelzl@51642
  1284
  show ?thesis apply (unfold_locales)
hoelzl@51642
  1285
    apply (simp add: f.add)
hoelzl@51642
  1286
    apply (simp add: f.scaleR)
hoelzl@51642
  1287
    apply (simp add: f.bounded)
hoelzl@51642
  1288
    done
hoelzl@51642
  1289
qed
hoelzl@51642
  1290
hoelzl@51642
  1291
lemma bounded_linear_compose:
hoelzl@51642
  1292
  assumes "bounded_linear f"
hoelzl@51642
  1293
  assumes "bounded_linear g"
hoelzl@51642
  1294
  shows "bounded_linear (\<lambda>x. f (g x))"
hoelzl@51642
  1295
proof -
hoelzl@51642
  1296
  interpret f: bounded_linear f by fact
hoelzl@51642
  1297
  interpret g: bounded_linear g by fact
hoelzl@51642
  1298
  show ?thesis proof (unfold_locales)
hoelzl@51642
  1299
    fix x y show "f (g (x + y)) = f (g x) + f (g y)"
hoelzl@51642
  1300
      by (simp only: f.add g.add)
hoelzl@51642
  1301
  next
hoelzl@51642
  1302
    fix r x show "f (g (scaleR r x)) = scaleR r (f (g x))"
hoelzl@51642
  1303
      by (simp only: f.scaleR g.scaleR)
hoelzl@51642
  1304
  next
hoelzl@51642
  1305
    from f.pos_bounded
hoelzl@51642
  1306
    obtain Kf where f: "\<And>x. norm (f x) \<le> norm x * Kf" and Kf: "0 < Kf" by fast
hoelzl@51642
  1307
    from g.pos_bounded
hoelzl@51642
  1308
    obtain Kg where g: "\<And>x. norm (g x) \<le> norm x * Kg" by fast
hoelzl@51642
  1309
    show "\<exists>K. \<forall>x. norm (f (g x)) \<le> norm x * K"
hoelzl@51642
  1310
    proof (intro exI allI)
hoelzl@51642
  1311
      fix x
hoelzl@51642
  1312
      have "norm (f (g x)) \<le> norm (g x) * Kf"
hoelzl@51642
  1313
        using f .
hoelzl@51642
  1314
      also have "\<dots> \<le> (norm x * Kg) * Kf"
hoelzl@51642
  1315
        using g Kf [THEN order_less_imp_le] by (rule mult_right_mono)
hoelzl@51642
  1316
      also have "(norm x * Kg) * Kf = norm x * (Kg * Kf)"
hoelzl@51642
  1317
        by (rule mult_assoc)
hoelzl@51642
  1318
      finally show "norm (f (g x)) \<le> norm x * (Kg * Kf)" .
hoelzl@51642
  1319
    qed
hoelzl@51642
  1320
  qed
hoelzl@51642
  1321
qed
hoelzl@51642
  1322
huffman@44282
  1323
lemma bounded_bilinear_mult:
huffman@44282
  1324
  "bounded_bilinear (op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra)"
huffman@22442
  1325
apply (rule bounded_bilinear.intro)
webertj@49962
  1326
apply (rule distrib_right)
webertj@49962
  1327
apply (rule distrib_left)
huffman@22442
  1328
apply (rule mult_scaleR_left)
huffman@22442
  1329
apply (rule mult_scaleR_right)
huffman@22442
  1330
apply (rule_tac x="1" in exI)
huffman@22442
  1331
apply (simp add: norm_mult_ineq)
huffman@22442
  1332
done
huffman@22442
  1333
huffman@44282
  1334
lemma bounded_linear_mult_left:
huffman@44282
  1335
  "bounded_linear (\<lambda>x::'a::real_normed_algebra. x * y)"
huffman@44282
  1336
  using bounded_bilinear_mult
huffman@44282
  1337
  by (rule bounded_bilinear.bounded_linear_left)
huffman@22442
  1338
huffman@44282
  1339
lemma bounded_linear_mult_right:
huffman@44282
  1340
  "bounded_linear (\<lambda>y::'a::real_normed_algebra. x * y)"
huffman@44282
  1341
  using bounded_bilinear_mult
huffman@44282
  1342
  by (rule bounded_bilinear.bounded_linear_right)
huffman@23127
  1343
hoelzl@51642
  1344
lemmas bounded_linear_mult_const =
hoelzl@51642
  1345
  bounded_linear_mult_left [THEN bounded_linear_compose]
hoelzl@51642
  1346
hoelzl@51642
  1347
lemmas bounded_linear_const_mult =
hoelzl@51642
  1348
  bounded_linear_mult_right [THEN bounded_linear_compose]
hoelzl@51642
  1349
huffman@44282
  1350
lemma bounded_linear_divide:
huffman@44282
  1351
  "bounded_linear (\<lambda>x::'a::real_normed_field. x / y)"
huffman@44282
  1352
  unfolding divide_inverse by (rule bounded_linear_mult_left)
huffman@23120
  1353
huffman@44282
  1354
lemma bounded_bilinear_scaleR: "bounded_bilinear scaleR"
huffman@22442
  1355
apply (rule bounded_bilinear.intro)
huffman@22442
  1356
apply (rule scaleR_left_distrib)
huffman@22442
  1357
apply (rule scaleR_right_distrib)
huffman@22973
  1358
apply simp
huffman@22442
  1359
apply (rule scaleR_left_commute)
huffman@31586
  1360
apply (rule_tac x="1" in exI, simp)
huffman@22442
  1361
done
huffman@22442
  1362
huffman@44282
  1363
lemma bounded_linear_scaleR_left: "bounded_linear (\<lambda>r. scaleR r x)"
huffman@44282
  1364
  using bounded_bilinear_scaleR
huffman@44282
  1365
  by (rule bounded_bilinear.bounded_linear_left)
huffman@23127
  1366
huffman@44282
  1367
lemma bounded_linear_scaleR_right: "bounded_linear (\<lambda>x. scaleR r x)"
huffman@44282
  1368
  using bounded_bilinear_scaleR
huffman@44282
  1369
  by (rule bounded_bilinear.bounded_linear_right)
huffman@23127
  1370
huffman@44282
  1371
lemma bounded_linear_of_real: "bounded_linear (\<lambda>r. of_real r)"
huffman@44282
  1372
  unfolding of_real_def by (rule bounded_linear_scaleR_left)
huffman@22625
  1373
hoelzl@51642
  1374
lemma real_bounded_linear:
hoelzl@51642
  1375
  fixes f :: "real \<Rightarrow> real"
hoelzl@51642
  1376
  shows "bounded_linear f \<longleftrightarrow> (\<exists>c::real. f = (\<lambda>x. x * c))"
hoelzl@51642
  1377
proof -
hoelzl@51642
  1378
  { fix x assume "bounded_linear f"
hoelzl@51642
  1379
    then interpret bounded_linear f .
hoelzl@51642
  1380
    from scaleR[of x 1] have "f x = x * f 1"
hoelzl@51642
  1381
      by simp }
hoelzl@51642
  1382
  then show ?thesis
hoelzl@51642
  1383
    by (auto intro: exI[of _ "f 1"] bounded_linear_mult_left)
hoelzl@51642
  1384
qed
hoelzl@51642
  1385
huffman@44571
  1386
instance real_normed_algebra_1 \<subseteq> perfect_space
huffman@44571
  1387
proof
huffman@44571
  1388
  fix x::'a
huffman@44571
  1389
  show "\<not> open {x}"
huffman@44571
  1390
    unfolding open_dist dist_norm
huffman@44571
  1391
    by (clarsimp, rule_tac x="x + of_real (e/2)" in exI, simp)
huffman@44571
  1392
qed
huffman@44571
  1393
hoelzl@51531
  1394
subsection {* Filters and Limits on Metric Space *}
hoelzl@51531
  1395
hoelzl@51531
  1396
lemma eventually_nhds_metric:
hoelzl@51531
  1397
  fixes a :: "'a :: metric_space"
hoelzl@51531
  1398
  shows "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
hoelzl@51531
  1399
unfolding eventually_nhds open_dist
hoelzl@51531
  1400
apply safe
hoelzl@51531
  1401
apply fast
hoelzl@51531
  1402
apply (rule_tac x="{x. dist x a < d}" in exI, simp)
hoelzl@51531
  1403
apply clarsimp
hoelzl@51531
  1404
apply (rule_tac x="d - dist x a" in exI, clarsimp)
hoelzl@51531
  1405
apply (simp only: less_diff_eq)
hoelzl@51531
  1406
apply (erule le_less_trans [OF dist_triangle])
hoelzl@51531
  1407
done
hoelzl@51531
  1408
hoelzl@51531
  1409
lemma eventually_at:
hoelzl@51641
  1410
  fixes a :: "'a :: metric_space"
hoelzl@51641
  1411
  shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<and> dist x a < d \<longrightarrow> P x)"
hoelzl@51641
  1412
  unfolding eventually_at_filter eventually_nhds_metric by (auto simp: dist_nz)
hoelzl@51531
  1413
hoelzl@51641
  1414
lemma eventually_at_le:
hoelzl@51641
  1415
  fixes a :: "'a::metric_space"
hoelzl@51641
  1416
  shows "eventually P (at a within S) \<longleftrightarrow> (\<exists>d>0. \<forall>x\<in>S. x \<noteq> a \<and> dist x a \<le> d \<longrightarrow> P x)"
hoelzl@51641
  1417
  unfolding eventually_at_filter eventually_nhds_metric
hoelzl@51641
  1418
  apply auto
hoelzl@51641
  1419
  apply (rule_tac x="d / 2" in exI)
hoelzl@51641
  1420
  apply auto
hoelzl@51641
  1421
  done
hoelzl@51531
  1422
hoelzl@51531
  1423
lemma tendstoI:
hoelzl@51531
  1424
  fixes l :: "'a :: metric_space"
hoelzl@51531
  1425
  assumes "\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@51531
  1426
  shows "(f ---> l) F"
hoelzl@51531
  1427
  apply (rule topological_tendstoI)
hoelzl@51531
  1428
  apply (simp add: open_dist)
hoelzl@51531
  1429
  apply (drule (1) bspec, clarify)
hoelzl@51531
  1430
  apply (drule assms)
hoelzl@51531
  1431
  apply (erule eventually_elim1, simp)
hoelzl@51531
  1432
  done
hoelzl@51531
  1433
hoelzl@51531
  1434
lemma tendstoD:
hoelzl@51531
  1435
  fixes l :: "'a :: metric_space"
hoelzl@51531
  1436
  shows "(f ---> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@51531
  1437
  apply (drule_tac S="{x. dist x l < e}" in topological_tendstoD)
hoelzl@51531
  1438
  apply (clarsimp simp add: open_dist)
hoelzl@51531
  1439
  apply (rule_tac x="e - dist x l" in exI, clarsimp)
hoelzl@51531
  1440
  apply (simp only: less_diff_eq)
hoelzl@51531
  1441
  apply (erule le_less_trans [OF dist_triangle])
hoelzl@51531
  1442
  apply simp
hoelzl@51531
  1443
  apply simp
hoelzl@51531
  1444
  done
hoelzl@51531
  1445
hoelzl@51531
  1446
lemma tendsto_iff:
hoelzl@51531
  1447
  fixes l :: "'a :: metric_space"
hoelzl@51531
  1448
  shows "(f ---> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
hoelzl@51531
  1449
  using tendstoI tendstoD by fast
hoelzl@51531
  1450
hoelzl@51531
  1451
lemma metric_tendsto_imp_tendsto:
hoelzl@51531
  1452
  fixes a :: "'a :: metric_space" and b :: "'b :: metric_space"
hoelzl@51531
  1453
  assumes f: "(f ---> a) F"
hoelzl@51531
  1454
  assumes le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
hoelzl@51531
  1455
  shows "(g ---> b) F"
hoelzl@51531
  1456
proof (rule tendstoI)
hoelzl@51531
  1457
  fix e :: real assume "0 < e"
hoelzl@51531
  1458
  with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
hoelzl@51531
  1459
  with le show "eventually (\<lambda>x. dist (g x) b < e) F"
hoelzl@51531
  1460
    using le_less_trans by (rule eventually_elim2)
hoelzl@51531
  1461
qed
hoelzl@51531
  1462
hoelzl@51531
  1463
lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
hoelzl@51531
  1464
  unfolding filterlim_at_top
hoelzl@51531
  1465
  apply (intro allI)
hoelzl@51531
  1466
  apply (rule_tac c="natceiling (Z + 1)" in eventually_sequentiallyI)
hoelzl@51531
  1467
  apply (auto simp: natceiling_le_eq)
hoelzl@51531
  1468
  done
hoelzl@51531
  1469
hoelzl@51531
  1470
subsubsection {* Limits of Sequences *}
hoelzl@51531
  1471
hoelzl@51531
  1472
lemma LIMSEQ_def: "X ----> (L::'a::metric_space) \<longleftrightarrow> (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
hoelzl@51531
  1473
  unfolding tendsto_iff eventually_sequentially ..
hoelzl@51531
  1474
hoelzl@51531
  1475
lemma LIMSEQ_iff_nz: "X ----> (L::'a::metric_space) = (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
hoelzl@51531
  1476
  unfolding LIMSEQ_def by (metis Suc_leD zero_less_Suc)
hoelzl@51531
  1477
hoelzl@51531
  1478
lemma metric_LIMSEQ_I:
hoelzl@51531
  1479
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X ----> (L::'a::metric_space)"
hoelzl@51531
  1480
by (simp add: LIMSEQ_def)
hoelzl@51531
  1481
hoelzl@51531
  1482
lemma metric_LIMSEQ_D:
hoelzl@51531
  1483
  "\<lbrakk>X ----> (L::'a::metric_space); 0 < r\<rbrakk> \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
hoelzl@51531
  1484
by (simp add: LIMSEQ_def)
hoelzl@51531
  1485
hoelzl@51531
  1486
hoelzl@51531
  1487
subsubsection {* Limits of Functions *}
hoelzl@51531
  1488
hoelzl@51531
  1489
lemma LIM_def: "f -- (a::'a::metric_space) --> (L::'b::metric_space) =
hoelzl@51531
  1490
     (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a & dist x a < s
hoelzl@51531
  1491
        --> dist (f x) L < r)"
hoelzl@51641
  1492
  unfolding tendsto_iff eventually_at by simp
hoelzl@51531
  1493
hoelzl@51531
  1494
lemma metric_LIM_I:
hoelzl@51531
  1495
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r)
hoelzl@51531
  1496
    \<Longrightarrow> f -- (a::'a::metric_space) --> (L::'b::metric_space)"
hoelzl@51531
  1497
by (simp add: LIM_def)
hoelzl@51531
  1498
hoelzl@51531
  1499
lemma metric_LIM_D:
hoelzl@51531
  1500
  "\<lbrakk>f -- (a::'a::metric_space) --> (L::'b::metric_space); 0 < r\<rbrakk>
hoelzl@51531
  1501
    \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
hoelzl@51531
  1502
by (simp add: LIM_def)
hoelzl@51531
  1503
hoelzl@51531
  1504
lemma metric_LIM_imp_LIM:
hoelzl@51531
  1505
  assumes f: "f -- a --> (l::'a::metric_space)"
hoelzl@51531
  1506
  assumes le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
hoelzl@51531
  1507
  shows "g -- a --> (m::'b::metric_space)"
hoelzl@51531
  1508
  by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
hoelzl@51531
  1509
hoelzl@51531
  1510
lemma metric_LIM_equal2:
hoelzl@51531
  1511
  assumes 1: "0 < R"
hoelzl@51531
  1512
  assumes 2: "\<And>x. \<lbrakk>x \<noteq> a; dist x a < R\<rbrakk> \<Longrightarrow> f x = g x"
hoelzl@51531
  1513
  shows "g -- a --> l \<Longrightarrow> f -- (a::'a::metric_space) --> l"
hoelzl@51531
  1514
apply (rule topological_tendstoI)
hoelzl@51531
  1515
apply (drule (2) topological_tendstoD)
hoelzl@51531
  1516
apply (simp add: eventually_at, safe)
hoelzl@51531
  1517
apply (rule_tac x="min d R" in exI, safe)
hoelzl@51531
  1518
apply (simp add: 1)
hoelzl@51531
  1519
apply (simp add: 2)
hoelzl@51531
  1520
done
hoelzl@51531
  1521
hoelzl@51531
  1522
lemma metric_LIM_compose2:
hoelzl@51531
  1523
  assumes f: "f -- (a::'a::metric_space) --> b"
hoelzl@51531
  1524
  assumes g: "g -- b --> c"
hoelzl@51531
  1525
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
hoelzl@51531
  1526
  shows "(\<lambda>x. g (f x)) -- a --> c"
hoelzl@51641
  1527
  using inj
hoelzl@51641
  1528
  by (intro tendsto_compose_eventually[OF g f]) (auto simp: eventually_at)
hoelzl@51531
  1529
hoelzl@51531
  1530
lemma metric_isCont_LIM_compose2:
hoelzl@51531
  1531
  fixes f :: "'a :: metric_space \<Rightarrow> _"
hoelzl@51531
  1532
  assumes f [unfolded isCont_def]: "isCont f a"
hoelzl@51531
  1533
  assumes g: "g -- f a --> l"
hoelzl@51531
  1534
  assumes inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
hoelzl@51531
  1535
  shows "(\<lambda>x. g (f x)) -- a --> l"
hoelzl@51531
  1536
by (rule metric_LIM_compose2 [OF f g inj])
hoelzl@51531
  1537
hoelzl@51531
  1538
subsection {* Complete metric spaces *}
hoelzl@51531
  1539
hoelzl@51531
  1540
subsection {* Cauchy sequences *}
hoelzl@51531
  1541
hoelzl@51531
  1542
definition (in metric_space) Cauchy :: "(nat \<Rightarrow> 'a) \<Rightarrow> bool" where
hoelzl@51531
  1543
  "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < e)"
hoelzl@51531
  1544
hoelzl@51531
  1545
subsection {* Cauchy Sequences *}
hoelzl@51531
  1546
hoelzl@51531
  1547
lemma metric_CauchyI:
hoelzl@51531
  1548
  "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
hoelzl@51531
  1549
  by (simp add: Cauchy_def)
hoelzl@51531
  1550
hoelzl@51531
  1551
lemma metric_CauchyD:
hoelzl@51531
  1552
  "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
hoelzl@51531
  1553
  by (simp add: Cauchy_def)
hoelzl@51531
  1554
hoelzl@51531
  1555
lemma metric_Cauchy_iff2:
hoelzl@51531
  1556
  "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"
hoelzl@51531
  1557
apply (simp add: Cauchy_def, auto)
hoelzl@51531
  1558
apply (drule reals_Archimedean, safe)
hoelzl@51531
  1559
apply (drule_tac x = n in spec, auto)
hoelzl@51531
  1560
apply (rule_tac x = M in exI, auto)
hoelzl@51531
  1561
apply (drule_tac x = m in spec, simp)
hoelzl@51531
  1562
apply (drule_tac x = na in spec, auto)
hoelzl@51531
  1563
done
hoelzl@51531
  1564
hoelzl@51531
  1565
lemma Cauchy_iff2:
hoelzl@51531
  1566
  "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse(real (Suc j))))"
hoelzl@51531
  1567
  unfolding metric_Cauchy_iff2 dist_real_def ..
hoelzl@51531
  1568
hoelzl@51531
  1569
lemma Cauchy_subseq_Cauchy:
hoelzl@51531
  1570
  "\<lbrakk> Cauchy X; subseq f \<rbrakk> \<Longrightarrow> Cauchy (X o f)"
hoelzl@51531
  1571
apply (auto simp add: Cauchy_def)
hoelzl@51531
  1572
apply (drule_tac x=e in spec, clarify)
hoelzl@51531
  1573
apply (rule_tac x=M in exI, clarify)
hoelzl@51531
  1574
apply (blast intro: le_trans [OF _ seq_suble] dest!: spec)
hoelzl@51531
  1575
done
hoelzl@51531
  1576
hoelzl@51531
  1577
theorem LIMSEQ_imp_Cauchy:
hoelzl@51531
  1578
  assumes X: "X ----> a" shows "Cauchy X"
hoelzl@51531
  1579
proof (rule metric_CauchyI)
hoelzl@51531
  1580
  fix e::real assume "0 < e"
hoelzl@51531
  1581
  hence "0 < e/2" by simp
hoelzl@51531
  1582
  with X have "\<exists>N. \<forall>n\<ge>N. dist (X n) a < e/2" by (rule metric_LIMSEQ_D)
hoelzl@51531
  1583
  then obtain N where N: "\<forall>n\<ge>N. dist (X n) a < e/2" ..
hoelzl@51531
  1584
  show "\<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < e"
hoelzl@51531
  1585
  proof (intro exI allI impI)
hoelzl@51531
  1586
    fix m assume "N \<le> m"
hoelzl@51531
  1587
    hence m: "dist (X m) a < e/2" using N by fast
hoelzl@51531
  1588
    fix n assume "N \<le> n"
hoelzl@51531
  1589
    hence n: "dist (X n) a < e/2" using N by fast
hoelzl@51531
  1590
    have "dist (X m) (X n) \<le> dist (X m) a + dist (X n) a"
hoelzl@51531
  1591
      by (rule dist_triangle2)
hoelzl@51531
  1592
    also from m n have "\<dots> < e" by simp
hoelzl@51531
  1593
    finally show "dist (X m) (X n) < e" .
hoelzl@51531
  1594
  qed
hoelzl@51531
  1595
qed
hoelzl@51531
  1596
hoelzl@51531
  1597
lemma convergent_Cauchy: "convergent X \<Longrightarrow> Cauchy X"
hoelzl@51531
  1598
unfolding convergent_def
hoelzl@51531
  1599
by (erule exE, erule LIMSEQ_imp_Cauchy)
hoelzl@51531
  1600
hoelzl@51531
  1601
subsubsection {* Cauchy Sequences are Convergent *}
hoelzl@51531
  1602
hoelzl@51531
  1603
class complete_space = metric_space +
hoelzl@51531
  1604
  assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
hoelzl@51531
  1605
hoelzl@51531
  1606
lemma Cauchy_convergent_iff:
hoelzl@51531
  1607
  fixes X :: "nat \<Rightarrow> 'a::complete_space"
hoelzl@51531
  1608
  shows "Cauchy X = convergent X"
hoelzl@51531
  1609
by (fast intro: Cauchy_convergent convergent_Cauchy)
hoelzl@51531
  1610
hoelzl@51531
  1611
subsection {* The set of real numbers is a complete metric space *}
hoelzl@51531
  1612
hoelzl@51531
  1613
text {*
hoelzl@51531
  1614
Proof that Cauchy sequences converge based on the one from
wenzelm@54703
  1615
@{url "http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html"}
hoelzl@51531
  1616
*}
hoelzl@51531
  1617
hoelzl@51531
  1618
text {*
hoelzl@51531
  1619
  If sequence @{term "X"} is Cauchy, then its limit is the lub of
hoelzl@51531
  1620
  @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
hoelzl@51531
  1621
*}
hoelzl@51531
  1622
hoelzl@51531
  1623
lemma increasing_LIMSEQ:
hoelzl@51531
  1624
  fixes f :: "nat \<Rightarrow> real"
hoelzl@51531
  1625
  assumes inc: "\<And>n. f n \<le> f (Suc n)"
hoelzl@51531
  1626
      and bdd: "\<And>n. f n \<le> l"
hoelzl@51531
  1627
      and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
hoelzl@51531
  1628
  shows "f ----> l"
hoelzl@51531
  1629
proof (rule increasing_tendsto)
hoelzl@51531
  1630
  fix x assume "x < l"
hoelzl@51531
  1631
  with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
hoelzl@51531
  1632
    by auto
hoelzl@51531
  1633
  from en[OF `0 < e`] obtain n where "l - e \<le> f n"
hoelzl@51531
  1634
    by (auto simp: field_simps)
hoelzl@51531
  1635
  with `e < l - x` `0 < e` have "x < f n" by simp
hoelzl@51531
  1636
  with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
hoelzl@51531
  1637
    by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
hoelzl@51531
  1638
qed (insert bdd, auto)
hoelzl@51531
  1639
hoelzl@51531
  1640
lemma real_Cauchy_convergent:
hoelzl@51531
  1641
  fixes X :: "nat \<Rightarrow> real"
hoelzl@51531
  1642
  assumes X: "Cauchy X"
hoelzl@51531
  1643
  shows "convergent X"
hoelzl@51531
  1644
proof -
hoelzl@51531
  1645
  def S \<equiv> "{x::real. \<exists>N. \<forall>n\<ge>N. x < X n}"
hoelzl@51531
  1646
  then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S" by auto
hoelzl@51531
  1647
hoelzl@51531
  1648
  { fix N x assume N: "\<forall>n\<ge>N. X n < x"
hoelzl@51531
  1649
  fix y::real assume "y \<in> S"
hoelzl@51531
  1650
  hence "\<exists>M. \<forall>n\<ge>M. y < X n"
hoelzl@51531
  1651
    by (simp add: S_def)
hoelzl@51531
  1652
  then obtain M where "\<forall>n\<ge>M. y < X n" ..
hoelzl@51531
  1653
  hence "y < X (max M N)" by simp
hoelzl@51531
  1654
  also have "\<dots> < x" using N by simp
hoelzl@54263
  1655
  finally have "y \<le> x"
hoelzl@54263
  1656
    by (rule order_less_imp_le) }
hoelzl@51531
  1657
  note bound_isUb = this 
hoelzl@51531
  1658
hoelzl@51531
  1659
  obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
hoelzl@51531
  1660
    using X[THEN metric_CauchyD, OF zero_less_one] by auto
hoelzl@51531
  1661
  hence N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
hoelzl@54263
  1662
  have [simp]: "S \<noteq> {}"
hoelzl@54263
  1663
  proof (intro exI ex_in_conv[THEN iffD1])
hoelzl@51531
  1664
    from N have "\<forall>n\<ge>N. X N - 1 < X n"
hoelzl@51531
  1665
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51531
  1666
    thus "X N - 1 \<in> S" by (rule mem_S)
hoelzl@51531
  1667
  qed
hoelzl@54263
  1668
  have [simp]: "bdd_above S"
hoelzl@51531
  1669
  proof
hoelzl@51531
  1670
    from N have "\<forall>n\<ge>N. X n < X N + 1"
hoelzl@51531
  1671
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@54263
  1672
    thus "\<And>s. s \<in> S \<Longrightarrow>  s \<le> X N + 1"
hoelzl@51531
  1673
      by (rule bound_isUb)
hoelzl@51531
  1674
  qed
hoelzl@54263
  1675
  have "X ----> Sup S"
hoelzl@51531
  1676
  proof (rule metric_LIMSEQ_I)
hoelzl@51531
  1677
  fix r::real assume "0 < r"
hoelzl@51531
  1678
  hence r: "0 < r/2" by simp
hoelzl@51531
  1679
  obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
hoelzl@51531
  1680
    using metric_CauchyD [OF X r] by auto
hoelzl@51531
  1681
  hence "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
hoelzl@51531
  1682
  hence N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
hoelzl@51531
  1683
    by (simp only: dist_real_def abs_diff_less_iff)
hoelzl@51531
  1684
hoelzl@51531
  1685
  from N have "\<forall>n\<ge>N. X N - r/2 < X n" by fast
hoelzl@51531
  1686
  hence "X N - r/2 \<in> S" by (rule mem_S)
hoelzl@54263
  1687
  hence 1: "X N - r/2 \<le> Sup S" by (simp add: cSup_upper)
hoelzl@51531
  1688
hoelzl@51531
  1689
  from N have "\<forall>n\<ge>N. X n < X N + r/2" by fast
hoelzl@54263
  1690
  from bound_isUb[OF this]
hoelzl@54263
  1691
  have 2: "Sup S \<le> X N + r/2"
hoelzl@54263
  1692
    by (intro cSup_least) simp_all
hoelzl@51531
  1693
hoelzl@54263
  1694
  show "\<exists>N. \<forall>n\<ge>N. dist (X n) (Sup S) < r"
hoelzl@51531
  1695
  proof (intro exI allI impI)
hoelzl@51531
  1696
    fix n assume n: "N \<le> n"
hoelzl@51531
  1697
    from N n have "X n < X N + r/2" and "X N - r/2 < X n" by simp+
hoelzl@54263
  1698
    thus "dist (X n) (Sup S) < r" using 1 2
hoelzl@51531
  1699
      by (simp add: abs_diff_less_iff dist_real_def)
hoelzl@51531
  1700
  qed
hoelzl@51531
  1701
  qed
hoelzl@51531
  1702
  then show ?thesis unfolding convergent_def by auto
hoelzl@51531
  1703
qed
hoelzl@51531
  1704
hoelzl@51531
  1705
instance real :: complete_space
hoelzl@51531
  1706
  by intro_classes (rule real_Cauchy_convergent)
hoelzl@51531
  1707
hoelzl@51531
  1708
class banach = real_normed_vector + complete_space
hoelzl@51531
  1709
hoelzl@51531
  1710
instance real :: banach by default
hoelzl@51531
  1711
hoelzl@51531
  1712
lemma tendsto_at_topI_sequentially:
hoelzl@51531
  1713
  fixes f :: "real \<Rightarrow> real"
hoelzl@51531
  1714
  assumes mono: "mono f"
hoelzl@51531
  1715
  assumes limseq: "(\<lambda>n. f (real n)) ----> y"
hoelzl@51531
  1716
  shows "(f ---> y) at_top"
hoelzl@51531
  1717
proof (rule tendstoI)
hoelzl@51531
  1718
  fix e :: real assume "0 < e"
hoelzl@51531
  1719
  with limseq obtain N :: nat where N: "\<And>n. N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e"
hoelzl@51531
  1720
    by (auto simp: LIMSEQ_def dist_real_def)
hoelzl@51531
  1721
  { fix x :: real
wenzelm@53381
  1722
    obtain n where "x \<le> real_of_nat n"
wenzelm@53381
  1723
      using ex_le_of_nat[of x] ..
hoelzl@51531
  1724
    note monoD[OF mono this]
hoelzl@51531
  1725
    also have "f (real_of_nat n) \<le> y"
hoelzl@51531
  1726
      by (rule LIMSEQ_le_const[OF limseq])
hoelzl@51531
  1727
         (auto intro: exI[of _ n] monoD[OF mono] simp: real_eq_of_nat[symmetric])
hoelzl@51531
  1728
    finally have "f x \<le> y" . }
hoelzl@51531
  1729
  note le = this
hoelzl@51531
  1730
  have "eventually (\<lambda>x. real N \<le> x) at_top"
hoelzl@51531
  1731
    by (rule eventually_ge_at_top)
hoelzl@51531
  1732
  then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
hoelzl@51531
  1733
  proof eventually_elim
hoelzl@51531
  1734
    fix x assume N': "real N \<le> x"
hoelzl@51531
  1735
    with N[of N] le have "y - f (real N) < e" by auto
hoelzl@51531
  1736
    moreover note monoD[OF mono N']
hoelzl@51531
  1737
    ultimately show "dist (f x) y < e"
hoelzl@51531
  1738
      using le[of x] by (auto simp: dist_real_def field_simps)
hoelzl@51531
  1739
  qed
hoelzl@51531
  1740
qed
hoelzl@51531
  1741
huffman@20504
  1742
end