src/HOL/Real/RealDef.thy
author huffman
Mon May 14 08:15:13 2007 +0200 (2007-05-14)
changeset 22958 b3a5569a81e5
parent 22456 6070e48ecb78
child 22962 4bb05ba38939
permissions -rw-r--r--
cleaned up
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
paulson@14334
    75
  real_abs_def:  "abs (r::real) == (if 0 \<le> r 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@14334
   296
lemma real_mult_1_right: "z * (1::real) = z"
obua@14738
   297
  by (rule OrderedGroup.mult_1_right)
paulson@14269
   298
paulson@14269
   299
paulson@14365
   300
subsection{*The @{text "\<le>"} Ordering*}
paulson@14269
   301
paulson@14365
   302
lemma real_le_refl: "w \<le> (w::real)"
paulson@14484
   303
by (cases w, force simp add: real_le_def)
paulson@14269
   304
paulson@14378
   305
text{*The arithmetic decision procedure is not set up for type preal.
paulson@14378
   306
  This lemma is currently unused, but it could simplify the proofs of the
paulson@14378
   307
  following two lemmas.*}
paulson@14378
   308
lemma preal_eq_le_imp_le:
paulson@14378
   309
  assumes eq: "a+b = c+d" and le: "c \<le> a"
paulson@14378
   310
  shows "b \<le> (d::preal)"
paulson@14378
   311
proof -
paulson@14378
   312
  have "c+d \<le> a+d" by (simp add: prems preal_cancels)
paulson@14378
   313
  hence "a+b \<le> a+d" by (simp add: prems)
paulson@14378
   314
  thus "b \<le> d" by (simp add: preal_cancels)
paulson@14378
   315
qed
paulson@14378
   316
paulson@14378
   317
lemma real_le_lemma:
paulson@14378
   318
  assumes l: "u1 + v2 \<le> u2 + v1"
paulson@14378
   319
      and "x1 + v1 = u1 + y1"
paulson@14378
   320
      and "x2 + v2 = u2 + y2"
paulson@14378
   321
  shows "x1 + y2 \<le> x2 + (y1::preal)"
paulson@14365
   322
proof -
paulson@14378
   323
  have "(x1+v1) + (u2+y2) = (u1+y1) + (x2+v2)" by (simp add: prems)
paulson@14378
   324
  hence "(x1+y2) + (u2+v1) = (x2+y1) + (u1+v2)" by (simp add: preal_add_ac)
paulson@14378
   325
  also have "... \<le> (x2+y1) + (u2+v1)"
paulson@14365
   326
         by (simp add: prems preal_add_le_cancel_left)
paulson@14378
   327
  finally show ?thesis by (simp add: preal_add_le_cancel_right)
paulson@14378
   328
qed						 
paulson@14378
   329
paulson@14378
   330
lemma real_le: 
paulson@14484
   331
     "(Abs_Real(realrel``{(x1,y1)}) \<le> Abs_Real(realrel``{(x2,y2)})) =  
paulson@14484
   332
      (x1 + y2 \<le> x2 + y1)"
paulson@14378
   333
apply (simp add: real_le_def) 
paulson@14387
   334
apply (auto intro: real_le_lemma)
paulson@14378
   335
done
paulson@14378
   336
paulson@14378
   337
lemma real_le_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::real)"
nipkow@15542
   338
by (cases z, cases w, simp add: real_le)
paulson@14378
   339
paulson@14378
   340
lemma real_trans_lemma:
paulson@14378
   341
  assumes "x + v \<le> u + y"
paulson@14378
   342
      and "u + v' \<le> u' + v"
paulson@14378
   343
      and "x2 + v2 = u2 + y2"
paulson@14378
   344
  shows "x + v' \<le> u' + (y::preal)"
paulson@14378
   345
proof -
paulson@14378
   346
  have "(x+v') + (u+v) = (x+v) + (u+v')" by (simp add: preal_add_ac)
paulson@14378
   347
  also have "... \<le> (u+y) + (u+v')" 
paulson@14378
   348
    by (simp add: preal_add_le_cancel_right prems) 
paulson@14378
   349
  also have "... \<le> (u+y) + (u'+v)" 
paulson@14378
   350
    by (simp add: preal_add_le_cancel_left prems) 
paulson@14378
   351
  also have "... = (u'+y) + (u+v)"  by (simp add: preal_add_ac)
paulson@14378
   352
  finally show ?thesis by (simp add: preal_add_le_cancel_right)
nipkow@15542
   353
qed
paulson@14269
   354
paulson@14365
   355
lemma real_le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::real)"
paulson@14484
   356
apply (cases i, cases j, cases k)
paulson@14484
   357
apply (simp add: real_le)
paulson@14378
   358
apply (blast intro: real_trans_lemma) 
paulson@14334
   359
done
paulson@14334
   360
paulson@14334
   361
(* Axiom 'order_less_le' of class 'order': *)
paulson@14334
   362
lemma real_less_le: "((w::real) < z) = (w \<le> z & w \<noteq> z)"
paulson@14365
   363
by (simp add: real_less_def)
paulson@14365
   364
paulson@14365
   365
instance real :: order
paulson@14365
   366
proof qed
paulson@14365
   367
 (assumption |
paulson@14365
   368
  rule real_le_refl real_le_trans real_le_anti_sym real_less_le)+
paulson@14365
   369
paulson@14378
   370
(* Axiom 'linorder_linear' of class 'linorder': *)
paulson@14378
   371
lemma real_le_linear: "(z::real) \<le> w | w \<le> z"
paulson@14484
   372
apply (cases z, cases w) 
paulson@14378
   373
apply (auto simp add: real_le real_zero_def preal_add_ac preal_cancels)
paulson@14334
   374
done
paulson@14334
   375
paulson@14334
   376
paulson@14334
   377
instance real :: linorder
paulson@14334
   378
  by (intro_classes, rule real_le_linear)
paulson@14334
   379
paulson@14334
   380
paulson@14378
   381
lemma real_le_eq_diff: "(x \<le> y) = (x-y \<le> (0::real))"
paulson@14484
   382
apply (cases x, cases y) 
paulson@14378
   383
apply (auto simp add: real_le real_zero_def real_diff_def real_add real_minus
paulson@14378
   384
                      preal_add_ac)
paulson@14378
   385
apply (simp_all add: preal_add_assoc [symmetric] preal_cancels)
nipkow@15542
   386
done
paulson@14378
   387
paulson@14484
   388
lemma real_add_left_mono: 
paulson@14484
   389
  assumes le: "x \<le> y" shows "z + x \<le> z + (y::real)"
paulson@14484
   390
proof -
paulson@14484
   391
  have "z + x - (z + y) = (z + -z) + (x - y)"
paulson@14484
   392
    by (simp add: diff_minus add_ac) 
paulson@14484
   393
  with le show ?thesis 
obua@14754
   394
    by (simp add: real_le_eq_diff[of x] real_le_eq_diff[of "z+x"] diff_minus)
paulson@14484
   395
qed
paulson@14334
   396
paulson@14365
   397
lemma real_sum_gt_zero_less: "(0 < S + (-W::real)) ==> (W < S)"
paulson@14365
   398
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)
paulson@14365
   399
paulson@14365
   400
lemma real_less_sum_gt_zero: "(W < S) ==> (0 < S + (-W::real))"
paulson@14365
   401
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)
paulson@14334
   402
paulson@14334
   403
lemma real_mult_order: "[| 0 < x; 0 < y |] ==> (0::real) < x * y"
paulson@14484
   404
apply (cases x, cases y)
paulson@14378
   405
apply (simp add: linorder_not_le [where 'a = real, symmetric] 
paulson@14378
   406
                 linorder_not_le [where 'a = preal] 
paulson@14378
   407
                  real_zero_def real_le real_mult)
paulson@14365
   408
  --{*Reduce to the (simpler) @{text "\<le>"} relation *}
wenzelm@16973
   409
apply (auto dest!: less_add_left_Ex
paulson@14365
   410
     simp add: preal_add_ac preal_mult_ac 
wenzelm@16973
   411
          preal_add_mult_distrib2 preal_cancels preal_self_less_add_left)
paulson@14334
   412
done
paulson@14334
   413
paulson@14334
   414
lemma real_mult_less_mono2: "[| (0::real) < z; x < y |] ==> z * x < z * y"
paulson@14334
   415
apply (rule real_sum_gt_zero_less)
paulson@14334
   416
apply (drule real_less_sum_gt_zero [of x y])
paulson@14334
   417
apply (drule real_mult_order, assumption)
paulson@14334
   418
apply (simp add: right_distrib)
paulson@14334
   419
done
paulson@14334
   420
paulson@14365
   421
text{*lemma for proving @{term "0<(1::real)"}*}
paulson@14365
   422
lemma real_zero_le_one: "0 \<le> (1::real)"
paulson@14387
   423
by (simp add: real_zero_def real_one_def real_le 
paulson@14378
   424
                 preal_self_less_add_left order_less_imp_le)
paulson@14334
   425
haftmann@22456
   426
instance real :: distrib_lattice
haftmann@22456
   427
  "inf x y \<equiv> min x y"
haftmann@22456
   428
  "sup x y \<equiv> max x y"
haftmann@22456
   429
  by default (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
haftmann@22456
   430
paulson@14378
   431
paulson@14334
   432
subsection{*The Reals Form an Ordered Field*}
paulson@14334
   433
paulson@14334
   434
instance real :: ordered_field
paulson@14334
   435
proof
paulson@14334
   436
  fix x y z :: real
paulson@14334
   437
  show "x \<le> y ==> z + x \<le> z + y" by (rule real_add_left_mono)
paulson@14334
   438
  show "x < y ==> 0 < z ==> z * x < z * y" by (simp add: real_mult_less_mono2)
paulson@14334
   439
  show "\<bar>x\<bar> = (if x < 0 then -x else x)"
paulson@14334
   440
    by (auto dest: order_le_less_trans simp add: real_abs_def linorder_not_le)
paulson@14334
   441
qed
paulson@14334
   442
paulson@14365
   443
text{*The function @{term real_of_preal} requires many proofs, but it seems
paulson@14365
   444
to be essential for proving completeness of the reals from that of the
paulson@14365
   445
positive reals.*}
paulson@14365
   446
paulson@14365
   447
lemma real_of_preal_add:
paulson@14365
   448
     "real_of_preal ((x::preal) + y) = real_of_preal x + real_of_preal y"
paulson@14365
   449
by (simp add: real_of_preal_def real_add preal_add_mult_distrib preal_mult_1 
paulson@14365
   450
              preal_add_ac)
paulson@14365
   451
paulson@14365
   452
lemma real_of_preal_mult:
paulson@14365
   453
     "real_of_preal ((x::preal) * y) = real_of_preal x* real_of_preal y"
paulson@14365
   454
by (simp add: real_of_preal_def real_mult preal_add_mult_distrib2
paulson@14365
   455
              preal_mult_1 preal_mult_1_right preal_add_ac preal_mult_ac)
paulson@14365
   456
paulson@14365
   457
paulson@14365
   458
text{*Gleason prop 9-4.4 p 127*}
paulson@14365
   459
lemma real_of_preal_trichotomy:
paulson@14365
   460
      "\<exists>m. (x::real) = real_of_preal m | x = 0 | x = -(real_of_preal m)"
paulson@14484
   461
apply (simp add: real_of_preal_def real_zero_def, cases x)
paulson@14365
   462
apply (auto simp add: real_minus preal_add_ac)
paulson@14365
   463
apply (cut_tac x = x and y = y in linorder_less_linear)
paulson@14365
   464
apply (auto dest!: less_add_left_Ex simp add: preal_add_assoc [symmetric])
paulson@14365
   465
done
paulson@14365
   466
paulson@14365
   467
lemma real_of_preal_leD:
paulson@14365
   468
      "real_of_preal m1 \<le> real_of_preal m2 ==> m1 \<le> m2"
paulson@14484
   469
by (simp add: real_of_preal_def real_le preal_cancels)
paulson@14365
   470
paulson@14365
   471
lemma real_of_preal_lessI: "m1 < m2 ==> real_of_preal m1 < real_of_preal m2"
paulson@14365
   472
by (auto simp add: real_of_preal_leD linorder_not_le [symmetric])
paulson@14365
   473
paulson@14365
   474
lemma real_of_preal_lessD:
paulson@14365
   475
      "real_of_preal m1 < real_of_preal m2 ==> m1 < m2"
paulson@14484
   476
by (simp add: real_of_preal_def real_le linorder_not_le [symmetric] 
paulson@14484
   477
              preal_cancels) 
paulson@14484
   478
paulson@14365
   479
paulson@14365
   480
lemma real_of_preal_less_iff [simp]:
paulson@14365
   481
     "(real_of_preal m1 < real_of_preal m2) = (m1 < m2)"
paulson@14365
   482
by (blast intro: real_of_preal_lessI real_of_preal_lessD)
paulson@14365
   483
paulson@14365
   484
lemma real_of_preal_le_iff:
paulson@14365
   485
     "(real_of_preal m1 \<le> real_of_preal m2) = (m1 \<le> m2)"
paulson@14365
   486
by (simp add: linorder_not_less [symmetric]) 
paulson@14365
   487
paulson@14365
   488
lemma real_of_preal_zero_less: "0 < real_of_preal m"
paulson@14365
   489
apply (auto simp add: real_zero_def real_of_preal_def real_less_def real_le_def
paulson@14365
   490
            preal_add_ac preal_cancels)
paulson@14365
   491
apply (simp_all add: preal_add_assoc [symmetric] preal_cancels)
paulson@14365
   492
apply (blast intro: preal_self_less_add_left order_less_imp_le)
paulson@14365
   493
apply (insert preal_not_eq_self [of "preal_of_rat 1" m]) 
paulson@14365
   494
apply (simp add: preal_add_ac) 
paulson@14365
   495
done
paulson@14365
   496
paulson@14365
   497
lemma real_of_preal_minus_less_zero: "- real_of_preal m < 0"
paulson@14365
   498
by (simp add: real_of_preal_zero_less)
paulson@14365
   499
paulson@14365
   500
lemma real_of_preal_not_minus_gt_zero: "~ 0 < - real_of_preal m"
paulson@14484
   501
proof -
paulson@14484
   502
  from real_of_preal_minus_less_zero
paulson@14484
   503
  show ?thesis by (blast dest: order_less_trans)
paulson@14484
   504
qed
paulson@14365
   505
paulson@14365
   506
paulson@14365
   507
subsection{*Theorems About the Ordering*}
paulson@14365
   508
paulson@14365
   509
text{*obsolete but used a lot*}
paulson@14365
   510
paulson@14365
   511
lemma real_not_refl2: "x < y ==> x \<noteq> (y::real)"
paulson@14365
   512
by blast 
paulson@14365
   513
paulson@14365
   514
lemma real_le_imp_less_or_eq: "!!(x::real). x \<le> y ==> x < y | x = y"
paulson@14365
   515
by (simp add: order_le_less)
paulson@14365
   516
paulson@14365
   517
lemma real_gt_zero_preal_Ex: "(0 < x) = (\<exists>y. x = real_of_preal y)"
paulson@14365
   518
apply (auto simp add: real_of_preal_zero_less)
paulson@14365
   519
apply (cut_tac x = x in real_of_preal_trichotomy)
paulson@14365
   520
apply (blast elim!: real_of_preal_not_minus_gt_zero [THEN notE])
paulson@14365
   521
done
paulson@14365
   522
paulson@14365
   523
lemma real_gt_preal_preal_Ex:
paulson@14365
   524
     "real_of_preal z < x ==> \<exists>y. x = real_of_preal y"
paulson@14365
   525
by (blast dest!: real_of_preal_zero_less [THEN order_less_trans]
paulson@14365
   526
             intro: real_gt_zero_preal_Ex [THEN iffD1])
paulson@14365
   527
paulson@14365
   528
lemma real_ge_preal_preal_Ex:
paulson@14365
   529
     "real_of_preal z \<le> x ==> \<exists>y. x = real_of_preal y"
paulson@14365
   530
by (blast dest: order_le_imp_less_or_eq real_gt_preal_preal_Ex)
paulson@14365
   531
paulson@14365
   532
lemma real_less_all_preal: "y \<le> 0 ==> \<forall>x. y < real_of_preal x"
paulson@14365
   533
by (auto elim: order_le_imp_less_or_eq [THEN disjE] 
paulson@14365
   534
            intro: real_of_preal_zero_less [THEN [2] order_less_trans] 
paulson@14365
   535
            simp add: real_of_preal_zero_less)
paulson@14365
   536
paulson@14365
   537
lemma real_less_all_real2: "~ 0 < y ==> \<forall>x. y < real_of_preal x"
paulson@14365
   538
by (blast intro!: real_less_all_preal linorder_not_less [THEN iffD1])
paulson@14365
   539
paulson@14334
   540
lemma real_add_less_le_mono: "[| w'<w; z'\<le>z |] ==> w' + z' < w + (z::real)"
obua@14738
   541
  by (rule OrderedGroup.add_less_le_mono)
paulson@14334
   542
paulson@14334
   543
lemma real_add_le_less_mono:
paulson@14334
   544
     "!!z z'::real. [| w'\<le>w; z'<z |] ==> w' + z' < w + z"
obua@14738
   545
  by (rule OrderedGroup.add_le_less_mono)
paulson@14334
   546
paulson@14334
   547
lemma real_le_square [simp]: "(0::real) \<le> x*x"
paulson@14334
   548
 by (rule Ring_and_Field.zero_le_square)
paulson@14334
   549
paulson@14334
   550
paulson@14334
   551
subsection{*More Lemmas*}
paulson@14334
   552
paulson@14334
   553
lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
paulson@14334
   554
by auto
paulson@14334
   555
paulson@14334
   556
lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
paulson@14334
   557
by auto
paulson@14334
   558
paulson@14334
   559
text{*The precondition could be weakened to @{term "0\<le>x"}*}
paulson@14334
   560
lemma real_mult_less_mono:
paulson@14334
   561
     "[| u<v;  x<y;  (0::real) < v;  0 < x |] ==> u*x < v* y"
paulson@14334
   562
 by (simp add: Ring_and_Field.mult_strict_mono order_less_imp_le)
paulson@14334
   563
paulson@14334
   564
lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
paulson@14334
   565
  by (force elim: order_less_asym
paulson@14334
   566
            simp add: Ring_and_Field.mult_less_cancel_right)
paulson@14334
   567
paulson@14334
   568
lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
paulson@14365
   569
apply (simp add: mult_le_cancel_right)
paulson@14365
   570
apply (blast intro: elim: order_less_asym) 
paulson@14365
   571
done
paulson@14334
   572
paulson@14334
   573
lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
nipkow@15923
   574
by(simp add:mult_commute)
paulson@14334
   575
paulson@14334
   576
text{*Only two uses?*}
paulson@14334
   577
lemma real_mult_less_mono':
paulson@14334
   578
     "[| x < y;  r1 < r2;  (0::real) \<le> r1;  0 \<le> x|] ==> r1 * x < r2 * y"
paulson@14334
   579
 by (rule Ring_and_Field.mult_strict_mono')
paulson@14334
   580
paulson@14334
   581
text{*FIXME: delete or at least combine the next two lemmas*}
paulson@14334
   582
lemma real_sum_squares_cancel: "x * x + y * y = 0 ==> x = (0::real)"
obua@14738
   583
apply (drule OrderedGroup.equals_zero_I [THEN sym])
paulson@14334
   584
apply (cut_tac x = y in real_le_square) 
paulson@14476
   585
apply (auto, drule order_antisym, auto)
paulson@14334
   586
done
paulson@14334
   587
paulson@14334
   588
lemma real_sum_squares_cancel2: "x * x + y * y = 0 ==> y = (0::real)"
paulson@14334
   589
apply (rule_tac y = x in real_sum_squares_cancel)
paulson@14476
   590
apply (simp add: add_commute)
paulson@14334
   591
done
paulson@14334
   592
paulson@14334
   593
lemma real_add_order: "[| 0 < x; 0 < y |] ==> (0::real) < x + y"
huffman@22958
   594
by (rule add_pos_pos)
paulson@14334
   595
paulson@14334
   596
lemma real_le_add_order: "[| 0 \<le> x; 0 \<le> y |] ==> (0::real) \<le> x + y"
huffman@22958
   597
by (rule add_nonneg_nonneg)
paulson@14334
   598
paulson@14365
   599
lemma real_inverse_unique: "x*y = (1::real) ==> y = inverse x"
huffman@22958
   600
by (rule inverse_unique [symmetric])
paulson@14334
   601
paulson@14365
   602
lemma real_inverse_gt_one: "[| (0::real) < x; x < 1 |] ==> 1 < inverse x"
huffman@22958
   603
by (simp add: one_less_inverse_iff)
paulson@14334
   604
paulson@14365
   605
lemma real_mult_self_sum_ge_zero: "(0::real) \<le> x*x + y*y"
huffman@22958
   606
by (intro add_nonneg_nonneg zero_le_square)
paulson@14365
   607
paulson@14334
   608
paulson@14365
   609
subsection{*Embedding the Integers into the Reals*}
paulson@14365
   610
paulson@14378
   611
defs (overloaded)
paulson@14378
   612
  real_of_nat_def: "real z == of_nat z"
paulson@14378
   613
  real_of_int_def: "real z == of_int z"
paulson@14365
   614
avigad@16819
   615
lemma real_eq_of_nat: "real = of_nat"
avigad@16819
   616
  apply (rule ext)
avigad@16819
   617
  apply (unfold real_of_nat_def)
avigad@16819
   618
  apply (rule refl)
avigad@16819
   619
  done
avigad@16819
   620
avigad@16819
   621
lemma real_eq_of_int: "real = of_int"
avigad@16819
   622
  apply (rule ext)
avigad@16819
   623
  apply (unfold real_of_int_def)
avigad@16819
   624
  apply (rule refl)
avigad@16819
   625
  done
avigad@16819
   626
paulson@14365
   627
lemma real_of_int_zero [simp]: "real (0::int) = 0"  
paulson@14378
   628
by (simp add: real_of_int_def) 
paulson@14365
   629
paulson@14365
   630
lemma real_of_one [simp]: "real (1::int) = (1::real)"
paulson@14378
   631
by (simp add: real_of_int_def) 
paulson@14334
   632
avigad@16819
   633
lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
paulson@14378
   634
by (simp add: real_of_int_def) 
paulson@14365
   635
avigad@16819
   636
lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
paulson@14378
   637
by (simp add: real_of_int_def) 
avigad@16819
   638
avigad@16819
   639
lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
avigad@16819
   640
by (simp add: real_of_int_def) 
paulson@14365
   641
avigad@16819
   642
lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
paulson@14378
   643
by (simp add: real_of_int_def) 
paulson@14334
   644
avigad@16819
   645
lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
avigad@16819
   646
  apply (subst real_eq_of_int)+
avigad@16819
   647
  apply (rule of_int_setsum)
avigad@16819
   648
done
avigad@16819
   649
avigad@16819
   650
lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
avigad@16819
   651
    (PROD x:A. real(f x))"
avigad@16819
   652
  apply (subst real_eq_of_int)+
avigad@16819
   653
  apply (rule of_int_setprod)
avigad@16819
   654
done
paulson@14365
   655
paulson@14365
   656
lemma real_of_int_zero_cancel [simp]: "(real x = 0) = (x = (0::int))"
paulson@14378
   657
by (simp add: real_of_int_def) 
paulson@14365
   658
paulson@14365
   659
lemma real_of_int_inject [iff]: "(real (x::int) = real y) = (x = y)"
paulson@14378
   660
by (simp add: real_of_int_def) 
paulson@14365
   661
paulson@14365
   662
lemma real_of_int_less_iff [iff]: "(real (x::int) < real y) = (x < y)"
paulson@14378
   663
by (simp add: real_of_int_def) 
paulson@14365
   664
paulson@14365
   665
lemma real_of_int_le_iff [simp]: "(real (x::int) \<le> real y) = (x \<le> y)"
paulson@14378
   666
by (simp add: real_of_int_def) 
paulson@14365
   667
avigad@16819
   668
lemma real_of_int_gt_zero_cancel_iff [simp]: "(0 < real (n::int)) = (0 < n)"
avigad@16819
   669
by (simp add: real_of_int_def) 
avigad@16819
   670
avigad@16819
   671
lemma real_of_int_ge_zero_cancel_iff [simp]: "(0 <= real (n::int)) = (0 <= n)"
avigad@16819
   672
by (simp add: real_of_int_def) 
avigad@16819
   673
avigad@16819
   674
lemma real_of_int_lt_zero_cancel_iff [simp]: "(real (n::int) < 0) = (n < 0)"
avigad@16819
   675
by (simp add: real_of_int_def)
avigad@16819
   676
avigad@16819
   677
lemma real_of_int_le_zero_cancel_iff [simp]: "(real (n::int) <= 0) = (n <= 0)"
avigad@16819
   678
by (simp add: real_of_int_def)
avigad@16819
   679
avigad@16888
   680
lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
avigad@16888
   681
by (auto simp add: abs_if)
avigad@16888
   682
avigad@16819
   683
lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
avigad@16819
   684
  apply (subgoal_tac "real n + 1 = real (n + 1)")
avigad@16819
   685
  apply (simp del: real_of_int_add)
avigad@16819
   686
  apply auto
avigad@16819
   687
done
avigad@16819
   688
avigad@16819
   689
lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
avigad@16819
   690
  apply (subgoal_tac "real m + 1 = real (m + 1)")
avigad@16819
   691
  apply (simp del: real_of_int_add)
avigad@16819
   692
  apply simp
avigad@16819
   693
done
avigad@16819
   694
avigad@16819
   695
lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
avigad@16819
   696
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
   697
proof -
avigad@16819
   698
  assume "d ~= 0"
avigad@16819
   699
  have "x = (x div d) * d + x mod d"
avigad@16819
   700
    by auto
avigad@16819
   701
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
   702
    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
avigad@16819
   703
  then have "real x / real d = ... / real d"
avigad@16819
   704
    by simp
avigad@16819
   705
  then show ?thesis
avigad@16819
   706
    by (auto simp add: add_divide_distrib ring_eq_simps prems)
avigad@16819
   707
qed
avigad@16819
   708
avigad@16819
   709
lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
avigad@16819
   710
    real(n div d) = real n / real d"
avigad@16819
   711
  apply (frule real_of_int_div_aux [of d n])
avigad@16819
   712
  apply simp
avigad@16819
   713
  apply (simp add: zdvd_iff_zmod_eq_0)
avigad@16819
   714
done
avigad@16819
   715
avigad@16819
   716
lemma real_of_int_div2:
avigad@16819
   717
  "0 <= real (n::int) / real (x) - real (n div x)"
avigad@16819
   718
  apply (case_tac "x = 0")
avigad@16819
   719
  apply simp
avigad@16819
   720
  apply (case_tac "0 < x")
avigad@16819
   721
  apply (simp add: compare_rls)
avigad@16819
   722
  apply (subst real_of_int_div_aux)
avigad@16819
   723
  apply simp
avigad@16819
   724
  apply simp
avigad@16819
   725
  apply (subst zero_le_divide_iff)
avigad@16819
   726
  apply auto
avigad@16819
   727
  apply (simp add: compare_rls)
avigad@16819
   728
  apply (subst real_of_int_div_aux)
avigad@16819
   729
  apply simp
avigad@16819
   730
  apply simp
avigad@16819
   731
  apply (subst zero_le_divide_iff)
avigad@16819
   732
  apply auto
avigad@16819
   733
done
avigad@16819
   734
avigad@16819
   735
lemma real_of_int_div3:
avigad@16819
   736
  "real (n::int) / real (x) - real (n div x) <= 1"
avigad@16819
   737
  apply(case_tac "x = 0")
avigad@16819
   738
  apply simp
avigad@16819
   739
  apply (simp add: compare_rls)
avigad@16819
   740
  apply (subst real_of_int_div_aux)
avigad@16819
   741
  apply assumption
avigad@16819
   742
  apply simp
avigad@16819
   743
  apply (subst divide_le_eq)
avigad@16819
   744
  apply clarsimp
avigad@16819
   745
  apply (rule conjI)
avigad@16819
   746
  apply (rule impI)
avigad@16819
   747
  apply (rule order_less_imp_le)
avigad@16819
   748
  apply simp
avigad@16819
   749
  apply (rule impI)
avigad@16819
   750
  apply (rule order_less_imp_le)
avigad@16819
   751
  apply simp
avigad@16819
   752
done
avigad@16819
   753
avigad@16819
   754
lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
avigad@16819
   755
  by (insert real_of_int_div2 [of n x], simp)
paulson@14365
   756
paulson@14365
   757
subsection{*Embedding the Naturals into the Reals*}
paulson@14365
   758
paulson@14334
   759
lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
paulson@14365
   760
by (simp add: real_of_nat_def)
paulson@14334
   761
paulson@14334
   762
lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
paulson@14365
   763
by (simp add: real_of_nat_def)
paulson@14334
   764
paulson@14365
   765
lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
paulson@14378
   766
by (simp add: real_of_nat_def)
paulson@14334
   767
paulson@14334
   768
(*Not for addsimps: often the LHS is used to represent a positive natural*)
paulson@14334
   769
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
paulson@14378
   770
by (simp add: real_of_nat_def)
paulson@14334
   771
paulson@14334
   772
lemma real_of_nat_less_iff [iff]: 
paulson@14334
   773
     "(real (n::nat) < real m) = (n < m)"
paulson@14365
   774
by (simp add: real_of_nat_def)
paulson@14334
   775
paulson@14334
   776
lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
paulson@14378
   777
by (simp add: real_of_nat_def)
paulson@14334
   778
paulson@14334
   779
lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
paulson@14378
   780
by (simp add: real_of_nat_def zero_le_imp_of_nat)
paulson@14334
   781
paulson@14365
   782
lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
paulson@14378
   783
by (simp add: real_of_nat_def del: of_nat_Suc)
paulson@14365
   784
paulson@14334
   785
lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
paulson@14378
   786
by (simp add: real_of_nat_def)
paulson@14334
   787
avigad@16819
   788
lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
avigad@16819
   789
    (SUM x:A. real(f x))"
avigad@16819
   790
  apply (subst real_eq_of_nat)+
avigad@16819
   791
  apply (rule of_nat_setsum)
avigad@16819
   792
done
avigad@16819
   793
avigad@16819
   794
lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
avigad@16819
   795
    (PROD x:A. real(f x))"
avigad@16819
   796
  apply (subst real_eq_of_nat)+
avigad@16819
   797
  apply (rule of_nat_setprod)
avigad@16819
   798
done
avigad@16819
   799
avigad@16819
   800
lemma real_of_card: "real (card A) = setsum (%x.1) A"
avigad@16819
   801
  apply (subst card_eq_setsum)
avigad@16819
   802
  apply (subst real_of_nat_setsum)
avigad@16819
   803
  apply simp
avigad@16819
   804
done
avigad@16819
   805
paulson@14334
   806
lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
paulson@14378
   807
by (simp add: real_of_nat_def)
paulson@14334
   808
paulson@14387
   809
lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
paulson@14378
   810
by (simp add: real_of_nat_def)
paulson@14334
   811
paulson@14365
   812
lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
paulson@14378
   813
by (simp add: add: real_of_nat_def) 
paulson@14334
   814
paulson@14365
   815
lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
paulson@14378
   816
by (simp add: add: real_of_nat_def) 
paulson@14365
   817
paulson@14365
   818
lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
paulson@14378
   819
by (simp add: add: real_of_nat_def)
paulson@14334
   820
paulson@14365
   821
lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
paulson@14378
   822
by (simp add: add: real_of_nat_def)
paulson@14334
   823
paulson@14365
   824
lemma real_of_nat_ge_zero_cancel_iff [simp]: "(0 \<le> real (n::nat)) = (0 \<le> n)"
paulson@14378
   825
by (simp add: add: real_of_nat_def)
paulson@14334
   826
avigad@16819
   827
lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
avigad@16819
   828
  apply (subgoal_tac "real n + 1 = real (Suc n)")
avigad@16819
   829
  apply simp
avigad@16819
   830
  apply (auto simp add: real_of_nat_Suc)
avigad@16819
   831
done
avigad@16819
   832
avigad@16819
   833
lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
avigad@16819
   834
  apply (subgoal_tac "real m + 1 = real (Suc m)")
avigad@16819
   835
  apply (simp add: less_Suc_eq_le)
avigad@16819
   836
  apply (simp add: real_of_nat_Suc)
avigad@16819
   837
done
avigad@16819
   838
avigad@16819
   839
lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
avigad@16819
   840
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
   841
proof -
avigad@16819
   842
  assume "0 < d"
avigad@16819
   843
  have "x = (x div d) * d + x mod d"
avigad@16819
   844
    by auto
avigad@16819
   845
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
   846
    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
avigad@16819
   847
  then have "real x / real d = \<dots> / real d"
avigad@16819
   848
    by simp
avigad@16819
   849
  then show ?thesis
avigad@16819
   850
    by (auto simp add: add_divide_distrib ring_eq_simps prems)
avigad@16819
   851
qed
avigad@16819
   852
avigad@16819
   853
lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
avigad@16819
   854
    real(n div d) = real n / real d"
avigad@16819
   855
  apply (frule real_of_nat_div_aux [of d n])
avigad@16819
   856
  apply simp
avigad@16819
   857
  apply (subst dvd_eq_mod_eq_0 [THEN sym])
avigad@16819
   858
  apply assumption
avigad@16819
   859
done
avigad@16819
   860
avigad@16819
   861
lemma real_of_nat_div2:
avigad@16819
   862
  "0 <= real (n::nat) / real (x) - real (n div x)"
avigad@16819
   863
  apply(case_tac "x = 0")
avigad@16819
   864
  apply simp
avigad@16819
   865
  apply (simp add: compare_rls)
avigad@16819
   866
  apply (subst real_of_nat_div_aux)
avigad@16819
   867
  apply assumption
avigad@16819
   868
  apply simp
avigad@16819
   869
  apply (subst zero_le_divide_iff)
avigad@16819
   870
  apply simp
avigad@16819
   871
done
avigad@16819
   872
avigad@16819
   873
lemma real_of_nat_div3:
avigad@16819
   874
  "real (n::nat) / real (x) - real (n div x) <= 1"
avigad@16819
   875
  apply(case_tac "x = 0")
avigad@16819
   876
  apply simp
avigad@16819
   877
  apply (simp add: compare_rls)
avigad@16819
   878
  apply (subst real_of_nat_div_aux)
avigad@16819
   879
  apply assumption
avigad@16819
   880
  apply simp
avigad@16819
   881
done
avigad@16819
   882
avigad@16819
   883
lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
avigad@16819
   884
  by (insert real_of_nat_div2 [of n x], simp)
avigad@16819
   885
paulson@14365
   886
lemma real_of_int_real_of_nat: "real (int n) = real n"
paulson@14378
   887
by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)
paulson@14378
   888
paulson@14426
   889
lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
paulson@14426
   890
by (simp add: real_of_int_def real_of_nat_def)
paulson@14334
   891
avigad@16819
   892
lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
avigad@16819
   893
  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
avigad@16819
   894
  apply force
avigad@16819
   895
  apply (simp only: real_of_int_real_of_nat)
avigad@16819
   896
done
paulson@14387
   897
paulson@14387
   898
subsection{*Numerals and Arithmetic*}
paulson@14387
   899
paulson@14387
   900
instance real :: number ..
paulson@14387
   901
paulson@15013
   902
defs (overloaded)
haftmann@20485
   903
  real_number_of_def: "(number_of w :: real) == of_int w"
paulson@15013
   904
    --{*the type constraint is essential!*}
paulson@14387
   905
paulson@14387
   906
instance real :: number_ring
paulson@15013
   907
by (intro_classes, simp add: real_number_of_def) 
paulson@14387
   908
paulson@14387
   909
text{*Collapse applications of @{term real} to @{term number_of}*}
paulson@14387
   910
lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
paulson@14387
   911
by (simp add:  real_of_int_def of_int_number_of_eq)
paulson@14387
   912
paulson@14387
   913
lemma real_of_nat_number_of [simp]:
paulson@14387
   914
     "real (number_of v :: nat) =  
paulson@14387
   915
        (if neg (number_of v :: int) then 0  
paulson@14387
   916
         else (number_of v :: real))"
paulson@14387
   917
by (simp add: real_of_int_real_of_nat [symmetric] int_nat_number_of)
paulson@14387
   918
 
paulson@14387
   919
paulson@14387
   920
use "real_arith.ML"
paulson@14387
   921
paulson@14387
   922
setup real_arith_setup
paulson@14387
   923
kleing@19023
   924
kleing@19023
   925
lemma real_diff_mult_distrib:
kleing@19023
   926
  fixes a::real
kleing@19023
   927
  shows "a * (b - c) = a * b - a * c" 
kleing@19023
   928
proof -
kleing@19023
   929
  have "a * (b - c) = a * (b + -c)" by simp
kleing@19023
   930
  also have "\<dots> = (b + -c) * a" by simp
kleing@19023
   931
  also have "\<dots> = b*a + (-c)*a" by (rule real_add_mult_distrib)
kleing@19023
   932
  also have "\<dots> = a*b - a*c" by simp
kleing@19023
   933
  finally show ?thesis .
kleing@19023
   934
qed
kleing@19023
   935
kleing@19023
   936
paulson@14387
   937
subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
paulson@14387
   938
paulson@14387
   939
text{*Needed in this non-standard form by Hyperreal/Transcendental*}
paulson@14387
   940
lemma real_0_le_divide_iff:
paulson@14387
   941
     "((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
paulson@14387
   942
by (simp add: real_divide_def zero_le_mult_iff, auto)
paulson@14387
   943
paulson@14387
   944
lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
paulson@14387
   945
by arith
paulson@14387
   946
paulson@15085
   947
lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
paulson@14387
   948
by auto
paulson@14387
   949
paulson@15085
   950
lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
paulson@14387
   951
by auto
paulson@14387
   952
paulson@15085
   953
lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
paulson@14387
   954
by auto
paulson@14387
   955
paulson@15085
   956
lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
paulson@14387
   957
by auto
paulson@14387
   958
paulson@15085
   959
lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
paulson@14387
   960
by auto
paulson@14387
   961
paulson@14387
   962
paulson@14387
   963
(*
paulson@14387
   964
FIXME: we should have this, as for type int, but many proofs would break.
paulson@14387
   965
It replaces x+-y by x-y.
paulson@15086
   966
declare real_diff_def [symmetric, simp]
paulson@14387
   967
*)
paulson@14387
   968
paulson@14387
   969
paulson@14387
   970
subsubsection{*Density of the Reals*}
paulson@14387
   971
paulson@14387
   972
lemma real_lbound_gt_zero:
paulson@14387
   973
     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
paulson@14387
   974
apply (rule_tac x = " (min d1 d2) /2" in exI)
paulson@14387
   975
apply (simp add: min_def)
paulson@14387
   976
done
paulson@14387
   977
paulson@14387
   978
paulson@14387
   979
text{*Similar results are proved in @{text Ring_and_Field}*}
paulson@14387
   980
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
paulson@14387
   981
  by auto
paulson@14387
   982
paulson@14387
   983
lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
paulson@14387
   984
  by auto
paulson@14387
   985
paulson@14387
   986
paulson@14387
   987
subsection{*Absolute Value Function for the Reals*}
paulson@14387
   988
paulson@14387
   989
lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
paulson@15003
   990
by (simp add: abs_if)
paulson@14387
   991
paulson@14387
   992
lemma abs_interval_iff: "(abs x < r) = (-r < x & x < (r::real))"
paulson@14387
   993
by (force simp add: Ring_and_Field.abs_less_iff)
paulson@14387
   994
paulson@14387
   995
lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
obua@14738
   996
by (force simp add: OrderedGroup.abs_le_iff)
paulson@14387
   997
paulson@14387
   998
lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
paulson@15003
   999
by (simp add: abs_if)
paulson@14387
  1000
paulson@14387
  1001
lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
huffman@22958
  1002
by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
paulson@14387
  1003
paulson@14387
  1004
lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
webertj@20217
  1005
by simp
paulson@14387
  1006
 
paulson@14387
  1007
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
webertj@20217
  1008
by simp
paulson@14387
  1009
paulson@5588
  1010
end