src/HOL/Real/RealDef.thy
author huffman
Mon May 14 18:48:24 2007 +0200 (2007-05-14)
changeset 22970 b5910e3dec4c
parent 22962 4bb05ba38939
child 23031 9da9585c816e
permissions -rw-r--r--
move lemmas to RealPow.thy; tuned proofs
paulson@5588
     1
(*  Title       : Real/RealDef.thy
paulson@7219
     2
    ID          : $Id$
paulson@5588
     3
    Author      : Jacques D. Fleuriot
paulson@5588
     4
    Copyright   : 1998  University of Cambridge
paulson@14387
     5
    Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
avigad@16819
     6
    Additional contributions by Jeremy Avigad
paulson@14269
     7
*)
paulson@14269
     8
paulson@14387
     9
header{*Defining the Reals from the Positive Reals*}
paulson@14387
    10
nipkow@15131
    11
theory RealDef
nipkow@15140
    12
imports PReal
haftmann@16417
    13
uses ("real_arith.ML")
nipkow@15131
    14
begin
paulson@5588
    15
wenzelm@19765
    16
definition
wenzelm@21404
    17
  realrel   ::  "((preal * preal) * (preal * preal)) set" where
wenzelm@19765
    18
  "realrel = {p. \<exists>x1 y1 x2 y2. p = ((x1,y1),(x2,y2)) & x1+y2 = x2+y1}"
paulson@14269
    19
paulson@14484
    20
typedef (Real)  real = "UNIV//realrel"
paulson@14269
    21
  by (auto simp add: quotient_def)
paulson@5588
    22
wenzelm@14691
    23
instance real :: "{ord, zero, one, plus, times, minus, inverse}" ..
paulson@14269
    24
wenzelm@19765
    25
definition
paulson@14484
    26
paulson@14484
    27
  (** these don't use the overloaded "real" function: users don't see them **)
paulson@14484
    28
wenzelm@21404
    29
  real_of_preal :: "preal => real" where
wenzelm@19765
    30
  "real_of_preal m = Abs_Real(realrel``{(m + preal_of_rat 1, preal_of_rat 1)})"
paulson@14484
    31
paulson@14269
    32
consts
paulson@14269
    33
   (*overloaded constant for injecting other types into "real"*)
paulson@14269
    34
   real :: "'a => real"
paulson@5588
    35
paulson@5588
    36
paulson@14269
    37
defs (overloaded)
paulson@5588
    38
paulson@14269
    39
  real_zero_def:
paulson@14484
    40
  "0 == Abs_Real(realrel``{(preal_of_rat 1, preal_of_rat 1)})"
paulson@12018
    41
paulson@14269
    42
  real_one_def:
paulson@14484
    43
  "1 == Abs_Real(realrel``
paulson@14365
    44
               {(preal_of_rat 1 + preal_of_rat 1,
paulson@14365
    45
		 preal_of_rat 1)})"
paulson@5588
    46
paulson@14269
    47
  real_minus_def:
paulson@14484
    48
  "- r ==  contents (\<Union>(x,y) \<in> Rep_Real(r). { Abs_Real(realrel``{(y,x)}) })"
paulson@14484
    49
paulson@14484
    50
  real_add_def:
paulson@14484
    51
   "z + w ==
paulson@14484
    52
       contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w).
paulson@14484
    53
		 { Abs_Real(realrel``{(x+u, y+v)}) })"
bauerg@10606
    54
paulson@14269
    55
  real_diff_def:
paulson@14484
    56
   "r - (s::real) == r + - s"
paulson@14484
    57
paulson@14484
    58
  real_mult_def:
paulson@14484
    59
    "z * w ==
paulson@14484
    60
       contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w).
paulson@14484
    61
		 { Abs_Real(realrel``{(x*u + y*v, x*v + y*u)}) })"
paulson@5588
    62
paulson@14269
    63
  real_inverse_def:
wenzelm@11713
    64
  "inverse (R::real) == (SOME S. (R = 0 & S = 0) | S * R = 1)"
paulson@5588
    65
paulson@14269
    66
  real_divide_def:
bauerg@10606
    67
  "R / (S::real) == R * inverse S"
paulson@14269
    68
paulson@14484
    69
  real_le_def:
paulson@14484
    70
   "z \<le> (w::real) == 
paulson@14484
    71
    \<exists>x y u v. x+v \<le> u+y & (x,y) \<in> Rep_Real z & (u,v) \<in> Rep_Real w"
paulson@5588
    72
paulson@14365
    73
  real_less_def: "(x < (y::real)) == (x \<le> y & x \<noteq> y)"
paulson@14365
    74
huffman@22962
    75
  real_abs_def:  "abs (r::real) == (if r < 0 then - r else r)"
paulson@14334
    76
paulson@14334
    77
paulson@14365
    78
paulson@14329
    79
subsection{*Proving that realrel is an equivalence relation*}
paulson@14269
    80
paulson@14270
    81
lemma preal_trans_lemma:
paulson@14365
    82
  assumes "x + y1 = x1 + y"
paulson@14365
    83
      and "x + y2 = x2 + y"
paulson@14365
    84
  shows "x1 + y2 = x2 + (y1::preal)"
paulson@14365
    85
proof -
paulson@14365
    86
  have "(x1 + y2) + x = (x + y2) + x1" by (simp add: preal_add_ac) 
paulson@14365
    87
  also have "... = (x2 + y) + x1"  by (simp add: prems)
paulson@14365
    88
  also have "... = x2 + (x1 + y)"  by (simp add: preal_add_ac)
paulson@14365
    89
  also have "... = x2 + (x + y1)"  by (simp add: prems)
paulson@14365
    90
  also have "... = (x2 + y1) + x"  by (simp add: preal_add_ac)
paulson@14365
    91
  finally have "(x1 + y2) + x = (x2 + y1) + x" .
paulson@14365
    92
  thus ?thesis by (simp add: preal_add_right_cancel_iff) 
paulson@14365
    93
qed
paulson@14365
    94
paulson@14269
    95
paulson@14484
    96
lemma realrel_iff [simp]: "(((x1,y1),(x2,y2)) \<in> realrel) = (x1 + y2 = x2 + y1)"
paulson@14484
    97
by (simp add: realrel_def)
paulson@14269
    98
paulson@14269
    99
lemma equiv_realrel: "equiv UNIV realrel"
paulson@14365
   100
apply (auto simp add: equiv_def refl_def sym_def trans_def realrel_def)
paulson@14365
   101
apply (blast dest: preal_trans_lemma) 
paulson@14269
   102
done
paulson@14269
   103
paulson@14497
   104
text{*Reduces equality of equivalence classes to the @{term realrel} relation:
paulson@14497
   105
  @{term "(realrel `` {x} = realrel `` {y}) = ((x,y) \<in> realrel)"} *}
paulson@14269
   106
lemmas equiv_realrel_iff = 
paulson@14269
   107
       eq_equiv_class_iff [OF equiv_realrel UNIV_I UNIV_I]
paulson@14269
   108
paulson@14269
   109
declare equiv_realrel_iff [simp]
paulson@14269
   110
paulson@14497
   111
paulson@14484
   112
lemma realrel_in_real [simp]: "realrel``{(x,y)}: Real"
paulson@14484
   113
by (simp add: Real_def realrel_def quotient_def, blast)
paulson@14269
   114
huffman@22958
   115
declare Abs_Real_inject [simp]
paulson@14484
   116
declare Abs_Real_inverse [simp]
paulson@14269
   117
paulson@14269
   118
paulson@14484
   119
text{*Case analysis on the representation of a real number as an equivalence
paulson@14484
   120
      class of pairs of positive reals.*}
paulson@14484
   121
lemma eq_Abs_Real [case_names Abs_Real, cases type: real]: 
paulson@14484
   122
     "(!!x y. z = Abs_Real(realrel``{(x,y)}) ==> P) ==> P"
paulson@14484
   123
apply (rule Rep_Real [of z, unfolded Real_def, THEN quotientE])
paulson@14484
   124
apply (drule arg_cong [where f=Abs_Real])
paulson@14484
   125
apply (auto simp add: Rep_Real_inverse)
paulson@14269
   126
done
paulson@14269
   127
paulson@14269
   128
paulson@14329
   129
subsection{*Congruence property for addition*}
paulson@14269
   130
paulson@14269
   131
lemma real_add_congruent2_lemma:
paulson@14269
   132
     "[|a + ba = aa + b; ab + bc = ac + bb|]
paulson@14269
   133
      ==> a + ab + (ba + bc) = aa + ac + (b + (bb::preal))"
paulson@14269
   134
apply (simp add: preal_add_assoc) 
paulson@14269
   135
apply (rule preal_add_left_commute [of ab, THEN ssubst])
paulson@14269
   136
apply (simp add: preal_add_assoc [symmetric])
paulson@14269
   137
apply (simp add: preal_add_ac)
paulson@14269
   138
done
paulson@14269
   139
paulson@14269
   140
lemma real_add:
paulson@14497
   141
     "Abs_Real (realrel``{(x,y)}) + Abs_Real (realrel``{(u,v)}) =
paulson@14497
   142
      Abs_Real (realrel``{(x+u, y+v)})"
paulson@14497
   143
proof -
paulson@15169
   144
  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). {Abs_Real (realrel `` {(x+u, y+v)})}) w) z)
paulson@15169
   145
        respects2 realrel"
paulson@14497
   146
    by (simp add: congruent2_def, blast intro: real_add_congruent2_lemma) 
paulson@14497
   147
  thus ?thesis
paulson@14497
   148
    by (simp add: real_add_def UN_UN_split_split_eq
paulson@14658
   149
                  UN_equiv_class2 [OF equiv_realrel equiv_realrel])
paulson@14497
   150
qed
paulson@14269
   151
paulson@14269
   152
lemma real_add_commute: "(z::real) + w = w + z"
paulson@14497
   153
by (cases z, cases w, simp add: real_add preal_add_ac)
paulson@14269
   154
paulson@14269
   155
lemma real_add_assoc: "((z1::real) + z2) + z3 = z1 + (z2 + z3)"
paulson@14497
   156
by (cases z1, cases z2, cases z3, simp add: real_add preal_add_assoc)
paulson@14269
   157
paulson@14269
   158
lemma real_add_zero_left: "(0::real) + z = z"
paulson@14497
   159
by (cases z, simp add: real_add real_zero_def preal_add_ac)
paulson@14269
   160
obua@14738
   161
instance real :: comm_monoid_add
paulson@14269
   162
  by (intro_classes,
paulson@14269
   163
      (assumption | 
paulson@14269
   164
       rule real_add_commute real_add_assoc real_add_zero_left)+)
paulson@14269
   165
paulson@14269
   166
paulson@14334
   167
subsection{*Additive Inverse on real*}
paulson@14334
   168
paulson@14484
   169
lemma real_minus: "- Abs_Real(realrel``{(x,y)}) = Abs_Real(realrel `` {(y,x)})"
paulson@14484
   170
proof -
paulson@15169
   171
  have "(\<lambda>(x,y). {Abs_Real (realrel``{(y,x)})}) respects realrel"
paulson@14484
   172
    by (simp add: congruent_def preal_add_commute) 
paulson@14484
   173
  thus ?thesis
paulson@14484
   174
    by (simp add: real_minus_def UN_equiv_class [OF equiv_realrel])
paulson@14484
   175
qed
paulson@14334
   176
paulson@14334
   177
lemma real_add_minus_left: "(-z) + z = (0::real)"
paulson@14497
   178
by (cases z, simp add: real_minus real_add real_zero_def preal_add_commute)
paulson@14269
   179
paulson@14269
   180
paulson@14329
   181
subsection{*Congruence property for multiplication*}
paulson@14269
   182
paulson@14329
   183
lemma real_mult_congruent2_lemma:
paulson@14329
   184
     "!!(x1::preal). [| x1 + y2 = x2 + y1 |] ==>
paulson@14484
   185
          x * x1 + y * y1 + (x * y2 + y * x2) =
paulson@14484
   186
          x * x2 + y * y2 + (x * y1 + y * x1)"
paulson@14484
   187
apply (simp add: preal_add_left_commute preal_add_assoc [symmetric])
paulson@14269
   188
apply (simp add: preal_add_assoc preal_add_mult_distrib2 [symmetric])
paulson@14269
   189
apply (simp add: preal_add_commute)
paulson@14269
   190
done
paulson@14269
   191
paulson@14269
   192
lemma real_mult_congruent2:
paulson@15169
   193
    "(%p1 p2.
paulson@14484
   194
        (%(x1,y1). (%(x2,y2). 
paulson@15169
   195
          { Abs_Real (realrel``{(x1*x2 + y1*y2, x1*y2+y1*x2)}) }) p2) p1)
paulson@15169
   196
     respects2 realrel"
paulson@14658
   197
apply (rule congruent2_commuteI [OF equiv_realrel], clarify)
paulson@14269
   198
apply (simp add: preal_mult_commute preal_add_commute)
paulson@14269
   199
apply (auto simp add: real_mult_congruent2_lemma)
paulson@14269
   200
done
paulson@14269
   201
paulson@14269
   202
lemma real_mult:
paulson@14484
   203
      "Abs_Real((realrel``{(x1,y1)})) * Abs_Real((realrel``{(x2,y2)})) =
paulson@14484
   204
       Abs_Real(realrel `` {(x1*x2+y1*y2,x1*y2+y1*x2)})"
paulson@14484
   205
by (simp add: real_mult_def UN_UN_split_split_eq
paulson@14658
   206
         UN_equiv_class2 [OF equiv_realrel equiv_realrel real_mult_congruent2])
paulson@14269
   207
paulson@14269
   208
lemma real_mult_commute: "(z::real) * w = w * z"
paulson@14497
   209
by (cases z, cases w, simp add: real_mult preal_add_ac preal_mult_ac)
paulson@14269
   210
paulson@14269
   211
lemma real_mult_assoc: "((z1::real) * z2) * z3 = z1 * (z2 * z3)"
paulson@14484
   212
apply (cases z1, cases z2, cases z3)
paulson@14484
   213
apply (simp add: real_mult preal_add_mult_distrib2 preal_add_ac preal_mult_ac)
paulson@14269
   214
done
paulson@14269
   215
paulson@14269
   216
lemma real_mult_1: "(1::real) * z = z"
paulson@14484
   217
apply (cases z)
paulson@14484
   218
apply (simp add: real_mult real_one_def preal_add_mult_distrib2
paulson@14484
   219
                 preal_mult_1_right preal_mult_ac preal_add_ac)
paulson@14269
   220
done
paulson@14269
   221
paulson@14269
   222
lemma real_add_mult_distrib: "((z1::real) + z2) * w = (z1 * w) + (z2 * w)"
paulson@14484
   223
apply (cases z1, cases z2, cases w)
paulson@14484
   224
apply (simp add: real_add real_mult preal_add_mult_distrib2 
paulson@14484
   225
                 preal_add_ac preal_mult_ac)
paulson@14269
   226
done
paulson@14269
   227
paulson@14329
   228
text{*one and zero are distinct*}
paulson@14365
   229
lemma real_zero_not_eq_one: "0 \<noteq> (1::real)"
paulson@14484
   230
proof -
paulson@14484
   231
  have "preal_of_rat 1 < preal_of_rat 1 + preal_of_rat 1"
paulson@14484
   232
    by (simp add: preal_self_less_add_left) 
paulson@14484
   233
  thus ?thesis
paulson@14484
   234
    by (simp add: real_zero_def real_one_def preal_add_right_cancel_iff)
paulson@14484
   235
qed
paulson@14269
   236
paulson@14329
   237
subsection{*existence of inverse*}
paulson@14365
   238
paulson@14484
   239
lemma real_zero_iff: "Abs_Real (realrel `` {(x, x)}) = 0"
paulson@14497
   240
by (simp add: real_zero_def preal_add_commute)
paulson@14269
   241
paulson@14365
   242
text{*Instead of using an existential quantifier and constructing the inverse
paulson@14365
   243
within the proof, we could define the inverse explicitly.*}
paulson@14365
   244
paulson@14365
   245
lemma real_mult_inverse_left_ex: "x \<noteq> 0 ==> \<exists>y. y*x = (1::real)"
paulson@14484
   246
apply (simp add: real_zero_def real_one_def, cases x)
paulson@14269
   247
apply (cut_tac x = xa and y = y in linorder_less_linear)
paulson@14365
   248
apply (auto dest!: less_add_left_Ex simp add: real_zero_iff)
paulson@14334
   249
apply (rule_tac
paulson@14484
   250
        x = "Abs_Real (realrel `` { (preal_of_rat 1, 
paulson@14365
   251
                            inverse (D) + preal_of_rat 1)}) " 
paulson@14334
   252
       in exI)
paulson@14334
   253
apply (rule_tac [2]
paulson@14484
   254
        x = "Abs_Real (realrel `` { (inverse (D) + preal_of_rat 1,
paulson@14365
   255
                   preal_of_rat 1)})" 
paulson@14334
   256
       in exI)
paulson@14365
   257
apply (auto simp add: real_mult preal_mult_1_right
paulson@14329
   258
              preal_add_mult_distrib2 preal_add_mult_distrib preal_mult_1
paulson@14365
   259
              preal_mult_inverse_right preal_add_ac preal_mult_ac)
paulson@14269
   260
done
paulson@14269
   261
paulson@14365
   262
lemma real_mult_inverse_left: "x \<noteq> 0 ==> inverse(x)*x = (1::real)"
paulson@14484
   263
apply (simp add: real_inverse_def)
paulson@14365
   264
apply (frule real_mult_inverse_left_ex, safe)
paulson@14269
   265
apply (rule someI2, auto)
paulson@14269
   266
done
paulson@14334
   267
paulson@14341
   268
paulson@14341
   269
subsection{*The Real Numbers form a Field*}
paulson@14341
   270
paulson@14334
   271
instance real :: field
paulson@14334
   272
proof
paulson@14334
   273
  fix x y z :: real
paulson@14334
   274
  show "- x + x = 0" by (rule real_add_minus_left)
paulson@14334
   275
  show "x - y = x + (-y)" by (simp add: real_diff_def)
paulson@14334
   276
  show "(x * y) * z = x * (y * z)" by (rule real_mult_assoc)
paulson@14334
   277
  show "x * y = y * x" by (rule real_mult_commute)
paulson@14334
   278
  show "1 * x = x" by (rule real_mult_1)
paulson@14334
   279
  show "(x + y) * z = x * z + y * z" by (simp add: real_add_mult_distrib)
paulson@14334
   280
  show "0 \<noteq> (1::real)" by (rule real_zero_not_eq_one)
paulson@14365
   281
  show "x \<noteq> 0 ==> inverse x * x = 1" by (rule real_mult_inverse_left)
paulson@14430
   282
  show "x / y = x * inverse y" by (simp add: real_divide_def)
paulson@14334
   283
qed
paulson@14334
   284
paulson@14334
   285
paulson@14341
   286
text{*Inverse of zero!  Useful to simplify certain equations*}
paulson@14269
   287
paulson@14334
   288
lemma INVERSE_ZERO: "inverse 0 = (0::real)"
paulson@14484
   289
by (simp add: real_inverse_def)
paulson@14334
   290
paulson@14334
   291
instance real :: division_by_zero
paulson@14334
   292
proof
paulson@14334
   293
  show "inverse 0 = (0::real)" by (rule INVERSE_ZERO)
paulson@14334
   294
qed
paulson@14334
   295
paulson@14269
   296
paulson@14365
   297
subsection{*The @{text "\<le>"} Ordering*}
paulson@14269
   298
paulson@14365
   299
lemma real_le_refl: "w \<le> (w::real)"
paulson@14484
   300
by (cases w, force simp add: real_le_def)
paulson@14269
   301
paulson@14378
   302
text{*The arithmetic decision procedure is not set up for type preal.
paulson@14378
   303
  This lemma is currently unused, but it could simplify the proofs of the
paulson@14378
   304
  following two lemmas.*}
paulson@14378
   305
lemma preal_eq_le_imp_le:
paulson@14378
   306
  assumes eq: "a+b = c+d" and le: "c \<le> a"
paulson@14378
   307
  shows "b \<le> (d::preal)"
paulson@14378
   308
proof -
paulson@14378
   309
  have "c+d \<le> a+d" by (simp add: prems preal_cancels)
paulson@14378
   310
  hence "a+b \<le> a+d" by (simp add: prems)
paulson@14378
   311
  thus "b \<le> d" by (simp add: preal_cancels)
paulson@14378
   312
qed
paulson@14378
   313
paulson@14378
   314
lemma real_le_lemma:
paulson@14378
   315
  assumes l: "u1 + v2 \<le> u2 + v1"
paulson@14378
   316
      and "x1 + v1 = u1 + y1"
paulson@14378
   317
      and "x2 + v2 = u2 + y2"
paulson@14378
   318
  shows "x1 + y2 \<le> x2 + (y1::preal)"
paulson@14365
   319
proof -
paulson@14378
   320
  have "(x1+v1) + (u2+y2) = (u1+y1) + (x2+v2)" by (simp add: prems)
paulson@14378
   321
  hence "(x1+y2) + (u2+v1) = (x2+y1) + (u1+v2)" by (simp add: preal_add_ac)
paulson@14378
   322
  also have "... \<le> (x2+y1) + (u2+v1)"
paulson@14365
   323
         by (simp add: prems preal_add_le_cancel_left)
paulson@14378
   324
  finally show ?thesis by (simp add: preal_add_le_cancel_right)
paulson@14378
   325
qed						 
paulson@14378
   326
paulson@14378
   327
lemma real_le: 
paulson@14484
   328
     "(Abs_Real(realrel``{(x1,y1)}) \<le> Abs_Real(realrel``{(x2,y2)})) =  
paulson@14484
   329
      (x1 + y2 \<le> x2 + y1)"
paulson@14378
   330
apply (simp add: real_le_def) 
paulson@14387
   331
apply (auto intro: real_le_lemma)
paulson@14378
   332
done
paulson@14378
   333
paulson@14378
   334
lemma real_le_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::real)"
nipkow@15542
   335
by (cases z, cases w, simp add: real_le)
paulson@14378
   336
paulson@14378
   337
lemma real_trans_lemma:
paulson@14378
   338
  assumes "x + v \<le> u + y"
paulson@14378
   339
      and "u + v' \<le> u' + v"
paulson@14378
   340
      and "x2 + v2 = u2 + y2"
paulson@14378
   341
  shows "x + v' \<le> u' + (y::preal)"
paulson@14378
   342
proof -
paulson@14378
   343
  have "(x+v') + (u+v) = (x+v) + (u+v')" by (simp add: preal_add_ac)
paulson@14378
   344
  also have "... \<le> (u+y) + (u+v')" 
paulson@14378
   345
    by (simp add: preal_add_le_cancel_right prems) 
paulson@14378
   346
  also have "... \<le> (u+y) + (u'+v)" 
paulson@14378
   347
    by (simp add: preal_add_le_cancel_left prems) 
paulson@14378
   348
  also have "... = (u'+y) + (u+v)"  by (simp add: preal_add_ac)
paulson@14378
   349
  finally show ?thesis by (simp add: preal_add_le_cancel_right)
nipkow@15542
   350
qed
paulson@14269
   351
paulson@14365
   352
lemma real_le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::real)"
paulson@14484
   353
apply (cases i, cases j, cases k)
paulson@14484
   354
apply (simp add: real_le)
paulson@14378
   355
apply (blast intro: real_trans_lemma) 
paulson@14334
   356
done
paulson@14334
   357
paulson@14334
   358
(* Axiom 'order_less_le' of class 'order': *)
paulson@14334
   359
lemma real_less_le: "((w::real) < z) = (w \<le> z & w \<noteq> z)"
paulson@14365
   360
by (simp add: real_less_def)
paulson@14365
   361
paulson@14365
   362
instance real :: order
paulson@14365
   363
proof qed
paulson@14365
   364
 (assumption |
paulson@14365
   365
  rule real_le_refl real_le_trans real_le_anti_sym real_less_le)+
paulson@14365
   366
paulson@14378
   367
(* Axiom 'linorder_linear' of class 'linorder': *)
paulson@14378
   368
lemma real_le_linear: "(z::real) \<le> w | w \<le> z"
paulson@14484
   369
apply (cases z, cases w) 
paulson@14378
   370
apply (auto simp add: real_le real_zero_def preal_add_ac preal_cancels)
paulson@14334
   371
done
paulson@14334
   372
paulson@14334
   373
paulson@14334
   374
instance real :: linorder
paulson@14334
   375
  by (intro_classes, rule real_le_linear)
paulson@14334
   376
paulson@14334
   377
paulson@14378
   378
lemma real_le_eq_diff: "(x \<le> y) = (x-y \<le> (0::real))"
paulson@14484
   379
apply (cases x, cases y) 
paulson@14378
   380
apply (auto simp add: real_le real_zero_def real_diff_def real_add real_minus
paulson@14378
   381
                      preal_add_ac)
paulson@14378
   382
apply (simp_all add: preal_add_assoc [symmetric] preal_cancels)
nipkow@15542
   383
done
paulson@14378
   384
paulson@14484
   385
lemma real_add_left_mono: 
paulson@14484
   386
  assumes le: "x \<le> y" shows "z + x \<le> z + (y::real)"
paulson@14484
   387
proof -
paulson@14484
   388
  have "z + x - (z + y) = (z + -z) + (x - y)"
paulson@14484
   389
    by (simp add: diff_minus add_ac) 
paulson@14484
   390
  with le show ?thesis 
obua@14754
   391
    by (simp add: real_le_eq_diff[of x] real_le_eq_diff[of "z+x"] diff_minus)
paulson@14484
   392
qed
paulson@14334
   393
paulson@14365
   394
lemma real_sum_gt_zero_less: "(0 < S + (-W::real)) ==> (W < S)"
paulson@14365
   395
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)
paulson@14365
   396
paulson@14365
   397
lemma real_less_sum_gt_zero: "(W < S) ==> (0 < S + (-W::real))"
paulson@14365
   398
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)
paulson@14334
   399
paulson@14334
   400
lemma real_mult_order: "[| 0 < x; 0 < y |] ==> (0::real) < x * y"
paulson@14484
   401
apply (cases x, cases y)
paulson@14378
   402
apply (simp add: linorder_not_le [where 'a = real, symmetric] 
paulson@14378
   403
                 linorder_not_le [where 'a = preal] 
paulson@14378
   404
                  real_zero_def real_le real_mult)
paulson@14365
   405
  --{*Reduce to the (simpler) @{text "\<le>"} relation *}
wenzelm@16973
   406
apply (auto dest!: less_add_left_Ex
paulson@14365
   407
     simp add: preal_add_ac preal_mult_ac 
wenzelm@16973
   408
          preal_add_mult_distrib2 preal_cancels preal_self_less_add_left)
paulson@14334
   409
done
paulson@14334
   410
paulson@14334
   411
lemma real_mult_less_mono2: "[| (0::real) < z; x < y |] ==> z * x < z * y"
paulson@14334
   412
apply (rule real_sum_gt_zero_less)
paulson@14334
   413
apply (drule real_less_sum_gt_zero [of x y])
paulson@14334
   414
apply (drule real_mult_order, assumption)
paulson@14334
   415
apply (simp add: right_distrib)
paulson@14334
   416
done
paulson@14334
   417
haftmann@22456
   418
instance real :: distrib_lattice
haftmann@22456
   419
  "inf x y \<equiv> min x y"
haftmann@22456
   420
  "sup x y \<equiv> max x y"
haftmann@22456
   421
  by default (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
haftmann@22456
   422
paulson@14378
   423
paulson@14334
   424
subsection{*The Reals Form an Ordered Field*}
paulson@14334
   425
paulson@14334
   426
instance real :: ordered_field
paulson@14334
   427
proof
paulson@14334
   428
  fix x y z :: real
paulson@14334
   429
  show "x \<le> y ==> z + x \<le> z + y" by (rule real_add_left_mono)
huffman@22962
   430
  show "x < y ==> 0 < z ==> z * x < z * y" by (rule real_mult_less_mono2)
huffman@22962
   431
  show "\<bar>x\<bar> = (if x < 0 then -x else x)" by (simp only: real_abs_def)
paulson@14334
   432
qed
paulson@14334
   433
paulson@14365
   434
text{*The function @{term real_of_preal} requires many proofs, but it seems
paulson@14365
   435
to be essential for proving completeness of the reals from that of the
paulson@14365
   436
positive reals.*}
paulson@14365
   437
paulson@14365
   438
lemma real_of_preal_add:
paulson@14365
   439
     "real_of_preal ((x::preal) + y) = real_of_preal x + real_of_preal y"
paulson@14365
   440
by (simp add: real_of_preal_def real_add preal_add_mult_distrib preal_mult_1 
paulson@14365
   441
              preal_add_ac)
paulson@14365
   442
paulson@14365
   443
lemma real_of_preal_mult:
paulson@14365
   444
     "real_of_preal ((x::preal) * y) = real_of_preal x* real_of_preal y"
paulson@14365
   445
by (simp add: real_of_preal_def real_mult preal_add_mult_distrib2
paulson@14365
   446
              preal_mult_1 preal_mult_1_right preal_add_ac preal_mult_ac)
paulson@14365
   447
paulson@14365
   448
paulson@14365
   449
text{*Gleason prop 9-4.4 p 127*}
paulson@14365
   450
lemma real_of_preal_trichotomy:
paulson@14365
   451
      "\<exists>m. (x::real) = real_of_preal m | x = 0 | x = -(real_of_preal m)"
paulson@14484
   452
apply (simp add: real_of_preal_def real_zero_def, cases x)
paulson@14365
   453
apply (auto simp add: real_minus preal_add_ac)
paulson@14365
   454
apply (cut_tac x = x and y = y in linorder_less_linear)
paulson@14365
   455
apply (auto dest!: less_add_left_Ex simp add: preal_add_assoc [symmetric])
paulson@14365
   456
done
paulson@14365
   457
paulson@14365
   458
lemma real_of_preal_leD:
paulson@14365
   459
      "real_of_preal m1 \<le> real_of_preal m2 ==> m1 \<le> m2"
paulson@14484
   460
by (simp add: real_of_preal_def real_le preal_cancels)
paulson@14365
   461
paulson@14365
   462
lemma real_of_preal_lessI: "m1 < m2 ==> real_of_preal m1 < real_of_preal m2"
paulson@14365
   463
by (auto simp add: real_of_preal_leD linorder_not_le [symmetric])
paulson@14365
   464
paulson@14365
   465
lemma real_of_preal_lessD:
paulson@14365
   466
      "real_of_preal m1 < real_of_preal m2 ==> m1 < m2"
paulson@14484
   467
by (simp add: real_of_preal_def real_le linorder_not_le [symmetric] 
paulson@14484
   468
              preal_cancels) 
paulson@14484
   469
paulson@14365
   470
paulson@14365
   471
lemma real_of_preal_less_iff [simp]:
paulson@14365
   472
     "(real_of_preal m1 < real_of_preal m2) = (m1 < m2)"
paulson@14365
   473
by (blast intro: real_of_preal_lessI real_of_preal_lessD)
paulson@14365
   474
paulson@14365
   475
lemma real_of_preal_le_iff:
paulson@14365
   476
     "(real_of_preal m1 \<le> real_of_preal m2) = (m1 \<le> m2)"
paulson@14365
   477
by (simp add: linorder_not_less [symmetric]) 
paulson@14365
   478
paulson@14365
   479
lemma real_of_preal_zero_less: "0 < real_of_preal m"
paulson@14365
   480
apply (auto simp add: real_zero_def real_of_preal_def real_less_def real_le_def
paulson@14365
   481
            preal_add_ac preal_cancels)
paulson@14365
   482
apply (simp_all add: preal_add_assoc [symmetric] preal_cancels)
paulson@14365
   483
apply (blast intro: preal_self_less_add_left order_less_imp_le)
paulson@14365
   484
apply (insert preal_not_eq_self [of "preal_of_rat 1" m]) 
paulson@14365
   485
apply (simp add: preal_add_ac) 
paulson@14365
   486
done
paulson@14365
   487
paulson@14365
   488
lemma real_of_preal_minus_less_zero: "- real_of_preal m < 0"
paulson@14365
   489
by (simp add: real_of_preal_zero_less)
paulson@14365
   490
paulson@14365
   491
lemma real_of_preal_not_minus_gt_zero: "~ 0 < - real_of_preal m"
paulson@14484
   492
proof -
paulson@14484
   493
  from real_of_preal_minus_less_zero
paulson@14484
   494
  show ?thesis by (blast dest: order_less_trans)
paulson@14484
   495
qed
paulson@14365
   496
paulson@14365
   497
paulson@14365
   498
subsection{*Theorems About the Ordering*}
paulson@14365
   499
paulson@14365
   500
text{*obsolete but used a lot*}
paulson@14365
   501
paulson@14365
   502
lemma real_not_refl2: "x < y ==> x \<noteq> (y::real)"
paulson@14365
   503
by blast 
paulson@14365
   504
paulson@14365
   505
lemma real_le_imp_less_or_eq: "!!(x::real). x \<le> y ==> x < y | x = y"
paulson@14365
   506
by (simp add: order_le_less)
paulson@14365
   507
paulson@14365
   508
lemma real_gt_zero_preal_Ex: "(0 < x) = (\<exists>y. x = real_of_preal y)"
paulson@14365
   509
apply (auto simp add: real_of_preal_zero_less)
paulson@14365
   510
apply (cut_tac x = x in real_of_preal_trichotomy)
paulson@14365
   511
apply (blast elim!: real_of_preal_not_minus_gt_zero [THEN notE])
paulson@14365
   512
done
paulson@14365
   513
paulson@14365
   514
lemma real_gt_preal_preal_Ex:
paulson@14365
   515
     "real_of_preal z < x ==> \<exists>y. x = real_of_preal y"
paulson@14365
   516
by (blast dest!: real_of_preal_zero_less [THEN order_less_trans]
paulson@14365
   517
             intro: real_gt_zero_preal_Ex [THEN iffD1])
paulson@14365
   518
paulson@14365
   519
lemma real_ge_preal_preal_Ex:
paulson@14365
   520
     "real_of_preal z \<le> x ==> \<exists>y. x = real_of_preal y"
paulson@14365
   521
by (blast dest: order_le_imp_less_or_eq real_gt_preal_preal_Ex)
paulson@14365
   522
paulson@14365
   523
lemma real_less_all_preal: "y \<le> 0 ==> \<forall>x. y < real_of_preal x"
paulson@14365
   524
by (auto elim: order_le_imp_less_or_eq [THEN disjE] 
paulson@14365
   525
            intro: real_of_preal_zero_less [THEN [2] order_less_trans] 
paulson@14365
   526
            simp add: real_of_preal_zero_less)
paulson@14365
   527
paulson@14365
   528
lemma real_less_all_real2: "~ 0 < y ==> \<forall>x. y < real_of_preal x"
paulson@14365
   529
by (blast intro!: real_less_all_preal linorder_not_less [THEN iffD1])
paulson@14365
   530
paulson@14334
   531
lemma real_le_square [simp]: "(0::real) \<le> x*x"
paulson@14334
   532
 by (rule Ring_and_Field.zero_le_square)
paulson@14334
   533
paulson@14334
   534
paulson@14334
   535
subsection{*More Lemmas*}
paulson@14334
   536
paulson@14334
   537
lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
paulson@14334
   538
by auto
paulson@14334
   539
paulson@14334
   540
lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
paulson@14334
   541
by auto
paulson@14334
   542
paulson@14334
   543
text{*The precondition could be weakened to @{term "0\<le>x"}*}
paulson@14334
   544
lemma real_mult_less_mono:
paulson@14334
   545
     "[| u<v;  x<y;  (0::real) < v;  0 < x |] ==> u*x < v* y"
paulson@14334
   546
 by (simp add: Ring_and_Field.mult_strict_mono order_less_imp_le)
paulson@14334
   547
paulson@14334
   548
lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
paulson@14334
   549
  by (force elim: order_less_asym
paulson@14334
   550
            simp add: Ring_and_Field.mult_less_cancel_right)
paulson@14334
   551
paulson@14334
   552
lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
paulson@14365
   553
apply (simp add: mult_le_cancel_right)
paulson@14365
   554
apply (blast intro: elim: order_less_asym) 
paulson@14365
   555
done
paulson@14334
   556
paulson@14334
   557
lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
nipkow@15923
   558
by(simp add:mult_commute)
paulson@14334
   559
paulson@14334
   560
lemma real_add_order: "[| 0 < x; 0 < y |] ==> (0::real) < x + y"
huffman@22958
   561
by (rule add_pos_pos)
paulson@14334
   562
paulson@14334
   563
lemma real_le_add_order: "[| 0 \<le> x; 0 \<le> y |] ==> (0::real) \<le> x + y"
huffman@22958
   564
by (rule add_nonneg_nonneg)
paulson@14334
   565
paulson@14365
   566
lemma real_inverse_unique: "x*y = (1::real) ==> y = inverse x"
huffman@22958
   567
by (rule inverse_unique [symmetric])
paulson@14334
   568
paulson@14365
   569
lemma real_inverse_gt_one: "[| (0::real) < x; x < 1 |] ==> 1 < inverse x"
huffman@22958
   570
by (simp add: one_less_inverse_iff)
paulson@14334
   571
paulson@14334
   572
paulson@14365
   573
subsection{*Embedding the Integers into the Reals*}
paulson@14365
   574
paulson@14378
   575
defs (overloaded)
paulson@14378
   576
  real_of_nat_def: "real z == of_nat z"
paulson@14378
   577
  real_of_int_def: "real z == of_int z"
paulson@14365
   578
avigad@16819
   579
lemma real_eq_of_nat: "real = of_nat"
avigad@16819
   580
  apply (rule ext)
avigad@16819
   581
  apply (unfold real_of_nat_def)
avigad@16819
   582
  apply (rule refl)
avigad@16819
   583
  done
avigad@16819
   584
avigad@16819
   585
lemma real_eq_of_int: "real = of_int"
avigad@16819
   586
  apply (rule ext)
avigad@16819
   587
  apply (unfold real_of_int_def)
avigad@16819
   588
  apply (rule refl)
avigad@16819
   589
  done
avigad@16819
   590
paulson@14365
   591
lemma real_of_int_zero [simp]: "real (0::int) = 0"  
paulson@14378
   592
by (simp add: real_of_int_def) 
paulson@14365
   593
paulson@14365
   594
lemma real_of_one [simp]: "real (1::int) = (1::real)"
paulson@14378
   595
by (simp add: real_of_int_def) 
paulson@14334
   596
avigad@16819
   597
lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
paulson@14378
   598
by (simp add: real_of_int_def) 
paulson@14365
   599
avigad@16819
   600
lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
paulson@14378
   601
by (simp add: real_of_int_def) 
avigad@16819
   602
avigad@16819
   603
lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
avigad@16819
   604
by (simp add: real_of_int_def) 
paulson@14365
   605
avigad@16819
   606
lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
paulson@14378
   607
by (simp add: real_of_int_def) 
paulson@14334
   608
avigad@16819
   609
lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
avigad@16819
   610
  apply (subst real_eq_of_int)+
avigad@16819
   611
  apply (rule of_int_setsum)
avigad@16819
   612
done
avigad@16819
   613
avigad@16819
   614
lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
avigad@16819
   615
    (PROD x:A. real(f x))"
avigad@16819
   616
  apply (subst real_eq_of_int)+
avigad@16819
   617
  apply (rule of_int_setprod)
avigad@16819
   618
done
paulson@14365
   619
paulson@14365
   620
lemma real_of_int_zero_cancel [simp]: "(real x = 0) = (x = (0::int))"
paulson@14378
   621
by (simp add: real_of_int_def) 
paulson@14365
   622
paulson@14365
   623
lemma real_of_int_inject [iff]: "(real (x::int) = real y) = (x = y)"
paulson@14378
   624
by (simp add: real_of_int_def) 
paulson@14365
   625
paulson@14365
   626
lemma real_of_int_less_iff [iff]: "(real (x::int) < real y) = (x < y)"
paulson@14378
   627
by (simp add: real_of_int_def) 
paulson@14365
   628
paulson@14365
   629
lemma real_of_int_le_iff [simp]: "(real (x::int) \<le> real y) = (x \<le> y)"
paulson@14378
   630
by (simp add: real_of_int_def) 
paulson@14365
   631
avigad@16819
   632
lemma real_of_int_gt_zero_cancel_iff [simp]: "(0 < real (n::int)) = (0 < n)"
avigad@16819
   633
by (simp add: real_of_int_def) 
avigad@16819
   634
avigad@16819
   635
lemma real_of_int_ge_zero_cancel_iff [simp]: "(0 <= real (n::int)) = (0 <= n)"
avigad@16819
   636
by (simp add: real_of_int_def) 
avigad@16819
   637
avigad@16819
   638
lemma real_of_int_lt_zero_cancel_iff [simp]: "(real (n::int) < 0) = (n < 0)"
avigad@16819
   639
by (simp add: real_of_int_def)
avigad@16819
   640
avigad@16819
   641
lemma real_of_int_le_zero_cancel_iff [simp]: "(real (n::int) <= 0) = (n <= 0)"
avigad@16819
   642
by (simp add: real_of_int_def)
avigad@16819
   643
avigad@16888
   644
lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
avigad@16888
   645
by (auto simp add: abs_if)
avigad@16888
   646
avigad@16819
   647
lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
avigad@16819
   648
  apply (subgoal_tac "real n + 1 = real (n + 1)")
avigad@16819
   649
  apply (simp del: real_of_int_add)
avigad@16819
   650
  apply auto
avigad@16819
   651
done
avigad@16819
   652
avigad@16819
   653
lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
avigad@16819
   654
  apply (subgoal_tac "real m + 1 = real (m + 1)")
avigad@16819
   655
  apply (simp del: real_of_int_add)
avigad@16819
   656
  apply simp
avigad@16819
   657
done
avigad@16819
   658
avigad@16819
   659
lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
avigad@16819
   660
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
   661
proof -
avigad@16819
   662
  assume "d ~= 0"
avigad@16819
   663
  have "x = (x div d) * d + x mod d"
avigad@16819
   664
    by auto
avigad@16819
   665
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
   666
    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
avigad@16819
   667
  then have "real x / real d = ... / real d"
avigad@16819
   668
    by simp
avigad@16819
   669
  then show ?thesis
avigad@16819
   670
    by (auto simp add: add_divide_distrib ring_eq_simps prems)
avigad@16819
   671
qed
avigad@16819
   672
avigad@16819
   673
lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
avigad@16819
   674
    real(n div d) = real n / real d"
avigad@16819
   675
  apply (frule real_of_int_div_aux [of d n])
avigad@16819
   676
  apply simp
avigad@16819
   677
  apply (simp add: zdvd_iff_zmod_eq_0)
avigad@16819
   678
done
avigad@16819
   679
avigad@16819
   680
lemma real_of_int_div2:
avigad@16819
   681
  "0 <= real (n::int) / real (x) - real (n div x)"
avigad@16819
   682
  apply (case_tac "x = 0")
avigad@16819
   683
  apply simp
avigad@16819
   684
  apply (case_tac "0 < x")
avigad@16819
   685
  apply (simp add: compare_rls)
avigad@16819
   686
  apply (subst real_of_int_div_aux)
avigad@16819
   687
  apply simp
avigad@16819
   688
  apply simp
avigad@16819
   689
  apply (subst zero_le_divide_iff)
avigad@16819
   690
  apply auto
avigad@16819
   691
  apply (simp add: compare_rls)
avigad@16819
   692
  apply (subst real_of_int_div_aux)
avigad@16819
   693
  apply simp
avigad@16819
   694
  apply simp
avigad@16819
   695
  apply (subst zero_le_divide_iff)
avigad@16819
   696
  apply auto
avigad@16819
   697
done
avigad@16819
   698
avigad@16819
   699
lemma real_of_int_div3:
avigad@16819
   700
  "real (n::int) / real (x) - real (n div x) <= 1"
avigad@16819
   701
  apply(case_tac "x = 0")
avigad@16819
   702
  apply simp
avigad@16819
   703
  apply (simp add: compare_rls)
avigad@16819
   704
  apply (subst real_of_int_div_aux)
avigad@16819
   705
  apply assumption
avigad@16819
   706
  apply simp
avigad@16819
   707
  apply (subst divide_le_eq)
avigad@16819
   708
  apply clarsimp
avigad@16819
   709
  apply (rule conjI)
avigad@16819
   710
  apply (rule impI)
avigad@16819
   711
  apply (rule order_less_imp_le)
avigad@16819
   712
  apply simp
avigad@16819
   713
  apply (rule impI)
avigad@16819
   714
  apply (rule order_less_imp_le)
avigad@16819
   715
  apply simp
avigad@16819
   716
done
avigad@16819
   717
avigad@16819
   718
lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
avigad@16819
   719
  by (insert real_of_int_div2 [of n x], simp)
paulson@14365
   720
paulson@14365
   721
subsection{*Embedding the Naturals into the Reals*}
paulson@14365
   722
paulson@14334
   723
lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
paulson@14365
   724
by (simp add: real_of_nat_def)
paulson@14334
   725
paulson@14334
   726
lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
paulson@14365
   727
by (simp add: real_of_nat_def)
paulson@14334
   728
paulson@14365
   729
lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
paulson@14378
   730
by (simp add: real_of_nat_def)
paulson@14334
   731
paulson@14334
   732
(*Not for addsimps: often the LHS is used to represent a positive natural*)
paulson@14334
   733
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
paulson@14378
   734
by (simp add: real_of_nat_def)
paulson@14334
   735
paulson@14334
   736
lemma real_of_nat_less_iff [iff]: 
paulson@14334
   737
     "(real (n::nat) < real m) = (n < m)"
paulson@14365
   738
by (simp add: real_of_nat_def)
paulson@14334
   739
paulson@14334
   740
lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
paulson@14378
   741
by (simp add: real_of_nat_def)
paulson@14334
   742
paulson@14334
   743
lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
paulson@14378
   744
by (simp add: real_of_nat_def zero_le_imp_of_nat)
paulson@14334
   745
paulson@14365
   746
lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
paulson@14378
   747
by (simp add: real_of_nat_def del: of_nat_Suc)
paulson@14365
   748
paulson@14334
   749
lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
paulson@14378
   750
by (simp add: real_of_nat_def)
paulson@14334
   751
avigad@16819
   752
lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
avigad@16819
   753
    (SUM x:A. real(f x))"
avigad@16819
   754
  apply (subst real_eq_of_nat)+
avigad@16819
   755
  apply (rule of_nat_setsum)
avigad@16819
   756
done
avigad@16819
   757
avigad@16819
   758
lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
avigad@16819
   759
    (PROD x:A. real(f x))"
avigad@16819
   760
  apply (subst real_eq_of_nat)+
avigad@16819
   761
  apply (rule of_nat_setprod)
avigad@16819
   762
done
avigad@16819
   763
avigad@16819
   764
lemma real_of_card: "real (card A) = setsum (%x.1) A"
avigad@16819
   765
  apply (subst card_eq_setsum)
avigad@16819
   766
  apply (subst real_of_nat_setsum)
avigad@16819
   767
  apply simp
avigad@16819
   768
done
avigad@16819
   769
paulson@14334
   770
lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
paulson@14378
   771
by (simp add: real_of_nat_def)
paulson@14334
   772
paulson@14387
   773
lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
paulson@14378
   774
by (simp add: real_of_nat_def)
paulson@14334
   775
paulson@14365
   776
lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
paulson@14378
   777
by (simp add: add: real_of_nat_def) 
paulson@14334
   778
paulson@14365
   779
lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
paulson@14378
   780
by (simp add: add: real_of_nat_def) 
paulson@14365
   781
paulson@14365
   782
lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
paulson@14378
   783
by (simp add: add: real_of_nat_def)
paulson@14334
   784
paulson@14365
   785
lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
paulson@14378
   786
by (simp add: add: real_of_nat_def)
paulson@14334
   787
paulson@14365
   788
lemma real_of_nat_ge_zero_cancel_iff [simp]: "(0 \<le> real (n::nat)) = (0 \<le> n)"
paulson@14378
   789
by (simp add: add: real_of_nat_def)
paulson@14334
   790
avigad@16819
   791
lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
avigad@16819
   792
  apply (subgoal_tac "real n + 1 = real (Suc n)")
avigad@16819
   793
  apply simp
avigad@16819
   794
  apply (auto simp add: real_of_nat_Suc)
avigad@16819
   795
done
avigad@16819
   796
avigad@16819
   797
lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
avigad@16819
   798
  apply (subgoal_tac "real m + 1 = real (Suc m)")
avigad@16819
   799
  apply (simp add: less_Suc_eq_le)
avigad@16819
   800
  apply (simp add: real_of_nat_Suc)
avigad@16819
   801
done
avigad@16819
   802
avigad@16819
   803
lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
avigad@16819
   804
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
   805
proof -
avigad@16819
   806
  assume "0 < d"
avigad@16819
   807
  have "x = (x div d) * d + x mod d"
avigad@16819
   808
    by auto
avigad@16819
   809
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
   810
    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
avigad@16819
   811
  then have "real x / real d = \<dots> / real d"
avigad@16819
   812
    by simp
avigad@16819
   813
  then show ?thesis
avigad@16819
   814
    by (auto simp add: add_divide_distrib ring_eq_simps prems)
avigad@16819
   815
qed
avigad@16819
   816
avigad@16819
   817
lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
avigad@16819
   818
    real(n div d) = real n / real d"
avigad@16819
   819
  apply (frule real_of_nat_div_aux [of d n])
avigad@16819
   820
  apply simp
avigad@16819
   821
  apply (subst dvd_eq_mod_eq_0 [THEN sym])
avigad@16819
   822
  apply assumption
avigad@16819
   823
done
avigad@16819
   824
avigad@16819
   825
lemma real_of_nat_div2:
avigad@16819
   826
  "0 <= real (n::nat) / real (x) - real (n div x)"
avigad@16819
   827
  apply(case_tac "x = 0")
avigad@16819
   828
  apply simp
avigad@16819
   829
  apply (simp add: compare_rls)
avigad@16819
   830
  apply (subst real_of_nat_div_aux)
avigad@16819
   831
  apply assumption
avigad@16819
   832
  apply simp
avigad@16819
   833
  apply (subst zero_le_divide_iff)
avigad@16819
   834
  apply simp
avigad@16819
   835
done
avigad@16819
   836
avigad@16819
   837
lemma real_of_nat_div3:
avigad@16819
   838
  "real (n::nat) / real (x) - real (n div x) <= 1"
avigad@16819
   839
  apply(case_tac "x = 0")
avigad@16819
   840
  apply simp
avigad@16819
   841
  apply (simp add: compare_rls)
avigad@16819
   842
  apply (subst real_of_nat_div_aux)
avigad@16819
   843
  apply assumption
avigad@16819
   844
  apply simp
avigad@16819
   845
done
avigad@16819
   846
avigad@16819
   847
lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
avigad@16819
   848
  by (insert real_of_nat_div2 [of n x], simp)
avigad@16819
   849
paulson@14365
   850
lemma real_of_int_real_of_nat: "real (int n) = real n"
paulson@14378
   851
by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)
paulson@14378
   852
paulson@14426
   853
lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
paulson@14426
   854
by (simp add: real_of_int_def real_of_nat_def)
paulson@14334
   855
avigad@16819
   856
lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
avigad@16819
   857
  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
avigad@16819
   858
  apply force
avigad@16819
   859
  apply (simp only: real_of_int_real_of_nat)
avigad@16819
   860
done
paulson@14387
   861
paulson@14387
   862
subsection{*Numerals and Arithmetic*}
paulson@14387
   863
paulson@14387
   864
instance real :: number ..
paulson@14387
   865
paulson@15013
   866
defs (overloaded)
haftmann@20485
   867
  real_number_of_def: "(number_of w :: real) == of_int w"
paulson@15013
   868
    --{*the type constraint is essential!*}
paulson@14387
   869
paulson@14387
   870
instance real :: number_ring
paulson@15013
   871
by (intro_classes, simp add: real_number_of_def) 
paulson@14387
   872
paulson@14387
   873
text{*Collapse applications of @{term real} to @{term number_of}*}
paulson@14387
   874
lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
paulson@14387
   875
by (simp add:  real_of_int_def of_int_number_of_eq)
paulson@14387
   876
paulson@14387
   877
lemma real_of_nat_number_of [simp]:
paulson@14387
   878
     "real (number_of v :: nat) =  
paulson@14387
   879
        (if neg (number_of v :: int) then 0  
paulson@14387
   880
         else (number_of v :: real))"
paulson@14387
   881
by (simp add: real_of_int_real_of_nat [symmetric] int_nat_number_of)
paulson@14387
   882
 
paulson@14387
   883
paulson@14387
   884
use "real_arith.ML"
paulson@14387
   885
paulson@14387
   886
setup real_arith_setup
paulson@14387
   887
kleing@19023
   888
kleing@19023
   889
lemma real_diff_mult_distrib:
kleing@19023
   890
  fixes a::real
kleing@19023
   891
  shows "a * (b - c) = a * b - a * c" 
kleing@19023
   892
proof -
kleing@19023
   893
  have "a * (b - c) = a * (b + -c)" by simp
kleing@19023
   894
  also have "\<dots> = (b + -c) * a" by simp
kleing@19023
   895
  also have "\<dots> = b*a + (-c)*a" by (rule real_add_mult_distrib)
kleing@19023
   896
  also have "\<dots> = a*b - a*c" by simp
kleing@19023
   897
  finally show ?thesis .
kleing@19023
   898
qed
kleing@19023
   899
kleing@19023
   900
paulson@14387
   901
subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
paulson@14387
   902
paulson@14387
   903
text{*Needed in this non-standard form by Hyperreal/Transcendental*}
paulson@14387
   904
lemma real_0_le_divide_iff:
paulson@14387
   905
     "((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
paulson@14387
   906
by (simp add: real_divide_def zero_le_mult_iff, auto)
paulson@14387
   907
paulson@14387
   908
lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
paulson@14387
   909
by arith
paulson@14387
   910
paulson@15085
   911
lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
paulson@14387
   912
by auto
paulson@14387
   913
paulson@15085
   914
lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
paulson@14387
   915
by auto
paulson@14387
   916
paulson@15085
   917
lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
paulson@14387
   918
by auto
paulson@14387
   919
paulson@15085
   920
lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
paulson@14387
   921
by auto
paulson@14387
   922
paulson@15085
   923
lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
paulson@14387
   924
by auto
paulson@14387
   925
paulson@14387
   926
paulson@14387
   927
(*
paulson@14387
   928
FIXME: we should have this, as for type int, but many proofs would break.
paulson@14387
   929
It replaces x+-y by x-y.
paulson@15086
   930
declare real_diff_def [symmetric, simp]
paulson@14387
   931
*)
paulson@14387
   932
paulson@14387
   933
paulson@14387
   934
subsubsection{*Density of the Reals*}
paulson@14387
   935
paulson@14387
   936
lemma real_lbound_gt_zero:
paulson@14387
   937
     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
paulson@14387
   938
apply (rule_tac x = " (min d1 d2) /2" in exI)
paulson@14387
   939
apply (simp add: min_def)
paulson@14387
   940
done
paulson@14387
   941
paulson@14387
   942
paulson@14387
   943
text{*Similar results are proved in @{text Ring_and_Field}*}
paulson@14387
   944
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
paulson@14387
   945
  by auto
paulson@14387
   946
paulson@14387
   947
lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
paulson@14387
   948
  by auto
paulson@14387
   949
paulson@14387
   950
paulson@14387
   951
subsection{*Absolute Value Function for the Reals*}
paulson@14387
   952
paulson@14387
   953
lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
paulson@15003
   954
by (simp add: abs_if)
paulson@14387
   955
paulson@14387
   956
lemma abs_interval_iff: "(abs x < r) = (-r < x & x < (r::real))"
paulson@14387
   957
by (force simp add: Ring_and_Field.abs_less_iff)
paulson@14387
   958
paulson@14387
   959
lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
obua@14738
   960
by (force simp add: OrderedGroup.abs_le_iff)
paulson@14387
   961
paulson@14387
   962
lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
paulson@15003
   963
by (simp add: abs_if)
paulson@14387
   964
paulson@14387
   965
lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
huffman@22958
   966
by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
paulson@14387
   967
paulson@14387
   968
lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
webertj@20217
   969
by simp
paulson@14387
   970
 
paulson@14387
   971
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
webertj@20217
   972
by simp
paulson@14387
   973
paulson@5588
   974
end