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