src/HOL/Real_Vector_Spaces.thy
author wenzelm
Sun Sep 18 20:33:48 2016 +0200 (2016-09-18)
changeset 63915 bab633745c7f
parent 63680 6e1e8b5abbfa
child 63927 0efb482beb84
permissions -rw-r--r--
tuned proofs;
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
wenzelm@60758
     6
section \<open>Vector Spaces and Algebras over the Reals\<close>
huffman@20504
     7
hoelzl@51524
     8
theory Real_Vector_Spaces
hoelzl@51531
     9
imports Real Topological_Spaces
huffman@20504
    10
begin
huffman@20504
    11
wenzelm@60758
    12
subsection \<open>Locale for additive functions\<close>
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))"
wenzelm@63915
    37
  by (induct A rule: infinite_finite_induct) (simp_all add: zero add)
huffman@22942
    38
huffman@27443
    39
end
huffman@20504
    40
wenzelm@63545
    41
wenzelm@60758
    42
subsection \<open>Vector spaces\<close>
huffman@28029
    43
huffman@28029
    44
locale vector_space =
huffman@28029
    45
  fixes scale :: "'a::field \<Rightarrow> 'b::ab_group_add \<Rightarrow> 'b"
wenzelm@63545
    46
  assumes scale_right_distrib [algebra_simps]: "scale a (x + y) = scale a x + scale a y"
wenzelm@63545
    47
    and scale_left_distrib [algebra_simps]: "scale (a + b) x = scale a x + scale b x"
wenzelm@63545
    48
    and scale_scale [simp]: "scale a (scale b x) = scale (a * b) x"
wenzelm@63545
    49
    and scale_one [simp]: "scale 1 x = x"
huffman@28029
    50
begin
huffman@28029
    51
wenzelm@63545
    52
lemma scale_left_commute: "scale a (scale b x) = scale b (scale a x)"
wenzelm@63545
    53
  by (simp add: mult.commute)
huffman@28029
    54
huffman@28029
    55
lemma scale_zero_left [simp]: "scale 0 x = 0"
huffman@28029
    56
  and scale_minus_left [simp]: "scale (- a) x = - (scale a x)"
wenzelm@63545
    57
  and scale_left_diff_distrib [algebra_simps]: "scale (a - b) x = scale a x - scale b x"
huffman@44282
    58
  and scale_setsum_left: "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)"
huffman@28029
    59
proof -
ballarin@29229
    60
  interpret s: additive "\<lambda>a. scale a x"
wenzelm@63545
    61
    by standard (rule scale_left_distrib)
huffman@28029
    62
  show "scale 0 x = 0" by (rule s.zero)
huffman@28029
    63
  show "scale (- a) x = - (scale a x)" by (rule s.minus)
huffman@28029
    64
  show "scale (a - b) x = scale a x - scale b x" by (rule s.diff)
huffman@44282
    65
  show "scale (setsum f A) x = (\<Sum>a\<in>A. scale (f a) x)" by (rule s.setsum)
huffman@28029
    66
qed
huffman@28029
    67
huffman@28029
    68
lemma scale_zero_right [simp]: "scale a 0 = 0"
huffman@28029
    69
  and scale_minus_right [simp]: "scale a (- x) = - (scale a x)"
wenzelm@63545
    70
  and scale_right_diff_distrib [algebra_simps]: "scale a (x - y) = scale a x - scale a y"
huffman@44282
    71
  and scale_setsum_right: "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))"
huffman@28029
    72
proof -
ballarin@29229
    73
  interpret s: additive "\<lambda>x. scale a x"
wenzelm@63545
    74
    by standard (rule scale_right_distrib)
huffman@28029
    75
  show "scale a 0 = 0" by (rule s.zero)
huffman@28029
    76
  show "scale a (- x) = - (scale a x)" by (rule s.minus)
huffman@28029
    77
  show "scale a (x - y) = scale a x - scale a y" by (rule s.diff)
huffman@44282
    78
  show "scale a (setsum f A) = (\<Sum>x\<in>A. scale a (f x))" by (rule s.setsum)
huffman@28029
    79
qed
huffman@28029
    80
wenzelm@63545
    81
lemma scale_eq_0_iff [simp]: "scale a x = 0 \<longleftrightarrow> a = 0 \<or> x = 0"
wenzelm@63545
    82
proof (cases "a = 0")
wenzelm@63545
    83
  case True
wenzelm@63545
    84
  then show ?thesis by simp
huffman@28029
    85
next
wenzelm@63545
    86
  case False
wenzelm@63545
    87
  have "x = 0" if "scale a x = 0"
wenzelm@63545
    88
  proof -
wenzelm@63545
    89
    from False that have "scale (inverse a) (scale a x) = 0" by simp
wenzelm@63545
    90
    with False show ?thesis by simp
wenzelm@63545
    91
  qed
wenzelm@63545
    92
  then show ?thesis by force
huffman@28029
    93
qed
huffman@28029
    94
huffman@28029
    95
lemma scale_left_imp_eq:
wenzelm@63545
    96
  assumes nonzero: "a \<noteq> 0"
wenzelm@63545
    97
    and scale: "scale a x = scale a y"
wenzelm@63545
    98
  shows "x = y"
huffman@28029
    99
proof -
wenzelm@63545
   100
  from scale have "scale a (x - y) = 0"
huffman@28029
   101
     by (simp add: scale_right_diff_distrib)
wenzelm@63545
   102
  with nonzero have "x - y = 0" by simp
wenzelm@63545
   103
  then show "x = y" by (simp only: right_minus_eq)
huffman@28029
   104
qed
huffman@28029
   105
huffman@28029
   106
lemma scale_right_imp_eq:
wenzelm@63545
   107
  assumes nonzero: "x \<noteq> 0"
wenzelm@63545
   108
    and scale: "scale a x = scale b x"
wenzelm@63545
   109
  shows "a = b"
huffman@28029
   110
proof -
wenzelm@63545
   111
  from scale have "scale (a - b) x = 0"
huffman@28029
   112
     by (simp add: scale_left_diff_distrib)
wenzelm@63545
   113
  with nonzero have "a - b = 0" by simp
wenzelm@63545
   114
  then show "a = b" by (simp only: right_minus_eq)
huffman@28029
   115
qed
huffman@28029
   116
wenzelm@63545
   117
lemma scale_cancel_left [simp]: "scale a x = scale a y \<longleftrightarrow> x = y \<or> a = 0"
wenzelm@63545
   118
  by (auto intro: scale_left_imp_eq)
huffman@28029
   119
wenzelm@63545
   120
lemma scale_cancel_right [simp]: "scale a x = scale b x \<longleftrightarrow> a = b \<or> x = 0"
wenzelm@63545
   121
  by (auto intro: scale_right_imp_eq)
huffman@28029
   122
huffman@28029
   123
end
huffman@28029
   124
wenzelm@63545
   125
wenzelm@60758
   126
subsection \<open>Real vector spaces\<close>
huffman@20504
   127
haftmann@29608
   128
class scaleR =
haftmann@25062
   129
  fixes scaleR :: "real \<Rightarrow> 'a \<Rightarrow> 'a" (infixr "*\<^sub>R" 75)
haftmann@24748
   130
begin
huffman@20504
   131
wenzelm@63545
   132
abbreviation divideR :: "'a \<Rightarrow> real \<Rightarrow> 'a"  (infixl "'/\<^sub>R" 70)
wenzelm@63545
   133
  where "x /\<^sub>R r \<equiv> scaleR (inverse r) x"
haftmann@24748
   134
haftmann@24748
   135
end
haftmann@24748
   136
haftmann@24588
   137
class real_vector = scaleR + ab_group_add +
huffman@44282
   138
  assumes scaleR_add_right: "scaleR a (x + y) = scaleR a x + scaleR a y"
huffman@44282
   139
  and scaleR_add_left: "scaleR (a + b) x = scaleR a x + scaleR b x"
huffman@30070
   140
  and scaleR_scaleR: "scaleR a (scaleR b x) = scaleR (a * b) x"
huffman@30070
   141
  and scaleR_one: "scaleR 1 x = x"
huffman@20504
   142
wenzelm@63545
   143
interpretation real_vector: vector_space "scaleR :: real \<Rightarrow> 'a \<Rightarrow> 'a::real_vector"
wenzelm@63545
   144
  apply unfold_locales
wenzelm@63545
   145
     apply (rule scaleR_add_right)
wenzelm@63545
   146
    apply (rule scaleR_add_left)
wenzelm@63545
   147
   apply (rule scaleR_scaleR)
wenzelm@63545
   148
  apply (rule scaleR_one)
wenzelm@63545
   149
  done
huffman@28009
   150
wenzelm@60758
   151
text \<open>Recover original theorem names\<close>
huffman@28009
   152
huffman@28009
   153
lemmas scaleR_left_commute = real_vector.scale_left_commute
huffman@28009
   154
lemmas scaleR_zero_left = real_vector.scale_zero_left
huffman@28009
   155
lemmas scaleR_minus_left = real_vector.scale_minus_left
huffman@44282
   156
lemmas scaleR_diff_left = real_vector.scale_left_diff_distrib
huffman@44282
   157
lemmas scaleR_setsum_left = real_vector.scale_setsum_left
huffman@28009
   158
lemmas scaleR_zero_right = real_vector.scale_zero_right
huffman@28009
   159
lemmas scaleR_minus_right = real_vector.scale_minus_right
huffman@44282
   160
lemmas scaleR_diff_right = real_vector.scale_right_diff_distrib
huffman@44282
   161
lemmas scaleR_setsum_right = real_vector.scale_setsum_right
huffman@28009
   162
lemmas scaleR_eq_0_iff = real_vector.scale_eq_0_iff
huffman@28009
   163
lemmas scaleR_left_imp_eq = real_vector.scale_left_imp_eq
huffman@28009
   164
lemmas scaleR_right_imp_eq = real_vector.scale_right_imp_eq
huffman@28009
   165
lemmas scaleR_cancel_left = real_vector.scale_cancel_left
huffman@28009
   166
lemmas scaleR_cancel_right = real_vector.scale_cancel_right
huffman@28009
   167
wenzelm@60758
   168
text \<open>Legacy names\<close>
huffman@44282
   169
huffman@44282
   170
lemmas scaleR_left_distrib = scaleR_add_left
huffman@44282
   171
lemmas scaleR_right_distrib = scaleR_add_right
huffman@44282
   172
lemmas scaleR_left_diff_distrib = scaleR_diff_left
huffman@44282
   173
lemmas scaleR_right_diff_distrib = scaleR_diff_right
huffman@44282
   174
wenzelm@63545
   175
lemma scaleR_minus1_left [simp]: "scaleR (-1) x = - x"
wenzelm@63545
   176
  for x :: "'a::real_vector"
huffman@31285
   177
  using scaleR_minus_left [of 1 x] by simp
hoelzl@62101
   178
haftmann@24588
   179
class real_algebra = real_vector + ring +
haftmann@25062
   180
  assumes mult_scaleR_left [simp]: "scaleR a x * y = scaleR a (x * y)"
wenzelm@63545
   181
    and mult_scaleR_right [simp]: "x * scaleR a y = scaleR a (x * y)"
huffman@20504
   182
haftmann@24588
   183
class real_algebra_1 = real_algebra + ring_1
huffman@20554
   184
haftmann@24588
   185
class real_div_algebra = real_algebra_1 + division_ring
huffman@20584
   186
haftmann@24588
   187
class real_field = real_div_algebra + field
huffman@20584
   188
huffman@30069
   189
instantiation real :: real_field
huffman@30069
   190
begin
huffman@30069
   191
wenzelm@63545
   192
definition real_scaleR_def [simp]: "scaleR a x = a * x"
huffman@30069
   193
wenzelm@63545
   194
instance
wenzelm@63545
   195
  by standard (simp_all add: algebra_simps)
huffman@20554
   196
huffman@30069
   197
end
huffman@30069
   198
wenzelm@63545
   199
interpretation scaleR_left: additive "(\<lambda>a. scaleR a x :: 'a::real_vector)"
wenzelm@63545
   200
  by standard (rule scaleR_left_distrib)
huffman@20504
   201
wenzelm@63545
   202
interpretation scaleR_right: additive "(\<lambda>x. scaleR a x :: 'a::real_vector)"
wenzelm@63545
   203
  by standard (rule scaleR_right_distrib)
huffman@20504
   204
huffman@20584
   205
lemma nonzero_inverse_scaleR_distrib:
wenzelm@63545
   206
  "a \<noteq> 0 \<Longrightarrow> x \<noteq> 0 \<Longrightarrow> inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
wenzelm@63545
   207
  for x :: "'a::real_div_algebra"
wenzelm@63545
   208
  by (rule inverse_unique) simp
huffman@20584
   209
wenzelm@63545
   210
lemma inverse_scaleR_distrib: "inverse (scaleR a x) = scaleR (inverse a) (inverse x)"
wenzelm@63545
   211
  for x :: "'a::{real_div_algebra,division_ring}"
wenzelm@63545
   212
  apply (cases "a = 0")
wenzelm@63545
   213
   apply simp
wenzelm@63545
   214
  apply (cases "x = 0")
wenzelm@63545
   215
   apply simp
wenzelm@63545
   216
  apply (erule (1) nonzero_inverse_scaleR_distrib)
eberlm@61531
   217
  done
eberlm@61531
   218
wenzelm@63545
   219
lemma setsum_constant_scaleR: "(\<Sum>x\<in>A. y) = of_nat (card A) *\<^sub>R y"
wenzelm@63545
   220
  for y :: "'a::real_vector"
wenzelm@63915
   221
  by (induct A rule: infinite_finite_induct) (simp_all add: algebra_simps)
wenzelm@63545
   222
wenzelm@63545
   223
lemma vector_add_divide_simps:
wenzelm@63545
   224
  "v + (b / z) *\<^sub>R w = (if z = 0 then v else (z *\<^sub>R v + b *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   225
  "a *\<^sub>R v + (b / z) *\<^sub>R w = (if z = 0 then a *\<^sub>R v else ((a * z) *\<^sub>R v + b *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   226
  "(a / z) *\<^sub>R v + w = (if z = 0 then w else (a *\<^sub>R v + z *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   227
  "(a / z) *\<^sub>R v + b *\<^sub>R w = (if z = 0 then b *\<^sub>R w else (a *\<^sub>R v + (b * z) *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   228
  "v - (b / z) *\<^sub>R w = (if z = 0 then v else (z *\<^sub>R v - b *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   229
  "a *\<^sub>R v - (b / z) *\<^sub>R w = (if z = 0 then a *\<^sub>R v else ((a * z) *\<^sub>R v - b *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   230
  "(a / z) *\<^sub>R v - w = (if z = 0 then -w else (a *\<^sub>R v - z *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   231
  "(a / z) *\<^sub>R v - b *\<^sub>R w = (if z = 0 then -b *\<^sub>R w else (a *\<^sub>R v - (b * z) *\<^sub>R w) /\<^sub>R z)"
wenzelm@63545
   232
  for v :: "'a :: real_vector"
wenzelm@63545
   233
  by (simp_all add: divide_inverse_commute scaleR_add_right real_vector.scale_right_diff_distrib)
lp15@63114
   234
lp15@60800
   235
lemma real_vector_affinity_eq:
lp15@60800
   236
  fixes x :: "'a :: real_vector"
lp15@60800
   237
  assumes m0: "m \<noteq> 0"
lp15@60800
   238
  shows "m *\<^sub>R x + c = y \<longleftrightarrow> x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
wenzelm@63545
   239
    (is "?lhs \<longleftrightarrow> ?rhs")
lp15@60800
   240
proof
wenzelm@63545
   241
  assume ?lhs
wenzelm@63545
   242
  then have "m *\<^sub>R x = y - c" by (simp add: field_simps)
wenzelm@63545
   243
  then have "inverse m *\<^sub>R (m *\<^sub>R x) = inverse m *\<^sub>R (y - c)" by simp
lp15@60800
   244
  then show "x = inverse m *\<^sub>R y - (inverse m *\<^sub>R c)"
lp15@60800
   245
    using m0
lp15@60800
   246
  by (simp add: real_vector.scale_right_diff_distrib)
lp15@60800
   247
next
wenzelm@63545
   248
  assume ?rhs
wenzelm@63545
   249
  with m0 show "m *\<^sub>R x + c = y"
wenzelm@63545
   250
    by (simp add: real_vector.scale_right_diff_distrib)
lp15@60800
   251
qed
lp15@60800
   252
wenzelm@63545
   253
lemma real_vector_eq_affinity: "m \<noteq> 0 \<Longrightarrow> y = m *\<^sub>R x + c \<longleftrightarrow> inverse m *\<^sub>R y - (inverse m *\<^sub>R c) = x"
wenzelm@63545
   254
  for x :: "'a::real_vector"
lp15@60800
   255
  using real_vector_affinity_eq[where m=m and x=x and y=y and c=c]
lp15@60800
   256
  by metis
lp15@60800
   257
wenzelm@63545
   258
lemma scaleR_eq_iff [simp]: "b + u *\<^sub>R a = a + u *\<^sub>R b \<longleftrightarrow> a = b \<or> u = 1"
wenzelm@63545
   259
  for a :: "'a::real_vector"
wenzelm@63545
   260
proof (cases "u = 1")
wenzelm@63545
   261
  case True
wenzelm@63545
   262
  then show ?thesis by auto
lp15@62948
   263
next
lp15@62948
   264
  case False
wenzelm@63545
   265
  have "a = b" if "b + u *\<^sub>R a = a + u *\<^sub>R b"
wenzelm@63545
   266
  proof -
wenzelm@63545
   267
    from that have "(u - 1) *\<^sub>R a = (u - 1) *\<^sub>R b"
lp15@62948
   268
      by (simp add: algebra_simps)
wenzelm@63545
   269
    with False show ?thesis
lp15@62948
   270
      by auto
wenzelm@63545
   271
  qed
lp15@62948
   272
  then show ?thesis by auto
lp15@62948
   273
qed
lp15@62948
   274
wenzelm@63545
   275
lemma scaleR_collapse [simp]: "(1 - u) *\<^sub>R a + u *\<^sub>R a = a"
wenzelm@63545
   276
  for a :: "'a::real_vector"
wenzelm@63545
   277
  by (simp add: algebra_simps)
lp15@62948
   278
huffman@20554
   279
wenzelm@63545
   280
subsection \<open>Embedding of the Reals into any \<open>real_algebra_1\<close>: \<open>of_real\<close>\<close>
huffman@20554
   281
wenzelm@63545
   282
definition of_real :: "real \<Rightarrow> 'a::real_algebra_1"
wenzelm@63545
   283
  where "of_real r = scaleR r 1"
huffman@20554
   284
huffman@21809
   285
lemma scaleR_conv_of_real: "scaleR r x = of_real r * x"
wenzelm@63545
   286
  by (simp add: of_real_def)
huffman@20763
   287
huffman@20554
   288
lemma of_real_0 [simp]: "of_real 0 = 0"
wenzelm@63545
   289
  by (simp add: of_real_def)
huffman@20554
   290
huffman@20554
   291
lemma of_real_1 [simp]: "of_real 1 = 1"
wenzelm@63545
   292
  by (simp add: of_real_def)
huffman@20554
   293
huffman@20554
   294
lemma of_real_add [simp]: "of_real (x + y) = of_real x + of_real y"
wenzelm@63545
   295
  by (simp add: of_real_def scaleR_left_distrib)
huffman@20554
   296
huffman@20554
   297
lemma of_real_minus [simp]: "of_real (- x) = - of_real x"
wenzelm@63545
   298
  by (simp add: of_real_def)
huffman@20554
   299
huffman@20554
   300
lemma of_real_diff [simp]: "of_real (x - y) = of_real x - of_real y"
wenzelm@63545
   301
  by (simp add: of_real_def scaleR_left_diff_distrib)
huffman@20554
   302
huffman@20554
   303
lemma of_real_mult [simp]: "of_real (x * y) = of_real x * of_real y"
wenzelm@63545
   304
  by (simp add: of_real_def mult.commute)
huffman@20554
   305
hoelzl@56889
   306
lemma of_real_setsum[simp]: "of_real (setsum f s) = (\<Sum>x\<in>s. of_real (f x))"
hoelzl@56889
   307
  by (induct s rule: infinite_finite_induct) auto
hoelzl@56889
   308
hoelzl@56889
   309
lemma of_real_setprod[simp]: "of_real (setprod f s) = (\<Prod>x\<in>s. of_real (f x))"
hoelzl@56889
   310
  by (induct s rule: infinite_finite_induct) auto
hoelzl@56889
   311
huffman@20584
   312
lemma nonzero_of_real_inverse:
wenzelm@63545
   313
  "x \<noteq> 0 \<Longrightarrow> of_real (inverse x) = inverse (of_real x :: 'a::real_div_algebra)"
wenzelm@63545
   314
  by (simp add: of_real_def nonzero_inverse_scaleR_distrib)
huffman@20584
   315
huffman@20584
   316
lemma of_real_inverse [simp]:
wenzelm@63545
   317
  "of_real (inverse x) = inverse (of_real x :: 'a::{real_div_algebra,division_ring})"
wenzelm@63545
   318
  by (simp add: of_real_def inverse_scaleR_distrib)
huffman@20584
   319
huffman@20584
   320
lemma nonzero_of_real_divide:
wenzelm@63545
   321
  "y \<noteq> 0 \<Longrightarrow> of_real (x / y) = (of_real x / of_real y :: 'a::real_field)"
wenzelm@63545
   322
  by (simp add: divide_inverse nonzero_of_real_inverse)
huffman@20722
   323
huffman@20722
   324
lemma of_real_divide [simp]:
paulson@62131
   325
  "of_real (x / y) = (of_real x / of_real y :: 'a::real_div_algebra)"
wenzelm@63545
   326
  by (simp add: divide_inverse)
huffman@20584
   327
huffman@20722
   328
lemma of_real_power [simp]:
haftmann@31017
   329
  "of_real (x ^ n) = (of_real x :: 'a::{real_algebra_1}) ^ n"
wenzelm@63545
   330
  by (induct n) simp_all
huffman@20722
   331
wenzelm@63545
   332
lemma of_real_eq_iff [simp]: "of_real x = of_real y \<longleftrightarrow> x = y"
wenzelm@63545
   333
  by (simp add: of_real_def)
huffman@20554
   334
wenzelm@63545
   335
lemma inj_of_real: "inj of_real"
haftmann@38621
   336
  by (auto intro: injI)
haftmann@38621
   337
huffman@20584
   338
lemmas of_real_eq_0_iff [simp] = of_real_eq_iff [of _ 0, simplified]
huffman@20554
   339
huffman@20554
   340
lemma of_real_eq_id [simp]: "of_real = (id :: real \<Rightarrow> real)"
wenzelm@63545
   341
  by (rule ext) (simp add: of_real_def)
huffman@20554
   342
wenzelm@63545
   343
text \<open>Collapse nested embeddings.\<close>
huffman@20554
   344
lemma of_real_of_nat_eq [simp]: "of_real (of_nat n) = of_nat n"
wenzelm@63545
   345
  by (induct n) auto
huffman@20554
   346
huffman@20554
   347
lemma of_real_of_int_eq [simp]: "of_real (of_int z) = of_int z"
wenzelm@63545
   348
  by (cases z rule: int_diff_cases) simp
huffman@20554
   349
lp15@60155
   350
lemma of_real_numeral [simp]: "of_real (numeral w) = numeral w"
wenzelm@63545
   351
  using of_real_of_int_eq [of "numeral w"] by simp
huffman@47108
   352
lp15@60155
   353
lemma of_real_neg_numeral [simp]: "of_real (- numeral w) = - numeral w"
wenzelm@63545
   354
  using of_real_of_int_eq [of "- numeral w"] by simp
huffman@20554
   355
wenzelm@63545
   356
text \<open>Every real algebra has characteristic zero.\<close>
huffman@22912
   357
instance real_algebra_1 < ring_char_0
huffman@22912
   358
proof
wenzelm@63545
   359
  from inj_of_real inj_of_nat have "inj (of_real \<circ> of_nat)"
wenzelm@63545
   360
    by (rule inj_comp)
wenzelm@63545
   361
  then show "inj (of_nat :: nat \<Rightarrow> 'a)"
wenzelm@63545
   362
    by (simp add: comp_def)
huffman@22912
   363
qed
huffman@22912
   364
huffman@27553
   365
instance real_field < field_char_0 ..
huffman@27553
   366
huffman@20554
   367
wenzelm@60758
   368
subsection \<open>The Set of Real Numbers\<close>
huffman@20554
   369
wenzelm@61070
   370
definition Reals :: "'a::real_algebra_1 set"  ("\<real>")
wenzelm@61070
   371
  where "\<real> = range of_real"
huffman@20554
   372
wenzelm@61070
   373
lemma Reals_of_real [simp]: "of_real r \<in> \<real>"
wenzelm@63545
   374
  by (simp add: Reals_def)
huffman@20554
   375
wenzelm@61070
   376
lemma Reals_of_int [simp]: "of_int z \<in> \<real>"
wenzelm@63545
   377
  by (subst of_real_of_int_eq [symmetric], rule Reals_of_real)
huffman@20718
   378
wenzelm@61070
   379
lemma Reals_of_nat [simp]: "of_nat n \<in> \<real>"
wenzelm@63545
   380
  by (subst of_real_of_nat_eq [symmetric], rule Reals_of_real)
huffman@21809
   381
wenzelm@61070
   382
lemma Reals_numeral [simp]: "numeral w \<in> \<real>"
wenzelm@63545
   383
  by (subst of_real_numeral [symmetric], rule Reals_of_real)
huffman@47108
   384
wenzelm@61070
   385
lemma Reals_0 [simp]: "0 \<in> \<real>"
wenzelm@63545
   386
  apply (unfold Reals_def)
wenzelm@63545
   387
  apply (rule range_eqI)
wenzelm@63545
   388
  apply (rule of_real_0 [symmetric])
wenzelm@63545
   389
  done
huffman@20554
   390
wenzelm@61070
   391
lemma Reals_1 [simp]: "1 \<in> \<real>"
wenzelm@63545
   392
  apply (unfold Reals_def)
wenzelm@63545
   393
  apply (rule range_eqI)
wenzelm@63545
   394
  apply (rule of_real_1 [symmetric])
wenzelm@63545
   395
  done
huffman@20554
   396
wenzelm@63545
   397
lemma Reals_add [simp]: "a \<in> \<real> \<Longrightarrow> b \<in> \<real> \<Longrightarrow> a + b \<in> \<real>"
wenzelm@63545
   398
  apply (auto simp add: Reals_def)
wenzelm@63545
   399
  apply (rule range_eqI)
wenzelm@63545
   400
  apply (rule of_real_add [symmetric])
wenzelm@63545
   401
  done
huffman@20554
   402
wenzelm@61070
   403
lemma Reals_minus [simp]: "a \<in> \<real> \<Longrightarrow> - a \<in> \<real>"
wenzelm@63545
   404
  apply (auto simp add: Reals_def)
wenzelm@63545
   405
  apply (rule range_eqI)
wenzelm@63545
   406
  apply (rule of_real_minus [symmetric])
wenzelm@63545
   407
  done
huffman@20584
   408
wenzelm@63545
   409
lemma Reals_diff [simp]: "a \<in> \<real> \<Longrightarrow> b \<in> \<real> \<Longrightarrow> a - b \<in> \<real>"
wenzelm@63545
   410
  apply (auto simp add: Reals_def)
wenzelm@63545
   411
  apply (rule range_eqI)
wenzelm@63545
   412
  apply (rule of_real_diff [symmetric])
wenzelm@63545
   413
  done
huffman@20584
   414
wenzelm@63545
   415
lemma Reals_mult [simp]: "a \<in> \<real> \<Longrightarrow> b \<in> \<real> \<Longrightarrow> a * b \<in> \<real>"
wenzelm@63545
   416
  apply (auto simp add: Reals_def)
wenzelm@63545
   417
  apply (rule range_eqI)
wenzelm@63545
   418
  apply (rule of_real_mult [symmetric])
wenzelm@63545
   419
  done
huffman@20554
   420
wenzelm@63545
   421
lemma nonzero_Reals_inverse: "a \<in> \<real> \<Longrightarrow> a \<noteq> 0 \<Longrightarrow> inverse a \<in> \<real>"
wenzelm@63545
   422
  for a :: "'a::real_div_algebra"
wenzelm@63545
   423
  apply (auto simp add: Reals_def)
wenzelm@63545
   424
  apply (rule range_eqI)
wenzelm@63545
   425
  apply (erule nonzero_of_real_inverse [symmetric])
wenzelm@63545
   426
  done
huffman@20584
   427
wenzelm@63545
   428
lemma Reals_inverse: "a \<in> \<real> \<Longrightarrow> inverse a \<in> \<real>"
wenzelm@63545
   429
  for a :: "'a::{real_div_algebra,division_ring}"
wenzelm@63545
   430
  apply (auto simp add: Reals_def)
wenzelm@63545
   431
  apply (rule range_eqI)
wenzelm@63545
   432
  apply (rule of_real_inverse [symmetric])
wenzelm@63545
   433
  done
huffman@20584
   434
wenzelm@63545
   435
lemma Reals_inverse_iff [simp]: "inverse x \<in> \<real> \<longleftrightarrow> x \<in> \<real>"
wenzelm@63545
   436
  for x :: "'a::{real_div_algebra,division_ring}"
wenzelm@63545
   437
  by (metis Reals_inverse inverse_inverse_eq)
lp15@55719
   438
wenzelm@63545
   439
lemma nonzero_Reals_divide: "a \<in> \<real> \<Longrightarrow> b \<in> \<real> \<Longrightarrow> b \<noteq> 0 \<Longrightarrow> a / b \<in> \<real>"
wenzelm@63545
   440
  for a b :: "'a::real_field"
wenzelm@63545
   441
  apply (auto simp add: Reals_def)
wenzelm@63545
   442
  apply (rule range_eqI)
wenzelm@63545
   443
  apply (erule nonzero_of_real_divide [symmetric])
wenzelm@63545
   444
  done
huffman@20584
   445
wenzelm@63545
   446
lemma Reals_divide [simp]: "a \<in> \<real> \<Longrightarrow> b \<in> \<real> \<Longrightarrow> a / b \<in> \<real>"
wenzelm@63545
   447
  for a b :: "'a::{real_field,field}"
wenzelm@63545
   448
  apply (auto simp add: Reals_def)
wenzelm@63545
   449
  apply (rule range_eqI)
wenzelm@63545
   450
  apply (rule of_real_divide [symmetric])
wenzelm@63545
   451
  done
huffman@20584
   452
wenzelm@63545
   453
lemma Reals_power [simp]: "a \<in> \<real> \<Longrightarrow> a ^ n \<in> \<real>"
wenzelm@63545
   454
  for a :: "'a::real_algebra_1"
wenzelm@63545
   455
  apply (auto simp add: Reals_def)
wenzelm@63545
   456
  apply (rule range_eqI)
wenzelm@63545
   457
  apply (rule of_real_power [symmetric])
wenzelm@63545
   458
  done
huffman@20722
   459
huffman@20554
   460
lemma Reals_cases [cases set: Reals]:
huffman@20554
   461
  assumes "q \<in> \<real>"
huffman@20554
   462
  obtains (of_real) r where "q = of_real r"
huffman@20554
   463
  unfolding Reals_def
huffman@20554
   464
proof -
wenzelm@60758
   465
  from \<open>q \<in> \<real>\<close> have "q \<in> range of_real" unfolding Reals_def .
huffman@20554
   466
  then obtain r where "q = of_real r" ..
huffman@20554
   467
  then show thesis ..
huffman@20554
   468
qed
huffman@20554
   469
wenzelm@63915
   470
lemma setsum_in_Reals [intro,simp]: "(\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>) \<Longrightarrow> setsum f s \<in> \<real>"
wenzelm@63915
   471
proof (induct s rule: infinite_finite_induct)
wenzelm@63915
   472
  case infinite
wenzelm@63915
   473
  then show ?case by (metis Reals_0 setsum.infinite)
wenzelm@63915
   474
qed simp_all
lp15@55719
   475
wenzelm@63915
   476
lemma setprod_in_Reals [intro,simp]: "(\<And>i. i \<in> s \<Longrightarrow> f i \<in> \<real>) \<Longrightarrow> setprod f s \<in> \<real>"
wenzelm@63915
   477
proof (induct s rule: infinite_finite_induct)
wenzelm@63915
   478
  case infinite
wenzelm@63915
   479
  then show ?case by (metis Reals_1 setprod.infinite)
wenzelm@63915
   480
qed simp_all
lp15@55719
   481
huffman@20554
   482
lemma Reals_induct [case_names of_real, induct set: Reals]:
huffman@20554
   483
  "q \<in> \<real> \<Longrightarrow> (\<And>r. P (of_real r)) \<Longrightarrow> P q"
huffman@20554
   484
  by (rule Reals_cases) auto
huffman@20554
   485
wenzelm@63545
   486
wenzelm@60758
   487
subsection \<open>Ordered real vector spaces\<close>
immler@54778
   488
immler@54778
   489
class ordered_real_vector = real_vector + ordered_ab_group_add +
immler@54778
   490
  assumes scaleR_left_mono: "x \<le> y \<Longrightarrow> 0 \<le> a \<Longrightarrow> a *\<^sub>R x \<le> a *\<^sub>R y"
wenzelm@63545
   491
    and scaleR_right_mono: "a \<le> b \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> b *\<^sub>R x"
immler@54778
   492
begin
immler@54778
   493
wenzelm@63545
   494
lemma scaleR_mono: "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"
wenzelm@63545
   495
  apply (erule scaleR_right_mono [THEN order_trans])
wenzelm@63545
   496
   apply assumption
wenzelm@63545
   497
  apply (erule scaleR_left_mono)
wenzelm@63545
   498
  apply assumption
wenzelm@63545
   499
  done
immler@54778
   500
wenzelm@63545
   501
lemma scaleR_mono': "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
   502
  by (rule scaleR_mono) (auto intro: order.trans)
immler@54778
   503
immler@54785
   504
lemma pos_le_divideRI:
immler@54785
   505
  assumes "0 < c"
wenzelm@63545
   506
    and "c *\<^sub>R a \<le> b"
immler@54785
   507
  shows "a \<le> b /\<^sub>R c"
immler@54785
   508
proof -
immler@54785
   509
  from scaleR_left_mono[OF assms(2)] assms(1)
immler@54785
   510
  have "c *\<^sub>R a /\<^sub>R c \<le> b /\<^sub>R c"
immler@54785
   511
    by simp
immler@54785
   512
  with assms show ?thesis
immler@54785
   513
    by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
immler@54785
   514
qed
immler@54785
   515
immler@54785
   516
lemma pos_le_divideR_eq:
immler@54785
   517
  assumes "0 < c"
immler@54785
   518
  shows "a \<le> b /\<^sub>R c \<longleftrightarrow> c *\<^sub>R a \<le> b"
wenzelm@63545
   519
    (is "?lhs \<longleftrightarrow> ?rhs")
wenzelm@63545
   520
proof
wenzelm@63545
   521
  assume ?lhs
wenzelm@63545
   522
  from scaleR_left_mono[OF this] assms have "c *\<^sub>R a \<le> c *\<^sub>R (b /\<^sub>R c)"
immler@54785
   523
    by simp
wenzelm@63545
   524
  with assms show ?rhs
immler@54785
   525
    by (simp add: scaleR_one scaleR_scaleR inverse_eq_divide)
wenzelm@63545
   526
next
wenzelm@63545
   527
  assume ?rhs
wenzelm@63545
   528
  with assms show ?lhs by (rule pos_le_divideRI)
wenzelm@63545
   529
qed
immler@54785
   530
wenzelm@63545
   531
lemma scaleR_image_atLeastAtMost: "c > 0 \<Longrightarrow> scaleR c ` {x..y} = {c *\<^sub>R x..c *\<^sub>R y}"
immler@54785
   532
  apply (auto intro!: scaleR_left_mono)
immler@54785
   533
  apply (rule_tac x = "inverse c *\<^sub>R xa" in image_eqI)
wenzelm@63545
   534
   apply (simp_all add: pos_le_divideR_eq[symmetric] scaleR_scaleR scaleR_one)
immler@54785
   535
  done
immler@54785
   536
immler@54778
   537
end
immler@54778
   538
paulson@60303
   539
lemma neg_le_divideR_eq:
paulson@60303
   540
  fixes a :: "'a :: ordered_real_vector"
paulson@60303
   541
  assumes "c < 0"
paulson@60303
   542
  shows "a \<le> b /\<^sub>R c \<longleftrightarrow> b \<le> c *\<^sub>R a"
wenzelm@63545
   543
  using pos_le_divideR_eq [of "-c" a "-b"] assms by simp
paulson@60303
   544
wenzelm@63545
   545
lemma scaleR_nonneg_nonneg: "0 \<le> a \<Longrightarrow> 0 \<le> x \<Longrightarrow> 0 \<le> a *\<^sub>R x"
wenzelm@63545
   546
  for x :: "'a::ordered_real_vector"
wenzelm@63545
   547
  using scaleR_left_mono [of 0 x a] by simp
immler@54778
   548
wenzelm@63545
   549
lemma scaleR_nonneg_nonpos: "0 \<le> a \<Longrightarrow> x \<le> 0 \<Longrightarrow> a *\<^sub>R x \<le> 0"
wenzelm@63545
   550
  for x :: "'a::ordered_real_vector"
immler@54778
   551
  using scaleR_left_mono [of x 0 a] by simp
immler@54778
   552
wenzelm@63545
   553
lemma scaleR_nonpos_nonneg: "a \<le> 0 \<Longrightarrow> 0 \<le> x \<Longrightarrow> a *\<^sub>R x \<le> 0"
wenzelm@63545
   554
  for x :: "'a::ordered_real_vector"
immler@54778
   555
  using scaleR_right_mono [of a 0 x] by simp
immler@54778
   556
wenzelm@63545
   557
lemma split_scaleR_neg_le: "(0 \<le> a \<and> x \<le> 0) \<or> (a \<le> 0 \<and> 0 \<le> x) \<Longrightarrow> a *\<^sub>R x \<le> 0"
wenzelm@63545
   558
  for x :: "'a::ordered_real_vector"
immler@54778
   559
  by (auto simp add: scaleR_nonneg_nonpos scaleR_nonpos_nonneg)
immler@54778
   560
wenzelm@63545
   561
lemma le_add_iff1: "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> (a - b) *\<^sub>R e + c \<le> d"
wenzelm@63545
   562
  for c d e :: "'a::ordered_real_vector"
immler@54778
   563
  by (simp add: algebra_simps)
immler@54778
   564
wenzelm@63545
   565
lemma le_add_iff2: "a *\<^sub>R e + c \<le> b *\<^sub>R e + d \<longleftrightarrow> c \<le> (b - a) *\<^sub>R e + d"
wenzelm@63545
   566
  for c d e :: "'a::ordered_real_vector"
immler@54778
   567
  by (simp add: algebra_simps)
immler@54778
   568
wenzelm@63545
   569
lemma scaleR_left_mono_neg: "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b"
wenzelm@63545
   570
  for a b :: "'a::ordered_real_vector"
immler@54778
   571
  apply (drule scaleR_left_mono [of _ _ "- c"])
wenzelm@63545
   572
   apply simp_all
immler@54778
   573
  done
immler@54778
   574
wenzelm@63545
   575
lemma scaleR_right_mono_neg: "b \<le> a \<Longrightarrow> c \<le> 0 \<Longrightarrow> a *\<^sub>R c \<le> b *\<^sub>R c"
wenzelm@63545
   576
  for c :: "'a::ordered_real_vector"
immler@54778
   577
  apply (drule scaleR_right_mono [of _ _ "- c"])
wenzelm@63545
   578
   apply simp_all
immler@54778
   579
  done
immler@54778
   580
wenzelm@63545
   581
lemma scaleR_nonpos_nonpos: "a \<le> 0 \<Longrightarrow> b \<le> 0 \<Longrightarrow> 0 \<le> a *\<^sub>R b"
wenzelm@63545
   582
  for b :: "'a::ordered_real_vector"
wenzelm@63545
   583
  using scaleR_right_mono_neg [of a 0 b] by simp
immler@54778
   584
wenzelm@63545
   585
lemma split_scaleR_pos_le: "(0 \<le> a \<and> 0 \<le> b) \<or> (a \<le> 0 \<and> b \<le> 0) \<Longrightarrow> 0 \<le> a *\<^sub>R b"
wenzelm@63545
   586
  for b :: "'a::ordered_real_vector"
immler@54778
   587
  by (auto simp add: scaleR_nonneg_nonneg scaleR_nonpos_nonpos)
immler@54778
   588
immler@54778
   589
lemma zero_le_scaleR_iff:
wenzelm@63545
   590
  fixes b :: "'a::ordered_real_vector"
wenzelm@63545
   591
  shows "0 \<le> a *\<^sub>R b \<longleftrightarrow> 0 < a \<and> 0 \<le> b \<or> a < 0 \<and> b \<le> 0 \<or> a = 0"
wenzelm@63545
   592
    (is "?lhs = ?rhs")
wenzelm@63545
   593
proof (cases "a = 0")
wenzelm@63545
   594
  case True
wenzelm@63545
   595
  then show ?thesis by simp
wenzelm@63545
   596
next
wenzelm@63545
   597
  case False
immler@54778
   598
  show ?thesis
immler@54778
   599
  proof
wenzelm@63545
   600
    assume ?lhs
wenzelm@63545
   601
    from \<open>a \<noteq> 0\<close> consider "a > 0" | "a < 0" by arith
wenzelm@63545
   602
    then show ?rhs
wenzelm@63545
   603
    proof cases
wenzelm@63545
   604
      case 1
wenzelm@63545
   605
      with \<open>?lhs\<close> have "inverse a *\<^sub>R 0 \<le> inverse a *\<^sub>R (a *\<^sub>R b)"
immler@54778
   606
        by (intro scaleR_mono) auto
wenzelm@63545
   607
      with 1 show ?thesis
immler@54778
   608
        by simp
wenzelm@63545
   609
    next
wenzelm@63545
   610
      case 2
wenzelm@63545
   611
      with \<open>?lhs\<close> have "- inverse a *\<^sub>R 0 \<le> - inverse a *\<^sub>R (a *\<^sub>R b)"
immler@54778
   612
        by (intro scaleR_mono) auto
wenzelm@63545
   613
      with 2 show ?thesis
immler@54778
   614
        by simp
wenzelm@63545
   615
    qed
wenzelm@63545
   616
  next
wenzelm@63545
   617
    assume ?rhs
wenzelm@63545
   618
    then show ?lhs
wenzelm@63545
   619
      by (auto simp: not_le \<open>a \<noteq> 0\<close> intro!: split_scaleR_pos_le)
wenzelm@63545
   620
  qed
wenzelm@63545
   621
qed
immler@54778
   622
wenzelm@63545
   623
lemma scaleR_le_0_iff: "a *\<^sub>R b \<le> 0 \<longleftrightarrow> 0 < a \<and> b \<le> 0 \<or> a < 0 \<and> 0 \<le> b \<or> a = 0"
wenzelm@63545
   624
  for b::"'a::ordered_real_vector"
immler@54778
   625
  by (insert zero_le_scaleR_iff [of "-a" b]) force
immler@54778
   626
wenzelm@63545
   627
lemma scaleR_le_cancel_left: "c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> (0 < c \<longrightarrow> a \<le> b) \<and> (c < 0 \<longrightarrow> b \<le> a)"
wenzelm@63545
   628
  for b :: "'a::ordered_real_vector"
immler@54778
   629
  by (auto simp add: neq_iff scaleR_left_mono scaleR_left_mono_neg
wenzelm@63545
   630
      dest: scaleR_left_mono[where a="inverse c"] scaleR_left_mono_neg[where c="inverse c"])
immler@54778
   631
wenzelm@63545
   632
lemma scaleR_le_cancel_left_pos: "0 < c \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> a \<le> b"
wenzelm@63545
   633
  for b :: "'a::ordered_real_vector"
immler@54778
   634
  by (auto simp: scaleR_le_cancel_left)
immler@54778
   635
wenzelm@63545
   636
lemma scaleR_le_cancel_left_neg: "c < 0 \<Longrightarrow> c *\<^sub>R a \<le> c *\<^sub>R b \<longleftrightarrow> b \<le> a"
wenzelm@63545
   637
  for b :: "'a::ordered_real_vector"
immler@54778
   638
  by (auto simp: scaleR_le_cancel_left)
immler@54778
   639
wenzelm@63545
   640
lemma scaleR_left_le_one_le: "0 \<le> x \<Longrightarrow> a \<le> 1 \<Longrightarrow> a *\<^sub>R x \<le> x"
wenzelm@63545
   641
  for x :: "'a::ordered_real_vector" and a :: real
immler@54778
   642
  using scaleR_right_mono[of a 1 x] by simp
immler@54778
   643
huffman@20504
   644
wenzelm@60758
   645
subsection \<open>Real normed vector spaces\<close>
huffman@20504
   646
hoelzl@51531
   647
class dist =
hoelzl@51531
   648
  fixes dist :: "'a \<Rightarrow> 'a \<Rightarrow> real"
hoelzl@51531
   649
haftmann@29608
   650
class norm =
huffman@22636
   651
  fixes norm :: "'a \<Rightarrow> real"
huffman@20504
   652
huffman@24520
   653
class sgn_div_norm = scaleR + norm + sgn +
haftmann@25062
   654
  assumes sgn_div_norm: "sgn x = x /\<^sub>R norm x"
nipkow@24506
   655
huffman@31289
   656
class dist_norm = dist + norm + minus +
huffman@31289
   657
  assumes dist_norm: "dist x y = norm (x - y)"
huffman@31289
   658
hoelzl@62101
   659
class uniformity_dist = dist + uniformity +
hoelzl@62101
   660
  assumes uniformity_dist: "uniformity = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
   661
begin
hoelzl@51531
   662
hoelzl@62101
   663
lemma eventually_uniformity_metric:
hoelzl@62101
   664
  "eventually P uniformity \<longleftrightarrow> (\<exists>e>0. \<forall>x y. dist x y < e \<longrightarrow> P (x, y))"
hoelzl@62101
   665
  unfolding uniformity_dist
hoelzl@62101
   666
  by (subst eventually_INF_base)
hoelzl@62101
   667
     (auto simp: eventually_principal subset_eq intro: bexI[of _ "min _ _"])
hoelzl@62101
   668
hoelzl@62101
   669
end
hoelzl@62101
   670
hoelzl@62101
   671
class real_normed_vector = real_vector + sgn_div_norm + dist_norm + uniformity_dist + open_uniformity +
hoelzl@51002
   672
  assumes norm_eq_zero [simp]: "norm x = 0 \<longleftrightarrow> x = 0"
wenzelm@63545
   673
    and norm_triangle_ineq: "norm (x + y) \<le> norm x + norm y"
wenzelm@63545
   674
    and norm_scaleR [simp]: "norm (scaleR a x) = \<bar>a\<bar> * norm x"
hoelzl@51002
   675
begin
hoelzl@51002
   676
hoelzl@51002
   677
lemma norm_ge_zero [simp]: "0 \<le> norm x"
hoelzl@51002
   678
proof -
lp15@60026
   679
  have "0 = norm (x + -1 *\<^sub>R x)"
hoelzl@51002
   680
    using scaleR_add_left[of 1 "-1" x] norm_scaleR[of 0 x] by (simp add: scaleR_one)
hoelzl@51002
   681
  also have "\<dots> \<le> norm x + norm (-1 *\<^sub>R x)" by (rule norm_triangle_ineq)
hoelzl@51002
   682
  finally show ?thesis by simp
hoelzl@51002
   683
qed
hoelzl@51002
   684
hoelzl@51002
   685
end
huffman@20504
   686
haftmann@24588
   687
class real_normed_algebra = real_algebra + real_normed_vector +
haftmann@25062
   688
  assumes norm_mult_ineq: "norm (x * y) \<le> norm x * norm y"
huffman@20504
   689
haftmann@24588
   690
class real_normed_algebra_1 = real_algebra_1 + real_normed_algebra +
haftmann@25062
   691
  assumes norm_one [simp]: "norm 1 = 1"
hoelzl@62101
   692
wenzelm@63545
   693
lemma (in real_normed_algebra_1) scaleR_power [simp]: "(scaleR x y) ^ n = scaleR (x^n) (y^n)"
wenzelm@63545
   694
  by (induct n) (simp_all add: scaleR_one scaleR_scaleR mult_ac)
huffman@22852
   695
haftmann@24588
   696
class real_normed_div_algebra = real_div_algebra + real_normed_vector +
haftmann@25062
   697
  assumes norm_mult: "norm (x * y) = norm x * norm y"
huffman@20504
   698
haftmann@24588
   699
class real_normed_field = real_field + real_normed_div_algebra
huffman@20584
   700
huffman@22852
   701
instance real_normed_div_algebra < real_normed_algebra_1
huffman@20554
   702
proof
wenzelm@63545
   703
  show "norm (x * y) \<le> norm x * norm y" for x y :: 'a
huffman@20554
   704
    by (simp add: norm_mult)
huffman@22852
   705
next
huffman@22852
   706
  have "norm (1 * 1::'a) = norm (1::'a) * norm (1::'a)"
huffman@22852
   707
    by (rule norm_mult)
wenzelm@63545
   708
  then show "norm (1::'a) = 1" by simp
huffman@20554
   709
qed
huffman@20554
   710
huffman@22852
   711
lemma norm_zero [simp]: "norm (0::'a::real_normed_vector) = 0"
wenzelm@63545
   712
  by simp
huffman@20504
   713
wenzelm@63545
   714
lemma zero_less_norm_iff [simp]: "norm x > 0 \<longleftrightarrow> x \<noteq> 0"
wenzelm@63545
   715
  for x :: "'a::real_normed_vector"
wenzelm@63545
   716
  by (simp add: order_less_le)
huffman@20504
   717
wenzelm@63545
   718
lemma norm_not_less_zero [simp]: "\<not> norm x < 0"
wenzelm@63545
   719
  for x :: "'a::real_normed_vector"
wenzelm@63545
   720
  by (simp add: linorder_not_less)
huffman@20828
   721
wenzelm@63545
   722
lemma norm_le_zero_iff [simp]: "norm x \<le> 0 \<longleftrightarrow> x = 0"
wenzelm@63545
   723
  for x :: "'a::real_normed_vector"
wenzelm@63545
   724
  by (simp add: order_le_less)
huffman@20828
   725
wenzelm@63545
   726
lemma norm_minus_cancel [simp]: "norm (- x) = norm x"
wenzelm@63545
   727
  for x :: "'a::real_normed_vector"
huffman@20504
   728
proof -
huffman@21809
   729
  have "norm (- x) = norm (scaleR (- 1) x)"
huffman@20504
   730
    by (simp only: scaleR_minus_left scaleR_one)
huffman@20533
   731
  also have "\<dots> = \<bar>- 1\<bar> * norm x"
huffman@20504
   732
    by (rule norm_scaleR)
huffman@20504
   733
  finally show ?thesis by simp
huffman@20504
   734
qed
huffman@20504
   735
wenzelm@63545
   736
lemma norm_minus_commute: "norm (a - b) = norm (b - a)"
wenzelm@63545
   737
  for a b :: "'a::real_normed_vector"
huffman@20504
   738
proof -
huffman@22898
   739
  have "norm (- (b - a)) = norm (b - a)"
huffman@22898
   740
    by (rule norm_minus_cancel)
wenzelm@63545
   741
  then show ?thesis by simp
huffman@20504
   742
qed
wenzelm@63545
   743
wenzelm@63545
   744
lemma dist_add_cancel [simp]: "dist (a + b) (a + c) = dist b c"
wenzelm@63545
   745
  for a :: "'a::real_normed_vector"
wenzelm@63545
   746
  by (simp add: dist_norm)
lp15@63114
   747
wenzelm@63545
   748
lemma dist_add_cancel2 [simp]: "dist (b + a) (c + a) = dist b c"
wenzelm@63545
   749
  for a :: "'a::real_normed_vector"
wenzelm@63545
   750
  by (simp add: dist_norm)
lp15@63114
   751
wenzelm@63545
   752
lemma dist_scaleR [simp]: "dist (x *\<^sub>R a) (y *\<^sub>R a) = \<bar>x - y\<bar> * norm a"
wenzelm@63545
   753
  for a :: "'a::real_normed_vector"
wenzelm@63545
   754
  by (metis dist_norm norm_scaleR scaleR_left.diff)
huffman@20504
   755
wenzelm@63545
   756
lemma norm_uminus_minus: "norm (- x - y :: 'a :: real_normed_vector) = norm (x + y)"
eberlm@61524
   757
  by (subst (2) norm_minus_cancel[symmetric], subst minus_add_distrib) simp
eberlm@61524
   758
wenzelm@63545
   759
lemma norm_triangle_ineq2: "norm a - norm b \<le> norm (a - b)"
wenzelm@63545
   760
  for a b :: "'a::real_normed_vector"
huffman@20504
   761
proof -
huffman@20533
   762
  have "norm (a - b + b) \<le> norm (a - b) + norm b"
huffman@20504
   763
    by (rule norm_triangle_ineq)
wenzelm@63545
   764
  then show ?thesis by simp
huffman@20504
   765
qed
huffman@20504
   766
wenzelm@63545
   767
lemma norm_triangle_ineq3: "\<bar>norm a - norm b\<bar> \<le> norm (a - b)"
wenzelm@63545
   768
  for a b :: "'a::real_normed_vector"
wenzelm@63545
   769
  apply (subst abs_le_iff)
wenzelm@63545
   770
  apply auto
wenzelm@63545
   771
   apply (rule norm_triangle_ineq2)
wenzelm@63545
   772
  apply (subst norm_minus_commute)
wenzelm@63545
   773
  apply (rule norm_triangle_ineq2)
wenzelm@63545
   774
  done
huffman@20584
   775
wenzelm@63545
   776
lemma norm_triangle_ineq4: "norm (a - b) \<le> norm a + norm b"
wenzelm@63545
   777
  for a b :: "'a::real_normed_vector"
huffman@20504
   778
proof -
huffman@22898
   779
  have "norm (a + - b) \<le> norm a + norm (- b)"
huffman@20504
   780
    by (rule norm_triangle_ineq)
haftmann@54230
   781
  then show ?thesis by simp
huffman@22898
   782
qed
huffman@22898
   783
wenzelm@63545
   784
lemma norm_diff_ineq: "norm a - norm b \<le> norm (a + b)"
wenzelm@63545
   785
  for a b :: "'a::real_normed_vector"
huffman@22898
   786
proof -
huffman@22898
   787
  have "norm a - norm (- b) \<le> norm (a - - b)"
huffman@22898
   788
    by (rule norm_triangle_ineq2)
wenzelm@63545
   789
  then show ?thesis by simp
huffman@20504
   790
qed
huffman@20504
   791
wenzelm@63545
   792
lemma norm_add_leD: "norm (a + b) \<le> c \<Longrightarrow> norm b \<le> norm a + c"
wenzelm@63545
   793
  for a b :: "'a::real_normed_vector"
wenzelm@63545
   794
  by (metis add.commute diff_le_eq norm_diff_ineq order.trans)
lp15@61762
   795
wenzelm@63545
   796
lemma norm_diff_triangle_ineq: "norm ((a + b) - (c + d)) \<le> norm (a - c) + norm (b - d)"
wenzelm@63545
   797
  for a b c d :: "'a::real_normed_vector"
huffman@20551
   798
proof -
huffman@20551
   799
  have "norm ((a + b) - (c + d)) = norm ((a - c) + (b - d))"
haftmann@54230
   800
    by (simp add: algebra_simps)
huffman@20551
   801
  also have "\<dots> \<le> norm (a - c) + norm (b - d)"
huffman@20551
   802
    by (rule norm_triangle_ineq)
huffman@20551
   803
  finally show ?thesis .
huffman@20551
   804
qed
huffman@20551
   805
lp15@60800
   806
lemma norm_diff_triangle_le:
lp15@60800
   807
  fixes x y z :: "'a::real_normed_vector"
lp15@60800
   808
  assumes "norm (x - y) \<le> e1"  "norm (y - z) \<le> e2"
wenzelm@63545
   809
  shows "norm (x - z) \<le> e1 + e2"
lp15@60800
   810
  using norm_diff_triangle_ineq [of x y y z] assms by simp
lp15@60800
   811
lp15@60800
   812
lemma norm_diff_triangle_less:
lp15@60800
   813
  fixes x y z :: "'a::real_normed_vector"
lp15@60800
   814
  assumes "norm (x - y) < e1"  "norm (y - z) < e2"
wenzelm@63545
   815
  shows "norm (x - z) < e1 + e2"
lp15@60800
   816
  using norm_diff_triangle_ineq [of x y y z] assms by simp
lp15@60800
   817
lp15@60026
   818
lemma norm_triangle_mono:
lp15@55719
   819
  fixes a b :: "'a::real_normed_vector"
wenzelm@63545
   820
  shows "norm a \<le> r \<Longrightarrow> norm b \<le> s \<Longrightarrow> norm (a + b) \<le> r + s"
wenzelm@63545
   821
  by (metis add_mono_thms_linordered_semiring(1) norm_triangle_ineq order.trans)
lp15@55719
   822
hoelzl@56194
   823
lemma norm_setsum:
hoelzl@56194
   824
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56194
   825
  shows "norm (setsum f A) \<le> (\<Sum>i\<in>A. norm (f i))"
hoelzl@56194
   826
  by (induct A rule: infinite_finite_induct) (auto intro: norm_triangle_mono)
hoelzl@56194
   827
hoelzl@56369
   828
lemma setsum_norm_le:
hoelzl@56369
   829
  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
hoelzl@56369
   830
  assumes fg: "\<forall>x \<in> S. norm (f x) \<le> g x"
hoelzl@56369
   831
  shows "norm (setsum f S) \<le> setsum g S"
hoelzl@56369
   832
  by (rule order_trans [OF norm_setsum setsum_mono]) (simp add: fg)
hoelzl@56369
   833
wenzelm@63545
   834
lemma abs_norm_cancel [simp]: "\<bar>norm a\<bar> = norm a"
wenzelm@63545
   835
  for a :: "'a::real_normed_vector"
wenzelm@63545
   836
  by (rule abs_of_nonneg [OF norm_ge_zero])
huffman@22857
   837
wenzelm@63545
   838
lemma norm_add_less: "norm x < r \<Longrightarrow> norm y < s \<Longrightarrow> norm (x + y) < r + s"
wenzelm@63545
   839
  for x y :: "'a::real_normed_vector"
wenzelm@63545
   840
  by (rule order_le_less_trans [OF norm_triangle_ineq add_strict_mono])
huffman@22880
   841
wenzelm@63545
   842
lemma norm_mult_less: "norm x < r \<Longrightarrow> norm y < s \<Longrightarrow> norm (x * y) < r * s"
wenzelm@63545
   843
  for x y :: "'a::real_normed_algebra"
wenzelm@63545
   844
  by (rule order_le_less_trans [OF norm_mult_ineq]) (simp add: mult_strict_mono')
huffman@22880
   845
wenzelm@63545
   846
lemma norm_of_real [simp]: "norm (of_real r :: 'a::real_normed_algebra_1) = \<bar>r\<bar>"
wenzelm@63545
   847
  by (simp add: of_real_def)
huffman@20560
   848
wenzelm@63545
   849
lemma norm_numeral [simp]: "norm (numeral w::'a::real_normed_algebra_1) = numeral w"
wenzelm@63545
   850
  by (subst of_real_numeral [symmetric], subst norm_of_real, simp)
huffman@47108
   851
wenzelm@63545
   852
lemma norm_neg_numeral [simp]: "norm (- numeral w::'a::real_normed_algebra_1) = numeral w"
wenzelm@63545
   853
  by (subst of_real_neg_numeral [symmetric], subst norm_of_real, simp)
huffman@22876
   854
wenzelm@63545
   855
lemma norm_of_real_add1 [simp]: "norm (of_real x + 1 :: 'a :: real_normed_div_algebra) = \<bar>x + 1\<bar>"
lp15@62379
   856
  by (metis norm_of_real of_real_1 of_real_add)
lp15@62379
   857
lp15@62379
   858
lemma norm_of_real_addn [simp]:
wenzelm@63545
   859
  "norm (of_real x + numeral b :: 'a :: real_normed_div_algebra) = \<bar>x + numeral b\<bar>"
lp15@62379
   860
  by (metis norm_of_real of_real_add of_real_numeral)
lp15@62379
   861
wenzelm@63545
   862
lemma norm_of_int [simp]: "norm (of_int z::'a::real_normed_algebra_1) = \<bar>of_int z\<bar>"
wenzelm@63545
   863
  by (subst of_real_of_int_eq [symmetric], rule norm_of_real)
huffman@22876
   864
wenzelm@63545
   865
lemma norm_of_nat [simp]: "norm (of_nat n::'a::real_normed_algebra_1) = of_nat n"
wenzelm@63545
   866
  apply (subst of_real_of_nat_eq [symmetric])
wenzelm@63545
   867
  apply (subst norm_of_real, simp)
wenzelm@63545
   868
  done
huffman@22876
   869
wenzelm@63545
   870
lemma nonzero_norm_inverse: "a \<noteq> 0 \<Longrightarrow> norm (inverse a) = inverse (norm a)"
wenzelm@63545
   871
  for a :: "'a::real_normed_div_algebra"
wenzelm@63545
   872
  apply (rule inverse_unique [symmetric])
wenzelm@63545
   873
  apply (simp add: norm_mult [symmetric])
wenzelm@63545
   874
  done
huffman@20504
   875
wenzelm@63545
   876
lemma norm_inverse: "norm (inverse a) = inverse (norm a)"
wenzelm@63545
   877
  for a :: "'a::{real_normed_div_algebra,division_ring}"
wenzelm@63545
   878
  apply (cases "a = 0")
wenzelm@63545
   879
   apply simp
wenzelm@63545
   880
  apply (erule nonzero_norm_inverse)
wenzelm@63545
   881
  done
huffman@20504
   882
wenzelm@63545
   883
lemma nonzero_norm_divide: "b \<noteq> 0 \<Longrightarrow> norm (a / b) = norm a / norm b"
wenzelm@63545
   884
  for a b :: "'a::real_normed_field"
wenzelm@63545
   885
  by (simp add: divide_inverse norm_mult nonzero_norm_inverse)
huffman@20584
   886
wenzelm@63545
   887
lemma norm_divide: "norm (a / b) = norm a / norm b"
wenzelm@63545
   888
  for a b :: "'a::{real_normed_field,field}"
wenzelm@63545
   889
  by (simp add: divide_inverse norm_mult norm_inverse)
huffman@20584
   890
wenzelm@63545
   891
lemma norm_power_ineq: "norm (x ^ n) \<le> norm x ^ n"
wenzelm@63545
   892
  for x :: "'a::real_normed_algebra_1"
huffman@22852
   893
proof (induct n)
wenzelm@63545
   894
  case 0
wenzelm@63545
   895
  show "norm (x ^ 0) \<le> norm x ^ 0" by simp
huffman@22852
   896
next
huffman@22852
   897
  case (Suc n)
huffman@22852
   898
  have "norm (x * x ^ n) \<le> norm x * norm (x ^ n)"
huffman@22852
   899
    by (rule norm_mult_ineq)
huffman@22852
   900
  also from Suc have "\<dots> \<le> norm x * norm x ^ n"
huffman@22852
   901
    using norm_ge_zero by (rule mult_left_mono)
huffman@22852
   902
  finally show "norm (x ^ Suc n) \<le> norm x ^ Suc n"
huffman@30273
   903
    by simp
huffman@22852
   904
qed
huffman@22852
   905
wenzelm@63545
   906
lemma norm_power: "norm (x ^ n) = norm x ^ n"
wenzelm@63545
   907
  for x :: "'a::real_normed_div_algebra"
wenzelm@63545
   908
  by (induct n) (simp_all add: norm_mult)
huffman@20684
   909
lp15@62948
   910
lemma power_eq_imp_eq_norm:
lp15@62948
   911
  fixes w :: "'a::real_normed_div_algebra"
lp15@62948
   912
  assumes eq: "w ^ n = z ^ n" and "n > 0"
lp15@62948
   913
    shows "norm w = norm z"
lp15@62948
   914
proof -
lp15@62948
   915
  have "norm w ^ n = norm z ^ n"
lp15@62948
   916
    by (metis (no_types) eq norm_power)
lp15@62948
   917
  then show ?thesis
lp15@62948
   918
    using assms by (force intro: power_eq_imp_eq_base)
lp15@62948
   919
qed
lp15@62948
   920
wenzelm@63545
   921
lemma norm_mult_numeral1 [simp]: "norm (numeral w * a) = numeral w * norm a"
wenzelm@63545
   922
  for a b :: "'a::{real_normed_field,field}"
wenzelm@63545
   923
  by (simp add: norm_mult)
paulson@60762
   924
wenzelm@63545
   925
lemma norm_mult_numeral2 [simp]: "norm (a * numeral w) = norm a * numeral w"
wenzelm@63545
   926
  for a b :: "'a::{real_normed_field,field}"
wenzelm@63545
   927
  by (simp add: norm_mult)
paulson@60762
   928
wenzelm@63545
   929
lemma norm_divide_numeral [simp]: "norm (a / numeral w) = norm a / numeral w"
wenzelm@63545
   930
  for a b :: "'a::{real_normed_field,field}"
wenzelm@63545
   931
  by (simp add: norm_divide)
paulson@60762
   932
paulson@60762
   933
lemma norm_of_real_diff [simp]:
wenzelm@63545
   934
  "norm (of_real b - of_real a :: 'a::real_normed_algebra_1) \<le> \<bar>b - a\<bar>"
paulson@60762
   935
  by (metis norm_of_real of_real_diff order_refl)
paulson@60762
   936
wenzelm@63545
   937
text \<open>Despite a superficial resemblance, \<open>norm_eq_1\<close> is not relevant.\<close>
lp15@59613
   938
lemma square_norm_one:
lp15@59613
   939
  fixes x :: "'a::real_normed_div_algebra"
wenzelm@63545
   940
  assumes "x\<^sup>2 = 1"
wenzelm@63545
   941
  shows "norm x = 1"
lp15@59613
   942
  by (metis assms norm_minus_cancel norm_one power2_eq_1_iff)
lp15@59613
   943
wenzelm@63545
   944
lemma norm_less_p1: "norm x < norm (of_real (norm x) + 1 :: 'a)"
wenzelm@63545
   945
  for x :: "'a::real_normed_algebra_1"
lp15@59658
   946
proof -
lp15@59658
   947
  have "norm x < norm (of_real (norm x + 1) :: 'a)"
lp15@59658
   948
    by (simp add: of_real_def)
lp15@59658
   949
  then show ?thesis
lp15@59658
   950
    by simp
lp15@59658
   951
qed
lp15@59658
   952
wenzelm@63545
   953
lemma setprod_norm: "setprod (\<lambda>x. norm (f x)) A = norm (setprod f A)"
wenzelm@63545
   954
  for f :: "'a \<Rightarrow> 'b::{comm_semiring_1,real_normed_div_algebra}"
hoelzl@57275
   955
  by (induct A rule: infinite_finite_induct) (auto simp: norm_mult)
hoelzl@57275
   956
lp15@60026
   957
lemma norm_setprod_le:
wenzelm@63545
   958
  "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a :: 'a :: {real_normed_algebra_1,comm_monoid_mult}))"
wenzelm@63545
   959
proof (induct A rule: infinite_finite_induct)
wenzelm@63545
   960
  case empty
wenzelm@63545
   961
  then show ?case by simp
wenzelm@63545
   962
next
hoelzl@57275
   963
  case (insert a A)
hoelzl@57275
   964
  then have "norm (setprod f (insert a A)) \<le> norm (f a) * norm (setprod f A)"
hoelzl@57275
   965
    by (simp add: norm_mult_ineq)
hoelzl@57275
   966
  also have "norm (setprod f A) \<le> (\<Prod>a\<in>A. norm (f a))"
hoelzl@57275
   967
    by (rule insert)
hoelzl@57275
   968
  finally show ?case
hoelzl@57275
   969
    by (simp add: insert mult_left_mono)
wenzelm@63545
   970
next
wenzelm@63545
   971
  case infinite
wenzelm@63545
   972
  then show ?case by simp
wenzelm@63545
   973
qed
hoelzl@57275
   974
hoelzl@57275
   975
lemma norm_setprod_diff:
hoelzl@57275
   976
  fixes z w :: "'i \<Rightarrow> 'a::{real_normed_algebra_1, comm_monoid_mult}"
hoelzl@57275
   977
  shows "(\<And>i. i \<in> I \<Longrightarrow> norm (z i) \<le> 1) \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> norm (w i) \<le> 1) \<Longrightarrow>
lp15@60026
   978
    norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) \<le> (\<Sum>i\<in>I. norm (z i - w i))"
hoelzl@57275
   979
proof (induction I rule: infinite_finite_induct)
wenzelm@63545
   980
  case empty
wenzelm@63545
   981
  then show ?case by simp
wenzelm@63545
   982
next
hoelzl@57275
   983
  case (insert i I)
hoelzl@57275
   984
  note insert.hyps[simp]
hoelzl@57275
   985
hoelzl@57275
   986
  have "norm ((\<Prod>i\<in>insert i I. z i) - (\<Prod>i\<in>insert i I. w i)) =
hoelzl@57275
   987
    norm ((\<Prod>i\<in>I. z i) * (z i - w i) + ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) * w i)"
hoelzl@57275
   988
    (is "_ = norm (?t1 + ?t2)")
hoelzl@57275
   989
    by (auto simp add: field_simps)
wenzelm@63545
   990
  also have "\<dots> \<le> norm ?t1 + norm ?t2"
hoelzl@57275
   991
    by (rule norm_triangle_ineq)
hoelzl@57275
   992
  also have "norm ?t1 \<le> norm (\<Prod>i\<in>I. z i) * norm (z i - w i)"
hoelzl@57275
   993
    by (rule norm_mult_ineq)
hoelzl@57275
   994
  also have "\<dots> \<le> (\<Prod>i\<in>I. norm (z i)) * norm(z i - w i)"
hoelzl@57275
   995
    by (rule mult_right_mono) (auto intro: norm_setprod_le)
hoelzl@57275
   996
  also have "(\<Prod>i\<in>I. norm (z i)) \<le> (\<Prod>i\<in>I. 1)"
hoelzl@57275
   997
    by (intro setprod_mono) (auto intro!: insert)
hoelzl@57275
   998
  also have "norm ?t2 \<le> norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) * norm (w i)"
hoelzl@57275
   999
    by (rule norm_mult_ineq)
hoelzl@57275
  1000
  also have "norm (w i) \<le> 1"
hoelzl@57275
  1001
    by (auto intro: insert)
hoelzl@57275
  1002
  also have "norm ((\<Prod>i\<in>I. z i) - (\<Prod>i\<in>I. w i)) \<le> (\<Sum>i\<in>I. norm (z i - w i))"
hoelzl@57275
  1003
    using insert by auto
hoelzl@57275
  1004
  finally show ?case
haftmann@57514
  1005
    by (auto simp add: ac_simps mult_right_mono mult_left_mono)
wenzelm@63545
  1006
next
wenzelm@63545
  1007
  case infinite
wenzelm@63545
  1008
  then show ?case by simp
wenzelm@63545
  1009
qed
hoelzl@57275
  1010
lp15@60026
  1011
lemma norm_power_diff:
hoelzl@57275
  1012
  fixes z w :: "'a::{real_normed_algebra_1, comm_monoid_mult}"
hoelzl@57275
  1013
  assumes "norm z \<le> 1" "norm w \<le> 1"
hoelzl@57275
  1014
  shows "norm (z^m - w^m) \<le> m * norm (z - w)"
hoelzl@57275
  1015
proof -
hoelzl@57275
  1016
  have "norm (z^m - w^m) = norm ((\<Prod> i < m. z) - (\<Prod> i < m. w))"
hoelzl@57275
  1017
    by (simp add: setprod_constant)
hoelzl@57275
  1018
  also have "\<dots> \<le> (\<Sum>i<m. norm (z - w))"
hoelzl@57275
  1019
    by (intro norm_setprod_diff) (auto simp add: assms)
hoelzl@57275
  1020
  also have "\<dots> = m * norm (z - w)"
lp15@61609
  1021
    by simp
hoelzl@57275
  1022
  finally show ?thesis .
lp15@55719
  1023
qed
lp15@55719
  1024
wenzelm@63545
  1025
wenzelm@60758
  1026
subsection \<open>Metric spaces\<close>
hoelzl@51531
  1027
hoelzl@62101
  1028
class metric_space = uniformity_dist + open_uniformity +
hoelzl@51531
  1029
  assumes dist_eq_0_iff [simp]: "dist x y = 0 \<longleftrightarrow> x = y"
wenzelm@63545
  1030
    and dist_triangle2: "dist x y \<le> dist x z + dist y z"
hoelzl@51531
  1031
begin
hoelzl@51531
  1032
hoelzl@51531
  1033
lemma dist_self [simp]: "dist x x = 0"
wenzelm@63545
  1034
  by simp
hoelzl@51531
  1035
hoelzl@51531
  1036
lemma zero_le_dist [simp]: "0 \<le> dist x y"
wenzelm@63545
  1037
  using dist_triangle2 [of x x y] by simp
hoelzl@51531
  1038
hoelzl@51531
  1039
lemma zero_less_dist_iff: "0 < dist x y \<longleftrightarrow> x \<noteq> y"
wenzelm@63545
  1040
  by (simp add: less_le)
hoelzl@51531
  1041
hoelzl@51531
  1042
lemma dist_not_less_zero [simp]: "\<not> dist x y < 0"
wenzelm@63545
  1043
  by (simp add: not_less)
hoelzl@51531
  1044
hoelzl@51531
  1045
lemma dist_le_zero_iff [simp]: "dist x y \<le> 0 \<longleftrightarrow> x = y"
wenzelm@63545
  1046
  by (simp add: le_less)
hoelzl@51531
  1047
hoelzl@51531
  1048
lemma dist_commute: "dist x y = dist y x"
hoelzl@51531
  1049
proof (rule order_antisym)
hoelzl@51531
  1050
  show "dist x y \<le> dist y x"
hoelzl@51531
  1051
    using dist_triangle2 [of x y x] by simp
hoelzl@51531
  1052
  show "dist y x \<le> dist x y"
hoelzl@51531
  1053
    using dist_triangle2 [of y x y] by simp
hoelzl@51531
  1054
qed
hoelzl@51531
  1055
lp15@62533
  1056
lemma dist_commute_lessI: "dist y x < e \<Longrightarrow> dist x y < e"
lp15@62533
  1057
  by (simp add: dist_commute)
lp15@62533
  1058
hoelzl@51531
  1059
lemma dist_triangle: "dist x z \<le> dist x y + dist y z"
lp15@62533
  1060
  using dist_triangle2 [of x z y] by (simp add: dist_commute)
hoelzl@51531
  1061
hoelzl@51531
  1062
lemma dist_triangle3: "dist x y \<le> dist a x + dist a y"
lp15@62533
  1063
  using dist_triangle2 [of x y a] by (simp add: dist_commute)
hoelzl@51531
  1064
wenzelm@63545
  1065
lemma dist_pos_lt: "x \<noteq> y \<Longrightarrow> 0 < dist x y"
wenzelm@63545
  1066
  by (simp add: zero_less_dist_iff)
hoelzl@51531
  1067
wenzelm@63545
  1068
lemma dist_nz: "x \<noteq> y \<longleftrightarrow> 0 < dist x y"
wenzelm@63545
  1069
  by (simp add: zero_less_dist_iff)
hoelzl@51531
  1070
paulson@62087
  1071
declare dist_nz [symmetric, simp]
paulson@62087
  1072
wenzelm@63545
  1073
lemma dist_triangle_le: "dist x z + dist y z \<le> e \<Longrightarrow> dist x y \<le> e"
wenzelm@63545
  1074
  by (rule order_trans [OF dist_triangle2])
hoelzl@51531
  1075
wenzelm@63545
  1076
lemma dist_triangle_lt: "dist x z + dist y z < e \<Longrightarrow> dist x y < e"
wenzelm@63545
  1077
  by (rule le_less_trans [OF dist_triangle2])
hoelzl@51531
  1078
wenzelm@63545
  1079
lemma dist_triangle_less_add: "dist x1 y < e1 \<Longrightarrow> dist x2 y < e2 \<Longrightarrow> dist x1 x2 < e1 + e2"
wenzelm@63545
  1080
  by (rule dist_triangle_lt [where z=y]) simp
lp15@62948
  1081
wenzelm@63545
  1082
lemma dist_triangle_half_l: "dist x1 y < e / 2 \<Longrightarrow> dist x2 y < e / 2 \<Longrightarrow> dist x1 x2 < e"
wenzelm@63545
  1083
  by (rule dist_triangle_lt [where z=y]) simp
hoelzl@51531
  1084
wenzelm@63545
  1085
lemma dist_triangle_half_r: "dist y x1 < e / 2 \<Longrightarrow> dist y x2 < e / 2 \<Longrightarrow> dist x1 x2 < e"
wenzelm@63545
  1086
  by (rule dist_triangle_half_l) (simp_all add: dist_commute)
hoelzl@51531
  1087
hoelzl@62101
  1088
subclass uniform_space
hoelzl@51531
  1089
proof
wenzelm@63545
  1090
  fix E x
wenzelm@63545
  1091
  assume "eventually E uniformity"
hoelzl@62101
  1092
  then obtain e where E: "0 < e" "\<And>x y. dist x y < e \<Longrightarrow> E (x, y)"
wenzelm@63545
  1093
    by (auto simp: eventually_uniformity_metric)
hoelzl@62101
  1094
  then show "E (x, x)" "\<forall>\<^sub>F (x, y) in uniformity. E (y, x)"
wenzelm@63545
  1095
    by (auto simp: eventually_uniformity_metric dist_commute)
hoelzl@62101
  1096
  show "\<exists>D. eventually D uniformity \<and> (\<forall>x y z. D (x, y) \<longrightarrow> D (y, z) \<longrightarrow> E (x, z))"
wenzelm@63545
  1097
    using E dist_triangle_half_l[where e=e]
wenzelm@63545
  1098
    unfolding eventually_uniformity_metric
hoelzl@62101
  1099
    by (intro exI[of _ "\<lambda>(x, y). dist x y < e / 2"] exI[of _ "e/2"] conjI)
wenzelm@63545
  1100
      (auto simp: dist_commute)
hoelzl@51531
  1101
qed
hoelzl@51531
  1102
hoelzl@62101
  1103
lemma open_dist: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
wenzelm@63545
  1104
  by (simp add: dist_commute open_uniformity eventually_uniformity_metric)
hoelzl@62101
  1105
hoelzl@51531
  1106
lemma open_ball: "open {y. dist x y < d}"
wenzelm@63545
  1107
  unfolding open_dist
wenzelm@63545
  1108
proof (intro ballI)
wenzelm@63545
  1109
  fix y
wenzelm@63545
  1110
  assume *: "y \<in> {y. dist x y < d}"
hoelzl@51531
  1111
  then show "\<exists>e>0. \<forall>z. dist z y < e \<longrightarrow> z \<in> {y. dist x y < d}"
hoelzl@51531
  1112
    by (auto intro!: exI[of _ "d - dist x y"] simp: field_simps dist_triangle_lt)
hoelzl@51531
  1113
qed
hoelzl@51531
  1114
hoelzl@51531
  1115
subclass first_countable_topology
hoelzl@51531
  1116
proof
lp15@60026
  1117
  fix x
hoelzl@51531
  1118
  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
  1119
  proof (safe intro!: exI[of _ "\<lambda>n. {y. dist x y < inverse (Suc n)}"])
wenzelm@63545
  1120
    fix S
wenzelm@63545
  1121
    assume "open S" "x \<in> S"
wenzelm@53374
  1122
    then obtain e where e: "0 < e" and "{y. dist x y < e} \<subseteq> S"
hoelzl@51531
  1123
      by (auto simp: open_dist subset_eq dist_commute)
hoelzl@51531
  1124
    moreover
wenzelm@53374
  1125
    from e obtain i where "inverse (Suc i) < e"
hoelzl@51531
  1126
      by (auto dest!: reals_Archimedean)
hoelzl@51531
  1127
    then have "{y. dist x y < inverse (Suc i)} \<subseteq> {y. dist x y < e}"
hoelzl@51531
  1128
      by auto
hoelzl@51531
  1129
    ultimately show "\<exists>i. {y. dist x y < inverse (Suc i)} \<subseteq> S"
hoelzl@51531
  1130
      by blast
hoelzl@51531
  1131
  qed (auto intro: open_ball)
hoelzl@51531
  1132
qed
hoelzl@51531
  1133
hoelzl@51531
  1134
end
hoelzl@51531
  1135
hoelzl@51531
  1136
instance metric_space \<subseteq> t2_space
hoelzl@51531
  1137
proof
hoelzl@51531
  1138
  fix x y :: "'a::metric_space"
hoelzl@51531
  1139
  assume xy: "x \<noteq> y"
hoelzl@51531
  1140
  let ?U = "{y'. dist x y' < dist x y / 2}"
hoelzl@51531
  1141
  let ?V = "{x'. dist y x' < dist x y / 2}"
wenzelm@63545
  1142
  have *: "d x z \<le> d x y + d y z \<Longrightarrow> d y z = d z y \<Longrightarrow> \<not> (d x y * 2 < d x z \<and> d z y * 2 < d x z)"
wenzelm@63545
  1143
    for d :: "'a \<Rightarrow> 'a \<Rightarrow> real" and x y z :: 'a
wenzelm@63545
  1144
    by arith
hoelzl@51531
  1145
  have "open ?U \<and> open ?V \<and> x \<in> ?U \<and> y \<in> ?V \<and> ?U \<inter> ?V = {}"
wenzelm@63545
  1146
    using dist_pos_lt[OF xy] *[of dist, OF dist_triangle dist_commute]
hoelzl@51531
  1147
    using open_ball[of _ "dist x y / 2"] by auto
hoelzl@51531
  1148
  then show "\<exists>U V. open U \<and> open V \<and> x \<in> U \<and> y \<in> V \<and> U \<inter> V = {}"
hoelzl@51531
  1149
    by blast
hoelzl@51531
  1150
qed
hoelzl@51531
  1151
wenzelm@60758
  1152
text \<open>Every normed vector space is a metric space.\<close>
huffman@31289
  1153
instance real_normed_vector < metric_space
huffman@31289
  1154
proof
wenzelm@63545
  1155
  fix x y z :: 'a
wenzelm@63545
  1156
  show "dist x y = 0 \<longleftrightarrow> x = y"
wenzelm@63545
  1157
    by (simp add: dist_norm)
wenzelm@63545
  1158
  show "dist x y \<le> dist x z + dist y z"
wenzelm@63545
  1159
    using norm_triangle_ineq4 [of "x - z" "y - z"] by (simp add: dist_norm)
huffman@31289
  1160
qed
huffman@31285
  1161
wenzelm@63545
  1162
wenzelm@60758
  1163
subsection \<open>Class instances for real numbers\<close>
huffman@31564
  1164
huffman@31564
  1165
instantiation real :: real_normed_field
huffman@31564
  1166
begin
huffman@31564
  1167
wenzelm@63545
  1168
definition dist_real_def: "dist x y = \<bar>x - y\<bar>"
hoelzl@51531
  1169
hoelzl@62101
  1170
definition uniformity_real_def [code del]:
hoelzl@62101
  1171
  "(uniformity :: (real \<times> real) filter) = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
hoelzl@62101
  1172
haftmann@52381
  1173
definition open_real_def [code del]:
hoelzl@62101
  1174
  "open (U :: real set) \<longleftrightarrow> (\<forall>x\<in>U. eventually (\<lambda>(x', y). x' = x \<longrightarrow> y \<in> U) uniformity)"
hoelzl@51531
  1175
wenzelm@63545
  1176
definition real_norm_def [simp]: "norm r = \<bar>r\<bar>"
huffman@31564
  1177
huffman@31564
  1178
instance
wenzelm@63545
  1179
  apply intro_classes
wenzelm@63545
  1180
         apply (unfold real_norm_def real_scaleR_def)
wenzelm@63545
  1181
         apply (rule dist_real_def)
wenzelm@63545
  1182
        apply (simp add: sgn_real_def)
wenzelm@63545
  1183
       apply (rule uniformity_real_def)
wenzelm@63545
  1184
      apply (rule open_real_def)
wenzelm@63545
  1185
     apply (rule abs_eq_0)
wenzelm@63545
  1186
    apply (rule abs_triangle_ineq)
wenzelm@63545
  1187
   apply (rule abs_mult)
wenzelm@63545
  1188
  apply (rule abs_mult)
wenzelm@63545
  1189
  done
huffman@31564
  1190
huffman@31564
  1191
end
huffman@31564
  1192
hoelzl@62102
  1193
declare uniformity_Abort[where 'a=real, code]
hoelzl@62102
  1194
wenzelm@63545
  1195
lemma dist_of_real [simp]: "dist (of_real x :: 'a) (of_real y) = dist x y"
wenzelm@63545
  1196
  for a :: "'a::real_normed_div_algebra"
wenzelm@63545
  1197
  by (metis dist_norm norm_of_real of_real_diff real_norm_def)
lp15@60800
  1198
haftmann@54890
  1199
declare [[code abort: "open :: real set \<Rightarrow> bool"]]
haftmann@52381
  1200
hoelzl@51531
  1201
instance real :: linorder_topology
hoelzl@51531
  1202
proof
hoelzl@51531
  1203
  show "(open :: real set \<Rightarrow> bool) = generate_topology (range lessThan \<union> range greaterThan)"
hoelzl@51531
  1204
  proof (rule ext, safe)
wenzelm@63545
  1205
    fix S :: "real set"
wenzelm@63545
  1206
    assume "open S"
wenzelm@53381
  1207
    then obtain f where "\<forall>x\<in>S. 0 < f x \<and> (\<forall>y. dist y x < f x \<longrightarrow> y \<in> S)"
hoelzl@62101
  1208
      unfolding open_dist bchoice_iff ..
hoelzl@51531
  1209
    then have *: "S = (\<Union>x\<in>S. {x - f x <..} \<inter> {..< x + f x})"
hoelzl@51531
  1210
      by (fastforce simp: dist_real_def)
hoelzl@51531
  1211
    show "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51531
  1212
      apply (subst *)
hoelzl@51531
  1213
      apply (intro generate_topology_Union generate_topology.Int)
wenzelm@63545
  1214
       apply (auto intro: generate_topology.Basis)
hoelzl@51531
  1215
      done
hoelzl@51531
  1216
  next
wenzelm@63545
  1217
    fix S :: "real set"
wenzelm@63545
  1218
    assume "generate_topology (range lessThan \<union> range greaterThan) S"
hoelzl@51531
  1219
    moreover have "\<And>a::real. open {..<a}"
hoelzl@62101
  1220
      unfolding open_dist dist_real_def
hoelzl@51531
  1221
    proof clarify
wenzelm@63545
  1222
      fix x a :: real
wenzelm@63545
  1223
      assume "x < a"
wenzelm@63545
  1224
      then have "0 < a - x \<and> (\<forall>y. \<bar>y - x\<bar> < a - x \<longrightarrow> y \<in> {..<a})" by auto
wenzelm@63545
  1225
      then show "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {..<a}" ..
hoelzl@51531
  1226
    qed
hoelzl@51531
  1227
    moreover have "\<And>a::real. open {a <..}"
hoelzl@62101
  1228
      unfolding open_dist dist_real_def
hoelzl@51531
  1229
    proof clarify
wenzelm@63545
  1230
      fix x a :: real
wenzelm@63545
  1231
      assume "a < x"
wenzelm@63545
  1232
      then have "0 < x - a \<and> (\<forall>y. \<bar>y - x\<bar> < x - a \<longrightarrow> y \<in> {a<..})" by auto
wenzelm@63545
  1233
      then show "\<exists>e>0. \<forall>y. \<bar>y - x\<bar> < e \<longrightarrow> y \<in> {a<..}" ..
hoelzl@51531
  1234
    qed
hoelzl@51531
  1235
    ultimately show "open S"
hoelzl@51531
  1236
      by induct auto
hoelzl@51531
  1237
  qed
hoelzl@51531
  1238
qed
hoelzl@51531
  1239
hoelzl@51775
  1240
instance real :: linear_continuum_topology ..
hoelzl@51518
  1241
hoelzl@51531
  1242
lemmas open_real_greaterThan = open_greaterThan[where 'a=real]
hoelzl@51531
  1243
lemmas open_real_lessThan = open_lessThan[where 'a=real]
hoelzl@51531
  1244
lemmas open_real_greaterThanLessThan = open_greaterThanLessThan[where 'a=real]
hoelzl@51531
  1245
lemmas closed_real_atMost = closed_atMost[where 'a=real]
hoelzl@51531
  1246
lemmas closed_real_atLeast = closed_atLeast[where 'a=real]
hoelzl@51531
  1247
lemmas closed_real_atLeastAtMost = closed_atLeastAtMost[where 'a=real]
hoelzl@51531
  1248
wenzelm@63545
  1249
wenzelm@60758
  1250
subsection \<open>Extra type constraints\<close>
huffman@31446
  1251
wenzelm@61799
  1252
text \<open>Only allow @{term "open"} in class \<open>topological_space\<close>.\<close>
wenzelm@60758
  1253
setup \<open>Sign.add_const_constraint
wenzelm@60758
  1254
  (@{const_name "open"}, SOME @{typ "'a::topological_space set \<Rightarrow> bool"})\<close>
huffman@31492
  1255
hoelzl@62101
  1256
text \<open>Only allow @{term "uniformity"} in class \<open>uniform_space\<close>.\<close>
hoelzl@62101
  1257
setup \<open>Sign.add_const_constraint
hoelzl@62101
  1258
  (@{const_name "uniformity"}, SOME @{typ "('a::uniformity \<times> 'a) filter"})\<close>
hoelzl@62101
  1259
wenzelm@61799
  1260
text \<open>Only allow @{term dist} in class \<open>metric_space\<close>.\<close>
wenzelm@60758
  1261
setup \<open>Sign.add_const_constraint
wenzelm@60758
  1262
  (@{const_name dist}, SOME @{typ "'a::metric_space \<Rightarrow> 'a \<Rightarrow> real"})\<close>
huffman@31446
  1263
wenzelm@61799
  1264
text \<open>Only allow @{term norm} in class \<open>real_normed_vector\<close>.\<close>
wenzelm@60758
  1265
setup \<open>Sign.add_const_constraint
wenzelm@60758
  1266
  (@{const_name norm}, SOME @{typ "'a::real_normed_vector \<Rightarrow> real"})\<close>
huffman@31446
  1267
wenzelm@63545
  1268
wenzelm@60758
  1269
subsection \<open>Sign function\<close>
huffman@22972
  1270
wenzelm@63545
  1271
lemma norm_sgn: "norm (sgn x) = (if x = 0 then 0 else 1)"
wenzelm@63545
  1272
  for x :: "'a::real_normed_vector"
wenzelm@63545
  1273
  by (simp add: sgn_div_norm)
huffman@22972
  1274
wenzelm@63545
  1275
lemma sgn_zero [simp]: "sgn (0::'a::real_normed_vector) = 0"
wenzelm@63545
  1276
  by (simp add: sgn_div_norm)
huffman@22972
  1277
wenzelm@63545
  1278
lemma sgn_zero_iff: "sgn x = 0 \<longleftrightarrow> x = 0"
wenzelm@63545
  1279
  for x :: "'a::real_normed_vector"
wenzelm@63545
  1280
  by (simp add: sgn_div_norm)
huffman@22972
  1281
wenzelm@63545
  1282
lemma sgn_minus: "sgn (- x) = - sgn x"
wenzelm@63545
  1283
  for x :: "'a::real_normed_vector"
wenzelm@63545
  1284
  by (simp add: sgn_div_norm)
huffman@22972
  1285
wenzelm@63545
  1286
lemma sgn_scaleR: "sgn (scaleR r x) = scaleR (sgn r) (sgn x)"
wenzelm@63545
  1287
  for x :: "'a::real_normed_vector"
wenzelm@63545
  1288
  by (simp add: sgn_div_norm ac_simps)
huffman@22973
  1289
huffman@22972
  1290
lemma sgn_one [simp]: "sgn (1::'a::real_normed_algebra_1) = 1"
wenzelm@63545
  1291
  by (simp add: sgn_div_norm)
huffman@22972
  1292
wenzelm@63545
  1293
lemma sgn_of_real: "sgn (of_real r :: 'a::real_normed_algebra_1) = of_real (sgn r)"
wenzelm@63545
  1294
  unfolding of_real_def by (simp only: sgn_scaleR sgn_one)
huffman@22972
  1295
wenzelm@63545
  1296
lemma sgn_mult: "sgn (x * y) = sgn x * sgn y"
wenzelm@63545
  1297
  for x y :: "'a::real_normed_div_algebra"
wenzelm@63545
  1298
  by (simp add: sgn_div_norm norm_mult mult.commute)
huffman@22973
  1299
wenzelm@63545
  1300
lemma real_sgn_eq: "sgn x = x / \<bar>x\<bar>"
wenzelm@63545
  1301
  for x :: real
lp15@61649
  1302
  by (simp add: sgn_div_norm divide_inverse)
huffman@22972
  1303
wenzelm@63545
  1304
lemma zero_le_sgn_iff [simp]: "0 \<le> sgn x \<longleftrightarrow> 0 \<le> x"
wenzelm@63545
  1305
  for x :: real
hoelzl@56889
  1306
  by (cases "0::real" x rule: linorder_cases) simp_all
lp15@60026
  1307
wenzelm@63545
  1308
lemma sgn_le_0_iff [simp]: "sgn x \<le> 0 \<longleftrightarrow> x \<le> 0"
wenzelm@63545
  1309
  for x :: real
hoelzl@56889
  1310
  by (cases "0::real" x rule: linorder_cases) simp_all
lp15@60026
  1311
hoelzl@51474
  1312
lemma norm_conv_dist: "norm x = dist x 0"
hoelzl@51474
  1313
  unfolding dist_norm by simp
huffman@22972
  1314
lp15@62379
  1315
declare norm_conv_dist [symmetric, simp]
lp15@62379
  1316
wenzelm@63545
  1317
lemma dist_0_norm [simp]: "dist 0 x = norm x"
wenzelm@63545
  1318
  for x :: "'a::real_normed_vector"
wenzelm@63545
  1319
  by (simp add: dist_norm)
lp15@62397
  1320
lp15@60307
  1321
lemma dist_diff [simp]: "dist a (a - b) = norm b"  "dist (a - b) a = norm b"
lp15@60307
  1322
  by (simp_all add: dist_norm)
lp15@61609
  1323
eberlm@61524
  1324
lemma dist_of_int: "dist (of_int m) (of_int n :: 'a :: real_normed_algebra_1) = of_int \<bar>m - n\<bar>"
eberlm@61524
  1325
proof -
eberlm@61524
  1326
  have "dist (of_int m) (of_int n :: 'a) = dist (of_int m :: 'a) (of_int m - (of_int (m - n)))"
eberlm@61524
  1327
    by simp
eberlm@61524
  1328
  also have "\<dots> = of_int \<bar>m - n\<bar>" by (subst dist_diff, subst norm_of_int) simp
eberlm@61524
  1329
  finally show ?thesis .
eberlm@61524
  1330
qed
eberlm@61524
  1331
lp15@61609
  1332
lemma dist_of_nat:
eberlm@61524
  1333
  "dist (of_nat m) (of_nat n :: 'a :: real_normed_algebra_1) = of_int \<bar>int m - int n\<bar>"
eberlm@61524
  1334
  by (subst (1 2) of_int_of_nat_eq [symmetric]) (rule dist_of_int)
lp15@61609
  1335
wenzelm@63545
  1336
wenzelm@60758
  1337
subsection \<open>Bounded Linear and Bilinear Operators\<close>
huffman@22442
  1338
huffman@53600
  1339
locale linear = additive f for f :: "'a::real_vector \<Rightarrow> 'b::real_vector" +
huffman@22442
  1340
  assumes scaleR: "f (scaleR r x) = scaleR r (f x)"
huffman@53600
  1341
lp15@60800
  1342
lemma linear_imp_scaleR:
wenzelm@63545
  1343
  assumes "linear D"
wenzelm@63545
  1344
  obtains d where "D = (\<lambda>x. x *\<^sub>R d)"
lp15@60800
  1345
  by (metis assms linear.scaleR mult.commute mult.left_neutral real_scaleR_def)
lp15@60800
  1346
lp15@62533
  1347
corollary real_linearD:
lp15@62533
  1348
  fixes f :: "real \<Rightarrow> real"
lp15@62533
  1349
  assumes "linear f" obtains c where "f = op* c"
wenzelm@63545
  1350
  by (rule linear_imp_scaleR [OF assms]) (force simp: scaleR_conv_of_real)
lp15@62533
  1351
huffman@53600
  1352
lemma linearI:
huffman@53600
  1353
  assumes "\<And>x y. f (x + y) = f x + f y"
wenzelm@63545
  1354
    and "\<And>c x. f (c *\<^sub>R x) = c *\<^sub>R f x"
huffman@53600
  1355
  shows "linear f"
wenzelm@61169
  1356
  by standard (rule assms)+
huffman@53600
  1357
huffman@53600
  1358
locale bounded_linear = linear f for f :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector" +
huffman@22442
  1359
  assumes bounded: "\<exists>K. \<forall>x. norm (f x) \<le> norm x * K"
huffman@27443
  1360
begin
huffman@22442
  1361
wenzelm@63545
  1362
lemma pos_bounded: "\<exists>K>0. \<forall>x. norm (f x) \<le> norm x * K"
huffman@22442
  1363
proof -
huffman@22442
  1364
  obtain K where K: "\<And>x. norm (f x) \<le> norm x * K"
lp15@61649
  1365
    using bounded by blast
huffman@22442
  1366
  show ?thesis
huffman@22442
  1367
  proof (intro exI impI conjI allI)
huffman@22442
  1368
    show "0 < max 1 K"
haftmann@54863
  1369
      by (rule order_less_le_trans [OF zero_less_one max.cobounded1])
huffman@22442
  1370
  next
huffman@22442
  1371
    fix x
huffman@22442
  1372
    have "norm (f x) \<le> norm x * K" using K .
huffman@22442
  1373
    also have "\<dots> \<le> norm x * max 1 K"
haftmann@54863
  1374
      by (rule mult_left_mono [OF max.cobounded2 norm_ge_zero])
huffman@22442
  1375
    finally show "norm (f x) \<le> norm x * max 1 K" .
huffman@22442
  1376
  qed
huffman@22442
  1377
qed
huffman@22442
  1378
wenzelm@63545
  1379
lemma nonneg_bounded: "\<exists>K\<ge>0. \<forall>x. norm (f x) \<le> norm x * K"
wenzelm@63545
  1380
  using pos_bounded by (auto intro: order_less_imp_le)
huffman@22442
  1381
wenzelm@63545
  1382
lemma linear: "linear f"
lp15@63469
  1383
  by (fact local.linear_axioms)
hoelzl@56369
  1384
huffman@27443
  1385
end
huffman@27443
  1386
huffman@44127
  1387
lemma bounded_linear_intro:
huffman@44127
  1388
  assumes "\<And>x y. f (x + y) = f x + f y"
wenzelm@63545
  1389
    and "\<And>r x. f (scaleR r x) = scaleR r (f x)"
wenzelm@63545
  1390
    and "\<And>x. norm (f x) \<le> norm x * K"
huffman@44127
  1391
  shows "bounded_linear f"
lp15@61649
  1392
  by standard (blast intro: assms)+
huffman@44127
  1393
huffman@22442
  1394
locale bounded_bilinear =
wenzelm@63545
  1395
  fixes prod :: "'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector \<Rightarrow> 'c::real_normed_vector"
huffman@22442
  1396
    (infixl "**" 70)
huffman@22442
  1397
  assumes add_left: "prod (a + a') b = prod a b + prod a' b"
wenzelm@63545
  1398
    and add_right: "prod a (b + b') = prod a b + prod a b'"
wenzelm@63545
  1399
    and scaleR_left: "prod (scaleR r a) b = scaleR r (prod a b)"
wenzelm@63545
  1400
    and scaleR_right: "prod a (scaleR r b) = scaleR r (prod a b)"
wenzelm@63545
  1401
    and bounded: "\<exists>K. \<forall>a b. norm (prod a b) \<le> norm a * norm b * K"
huffman@27443
  1402
begin
huffman@22442
  1403
wenzelm@63545
  1404
lemma pos_bounded: "\<exists>K>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
wenzelm@63545
  1405
  apply (insert bounded)
wenzelm@63545
  1406
  apply (erule exE)
wenzelm@63545
  1407
  apply (rule_tac x="max 1 K" in exI)
wenzelm@63545
  1408
  apply safe
wenzelm@63545
  1409
   apply (rule order_less_le_trans [OF zero_less_one max.cobounded1])
wenzelm@63545
  1410
  apply (drule spec)
wenzelm@63545
  1411
  apply (drule spec)
wenzelm@63545
  1412
  apply (erule order_trans)
wenzelm@63545
  1413
  apply (rule mult_left_mono [OF max.cobounded2])
wenzelm@63545
  1414
  apply (intro mult_nonneg_nonneg norm_ge_zero)
wenzelm@63545
  1415
  done
huffman@22442
  1416
wenzelm@63545
  1417
lemma nonneg_bounded: "\<exists>K\<ge>0. \<forall>a b. norm (a ** b) \<le> norm a * norm b * K"
wenzelm@63545
  1418
  using pos_bounded by (auto intro: order_less_imp_le)
huffman@22442
  1419
huffman@27443
  1420
lemma additive_right: "additive (\<lambda>b. prod a b)"
wenzelm@63545
  1421
  by (rule additive.intro, rule add_right)
huffman@22442
  1422
huffman@27443
  1423
lemma additive_left: "additive (\<lambda>a. prod a b)"
wenzelm@63545
  1424
  by (rule additive.intro, rule add_left)
huffman@22442
  1425
huffman@27443
  1426
lemma zero_left: "prod 0 b = 0"
wenzelm@63545
  1427
  by (rule additive.zero [OF additive_left])
huffman@22442
  1428
huffman@27443
  1429
lemma zero_right: "prod a 0 = 0"
wenzelm@63545
  1430
  by (rule additive.zero [OF additive_right])
huffman@22442
  1431
huffman@27443
  1432
lemma minus_left: "prod (- a) b = - prod a b"
wenzelm@63545
  1433
  by (rule additive.minus [OF additive_left])
huffman@22442
  1434
huffman@27443
  1435
lemma minus_right: "prod a (- b) = - prod a b"
wenzelm@63545
  1436
  by (rule additive.minus [OF additive_right])
huffman@22442
  1437
wenzelm@63545
  1438
lemma diff_left: "prod (a - a') b = prod a b - prod a' b"
wenzelm@63545
  1439
  by (rule additive.diff [OF additive_left])
huffman@22442
  1440
wenzelm@63545
  1441
lemma diff_right: "prod a (b - b') = prod a b - prod a b'"
wenzelm@63545
  1442
  by (rule additive.diff [OF additive_right])
huffman@22442
  1443
wenzelm@63545
  1444
lemma setsum_left: "prod (setsum g S) x = setsum ((\<lambda>i. prod (g i) x)) S"
wenzelm@63545
  1445
  by (rule additive.setsum [OF additive_left])
immler@61915
  1446
wenzelm@63545
  1447
lemma setsum_right: "prod x (setsum g S) = setsum ((\<lambda>i. (prod x (g i)))) S"
wenzelm@63545
  1448
  by (rule additive.setsum [OF additive_right])
immler@61915
  1449
immler@61915
  1450
wenzelm@63545
  1451
lemma bounded_linear_left: "bounded_linear (\<lambda>a. a ** b)"
wenzelm@63545
  1452
  apply (insert bounded)
wenzelm@63545
  1453
  apply safe
wenzelm@63545
  1454
  apply (rule_tac K="norm b * K" in bounded_linear_intro)
wenzelm@63545
  1455
    apply (rule add_left)
wenzelm@63545
  1456
   apply (rule scaleR_left)
wenzelm@63545
  1457
  apply (simp add: ac_simps)
wenzelm@63545
  1458
  done
huffman@22442
  1459
wenzelm@63545
  1460
lemma bounded_linear_right: "bounded_linear (\<lambda>b. a ** b)"
wenzelm@63545
  1461
  apply (insert bounded)
wenzelm@63545
  1462
  apply safe
wenzelm@63545
  1463
  apply (rule_tac K="norm a * K" in bounded_linear_intro)
wenzelm@63545
  1464
    apply (rule add_right)
wenzelm@63545
  1465
   apply (rule scaleR_right)
wenzelm@63545
  1466
  apply (simp add: ac_simps)
wenzelm@63545
  1467
  done
huffman@22442
  1468
wenzelm@63545
  1469
lemma prod_diff_prod: "(x ** y - a ** b) = (x - a) ** (y - b) + (x - a) ** b + a ** (y - b)"
wenzelm@63545
  1470
  by (simp add: diff_left diff_right)
huffman@22442
  1471
immler@61916
  1472
lemma flip: "bounded_bilinear (\<lambda>x y. y ** x)"
immler@61916
  1473
  apply standard
wenzelm@63545
  1474
      apply (rule add_right)
wenzelm@63545
  1475
     apply (rule add_left)
wenzelm@63545
  1476
    apply (rule scaleR_right)
wenzelm@63545
  1477
   apply (rule scaleR_left)
immler@61916
  1478
  apply (subst mult.commute)
wenzelm@63545
  1479
  apply (insert bounded)
immler@61916
  1480
  apply blast
immler@61916
  1481
  done
immler@61916
  1482
immler@61916
  1483
lemma comp1:
immler@61916
  1484
  assumes "bounded_linear g"
immler@61916
  1485
  shows "bounded_bilinear (\<lambda>x. op ** (g x))"
immler@61916
  1486
proof unfold_locales
immler@61916
  1487
  interpret g: bounded_linear g by fact
immler@61916
  1488
  show "\<And>a a' b. g (a + a') ** b = g a ** b + g a' ** b"
immler@61916
  1489
    "\<And>a b b'. g a ** (b + b') = g a ** b + g a ** b'"
immler@61916
  1490
    "\<And>r a b. g (r *\<^sub>R a) ** b = r *\<^sub>R (g a ** b)"
immler@61916
  1491
    "\<And>a r b. g a ** (r *\<^sub>R b) = r *\<^sub>R (g a ** b)"
immler@61916
  1492
    by (auto simp: g.add add_left add_right g.scaleR scaleR_left scaleR_right)
wenzelm@63545
  1493
  from g.nonneg_bounded nonneg_bounded obtain K L
wenzelm@63545
  1494
    where nn: "0 \<le> K" "0 \<le> L"
wenzelm@63545
  1495
      and K: "\<And>x. norm (g x) \<le> norm x * K"
wenzelm@63545
  1496
      and L: "\<And>a b. norm (a ** b) \<le> norm a * norm b * L"
immler@61916
  1497
    by auto
immler@61916
  1498
  have "norm (g a ** b) \<le> norm a * K * norm b * L" for a b
immler@61916
  1499
    by (auto intro!:  order_trans[OF K] order_trans[OF L] mult_mono simp: nn)
immler@61916
  1500
  then show "\<exists>K. \<forall>a b. norm (g a ** b) \<le> norm a * norm b * K"
immler@61916
  1501
    by (auto intro!: exI[where x="K * L"] simp: ac_simps)
immler@61916
  1502
qed
immler@61916
  1503
wenzelm@63545
  1504
lemma comp: "bounded_linear f \<Longrightarrow> bounded_linear g \<Longrightarrow> bounded_bilinear (\<lambda>x y. f x ** g y)"
immler@61916
  1505
  by (rule bounded_bilinear.flip[OF bounded_bilinear.comp1[OF bounded_bilinear.flip[OF comp1]]])
immler@61916
  1506
huffman@27443
  1507
end
huffman@27443
  1508
hoelzl@51642
  1509
lemma bounded_linear_ident[simp]: "bounded_linear (\<lambda>x. x)"
wenzelm@61169
  1510
  by standard (auto intro!: exI[of _ 1])
hoelzl@51642
  1511
hoelzl@51642
  1512
lemma bounded_linear_zero[simp]: "bounded_linear (\<lambda>x. 0)"
wenzelm@61169
  1513
  by standard (auto intro!: exI[of _ 1])
hoelzl@51642
  1514
hoelzl@51642
  1515
lemma bounded_linear_add:
hoelzl@51642
  1516
  assumes "bounded_linear f"
wenzelm@63545
  1517
    and "bounded_linear g"
hoelzl@51642
  1518
  shows "bounded_linear (\<lambda>x. f x + g x)"
hoelzl@51642
  1519
proof -
hoelzl@51642
  1520
  interpret f: bounded_linear f by fact
hoelzl@51642
  1521
  interpret g: bounded_linear g by fact
hoelzl@51642
  1522
  show ?thesis
hoelzl@51642
  1523
  proof
wenzelm@63545
  1524
    from f.bounded obtain Kf where Kf: "norm (f x) \<le> norm x * Kf" for x
wenzelm@63545
  1525
      by blast
wenzelm@63545
  1526
    from g.bounded obtain Kg where Kg: "norm (g x) \<le> norm x * Kg" for x
wenzelm@63545
  1527
      by blast
hoelzl@51642
  1528
    show "\<exists>K. \<forall>x. norm (f x + g x) \<le> norm x * K"
hoelzl@51642
  1529
      using add_mono[OF Kf Kg]
hoelzl@51642
  1530
      by (intro exI[of _ "Kf + Kg"]) (auto simp: field_simps intro: norm_triangle_ineq order_trans)
hoelzl@51642
  1531
  qed (simp_all add: f.add g.add f.scaleR g.scaleR scaleR_right_distrib)
hoelzl@51642
  1532
qed
hoelzl@51642
  1533
hoelzl@51642
  1534
lemma bounded_linear_minus:
hoelzl@51642
  1535
  assumes "bounded_linear f"
hoelzl@51642
  1536
  shows "bounded_linear (\<lambda>x. - f x)"
hoelzl@51642
  1537
proof -
hoelzl@51642
  1538
  interpret f: bounded_linear f by fact
wenzelm@63545
  1539
  show ?thesis
wenzelm@63545
  1540
    apply unfold_locales
wenzelm@63545
  1541
      apply (simp add: f.add)
wenzelm@63545
  1542
     apply (simp add: f.scaleR)
hoelzl@51642
  1543
    apply (simp add: f.bounded)
hoelzl@51642
  1544
    done
hoelzl@51642
  1545
qed
hoelzl@51642
  1546
immler@61915
  1547
lemma bounded_linear_sub: "bounded_linear f \<Longrightarrow> bounded_linear g \<Longrightarrow> bounded_linear (\<lambda>x. f x - g x)"
immler@61915
  1548
  using bounded_linear_add[of f "\<lambda>x. - g x"] bounded_linear_minus[of g]
immler@61915
  1549
  by (auto simp add: algebra_simps)
immler@61915
  1550
immler@61915
  1551
lemma bounded_linear_setsum:
immler@61915
  1552
  fixes f :: "'i \<Rightarrow> 'a::real_normed_vector \<Rightarrow> 'b::real_normed_vector"
wenzelm@63915
  1553
  shows "(\<And>i. i \<in> I \<Longrightarrow> bounded_linear (f i)) \<Longrightarrow> bounded_linear (\<lambda>x. \<Sum>i\<in>I. f i x)"
wenzelm@63915
  1554
  by (induct I rule: infinite_finite_induct) (auto intro!: bounded_linear_add)
immler@61915
  1555
hoelzl@51642
  1556
lemma bounded_linear_compose:
hoelzl@51642
  1557
  assumes "bounded_linear f"
wenzelm@63545
  1558
    and "bounded_linear g"
hoelzl@51642
  1559
  shows "bounded_linear (\<lambda>x. f (g x))"
hoelzl@51642
  1560
proof -
hoelzl@51642
  1561
  interpret f: bounded_linear f by fact
hoelzl@51642
  1562
  interpret g: bounded_linear g by fact
wenzelm@63545
  1563
  show ?thesis
wenzelm@63545
  1564
  proof unfold_locales
wenzelm@63545
  1565
    show "f (g (x + y)) = f (g x) + f (g y)" for x y
hoelzl@51642
  1566
      by (simp only: f.add g.add)
wenzelm@63545
  1567
    show "f (g (scaleR r x)) = scaleR r (f (g x))" for r x
hoelzl@51642
  1568
      by (simp only: f.scaleR g.scaleR)
wenzelm@63545
  1569
    from f.pos_bounded obtain Kf where f: "\<And>x. norm (f x) \<le> norm x * Kf" and Kf: "0 < Kf"
wenzelm@63545
  1570
      by blast
wenzelm@63545
  1571
    from g.pos_bounded obtain Kg where g: "\<And>x. norm (g x) \<le> norm x * Kg"
wenzelm@63545
  1572
      by blast
hoelzl@51642
  1573
    show "\<exists>K. \<forall>x. norm (f (g x)) \<le> norm x * K"
hoelzl@51642
  1574
    proof (intro exI allI)
hoelzl@51642
  1575
      fix x
hoelzl@51642
  1576
      have "norm (f (g x)) \<le> norm (g x) * Kf"
hoelzl@51642
  1577
        using f .
hoelzl@51642
  1578
      also have "\<dots> \<le> (norm x * Kg) * Kf"
hoelzl@51642
  1579
        using g Kf [THEN order_less_imp_le] by (rule mult_right_mono)
hoelzl@51642
  1580
      also have "(norm x * Kg) * Kf = norm x * (Kg * Kf)"
haftmann@57512
  1581
        by (rule mult.assoc)
hoelzl@51642
  1582
      finally show "norm (f (g x)) \<le> norm x * (Kg * Kf)" .
hoelzl@51642
  1583
    qed
hoelzl@51642
  1584
  qed
hoelzl@51642
  1585
qed
hoelzl@51642
  1586
wenzelm@63545
  1587
lemma bounded_bilinear_mult: "bounded_bilinear (op * :: 'a \<Rightarrow> 'a \<Rightarrow> 'a::real_normed_algebra)"
wenzelm@63545
  1588
  apply (rule bounded_bilinear.intro)
wenzelm@63545
  1589
      apply (rule distrib_right)
wenzelm@63545
  1590
     apply (rule distrib_left)
wenzelm@63545
  1591
    apply (rule mult_scaleR_left)
wenzelm@63545
  1592
   apply (rule mult_scaleR_right)
wenzelm@63545
  1593
  apply (rule_tac x="1" in exI)
wenzelm@63545
  1594
  apply (simp add: norm_mult_ineq)
wenzelm@63545
  1595
  done
huffman@22442
  1596
wenzelm@63545
  1597
lemma bounded_linear_mult_left: "bounded_linear (\<lambda>x::'a::real_normed_algebra. x * y)"
huffman@44282
  1598
  using bounded_bilinear_mult
huffman@44282
  1599
  by (rule bounded_bilinear.bounded_linear_left)
huffman@22442
  1600
wenzelm@63545
  1601
lemma bounded_linear_mult_right: "bounded_linear (\<lambda>y::'a::real_normed_algebra. x * y)"
huffman@44282
  1602
  using bounded_bilinear_mult
huffman@44282
  1603
  by (rule bounded_bilinear.bounded_linear_right)
huffman@23127
  1604
hoelzl@51642
  1605
lemmas bounded_linear_mult_const =
hoelzl@51642
  1606
  bounded_linear_mult_left [THEN bounded_linear_compose]
hoelzl@51642
  1607
hoelzl@51642
  1608
lemmas bounded_linear_const_mult =
hoelzl@51642
  1609
  bounded_linear_mult_right [THEN bounded_linear_compose]
hoelzl@51642
  1610
wenzelm@63545
  1611
lemma bounded_linear_divide: "bounded_linear (\<lambda>x. x / y)"
wenzelm@63545
  1612
  for y :: "'a::real_normed_field"
huffman@44282
  1613
  unfolding divide_inverse by (rule bounded_linear_mult_left)
huffman@23120
  1614
huffman@44282
  1615
lemma bounded_bilinear_scaleR: "bounded_bilinear scaleR"
wenzelm@63545
  1616
  apply (rule bounded_bilinear.intro)
wenzelm@63545
  1617
      apply (rule scaleR_left_distrib)
wenzelm@63545
  1618
     apply (rule scaleR_right_distrib)
wenzelm@63545
  1619
    apply simp
wenzelm@63545
  1620
   apply (rule scaleR_left_commute)
wenzelm@63545
  1621
  apply (rule_tac x="1" in exI)
wenzelm@63545
  1622
  apply simp
wenzelm@63545
  1623
  done
huffman@22442
  1624
huffman@44282
  1625
lemma bounded_linear_scaleR_left: "bounded_linear (\<lambda>r. scaleR r x)"
huffman@44282
  1626
  using bounded_bilinear_scaleR
huffman@44282
  1627
  by (rule bounded_bilinear.bounded_linear_left)
huffman@23127
  1628
huffman@44282
  1629
lemma bounded_linear_scaleR_right: "bounded_linear (\<lambda>x. scaleR r x)"
huffman@44282
  1630
  using bounded_bilinear_scaleR
huffman@44282
  1631
  by (rule bounded_bilinear.bounded_linear_right)
huffman@23127
  1632
immler@61915
  1633
lemmas bounded_linear_scaleR_const =
immler@61915
  1634
  bounded_linear_scaleR_left[THEN bounded_linear_compose]
immler@61915
  1635
immler@61915
  1636
lemmas bounded_linear_const_scaleR =
immler@61915
  1637
  bounded_linear_scaleR_right[THEN bounded_linear_compose]
immler@61915
  1638
huffman@44282
  1639
lemma bounded_linear_of_real: "bounded_linear (\<lambda>r. of_real r)"
huffman@44282
  1640
  unfolding of_real_def by (rule bounded_linear_scaleR_left)
huffman@22625
  1641
wenzelm@63545
  1642
lemma real_bounded_linear: "bounded_linear f \<longleftrightarrow> (\<exists>c::real. f = (\<lambda>x. x * c))"
wenzelm@63545
  1643
  for f :: "real \<Rightarrow> real"
hoelzl@51642
  1644
proof -
wenzelm@63545
  1645
  {
wenzelm@63545
  1646
    fix x
wenzelm@63545
  1647
    assume "bounded_linear f"
hoelzl@51642
  1648
    then interpret bounded_linear f .
hoelzl@51642
  1649
    from scaleR[of x 1] have "f x = x * f 1"
wenzelm@63545
  1650
      by simp
wenzelm@63545
  1651
  }
hoelzl@51642
  1652
  then show ?thesis
hoelzl@51642
  1653
    by (auto intro: exI[of _ "f 1"] bounded_linear_mult_left)
hoelzl@51642
  1654
qed
hoelzl@51642
  1655
wenzelm@63545
  1656
lemma bij_linear_imp_inv_linear: "linear f \<Longrightarrow> bij f \<Longrightarrow> linear (inv f)"
wenzelm@63545
  1657
  by (auto simp: linear_def linear_axioms_def additive_def bij_is_surj bij_is_inj surj_f_inv_f
wenzelm@63545
  1658
      intro!:  Hilbert_Choice.inv_f_eq)
lp15@61609
  1659
huffman@44571
  1660
instance real_normed_algebra_1 \<subseteq> perfect_space
huffman@44571
  1661
proof
wenzelm@63545
  1662
  show "\<not> open {x}" for x :: 'a
wenzelm@63545
  1663
    apply (simp only: open_dist dist_norm)
wenzelm@63545
  1664
    apply clarsimp
wenzelm@63545
  1665
    apply (rule_tac x = "x + of_real (e/2)" in exI)
wenzelm@63545
  1666
    apply simp
wenzelm@63545
  1667
    done
huffman@44571
  1668
qed
huffman@44571
  1669
wenzelm@63545
  1670
wenzelm@60758
  1671
subsection \<open>Filters and Limits on Metric Space\<close>
hoelzl@51531
  1672
hoelzl@57448
  1673
lemma (in metric_space) nhds_metric: "nhds x = (INF e:{0 <..}. principal {y. dist y x < e})"
hoelzl@57448
  1674
  unfolding nhds_def
hoelzl@57448
  1675
proof (safe intro!: INF_eq)
wenzelm@63545
  1676
  fix S
wenzelm@63545
  1677
  assume "open S" "x \<in> S"
hoelzl@57448
  1678
  then obtain e where "{y. dist y x < e} \<subseteq> S" "0 < e"
hoelzl@57448
  1679
    by (auto simp: open_dist subset_eq)
hoelzl@57448
  1680
  then show "\<exists>e\<in>{0<..}. principal {y. dist y x < e} \<le> principal S"
hoelzl@57448
  1681
    by auto
hoelzl@57448
  1682
qed (auto intro!: exI[of _ "{y. dist x y < e}" for e] open_ball simp: dist_commute)
hoelzl@57448
  1683
wenzelm@63545
  1684
lemma (in metric_space) tendsto_iff: "(f \<longlongrightarrow> l) F \<longleftrightarrow> (\<forall>e>0. eventually (\<lambda>x. dist (f x) l < e) F)"
hoelzl@57448
  1685
  unfolding nhds_metric filterlim_INF filterlim_principal by auto
hoelzl@57448
  1686
wenzelm@63545
  1687
lemma (in metric_space) tendstoI [intro?]:
wenzelm@63545
  1688
  "(\<And>e. 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F) \<Longrightarrow> (f \<longlongrightarrow> l) F"
hoelzl@57448
  1689
  by (auto simp: tendsto_iff)
hoelzl@57448
  1690
wenzelm@61973
  1691
lemma (in metric_space) tendstoD: "(f \<longlongrightarrow> l) F \<Longrightarrow> 0 < e \<Longrightarrow> eventually (\<lambda>x. dist (f x) l < e) F"
hoelzl@57448
  1692
  by (auto simp: tendsto_iff)
hoelzl@57448
  1693
hoelzl@57448
  1694
lemma (in metric_space) eventually_nhds_metric:
hoelzl@57448
  1695
  "eventually P (nhds a) \<longleftrightarrow> (\<exists>d>0. \<forall>x. dist x a < d \<longrightarrow> P x)"
hoelzl@57448
  1696
  unfolding nhds_metric
hoelzl@57448
  1697
  by (subst eventually_INF_base)
hoelzl@57448
  1698
     (auto simp: eventually_principal Bex_def subset_eq intro: exI[of _ "min a b" for a b])
hoelzl@51531
  1699
wenzelm@63545
  1700
lemma eventually_at: "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)"
wenzelm@63545
  1701
  for a :: "'a :: metric_space"
wenzelm@63545
  1702
  by (auto simp: eventually_at_filter eventually_nhds_metric)
hoelzl@51531
  1703
wenzelm@63545
  1704
lemma eventually_at_le: "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)"
wenzelm@63545
  1705
  for a :: "'a::metric_space"
wenzelm@63545
  1706
  apply (simp only: eventually_at_filter eventually_nhds_metric)
hoelzl@51641
  1707
  apply auto
hoelzl@51641
  1708
  apply (rule_tac x="d / 2" in exI)
hoelzl@51641
  1709
  apply auto
hoelzl@51641
  1710
  done
hoelzl@51531
  1711
eberlm@61531
  1712
lemma eventually_at_left_real: "a > (b :: real) \<Longrightarrow> eventually (\<lambda>x. x \<in> {b<..<a}) (at_left a)"
eberlm@61531
  1713
  by (subst eventually_at, rule exI[of _ "a - b"]) (force simp: dist_real_def)
eberlm@61531
  1714
eberlm@61531
  1715
lemma eventually_at_right_real: "a < (b :: real) \<Longrightarrow> eventually (\<lambda>x. x \<in> {a<..<b}) (at_right a)"
eberlm@61531
  1716
  by (subst eventually_at, rule exI[of _ "b - a"]) (force simp: dist_real_def)
eberlm@61531
  1717
hoelzl@51531
  1718
lemma metric_tendsto_imp_tendsto:
wenzelm@63545
  1719
  fixes a :: "'a :: metric_space"
wenzelm@63545
  1720
    and b :: "'b :: metric_space"
wenzelm@61973
  1721
  assumes f: "(f \<longlongrightarrow> a) F"
wenzelm@63545
  1722
    and le: "eventually (\<lambda>x. dist (g x) b \<le> dist (f x) a) F"
wenzelm@61973
  1723
  shows "(g \<longlongrightarrow> b) F"
hoelzl@51531
  1724
proof (rule tendstoI)
wenzelm@63545
  1725
  fix e :: real
wenzelm@63545
  1726
  assume "0 < e"
hoelzl@51531
  1727
  with f have "eventually (\<lambda>x. dist (f x) a < e) F" by (rule tendstoD)
hoelzl@51531
  1728
  with le show "eventually (\<lambda>x. dist (g x) b < e) F"
hoelzl@51531
  1729
    using le_less_trans by (rule eventually_elim2)
hoelzl@51531
  1730
qed
hoelzl@51531
  1731
hoelzl@51531
  1732
lemma filterlim_real_sequentially: "LIM x sequentially. real x :> at_top"
wenzelm@63545
  1733
  apply (simp only: filterlim_at_top)
hoelzl@51531
  1734
  apply (intro allI)
wenzelm@61942
  1735
  apply (rule_tac c="nat \<lceil>Z + 1\<rceil>" in eventually_sequentiallyI)
wenzelm@61942
  1736
  apply linarith
wenzelm@61942
  1737
  done
wenzelm@61942
  1738
immler@63556
  1739
lemma filterlim_nat_sequentially: "filterlim nat sequentially at_top"
immler@63556
  1740
  unfolding filterlim_at_top
immler@63556
  1741
  apply (rule allI)
immler@63556
  1742
  subgoal for Z by (auto intro!: eventually_at_top_linorderI[where c="int Z"])
immler@63556
  1743
  done
immler@63556
  1744
immler@63556
  1745
lemma filterlim_floor_sequentially: "filterlim floor at_top at_top"
immler@63556
  1746
  unfolding filterlim_at_top
immler@63556
  1747
  apply (rule allI)
immler@63556
  1748
  subgoal for Z by (auto simp: le_floor_iff intro!: eventually_at_top_linorderI[where c="of_int Z"])
immler@63556
  1749
  done
immler@63556
  1750
immler@63556
  1751
lemma filterlim_sequentially_iff_filterlim_real:
immler@63556
  1752
  "filterlim f sequentially F \<longleftrightarrow> filterlim (\<lambda>x. real (f x)) at_top F"
immler@63556
  1753
  apply (rule iffI)
immler@63556
  1754
  subgoal using filterlim_compose filterlim_real_sequentially by blast
immler@63556
  1755
  subgoal premises prems
immler@63556
  1756
  proof -
immler@63556
  1757
    have "filterlim (\<lambda>x. nat (floor (real (f x)))) sequentially F"
immler@63556
  1758
      by (intro filterlim_compose[OF filterlim_nat_sequentially]
immler@63556
  1759
          filterlim_compose[OF filterlim_floor_sequentially] prems)
immler@63556
  1760
    then show ?thesis by simp
immler@63556
  1761
  qed
immler@63556
  1762
  done
immler@63556
  1763
hoelzl@51531
  1764
wenzelm@60758
  1765
subsubsection \<open>Limits of Sequences\<close>
hoelzl@51531
  1766
wenzelm@63545
  1767
lemma lim_sequentially: "X \<longlonglongrightarrow> L \<longleftrightarrow> (\<forall>r>0. \<exists>no. \<forall>n\<ge>no. dist (X n) L < r)"
wenzelm@63545
  1768
  for L :: "'a::metric_space"
hoelzl@51531
  1769
  unfolding tendsto_iff eventually_sequentially ..
hoelzl@51531
  1770
lp15@60026
  1771
lemmas LIMSEQ_def = lim_sequentially  (*legacy binding*)
lp15@60026
  1772
wenzelm@63545
  1773
lemma LIMSEQ_iff_nz: "X \<longlonglongrightarrow> L \<longleftrightarrow> (\<forall>r>0. \<exists>no>0. \<forall>n\<ge>no. dist (X n) L < r)"
wenzelm@63545
  1774
  for L :: "'a::metric_space"
lp15@60017
  1775
  unfolding lim_sequentially by (metis Suc_leD zero_less_Suc)
hoelzl@51531
  1776
wenzelm@63545
  1777
lemma metric_LIMSEQ_I: "(\<And>r. 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r) \<Longrightarrow> X \<longlonglongrightarrow> L"
wenzelm@63545
  1778
  for L :: "'a::metric_space"
wenzelm@63545
  1779
  by (simp add: lim_sequentially)
hoelzl@51531
  1780
wenzelm@63545
  1781
lemma metric_LIMSEQ_D: "X \<longlonglongrightarrow> L \<Longrightarrow> 0 < r \<Longrightarrow> \<exists>no. \<forall>n\<ge>no. dist (X n) L < r"
wenzelm@63545
  1782
  for L :: "'a::metric_space"
wenzelm@63545
  1783
  by (simp add: lim_sequentially)
hoelzl@51531
  1784
hoelzl@51531
  1785
wenzelm@60758
  1786
subsubsection \<open>Limits of Functions\<close>
hoelzl@51531
  1787
wenzelm@63545
  1788
lemma LIM_def: "f \<midarrow>a\<rightarrow> L \<longleftrightarrow> (\<forall>r > 0. \<exists>s > 0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r)"
wenzelm@63545
  1789
  for a :: "'a::metric_space" and L :: "'b::metric_space"
hoelzl@51641
  1790
  unfolding tendsto_iff eventually_at by simp
hoelzl@51531
  1791
hoelzl@51531
  1792
lemma metric_LIM_I:
wenzelm@63545
  1793
  "(\<And>r. 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r) \<Longrightarrow> f \<midarrow>a\<rightarrow> L"
wenzelm@63545
  1794
  for a :: "'a::metric_space" and L :: "'b::metric_space"
wenzelm@63545
  1795
  by (simp add: LIM_def)
hoelzl@51531
  1796
wenzelm@63545
  1797
lemma metric_LIM_D: "f \<midarrow>a\<rightarrow> L \<Longrightarrow> 0 < r \<Longrightarrow> \<exists>s>0. \<forall>x. x \<noteq> a \<and> dist x a < s \<longrightarrow> dist (f x) L < r"
wenzelm@63545
  1798
  for a :: "'a::metric_space" and L :: "'b::metric_space"
wenzelm@63545
  1799
  by (simp add: LIM_def)
hoelzl@51531
  1800
hoelzl@51531
  1801
lemma metric_LIM_imp_LIM:
wenzelm@63545
  1802
  fixes l :: "'a::metric_space"
wenzelm@63545
  1803
    and m :: "'b::metric_space"
wenzelm@63545
  1804
  assumes f: "f \<midarrow>a\<rightarrow> l"
wenzelm@63545
  1805
    and le: "\<And>x. x \<noteq> a \<Longrightarrow> dist (g x) m \<le> dist (f x) l"
wenzelm@63545
  1806
  shows "g \<midarrow>a\<rightarrow> m"
hoelzl@51531
  1807
  by (rule metric_tendsto_imp_tendsto [OF f]) (auto simp add: eventually_at_topological le)
hoelzl@51531
  1808
hoelzl@51531
  1809
lemma metric_LIM_equal2:
wenzelm@63545
  1810
  fixes a :: "'a::metric_space"
wenzelm@63545
  1811
  assumes "0 < R"
wenzelm@63545
  1812
    and "\<And>x. x \<noteq> a \<Longrightarrow> dist x a < R \<Longrightarrow> f x = g x"
wenzelm@63545
  1813
  shows "g \<midarrow>a\<rightarrow> l \<Longrightarrow> f \<midarrow>a\<rightarrow> l"
wenzelm@63545
  1814
  apply (rule topological_tendstoI)
wenzelm@63545
  1815
  apply (drule (2) topological_tendstoD)
wenzelm@63545
  1816
  apply (simp add: eventually_at)
wenzelm@63545
  1817
  apply safe
wenzelm@63545
  1818
  apply (rule_tac x="min d R" in exI)
wenzelm@63545
  1819
  apply safe
wenzelm@63545
  1820
   apply (simp add: assms(1))
wenzelm@63545
  1821
  apply (simp add: assms(2))
wenzelm@63545
  1822
  done
hoelzl@51531
  1823
hoelzl@51531
  1824
lemma metric_LIM_compose2:
wenzelm@63545
  1825
  fixes a :: "'a::metric_space"
wenzelm@63545
  1826
  assumes f: "f \<midarrow>a\<rightarrow> b"
wenzelm@63545
  1827
    and g: "g \<midarrow>b\<rightarrow> c"
wenzelm@63545
  1828
    and inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> b"
wenzelm@61976
  1829
  shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> c"
wenzelm@63545
  1830
  using inj by (intro tendsto_compose_eventually[OF g f]) (auto simp: eventually_at)
hoelzl@51531
  1831
hoelzl@51531
  1832
lemma metric_isCont_LIM_compose2:
hoelzl@51531
  1833
  fixes f :: "'a :: metric_space \<Rightarrow> _"
hoelzl@51531
  1834
  assumes f [unfolded isCont_def]: "isCont f a"
wenzelm@63545
  1835
    and g: "g \<midarrow>f a\<rightarrow> l"
wenzelm@63545
  1836
    and inj: "\<exists>d>0. \<forall>x. x \<noteq> a \<and> dist x a < d \<longrightarrow> f x \<noteq> f a"
wenzelm@61976
  1837
  shows "(\<lambda>x. g (f x)) \<midarrow>a\<rightarrow> l"
wenzelm@63545
  1838
  by (rule metric_LIM_compose2 [OF f g inj])
wenzelm@63545
  1839
hoelzl@51531
  1840
wenzelm@60758
  1841
subsection \<open>Complete metric spaces\<close>
hoelzl@51531
  1842
wenzelm@60758
  1843
subsection \<open>Cauchy sequences\<close>
hoelzl@51531
  1844
hoelzl@62101
  1845
lemma (in metric_space) Cauchy_def: "Cauchy X = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e)"
hoelzl@62101
  1846
proof -
wenzelm@63545
  1847
  have *: "eventually P (INF M. principal {(X m, X n) | n m. m \<ge> M \<and> n \<ge> M}) \<longleftrightarrow>
hoelzl@62101
  1848
    (\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. P (X m, X n))" for P
wenzelm@63545
  1849
    apply (subst eventually_INF_base)
wenzelm@63545
  1850
    subgoal by simp
wenzelm@63545
  1851
    subgoal for a b
hoelzl@62101
  1852
      by (intro bexI[of _ "max a b"]) (auto simp: eventually_principal subset_eq)
wenzelm@63545
  1853
    subgoal by (auto simp: eventually_principal, blast)
wenzelm@63545
  1854
    done
hoelzl@62101
  1855
  have "Cauchy X \<longleftrightarrow> (INF M. principal {(X m, X n) | n m. m \<ge> M \<and> n \<ge> M}) \<le> uniformity"
hoelzl@62101
  1856
    unfolding Cauchy_uniform_iff le_filter_def * ..
hoelzl@62101
  1857
  also have "\<dots> = (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e)"
hoelzl@62101
  1858
    unfolding uniformity_dist le_INF_iff by (auto simp: * le_principal)
hoelzl@62101
  1859
  finally show ?thesis .
hoelzl@62101
  1860
qed
hoelzl@51531
  1861
wenzelm@63545
  1862
lemma (in metric_space) Cauchy_altdef: "Cauchy f \<longleftrightarrow> (\<forall>e>0. \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e)"
wenzelm@63545
  1863
  (is "?lhs \<longleftrightarrow> ?rhs")
eberlm@61531
  1864
proof
wenzelm@63545
  1865
  assume ?rhs
wenzelm@63545
  1866
  show ?lhs
wenzelm@63545
  1867
    unfolding Cauchy_def
eberlm@61531
  1868
  proof (intro allI impI)
eberlm@61531
  1869
    fix e :: real assume e: "e > 0"
wenzelm@63545
  1870
    with \<open>?rhs\<close> obtain M where M: "m \<ge> M \<Longrightarrow> n > m \<Longrightarrow> dist (f m) (f n) < e" for m n
wenzelm@63545
  1871
      by blast
eberlm@61531
  1872
    have "dist (f m) (f n) < e" if "m \<ge> M" "n \<ge> M" for m n
eberlm@61531
  1873
      using M[of m n] M[of n m] e that by (cases m n rule: linorder_cases) (auto simp: dist_commute)
wenzelm@63545
  1874
    then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (f m) (f n) < e"
wenzelm@63545
  1875
      by blast
eberlm@61531
  1876
  qed
eberlm@61531
  1877
next
wenzelm@63545
  1878
  assume ?lhs
wenzelm@63545
  1879
  show ?rhs
eberlm@61531
  1880
  proof (intro allI impI)
wenzelm@63545
  1881
    fix e :: real
wenzelm@63545
  1882
    assume e: "e > 0"
wenzelm@61799
  1883
    with \<open>Cauchy f\<close> obtain M where "\<And>m n. m \<ge> M \<Longrightarrow> n \<ge> M \<Longrightarrow> dist (f m) (f n) < e"
lp15@61649
  1884
      unfolding Cauchy_def by blast
wenzelm@63545
  1885
    then show "\<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (f m) (f n) < e"
wenzelm@63545
  1886
      by (intro exI[of _ M]) force
eberlm@61531
  1887
  qed
eberlm@61531
  1888
qed
hoelzl@51531
  1889
hoelzl@62101
  1890
lemma (in metric_space) metric_CauchyI:
hoelzl@51531
  1891
  "(\<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
  1892
  by (simp add: Cauchy_def)
hoelzl@51531
  1893
wenzelm@63545
  1894
lemma (in metric_space) CauchyI':
wenzelm@63545
  1895
  "(\<And>e. 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n>m. dist (X m) (X n) < e) \<Longrightarrow> Cauchy X"
eberlm@61531
  1896
  unfolding Cauchy_altdef by blast
eberlm@61531
  1897
hoelzl@62101
  1898
lemma (in metric_space) metric_CauchyD:
hoelzl@51531
  1899
  "Cauchy X \<Longrightarrow> 0 < e \<Longrightarrow> \<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < e"
hoelzl@51531
  1900
  by (simp add: Cauchy_def)
hoelzl@51531
  1901
hoelzl@62101
  1902
lemma (in metric_space) metric_Cauchy_iff2:
hoelzl@51531
  1903
  "Cauchy X = (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. dist (X m) (X n) < inverse(real (Suc j))))"
wenzelm@63545
  1904
  apply (simp add: Cauchy_def)
wenzelm@63545
  1905
  apply auto
wenzelm@63545
  1906
  apply (drule reals_Archimedean)
wenzelm@63545
  1907
  apply safe
wenzelm@63545
  1908
  apply (drule_tac x = n in spec)
wenzelm@63545
  1909
  apply auto
wenzelm@63545
  1910
  apply (rule_tac x = M in exI)
wenzelm@63545
  1911
  apply auto
wenzelm@63545
  1912
  apply (drule_tac x = m in spec)
wenzelm@63545
  1913
  apply simp
wenzelm@63545
  1914
  apply (drule_tac x = na in spec)
wenzelm@63545
  1915
  apply auto
wenzelm@63545
  1916
  done
hoelzl@51531
  1917
wenzelm@63545
  1918
lemma Cauchy_iff2: "Cauchy X \<longleftrightarrow> (\<forall>j. (\<exists>M. \<forall>m \<ge> M. \<forall>n \<ge> M. \<bar>X m - X n\<bar> < inverse (real (Suc j))))"
wenzelm@63545
  1919
  by (simp only: metric_Cauchy_iff2 dist_real_def)
hoelzl@51531
  1920
hoelzl@62101
  1921
lemma lim_1_over_n: "((\<lambda>n. 1 / of_nat n) \<longlongrightarrow> (0::'a::real_normed_field)) sequentially"
hoelzl@62101
  1922
proof (subst lim_sequentially, intro allI impI exI)
wenzelm@63545
  1923
  fix e :: real
wenzelm@63545
  1924
  assume e: "e > 0"
wenzelm@63545
  1925
  fix n :: nat
wenzelm@63545
  1926
  assume n: "n \<ge> nat \<lceil>inverse e + 1\<rceil>"
hoelzl@62101
  1927
  have "inverse e < of_nat (nat \<lceil>inverse e + 1\<rceil>)" by linarith
hoelzl@62101
  1928
  also note n
wenzelm@63545
  1929
  finally show "dist (1 / of_nat n :: 'a) 0 < e"
wenzelm@63545
  1930
    using e by (simp add: divide_simps mult.commute norm_divide)
hoelzl@51531
  1931
qed
hoelzl@51531
  1932
hoelzl@62101
  1933
lemma (in metric_space) complete_def:
hoelzl@62101
  1934
  shows "complete S = (\<forall>f. (\<forall>n. f n \<in> S) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>S. f \<longlonglongrightarrow> l))"
hoelzl@62101
  1935
  unfolding complete_uniform
hoelzl@62101
  1936
proof safe
wenzelm@63545
  1937
  fix f :: "nat \<Rightarrow> 'a"
wenzelm@63545
  1938
  assume f: "\<forall>n. f n \<in> S" "Cauchy f"
hoelzl@62101
  1939
    and *: "\<forall>F\<le>principal S. F \<noteq> bot \<longrightarrow> cauchy_filter F \<longrightarrow> (\<exists>x\<in>S. F \<le> nhds x)"
hoelzl@62101
  1940
  then show "\<exists>l\<in>S. f \<longlonglongrightarrow> l"
hoelzl@62101
  1941
    unfolding filterlim_def using f
hoelzl@62101
  1942
    by (intro *[rule_format])
hoelzl@62101
  1943
       (auto simp: filtermap_sequentually_ne_bot le_principal eventually_filtermap Cauchy_uniform)
hoelzl@62101
  1944
next
wenzelm@63545
  1945
  fix F :: "'a filter"
wenzelm@63545
  1946
  assume "F \<le> principal S" "F \<noteq> bot" "cauchy_filter F"
hoelzl@62101
  1947
  assume seq: "\<forall>f. (\<forall>n. f n \<in> S) \<and> Cauchy f \<longrightarrow> (\<exists>l\<in>S. f \<longlonglongrightarrow> l)"
hoelzl@62101
  1948
wenzelm@63545
  1949
  from \<open>F \<le> principal S\<close> \<open>cauchy_filter F\<close>
wenzelm@63545
  1950
  have FF_le: "F \<times>\<^sub>F F \<le> uniformity_on S"
hoelzl@62101
  1951
    by (simp add: cauchy_filter_def principal_prod_principal[symmetric] prod_filter_mono)
hoelzl@62101
  1952
hoelzl@62101
  1953
  let ?P = "\<lambda>P e. eventually P F \<and> (\<forall>x. P x \<longrightarrow> x \<in> S) \<and> (\<forall>x y. P x \<longrightarrow> P y \<longrightarrow> dist x y < e)"
wenzelm@63545
  1954
  have P: "\<exists>P. ?P P \<epsilon>" if "0 < \<epsilon>" for \<epsilon> :: real
wenzelm@63545
  1955
  proof -
wenzelm@63545
  1956
    from that have "eventually (\<lambda>(x, y). x \<in> S \<and> y \<in> S \<and> dist x y < \<epsilon>) (uniformity_on S)"
wenzelm@63545
  1957
      by (auto simp: eventually_inf_principal eventually_uniformity_metric)
wenzelm@63545
  1958
    from filter_leD[OF FF_le this] show ?thesis
wenzelm@63545
  1959
      by (auto simp: eventually_prod_same)
wenzelm@63545
  1960
  qed
hoelzl@62101
  1961
hoelzl@62101
  1962
  have "\<exists>P. \<forall>n. ?P (P n) (1 / Suc n) \<and> P (Suc n) \<le> P n"
hoelzl@62101
  1963
  proof (rule dependent_nat_choice)
hoelzl@62101
  1964
    show "\<exists>P. ?P P (1 / Suc 0)"
hoelzl@62101
  1965
      using P[of 1] by auto
hoelzl@62101
  1966
  next
hoelzl@62101
  1967
    fix P n assume "?P P (1/Suc n)"
hoelzl@62101
  1968
    moreover obtain Q where "?P Q (1 / Suc (Suc n))"
hoelzl@62101
  1969
      using P[of "1/Suc (Suc n)"] by auto
hoelzl@62101
  1970
    ultimately show "\<exists>Q. ?P Q (1 / Suc (Suc n)) \<and> Q \<le> P"
hoelzl@62101
  1971
      by (intro exI[of _ "\<lambda>x. P x \<and> Q x"]) (auto simp: eventually_conj_iff)
hoelzl@62101
  1972
  qed
wenzelm@63545
  1973
  then obtain P where P: "eventually (P n) F" "P n x \<Longrightarrow> x \<in> S"
wenzelm@63545
  1974
    "P n x \<Longrightarrow> P n y \<Longrightarrow> dist x y < 1 / Suc n" "P (Suc n) \<le> P n"
wenzelm@63545
  1975
    for n x y
hoelzl@62101
  1976
    by metis
hoelzl@62101
  1977
  have "antimono P"
hoelzl@62101
  1978
    using P(4) unfolding decseq_Suc_iff le_fun_def by blast
hoelzl@62101
  1979
wenzelm@63545
  1980
  obtain X where X: "P n (X n)" for n
hoelzl@62101
  1981
    using P(1)[THEN eventually_happens'[OF \<open>F \<noteq> bot\<close>]] by metis
hoelzl@62101
  1982
  have "Cauchy X"
hoelzl@62101
  1983
    unfolding metric_Cauchy_iff2 inverse_eq_divide
hoelzl@62101
  1984
  proof (intro exI allI impI)
wenzelm@63545
  1985
    fix j m n :: nat
wenzelm@63545
  1986
    assume "j \<le> m" "j \<le> n"
hoelzl@62101
  1987
    with \<open>antimono P\<close> X have "P j (X m)" "P j (X n)"
hoelzl@62101
  1988
      by (auto simp: antimono_def)
hoelzl@62101
  1989
    then show "dist (X m) (X n) < 1 / Suc j"
hoelzl@62101
  1990
      by (rule P)
hoelzl@62101
  1991
  qed
hoelzl@62101
  1992
  moreover have "\<forall>n. X n \<in> S"
hoelzl@62101
  1993
    using P(2) X by auto
hoelzl@62101
  1994
  ultimately obtain x where "X \<longlonglongrightarrow> x" "x \<in> S"
hoelzl@62101
  1995
    using seq by blast
hoelzl@62101
  1996
hoelzl@62101
  1997
  show "\<exists>x\<in>S. F \<le> nhds x"
hoelzl@62101
  1998
  proof (rule bexI)
wenzelm@63545
  1999
    have "eventually (\<lambda>y. dist y x < e) F" if "0 < e" for e :: real
wenzelm@63545
  2000
    proof -
wenzelm@63545
  2001
      from that have "(\<lambda>n. 1 / Suc n :: real) \<longlonglongrightarrow> 0 \<and> 0 < e / 2"
hoelzl@62101
  2002
        by (subst LIMSEQ_Suc_iff) (auto intro!: lim_1_over_n)
hoelzl@62101
  2003
      then have "\<forall>\<^sub>F n in sequentially. dist (X n) x < e / 2 \<and> 1 / Suc n < e / 2"
wenzelm@63545
  2004
        using \<open>X \<longlonglongrightarrow> x\<close>
wenzelm@63545
  2005
        unfolding tendsto_iff order_tendsto_iff[where 'a=real] eventually_conj_iff
wenzelm@63545
  2006
        by blast
hoelzl@62101
  2007
      then obtain n where "dist x (X n) < e / 2" "1 / Suc n < e / 2"
hoelzl@62101
  2008
        by (auto simp: eventually_sequentially dist_commute)
wenzelm@63545
  2009
      show ?thesis
hoelzl@62101
  2010
        using \<open>eventually (P n) F\<close>
hoelzl@62101
  2011
      proof eventually_elim
wenzelm@63545
  2012
        case (elim y)
hoelzl@62101
  2013
        then have "dist y (X n) < 1 / Suc n"
hoelzl@62101
  2014
          by (intro X P)
hoelzl@62101
  2015
        also have "\<dots> < e / 2" by fact
hoelzl@62101
  2016
        finally show "dist y x < e"
hoelzl@62101
  2017
          by (rule dist_triangle_half_l) fact
wenzelm@63545
  2018
      qed
wenzelm@63545
  2019
    qed
hoelzl@62101
  2020
    then show "F \<le> nhds x"
hoelzl@62101
  2021
      unfolding nhds_metric le_INF_iff le_principal by auto
hoelzl@62101
  2022
  qed fact
hoelzl@62101
  2023
qed
hoelzl@62101
  2024
hoelzl@62101
  2025
lemma (in metric_space) totally_bounded_metric:
hoelzl@62101
  2026
  "totally_bounded S \<longleftrightarrow> (\<forall>e>0. \<exists>k. finite k \<and> S \<subseteq> (\<Union>x\<in>k. {y. dist x y < e}))"
wenzelm@63545
  2027
  apply (simp only: totally_bounded_def eventually_uniformity_metric imp_ex)
hoelzl@62101
  2028
  apply (subst all_comm)
hoelzl@62101
  2029
  apply (intro arg_cong[where f=All] ext)
hoelzl@62101
  2030
  apply safe
hoelzl@62101
  2031
  subgoal for e
hoelzl@62101
  2032
    apply (erule allE[of _ "\<lambda>(x, y). dist x y < e"])
hoelzl@62101
  2033
    apply auto
hoelzl@62101
  2034
    done
hoelzl@62101
  2035
  subgoal for e P k
hoelzl@62101
  2036
    apply (intro exI[of _ k])
hoelzl@62101
  2037
    apply (force simp: subset_eq)
hoelzl@62101
  2038
    done
hoelzl@62101
  2039
  done
hoelzl@51531
  2040
wenzelm@63545
  2041
wenzelm@60758
  2042
subsubsection \<open>Cauchy Sequences are Convergent\<close>
hoelzl@51531
  2043
hoelzl@62101
  2044
(* TODO: update to uniform_space *)
hoelzl@51531
  2045
class complete_space = metric_space +
hoelzl@51531
  2046
  assumes Cauchy_convergent: "Cauchy X \<Longrightarrow> convergent X"
hoelzl@51531
  2047
wenzelm@63545
  2048
lemma Cauchy_convergent_iff: "Cauchy X \<longleftrightarrow> convergent X"
wenzelm@63545
  2049
  for X :: "nat \<Rightarrow> 'a::complete_space"
wenzelm@63545
  2050
  by (blast intro: Cauchy_convergent convergent_Cauchy)
wenzelm@63545
  2051
hoelzl@51531
  2052
wenzelm@60758
  2053
subsection \<open>The set of real numbers is a complete metric space\<close>
hoelzl@51531
  2054
wenzelm@60758
  2055
text \<open>
wenzelm@63545
  2056
  Proof that Cauchy sequences converge based on the one from
wenzelm@63680
  2057
  \<^url>\<open>http://pirate.shu.edu/~wachsmut/ira/numseq/proofs/cauconv.html\<close>
wenzelm@60758
  2058
\<close>
hoelzl@51531
  2059
wenzelm@60758
  2060
text \<open>
hoelzl@51531
  2061
  If sequence @{term "X"} is Cauchy, then its limit is the lub of
hoelzl@51531
  2062
  @{term "{r::real. \<exists>N. \<forall>n\<ge>N. r < X n}"}
wenzelm@60758
  2063
\<close>
hoelzl@51531
  2064
lemma increasing_LIMSEQ:
hoelzl@51531
  2065
  fixes f :: "nat \<Rightarrow> real"
hoelzl@51531
  2066
  assumes inc: "\<And>n. f n \<le> f (Suc n)"
wenzelm@63545
  2067
    and bdd: "\<And>n. f n \<le> l"
wenzelm@63545
  2068
    and en: "\<And>e. 0 < e \<Longrightarrow> \<exists>n. l \<le> f n + e"
wenzelm@61969
  2069
  shows "f \<longlonglongrightarrow> l"
hoelzl@51531
  2070
proof (rule increasing_tendsto)
wenzelm@63545
  2071
  fix x
wenzelm@63545
  2072
  assume "x < l"
hoelzl@51531
  2073
  with dense[of 0 "l - x"] obtain e where "0 < e" "e < l - x"
hoelzl@51531
  2074
    by auto
wenzelm@60758
  2075
  from en[OF \<open>0 < e\<close>] obtain n where "l - e \<le> f n"
hoelzl@51531
  2076
    by (auto simp: field_simps)
wenzelm@63545
  2077
  with \<open>e < l - x\<close> \<open>0 < e\<close> have "x < f n"
wenzelm@63545
  2078
    by simp
hoelzl@51531
  2079
  with incseq_SucI[of f, OF inc] show "eventually (\<lambda>n. x < f n) sequentially"
hoelzl@51531
  2080
    by (auto simp: eventually_sequentially incseq_def intro: less_le_trans)
wenzelm@63545
  2081
qed (use bdd in auto)
hoelzl@51531
  2082
hoelzl@51531
  2083
lemma real_Cauchy_convergent:
hoelzl@51531
  2084
  fixes X :: "nat \<Rightarrow> real"
hoelzl@51531
  2085
  assumes X: "Cauchy X"
hoelzl@51531
  2086
  shows "convergent X"
hoelzl@51531
  2087
proof -
wenzelm@63040
  2088
  define S :: "real set" where "S = {x. \<exists>N. \<forall>n\<ge>N. x < X n}"
wenzelm@63545
  2089
  then have mem_S: "\<And>N x. \<forall>n\<ge>N. x < X n \<Longrightarrow> x \<in> S"
wenzelm@63545
  2090
    by auto
hoelzl@51531
  2091
wenzelm@63545
  2092
  have bound_isUb: "y \<le> x" if N: "\<forall>n\<ge>N. X n < x" and "y \<in> S" for N and x y :: real
wenzelm@63545
  2093
  proof -
wenzelm@63545
  2094
    from that have "\<exists>M. \<forall>n\<ge>M. y < X n"
wenzelm@63545
  2095
      by (simp add: S_def)
wenzelm@63545
  2096
    then obtain M where "\<forall>n\<ge>M. y < X n" ..
wenzelm@63545
  2097
    then have "y < X (max M N)" by simp
wenzelm@63545
  2098
    also have "\<dots> < x" using N by simp
wenzelm@63545
  2099
    finally show ?thesis by (rule order_less_imp_le)
wenzelm@63545
  2100
  qed
hoelzl@51531
  2101
hoelzl@51531
  2102
  obtain N where "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m) (X n) < 1"
hoelzl@51531
  2103
    using X[THEN metric_CauchyD, OF zero_less_one] by auto
wenzelm@63545
  2104
  then have N: "\<forall>n\<ge>N. dist (X n) (X N) < 1" by simp
hoelzl@54263
  2105
  have [simp]: "S \<noteq> {}"
hoelzl@54263
  2106
  proof (intro exI ex_in_conv[THEN iffD1])
hoelzl@51531
  2107
    from N have "\<forall>n\<ge>N. X N - 1 < X n"
hoelzl@51531
  2108
      by (simp add: abs_diff_less_iff dist_real_def)
wenzelm@63545
  2109
    then show "X N - 1 \<in> S" by (rule mem_S)
hoelzl@51531
  2110
  qed
hoelzl@54263
  2111
  have [simp]: "bdd_above S"
hoelzl@51531
  2112
  proof
hoelzl@51531
  2113
    from N have "\<forall>n\<ge>N. X n < X N + 1"
hoelzl@51531
  2114
      by (simp add: abs_diff_less_iff dist_real_def)
wenzelm@63545
  2115
    then show "\<And>s. s \<in> S \<Longrightarrow>  s \<le> X N + 1"
hoelzl@51531
  2116
      by (rule bound_isUb)
hoelzl@51531
  2117
  qed
wenzelm@61969
  2118
  have "X \<longlonglongrightarrow> Sup S"
hoelzl@51531
  2119
  proof (rule metric_LIMSEQ_I)
wenzelm@63545
  2120
    fix r :: real
wenzelm@63545
  2121
    assume "0 < r"
wenzelm@63545
  2122
    then have r: "0 < r/2" by simp
wenzelm@63545
  2123
    obtain N where "\<forall>n\<ge>N. \<forall>m\<ge>N. dist (X n) (X m) < r/2"
wenzelm@63545
  2124
      using metric_CauchyD [OF X r] by auto
wenzelm@63545
  2125
    then have "\<forall>n\<ge>N. dist (X n) (X N) < r/2" by simp
wenzelm@63545
  2126
    then have N: "\<forall>n\<ge>N. X N - r/2 < X n \<and> X n < X N + r/2"
wenzelm@63545
  2127
      by (simp only: dist_real_def abs_diff_less_iff)
hoelzl@51531
  2128
wenzelm@63545
  2129
    from N have "\<forall>n\<ge>N. X N - r/2 < X n" by blast
wenzelm@63545
  2130
    then have "X N - r/2 \<in> S" by (rule mem_S)
wenzelm@63545
  2131
    then have 1: "X N - r/2 \<le> Sup S" by (simp add: cSup_upper)
hoelzl@51531
  2132
wenzelm@63545
  2133
    from N have "\<forall>n\<ge>N. X n < X N + r/2" by blast
wenzelm@63545
  2134
    from bound_isUb[OF this]
wenzelm@63545
  2135
    have 2: "Sup S \<le> X N + r/2"
wenzelm@63545
  2136
      by (intro cSup_least) simp_all
hoelzl@51531
  2137
wenzelm@63545
  2138
    show "\<exists>N. \<forall>n\<ge>N. dist (X n) (Sup S) < r"
wenzelm@63545
  2139
    proof (intro exI allI impI)
wenzelm@63545
  2140
      fix n
wenzelm@63545
  2141
      assume n: "N \<le> n"
wenzelm@63545
  2142
      from N n have "X n < X N + r/2" and "X N - r/2 < X n"
wenzelm@63545
  2143
        by simp_all
wenzelm@63545
  2144
      then show "dist (X n) (Sup S) < r" using 1 2
wenzelm@63545
  2145
        by (simp add: abs_diff_less_iff dist_real_def)
wenzelm@63545
  2146
    qed
hoelzl@51531
  2147
  qed
wenzelm@63545
  2148
  then show ?thesis by (auto simp: convergent_def)
hoelzl@51531
  2149
qed
hoelzl@51531
  2150
hoelzl@51531
  2151
instance real :: complete_space
hoelzl@51531
  2152
  by intro_classes (rule real_Cauchy_convergent)
hoelzl@51531
  2153
hoelzl@51531
  2154
class banach = real_normed_vector + complete_space
hoelzl@51531
  2155
wenzelm@61169
  2156
instance real :: banach ..
hoelzl@51531
  2157
hoelzl@51531
  2158
lemma tendsto_at_topI_sequentially:
hoelzl@57275
  2159
  fixes f :: "real \<Rightarrow> 'b::first_countable_topology"
wenzelm@61969
  2160
  assumes *: "\<And>X. filterlim X at_top sequentially \<Longrightarrow> (\<lambda>n. f (X n)) \<longlonglongrightarrow> y"
wenzelm@61973
  2161
  shows "(f \<longlongrightarrow> y) at_top"
hoelzl@57448
  2162
proof -
wenzelm@63545
  2163
  obtain A where A: "decseq A" "open (A n)" "y \<in> A n" "nhds y = (INF n. principal (A n))" for n
wenzelm@63545
  2164
    by (rule nhds_countable[of y]) (rule that)
hoelzl@57275
  2165
hoelzl@57448
  2166
  have "\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m"
hoelzl@57448
  2167
  proof (rule ccontr)
hoelzl@57448
  2168
    assume "\<not> (\<forall>m. \<exists>k. \<forall>x\<ge>k. f x \<in> A m)"
hoelzl@57448
  2169
    then obtain m where "\<And>k. \<exists>x\<ge>k. f x \<notin> A m"
hoelzl@57448
  2170
      by auto
hoelzl@57448
  2171
    then have "\<exists>X. \<forall>n. (f (X n) \<notin> A m) \<and> max n (X n) + 1 \<le> X (Suc n)"
hoelzl@57448
  2172
      by (intro dependent_nat_choice) (auto simp del: max.bounded_iff)
hoelzl@57448
  2173
    then obtain X where X: "\<And>n. f (X n) \<notin> A m" "\<And>n. max n (X n) + 1 \<le> X (Suc n)"
hoelzl@57448
  2174
      by auto
wenzelm@63545
  2175
    have "1 \<le> n \<Longrightarrow> real n \<le> X n" for n
wenzelm@63545
  2176
      using X[of "n - 1"] by auto
hoelzl@57448
  2177
    then have "filterlim X at_top sequentially"
hoelzl@57448
  2178
      by (force intro!: filterlim_at_top_mono[OF filterlim_real_sequentially]
wenzelm@63545
  2179
          simp: eventually_sequentially)
hoelzl@57448
  2180
    from topological_tendstoD[OF *[OF this] A(2, 3), of m] X(1) show False
hoelzl@57448
  2181
      by auto
hoelzl@57275
  2182
  qed
wenzelm@63545
  2183
  then obtain k where "k m \<le> x \<Longrightarrow> f x \<in> A m" for m x
hoelzl@57448
  2184
    by metis
hoelzl@57448
  2185
  then show ?thesis
wenzelm@63545
  2186
    unfolding at_top_def A by (intro filterlim_base[where i=k]) auto
hoelzl@57275
  2187
qed
hoelzl@57275
  2188
hoelzl@57275
  2189
lemma tendsto_at_topI_sequentially_real:
hoelzl@51531
  2190
  fixes f :: "real \<Rightarrow> real"
hoelzl@51531
  2191
  assumes mono: "mono f"
wenzelm@63545
  2192
    and limseq: "(\<lambda>n. f (real n)) \<longlonglongrightarrow> y"
wenzelm@61973
  2193
  shows "(f \<longlongrightarrow> y) at_top"
hoelzl@51531
  2194
proof (rule tendstoI)
wenzelm@63545
  2195
  fix e :: real
wenzelm@63545
  2196
  assume "0 < e"
wenzelm@63545
  2197
  with limseq obtain N :: nat where N: "N \<le> n \<Longrightarrow> \<bar>f (real n) - y\<bar> < e" for n
lp15@60017
  2198
    by (auto simp: lim_sequentially dist_real_def)
wenzelm@63545
  2199
  have le: "f x \<le> y" for x :: real
wenzelm@63545
  2200
  proof -
wenzelm@53381
  2201
    obtain n where "x \<le> real_of_nat n"
lp15@62623
  2202
      using real_arch_simple[of x] ..
hoelzl@51531
  2203
    note monoD[OF mono this]
hoelzl@51531
  2204
    also have "f (real_of_nat n) \<le> y"
lp15@61649
  2205
      by (rule LIMSEQ_le_const[OF limseq]) (auto intro!: exI[of _ n] monoD[OF mono])
wenzelm@63545
  2206
    finally show ?thesis .
wenzelm@63545
  2207
  qed
hoelzl@51531
  2208
  have "eventually (\<lambda>x. real N \<le> x) at_top"
hoelzl@51531
  2209
    by (rule eventually_ge_at_top)
hoelzl@51531
  2210
  then show "eventually (\<lambda>x. dist (f x) y < e) at_top"
hoelzl@51531
  2211
  proof eventually_elim
wenzelm@63545
  2212
    case (elim x)
hoelzl@51531
  2213
    with N[of N] le have "y - f (real N) < e" by auto
wenzelm@63545
  2214
    moreover note monoD[OF mono elim]
hoelzl@51531
  2215
    ultimately show "dist (f x) y < e"
hoelzl@51531
  2216
      using le[of x] by (auto simp: dist_real_def field_simps)
hoelzl@51531
  2217
  qed
hoelzl@51531
  2218
qed
hoelzl@51531
  2219
huffman@20504
  2220
end