src/HOL/RealDef.thy
author huffman
Wed Jun 17 16:55:01 2009 -0700 (2009-06-17)
changeset 31706 1db0c8f235fb
parent 31641 feea4d3d743d
child 31707 678d294a563c
permissions -rw-r--r--
new GCD library, courtesy of Jeremy Avigad
haftmann@28952
     1
(*  Title       : HOL/RealDef.thy
paulson@5588
     2
    Author      : Jacques D. Fleuriot
paulson@5588
     3
    Copyright   : 1998  University of Cambridge
paulson@14387
     4
    Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
avigad@16819
     5
    Additional contributions by Jeremy Avigad
paulson@14269
     6
*)
paulson@14269
     7
paulson@14387
     8
header{*Defining the Reals from the Positive Reals*}
paulson@14387
     9
nipkow@15131
    10
theory RealDef
nipkow@15140
    11
imports PReal
nipkow@15131
    12
begin
paulson@5588
    13
wenzelm@19765
    14
definition
wenzelm@21404
    15
  realrel   ::  "((preal * preal) * (preal * preal)) set" where
haftmann@28562
    16
  [code del]: "realrel = {p. \<exists>x1 y1 x2 y2. p = ((x1,y1),(x2,y2)) & x1+y2 = x2+y1}"
paulson@14269
    17
paulson@14484
    18
typedef (Real)  real = "UNIV//realrel"
paulson@14269
    19
  by (auto simp add: quotient_def)
paulson@5588
    20
wenzelm@19765
    21
definition
paulson@14484
    22
  (** these don't use the overloaded "real" function: users don't see them **)
wenzelm@21404
    23
  real_of_preal :: "preal => real" where
haftmann@28562
    24
  [code del]: "real_of_preal m = Abs_Real (realrel `` {(m + 1, 1)})"
paulson@14484
    25
haftmann@25762
    26
instantiation real :: "{zero, one, plus, minus, uminus, times, inverse, ord, abs, sgn}"
haftmann@25571
    27
begin
paulson@5588
    28
haftmann@25571
    29
definition
haftmann@28562
    30
  real_zero_def [code del]: "0 = Abs_Real(realrel``{(1, 1)})"
haftmann@25571
    31
haftmann@25571
    32
definition
haftmann@28562
    33
  real_one_def [code del]: "1 = Abs_Real(realrel``{(1 + 1, 1)})"
paulson@5588
    34
haftmann@25571
    35
definition
haftmann@28562
    36
  real_add_def [code del]: "z + w =
paulson@14484
    37
       contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w).
nipkow@27964
    38
                 { Abs_Real(realrel``{(x+u, y+v)}) })"
bauerg@10606
    39
haftmann@25571
    40
definition
haftmann@28562
    41
  real_minus_def [code del]: "- r =  contents (\<Union>(x,y) \<in> Rep_Real(r). { Abs_Real(realrel``{(y,x)}) })"
haftmann@25571
    42
haftmann@25571
    43
definition
haftmann@28562
    44
  real_diff_def [code del]: "r - (s::real) = r + - s"
paulson@14484
    45
haftmann@25571
    46
definition
haftmann@28562
    47
  real_mult_def [code del]:
haftmann@25571
    48
    "z * w =
paulson@14484
    49
       contents (\<Union>(x,y) \<in> Rep_Real(z). \<Union>(u,v) \<in> Rep_Real(w).
nipkow@27964
    50
                 { Abs_Real(realrel``{(x*u + y*v, x*v + y*u)}) })"
paulson@5588
    51
haftmann@25571
    52
definition
haftmann@28562
    53
  real_inverse_def [code del]: "inverse (R::real) = (THE S. (R = 0 & S = 0) | S * R = 1)"
haftmann@25571
    54
haftmann@25571
    55
definition
haftmann@28562
    56
  real_divide_def [code del]: "R / (S::real) = R * inverse S"
paulson@14269
    57
haftmann@25571
    58
definition
haftmann@28562
    59
  real_le_def [code del]: "z \<le> (w::real) \<longleftrightarrow>
haftmann@25571
    60
    (\<exists>x y u v. x+v \<le> u+y & (x,y) \<in> Rep_Real z & (u,v) \<in> Rep_Real w)"
haftmann@25571
    61
haftmann@25571
    62
definition
haftmann@28562
    63
  real_less_def [code del]: "x < (y\<Colon>real) \<longleftrightarrow> x \<le> y \<and> x \<noteq> y"
paulson@5588
    64
haftmann@25571
    65
definition
haftmann@25571
    66
  real_abs_def:  "abs (r::real) = (if r < 0 then - r else r)"
paulson@14334
    67
haftmann@25571
    68
definition
haftmann@25571
    69
  real_sgn_def: "sgn (x::real) = (if x=0 then 0 else if 0<x then 1 else - 1)"
haftmann@25571
    70
haftmann@25571
    71
instance ..
haftmann@25571
    72
haftmann@25571
    73
end
paulson@14334
    74
huffman@23287
    75
subsection {* Equivalence relation over positive reals *}
paulson@14269
    76
paulson@14270
    77
lemma preal_trans_lemma:
paulson@14365
    78
  assumes "x + y1 = x1 + y"
paulson@14365
    79
      and "x + y2 = x2 + y"
paulson@14365
    80
  shows "x1 + y2 = x2 + (y1::preal)"
paulson@14365
    81
proof -
huffman@23287
    82
  have "(x1 + y2) + x = (x + y2) + x1" by (simp add: add_ac)
paulson@14365
    83
  also have "... = (x2 + y) + x1"  by (simp add: prems)
huffman@23287
    84
  also have "... = x2 + (x1 + y)"  by (simp add: add_ac)
paulson@14365
    85
  also have "... = x2 + (x + y1)"  by (simp add: prems)
huffman@23287
    86
  also have "... = (x2 + y1) + x"  by (simp add: add_ac)
paulson@14365
    87
  finally have "(x1 + y2) + x = (x2 + y1) + x" .
huffman@23287
    88
  thus ?thesis by (rule add_right_imp_eq)
paulson@14365
    89
qed
paulson@14365
    90
paulson@14269
    91
paulson@14484
    92
lemma realrel_iff [simp]: "(((x1,y1),(x2,y2)) \<in> realrel) = (x1 + y2 = x2 + y1)"
paulson@14484
    93
by (simp add: realrel_def)
paulson@14269
    94
paulson@14269
    95
lemma equiv_realrel: "equiv UNIV realrel"
nipkow@30198
    96
apply (auto simp add: equiv_def refl_on_def sym_def trans_def realrel_def)
paulson@14365
    97
apply (blast dest: preal_trans_lemma) 
paulson@14269
    98
done
paulson@14269
    99
paulson@14497
   100
text{*Reduces equality of equivalence classes to the @{term realrel} relation:
paulson@14497
   101
  @{term "(realrel `` {x} = realrel `` {y}) = ((x,y) \<in> realrel)"} *}
paulson@14269
   102
lemmas equiv_realrel_iff = 
paulson@14269
   103
       eq_equiv_class_iff [OF equiv_realrel UNIV_I UNIV_I]
paulson@14269
   104
paulson@14269
   105
declare equiv_realrel_iff [simp]
paulson@14269
   106
paulson@14497
   107
paulson@14484
   108
lemma realrel_in_real [simp]: "realrel``{(x,y)}: Real"
paulson@14484
   109
by (simp add: Real_def realrel_def quotient_def, blast)
paulson@14269
   110
huffman@22958
   111
declare Abs_Real_inject [simp]
paulson@14484
   112
declare Abs_Real_inverse [simp]
paulson@14269
   113
paulson@14269
   114
paulson@14484
   115
text{*Case analysis on the representation of a real number as an equivalence
paulson@14484
   116
      class of pairs of positive reals.*}
paulson@14484
   117
lemma eq_Abs_Real [case_names Abs_Real, cases type: real]: 
paulson@14484
   118
     "(!!x y. z = Abs_Real(realrel``{(x,y)}) ==> P) ==> P"
paulson@14484
   119
apply (rule Rep_Real [of z, unfolded Real_def, THEN quotientE])
paulson@14484
   120
apply (drule arg_cong [where f=Abs_Real])
paulson@14484
   121
apply (auto simp add: Rep_Real_inverse)
paulson@14269
   122
done
paulson@14269
   123
paulson@14269
   124
huffman@23287
   125
subsection {* Addition and Subtraction *}
paulson@14269
   126
paulson@14269
   127
lemma real_add_congruent2_lemma:
paulson@14269
   128
     "[|a + ba = aa + b; ab + bc = ac + bb|]
paulson@14269
   129
      ==> a + ab + (ba + bc) = aa + ac + (b + (bb::preal))"
huffman@23287
   130
apply (simp add: add_assoc)
huffman@23287
   131
apply (rule add_left_commute [of ab, THEN ssubst])
huffman@23287
   132
apply (simp add: add_assoc [symmetric])
huffman@23287
   133
apply (simp add: add_ac)
paulson@14269
   134
done
paulson@14269
   135
paulson@14269
   136
lemma real_add:
paulson@14497
   137
     "Abs_Real (realrel``{(x,y)}) + Abs_Real (realrel``{(u,v)}) =
paulson@14497
   138
      Abs_Real (realrel``{(x+u, y+v)})"
paulson@14497
   139
proof -
paulson@15169
   140
  have "(\<lambda>z w. (\<lambda>(x,y). (\<lambda>(u,v). {Abs_Real (realrel `` {(x+u, y+v)})}) w) z)
paulson@15169
   141
        respects2 realrel"
paulson@14497
   142
    by (simp add: congruent2_def, blast intro: real_add_congruent2_lemma) 
paulson@14497
   143
  thus ?thesis
paulson@14497
   144
    by (simp add: real_add_def UN_UN_split_split_eq
paulson@14658
   145
                  UN_equiv_class2 [OF equiv_realrel equiv_realrel])
paulson@14497
   146
qed
paulson@14269
   147
paulson@14484
   148
lemma real_minus: "- Abs_Real(realrel``{(x,y)}) = Abs_Real(realrel `` {(y,x)})"
paulson@14484
   149
proof -
paulson@15169
   150
  have "(\<lambda>(x,y). {Abs_Real (realrel``{(y,x)})}) respects realrel"
huffman@23288
   151
    by (simp add: congruent_def add_commute) 
paulson@14484
   152
  thus ?thesis
paulson@14484
   153
    by (simp add: real_minus_def UN_equiv_class [OF equiv_realrel])
paulson@14484
   154
qed
paulson@14334
   155
huffman@23287
   156
instance real :: ab_group_add
huffman@23287
   157
proof
huffman@23287
   158
  fix x y z :: real
huffman@23287
   159
  show "(x + y) + z = x + (y + z)"
huffman@23287
   160
    by (cases x, cases y, cases z, simp add: real_add add_assoc)
huffman@23287
   161
  show "x + y = y + x"
huffman@23287
   162
    by (cases x, cases y, simp add: real_add add_commute)
huffman@23287
   163
  show "0 + x = x"
huffman@23287
   164
    by (cases x, simp add: real_add real_zero_def add_ac)
huffman@23287
   165
  show "- x + x = 0"
huffman@23287
   166
    by (cases x, simp add: real_minus real_add real_zero_def add_commute)
huffman@23287
   167
  show "x - y = x + - y"
huffman@23287
   168
    by (simp add: real_diff_def)
huffman@23287
   169
qed
paulson@14269
   170
paulson@14269
   171
huffman@23287
   172
subsection {* Multiplication *}
paulson@14269
   173
paulson@14329
   174
lemma real_mult_congruent2_lemma:
paulson@14329
   175
     "!!(x1::preal). [| x1 + y2 = x2 + y1 |] ==>
paulson@14484
   176
          x * x1 + y * y1 + (x * y2 + y * x2) =
paulson@14484
   177
          x * x2 + y * y2 + (x * y1 + y * x1)"
huffman@23287
   178
apply (simp add: add_left_commute add_assoc [symmetric])
huffman@23288
   179
apply (simp add: add_assoc right_distrib [symmetric])
huffman@23288
   180
apply (simp add: add_commute)
paulson@14269
   181
done
paulson@14269
   182
paulson@14269
   183
lemma real_mult_congruent2:
paulson@15169
   184
    "(%p1 p2.
paulson@14484
   185
        (%(x1,y1). (%(x2,y2). 
paulson@15169
   186
          { Abs_Real (realrel``{(x1*x2 + y1*y2, x1*y2+y1*x2)}) }) p2) p1)
paulson@15169
   187
     respects2 realrel"
paulson@14658
   188
apply (rule congruent2_commuteI [OF equiv_realrel], clarify)
huffman@23288
   189
apply (simp add: mult_commute add_commute)
paulson@14269
   190
apply (auto simp add: real_mult_congruent2_lemma)
paulson@14269
   191
done
paulson@14269
   192
paulson@14269
   193
lemma real_mult:
paulson@14484
   194
      "Abs_Real((realrel``{(x1,y1)})) * Abs_Real((realrel``{(x2,y2)})) =
paulson@14484
   195
       Abs_Real(realrel `` {(x1*x2+y1*y2,x1*y2+y1*x2)})"
paulson@14484
   196
by (simp add: real_mult_def UN_UN_split_split_eq
paulson@14658
   197
         UN_equiv_class2 [OF equiv_realrel equiv_realrel real_mult_congruent2])
paulson@14269
   198
paulson@14269
   199
lemma real_mult_commute: "(z::real) * w = w * z"
huffman@23288
   200
by (cases z, cases w, simp add: real_mult add_ac mult_ac)
paulson@14269
   201
paulson@14269
   202
lemma real_mult_assoc: "((z1::real) * z2) * z3 = z1 * (z2 * z3)"
paulson@14484
   203
apply (cases z1, cases z2, cases z3)
nipkow@29667
   204
apply (simp add: real_mult algebra_simps)
paulson@14269
   205
done
paulson@14269
   206
paulson@14269
   207
lemma real_mult_1: "(1::real) * z = z"
paulson@14484
   208
apply (cases z)
nipkow@29667
   209
apply (simp add: real_mult real_one_def algebra_simps)
paulson@14269
   210
done
paulson@14269
   211
paulson@14269
   212
lemma real_add_mult_distrib: "((z1::real) + z2) * w = (z1 * w) + (z2 * w)"
paulson@14484
   213
apply (cases z1, cases z2, cases w)
nipkow@29667
   214
apply (simp add: real_add real_mult algebra_simps)
paulson@14269
   215
done
paulson@14269
   216
paulson@14329
   217
text{*one and zero are distinct*}
paulson@14365
   218
lemma real_zero_not_eq_one: "0 \<noteq> (1::real)"
paulson@14484
   219
proof -
huffman@23287
   220
  have "(1::preal) < 1 + 1"
huffman@23287
   221
    by (simp add: preal_self_less_add_left)
paulson@14484
   222
  thus ?thesis
huffman@23288
   223
    by (simp add: real_zero_def real_one_def)
paulson@14484
   224
qed
paulson@14269
   225
huffman@23287
   226
instance real :: comm_ring_1
huffman@23287
   227
proof
huffman@23287
   228
  fix x y z :: real
huffman@23287
   229
  show "(x * y) * z = x * (y * z)" by (rule real_mult_assoc)
huffman@23287
   230
  show "x * y = y * x" by (rule real_mult_commute)
huffman@23287
   231
  show "1 * x = x" by (rule real_mult_1)
huffman@23287
   232
  show "(x + y) * z = x * z + y * z" by (rule real_add_mult_distrib)
huffman@23287
   233
  show "0 \<noteq> (1::real)" by (rule real_zero_not_eq_one)
huffman@23287
   234
qed
huffman@23287
   235
huffman@23287
   236
subsection {* Inverse and Division *}
paulson@14365
   237
paulson@14484
   238
lemma real_zero_iff: "Abs_Real (realrel `` {(x, x)}) = 0"
huffman@23288
   239
by (simp add: real_zero_def add_commute)
paulson@14269
   240
paulson@14365
   241
text{*Instead of using an existential quantifier and constructing the inverse
paulson@14365
   242
within the proof, we could define the inverse explicitly.*}
paulson@14365
   243
paulson@14365
   244
lemma real_mult_inverse_left_ex: "x \<noteq> 0 ==> \<exists>y. y*x = (1::real)"
paulson@14484
   245
apply (simp add: real_zero_def real_one_def, cases x)
paulson@14269
   246
apply (cut_tac x = xa and y = y in linorder_less_linear)
paulson@14365
   247
apply (auto dest!: less_add_left_Ex simp add: real_zero_iff)
paulson@14334
   248
apply (rule_tac
huffman@23287
   249
        x = "Abs_Real (realrel``{(1, inverse (D) + 1)})"
paulson@14334
   250
       in exI)
paulson@14334
   251
apply (rule_tac [2]
huffman@23287
   252
        x = "Abs_Real (realrel``{(inverse (D) + 1, 1)})" 
paulson@14334
   253
       in exI)
nipkow@29667
   254
apply (auto simp add: real_mult preal_mult_inverse_right algebra_simps)
paulson@14269
   255
done
paulson@14269
   256
paulson@14365
   257
lemma real_mult_inverse_left: "x \<noteq> 0 ==> inverse(x)*x = (1::real)"
paulson@14484
   258
apply (simp add: real_inverse_def)
huffman@23287
   259
apply (drule real_mult_inverse_left_ex, safe)
huffman@23287
   260
apply (rule theI, assumption, rename_tac z)
huffman@23287
   261
apply (subgoal_tac "(z * x) * y = z * (x * y)")
huffman@23287
   262
apply (simp add: mult_commute)
huffman@23287
   263
apply (rule mult_assoc)
paulson@14269
   264
done
paulson@14334
   265
paulson@14341
   266
paulson@14341
   267
subsection{*The Real Numbers form a Field*}
paulson@14341
   268
paulson@14334
   269
instance real :: field
paulson@14334
   270
proof
paulson@14334
   271
  fix x y z :: real
paulson@14365
   272
  show "x \<noteq> 0 ==> inverse x * x = 1" by (rule real_mult_inverse_left)
paulson@14430
   273
  show "x / y = x * inverse y" by (simp add: real_divide_def)
paulson@14334
   274
qed
paulson@14334
   275
paulson@14334
   276
paulson@14341
   277
text{*Inverse of zero!  Useful to simplify certain equations*}
paulson@14269
   278
paulson@14334
   279
lemma INVERSE_ZERO: "inverse 0 = (0::real)"
paulson@14484
   280
by (simp add: real_inverse_def)
paulson@14334
   281
paulson@14334
   282
instance real :: division_by_zero
paulson@14334
   283
proof
paulson@14334
   284
  show "inverse 0 = (0::real)" by (rule INVERSE_ZERO)
paulson@14334
   285
qed
paulson@14334
   286
paulson@14269
   287
paulson@14365
   288
subsection{*The @{text "\<le>"} Ordering*}
paulson@14269
   289
paulson@14365
   290
lemma real_le_refl: "w \<le> (w::real)"
paulson@14484
   291
by (cases w, force simp add: real_le_def)
paulson@14269
   292
paulson@14378
   293
text{*The arithmetic decision procedure is not set up for type preal.
paulson@14378
   294
  This lemma is currently unused, but it could simplify the proofs of the
paulson@14378
   295
  following two lemmas.*}
paulson@14378
   296
lemma preal_eq_le_imp_le:
paulson@14378
   297
  assumes eq: "a+b = c+d" and le: "c \<le> a"
paulson@14378
   298
  shows "b \<le> (d::preal)"
paulson@14378
   299
proof -
huffman@23288
   300
  have "c+d \<le> a+d" by (simp add: prems)
paulson@14378
   301
  hence "a+b \<le> a+d" by (simp add: prems)
huffman@23288
   302
  thus "b \<le> d" by simp
paulson@14378
   303
qed
paulson@14378
   304
paulson@14378
   305
lemma real_le_lemma:
paulson@14378
   306
  assumes l: "u1 + v2 \<le> u2 + v1"
paulson@14378
   307
      and "x1 + v1 = u1 + y1"
paulson@14378
   308
      and "x2 + v2 = u2 + y2"
paulson@14378
   309
  shows "x1 + y2 \<le> x2 + (y1::preal)"
paulson@14365
   310
proof -
paulson@14378
   311
  have "(x1+v1) + (u2+y2) = (u1+y1) + (x2+v2)" by (simp add: prems)
huffman@23288
   312
  hence "(x1+y2) + (u2+v1) = (x2+y1) + (u1+v2)" by (simp add: add_ac)
huffman@23288
   313
  also have "... \<le> (x2+y1) + (u2+v1)" by (simp add: prems)
huffman@23288
   314
  finally show ?thesis by simp
huffman@23288
   315
qed
paulson@14378
   316
paulson@14378
   317
lemma real_le: 
paulson@14484
   318
     "(Abs_Real(realrel``{(x1,y1)}) \<le> Abs_Real(realrel``{(x2,y2)})) =  
paulson@14484
   319
      (x1 + y2 \<le> x2 + y1)"
huffman@23288
   320
apply (simp add: real_le_def)
paulson@14387
   321
apply (auto intro: real_le_lemma)
paulson@14378
   322
done
paulson@14378
   323
paulson@14378
   324
lemma real_le_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::real)"
nipkow@15542
   325
by (cases z, cases w, simp add: real_le)
paulson@14378
   326
paulson@14378
   327
lemma real_trans_lemma:
paulson@14378
   328
  assumes "x + v \<le> u + y"
paulson@14378
   329
      and "u + v' \<le> u' + v"
paulson@14378
   330
      and "x2 + v2 = u2 + y2"
paulson@14378
   331
  shows "x + v' \<le> u' + (y::preal)"
paulson@14378
   332
proof -
huffman@23288
   333
  have "(x+v') + (u+v) = (x+v) + (u+v')" by (simp add: add_ac)
huffman@23288
   334
  also have "... \<le> (u+y) + (u+v')" by (simp add: prems)
huffman@23288
   335
  also have "... \<le> (u+y) + (u'+v)" by (simp add: prems)
huffman@23288
   336
  also have "... = (u'+y) + (u+v)"  by (simp add: add_ac)
huffman@23288
   337
  finally show ?thesis by simp
nipkow@15542
   338
qed
paulson@14269
   339
paulson@14365
   340
lemma real_le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::real)"
paulson@14484
   341
apply (cases i, cases j, cases k)
paulson@14484
   342
apply (simp add: real_le)
huffman@23288
   343
apply (blast intro: real_trans_lemma)
paulson@14334
   344
done
paulson@14334
   345
paulson@14365
   346
instance real :: order
haftmann@27682
   347
proof
haftmann@27682
   348
  fix u v :: real
haftmann@27682
   349
  show "u < v \<longleftrightarrow> u \<le> v \<and> \<not> v \<le> u" 
haftmann@27682
   350
    by (auto simp add: real_less_def intro: real_le_anti_sym)
haftmann@27682
   351
qed (assumption | rule real_le_refl real_le_trans real_le_anti_sym)+
paulson@14365
   352
paulson@14378
   353
(* Axiom 'linorder_linear' of class 'linorder': *)
paulson@14378
   354
lemma real_le_linear: "(z::real) \<le> w | w \<le> z"
huffman@23288
   355
apply (cases z, cases w)
huffman@23288
   356
apply (auto simp add: real_le real_zero_def add_ac)
paulson@14334
   357
done
paulson@14334
   358
paulson@14334
   359
instance real :: linorder
paulson@14334
   360
  by (intro_classes, rule real_le_linear)
paulson@14334
   361
paulson@14334
   362
paulson@14378
   363
lemma real_le_eq_diff: "(x \<le> y) = (x-y \<le> (0::real))"
paulson@14484
   364
apply (cases x, cases y) 
paulson@14378
   365
apply (auto simp add: real_le real_zero_def real_diff_def real_add real_minus
huffman@23288
   366
                      add_ac)
huffman@23288
   367
apply (simp_all add: add_assoc [symmetric])
nipkow@15542
   368
done
paulson@14378
   369
paulson@14484
   370
lemma real_add_left_mono: 
paulson@14484
   371
  assumes le: "x \<le> y" shows "z + x \<le> z + (y::real)"
paulson@14484
   372
proof -
chaieb@27668
   373
  have "z + x - (z + y) = (z + -z) + (x - y)" 
nipkow@29667
   374
    by (simp add: algebra_simps) 
paulson@14484
   375
  with le show ?thesis 
obua@14754
   376
    by (simp add: real_le_eq_diff[of x] real_le_eq_diff[of "z+x"] diff_minus)
paulson@14484
   377
qed
paulson@14334
   378
paulson@14365
   379
lemma real_sum_gt_zero_less: "(0 < S + (-W::real)) ==> (W < S)"
paulson@14365
   380
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)
paulson@14365
   381
paulson@14365
   382
lemma real_less_sum_gt_zero: "(W < S) ==> (0 < S + (-W::real))"
paulson@14365
   383
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)
paulson@14334
   384
paulson@14334
   385
lemma real_mult_order: "[| 0 < x; 0 < y |] ==> (0::real) < x * y"
paulson@14484
   386
apply (cases x, cases y)
paulson@14378
   387
apply (simp add: linorder_not_le [where 'a = real, symmetric] 
paulson@14378
   388
                 linorder_not_le [where 'a = preal] 
paulson@14378
   389
                  real_zero_def real_le real_mult)
paulson@14365
   390
  --{*Reduce to the (simpler) @{text "\<le>"} relation *}
wenzelm@16973
   391
apply (auto dest!: less_add_left_Ex
nipkow@29667
   392
     simp add: algebra_simps preal_self_less_add_left)
paulson@14334
   393
done
paulson@14334
   394
paulson@14334
   395
lemma real_mult_less_mono2: "[| (0::real) < z; x < y |] ==> z * x < z * y"
paulson@14334
   396
apply (rule real_sum_gt_zero_less)
paulson@14334
   397
apply (drule real_less_sum_gt_zero [of x y])
paulson@14334
   398
apply (drule real_mult_order, assumption)
paulson@14334
   399
apply (simp add: right_distrib)
paulson@14334
   400
done
paulson@14334
   401
haftmann@25571
   402
instantiation real :: distrib_lattice
haftmann@25571
   403
begin
haftmann@25571
   404
haftmann@25571
   405
definition
haftmann@25571
   406
  "(inf \<Colon> real \<Rightarrow> real \<Rightarrow> real) = min"
haftmann@25571
   407
haftmann@25571
   408
definition
haftmann@25571
   409
  "(sup \<Colon> real \<Rightarrow> real \<Rightarrow> real) = max"
haftmann@25571
   410
haftmann@25571
   411
instance
haftmann@22456
   412
  by default (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
haftmann@22456
   413
haftmann@25571
   414
end
haftmann@25571
   415
paulson@14378
   416
paulson@14334
   417
subsection{*The Reals Form an Ordered Field*}
paulson@14334
   418
paulson@14334
   419
instance real :: ordered_field
paulson@14334
   420
proof
paulson@14334
   421
  fix x y z :: real
paulson@14334
   422
  show "x \<le> y ==> z + x \<le> z + y" by (rule real_add_left_mono)
huffman@22962
   423
  show "x < y ==> 0 < z ==> z * x < z * y" by (rule real_mult_less_mono2)
huffman@22962
   424
  show "\<bar>x\<bar> = (if x < 0 then -x else x)" by (simp only: real_abs_def)
nipkow@24506
   425
  show "sgn x = (if x=0 then 0 else if 0<x then 1 else - 1)"
nipkow@24506
   426
    by (simp only: real_sgn_def)
paulson@14334
   427
qed
paulson@14334
   428
haftmann@25303
   429
instance real :: lordered_ab_group_add ..
haftmann@25303
   430
paulson@14365
   431
text{*The function @{term real_of_preal} requires many proofs, but it seems
paulson@14365
   432
to be essential for proving completeness of the reals from that of the
paulson@14365
   433
positive reals.*}
paulson@14365
   434
paulson@14365
   435
lemma real_of_preal_add:
paulson@14365
   436
     "real_of_preal ((x::preal) + y) = real_of_preal x + real_of_preal y"
nipkow@29667
   437
by (simp add: real_of_preal_def real_add algebra_simps)
paulson@14365
   438
paulson@14365
   439
lemma real_of_preal_mult:
paulson@14365
   440
     "real_of_preal ((x::preal) * y) = real_of_preal x* real_of_preal y"
nipkow@29667
   441
by (simp add: real_of_preal_def real_mult algebra_simps)
paulson@14365
   442
paulson@14365
   443
paulson@14365
   444
text{*Gleason prop 9-4.4 p 127*}
paulson@14365
   445
lemma real_of_preal_trichotomy:
paulson@14365
   446
      "\<exists>m. (x::real) = real_of_preal m | x = 0 | x = -(real_of_preal m)"
paulson@14484
   447
apply (simp add: real_of_preal_def real_zero_def, cases x)
huffman@23288
   448
apply (auto simp add: real_minus add_ac)
paulson@14365
   449
apply (cut_tac x = x and y = y in linorder_less_linear)
huffman@23288
   450
apply (auto dest!: less_add_left_Ex simp add: add_assoc [symmetric])
paulson@14365
   451
done
paulson@14365
   452
paulson@14365
   453
lemma real_of_preal_leD:
paulson@14365
   454
      "real_of_preal m1 \<le> real_of_preal m2 ==> m1 \<le> m2"
huffman@23288
   455
by (simp add: real_of_preal_def real_le)
paulson@14365
   456
paulson@14365
   457
lemma real_of_preal_lessI: "m1 < m2 ==> real_of_preal m1 < real_of_preal m2"
paulson@14365
   458
by (auto simp add: real_of_preal_leD linorder_not_le [symmetric])
paulson@14365
   459
paulson@14365
   460
lemma real_of_preal_lessD:
paulson@14365
   461
      "real_of_preal m1 < real_of_preal m2 ==> m1 < m2"
huffman@23288
   462
by (simp add: real_of_preal_def real_le linorder_not_le [symmetric])
paulson@14365
   463
paulson@14365
   464
lemma real_of_preal_less_iff [simp]:
paulson@14365
   465
     "(real_of_preal m1 < real_of_preal m2) = (m1 < m2)"
paulson@14365
   466
by (blast intro: real_of_preal_lessI real_of_preal_lessD)
paulson@14365
   467
paulson@14365
   468
lemma real_of_preal_le_iff:
paulson@14365
   469
     "(real_of_preal m1 \<le> real_of_preal m2) = (m1 \<le> m2)"
huffman@23288
   470
by (simp add: linorder_not_less [symmetric])
paulson@14365
   471
paulson@14365
   472
lemma real_of_preal_zero_less: "0 < real_of_preal m"
huffman@23288
   473
apply (insert preal_self_less_add_left [of 1 m])
huffman@23288
   474
apply (auto simp add: real_zero_def real_of_preal_def
huffman@23288
   475
                      real_less_def real_le_def add_ac)
huffman@23288
   476
apply (rule_tac x="m + 1" in exI, rule_tac x="1" in exI)
huffman@23288
   477
apply (simp add: add_ac)
paulson@14365
   478
done
paulson@14365
   479
paulson@14365
   480
lemma real_of_preal_minus_less_zero: "- real_of_preal m < 0"
paulson@14365
   481
by (simp add: real_of_preal_zero_less)
paulson@14365
   482
paulson@14365
   483
lemma real_of_preal_not_minus_gt_zero: "~ 0 < - real_of_preal m"
paulson@14484
   484
proof -
paulson@14484
   485
  from real_of_preal_minus_less_zero
paulson@14484
   486
  show ?thesis by (blast dest: order_less_trans)
paulson@14484
   487
qed
paulson@14365
   488
paulson@14365
   489
paulson@14365
   490
subsection{*Theorems About the Ordering*}
paulson@14365
   491
paulson@14365
   492
lemma real_gt_zero_preal_Ex: "(0 < x) = (\<exists>y. x = real_of_preal y)"
paulson@14365
   493
apply (auto simp add: real_of_preal_zero_less)
paulson@14365
   494
apply (cut_tac x = x in real_of_preal_trichotomy)
paulson@14365
   495
apply (blast elim!: real_of_preal_not_minus_gt_zero [THEN notE])
paulson@14365
   496
done
paulson@14365
   497
paulson@14365
   498
lemma real_gt_preal_preal_Ex:
paulson@14365
   499
     "real_of_preal z < x ==> \<exists>y. x = real_of_preal y"
paulson@14365
   500
by (blast dest!: real_of_preal_zero_less [THEN order_less_trans]
paulson@14365
   501
             intro: real_gt_zero_preal_Ex [THEN iffD1])
paulson@14365
   502
paulson@14365
   503
lemma real_ge_preal_preal_Ex:
paulson@14365
   504
     "real_of_preal z \<le> x ==> \<exists>y. x = real_of_preal y"
paulson@14365
   505
by (blast dest: order_le_imp_less_or_eq real_gt_preal_preal_Ex)
paulson@14365
   506
paulson@14365
   507
lemma real_less_all_preal: "y \<le> 0 ==> \<forall>x. y < real_of_preal x"
paulson@14365
   508
by (auto elim: order_le_imp_less_or_eq [THEN disjE] 
paulson@14365
   509
            intro: real_of_preal_zero_less [THEN [2] order_less_trans] 
paulson@14365
   510
            simp add: real_of_preal_zero_less)
paulson@14365
   511
paulson@14365
   512
lemma real_less_all_real2: "~ 0 < y ==> \<forall>x. y < real_of_preal x"
paulson@14365
   513
by (blast intro!: real_less_all_preal linorder_not_less [THEN iffD1])
paulson@14365
   514
paulson@14334
   515
paulson@14334
   516
subsection{*More Lemmas*}
paulson@14334
   517
paulson@14334
   518
lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
paulson@14334
   519
by auto
paulson@14334
   520
paulson@14334
   521
lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
paulson@14334
   522
by auto
paulson@14334
   523
paulson@14334
   524
lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
paulson@14334
   525
  by (force elim: order_less_asym
paulson@14334
   526
            simp add: Ring_and_Field.mult_less_cancel_right)
paulson@14334
   527
paulson@14334
   528
lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
paulson@14365
   529
apply (simp add: mult_le_cancel_right)
huffman@23289
   530
apply (blast intro: elim: order_less_asym)
paulson@14365
   531
done
paulson@14334
   532
paulson@14334
   533
lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
nipkow@15923
   534
by(simp add:mult_commute)
paulson@14334
   535
paulson@14365
   536
lemma real_inverse_gt_one: "[| (0::real) < x; x < 1 |] ==> 1 < inverse x"
huffman@23289
   537
by (simp add: one_less_inverse_iff) (* TODO: generalize/move *)
paulson@14334
   538
paulson@14334
   539
haftmann@24198
   540
subsection {* Embedding numbers into the Reals *}
haftmann@24198
   541
haftmann@24198
   542
abbreviation
haftmann@24198
   543
  real_of_nat :: "nat \<Rightarrow> real"
haftmann@24198
   544
where
haftmann@24198
   545
  "real_of_nat \<equiv> of_nat"
haftmann@24198
   546
haftmann@24198
   547
abbreviation
haftmann@24198
   548
  real_of_int :: "int \<Rightarrow> real"
haftmann@24198
   549
where
haftmann@24198
   550
  "real_of_int \<equiv> of_int"
haftmann@24198
   551
haftmann@24198
   552
abbreviation
haftmann@24198
   553
  real_of_rat :: "rat \<Rightarrow> real"
haftmann@24198
   554
where
haftmann@24198
   555
  "real_of_rat \<equiv> of_rat"
haftmann@24198
   556
haftmann@24198
   557
consts
haftmann@24198
   558
  (*overloaded constant for injecting other types into "real"*)
haftmann@24198
   559
  real :: "'a => real"
paulson@14365
   560
paulson@14378
   561
defs (overloaded)
haftmann@28520
   562
  real_of_nat_def [code unfold]: "real == real_of_nat"
haftmann@28520
   563
  real_of_int_def [code unfold]: "real == real_of_int"
paulson@14365
   564
avigad@16819
   565
lemma real_eq_of_nat: "real = of_nat"
haftmann@24198
   566
  unfolding real_of_nat_def ..
avigad@16819
   567
avigad@16819
   568
lemma real_eq_of_int: "real = of_int"
haftmann@24198
   569
  unfolding real_of_int_def ..
avigad@16819
   570
paulson@14365
   571
lemma real_of_int_zero [simp]: "real (0::int) = 0"  
paulson@14378
   572
by (simp add: real_of_int_def) 
paulson@14365
   573
paulson@14365
   574
lemma real_of_one [simp]: "real (1::int) = (1::real)"
paulson@14378
   575
by (simp add: real_of_int_def) 
paulson@14334
   576
avigad@16819
   577
lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
paulson@14378
   578
by (simp add: real_of_int_def) 
paulson@14365
   579
avigad@16819
   580
lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
paulson@14378
   581
by (simp add: real_of_int_def) 
avigad@16819
   582
avigad@16819
   583
lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
avigad@16819
   584
by (simp add: real_of_int_def) 
paulson@14365
   585
avigad@16819
   586
lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
paulson@14378
   587
by (simp add: real_of_int_def) 
paulson@14334
   588
avigad@16819
   589
lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
avigad@16819
   590
  apply (subst real_eq_of_int)+
avigad@16819
   591
  apply (rule of_int_setsum)
avigad@16819
   592
done
avigad@16819
   593
avigad@16819
   594
lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
avigad@16819
   595
    (PROD x:A. real(f x))"
avigad@16819
   596
  apply (subst real_eq_of_int)+
avigad@16819
   597
  apply (rule of_int_setprod)
avigad@16819
   598
done
paulson@14365
   599
chaieb@27668
   600
lemma real_of_int_zero_cancel [simp, algebra, presburger]: "(real x = 0) = (x = (0::int))"
paulson@14378
   601
by (simp add: real_of_int_def) 
paulson@14365
   602
chaieb@27668
   603
lemma real_of_int_inject [iff, algebra, presburger]: "(real (x::int) = real y) = (x = y)"
paulson@14378
   604
by (simp add: real_of_int_def) 
paulson@14365
   605
chaieb@27668
   606
lemma real_of_int_less_iff [iff, presburger]: "(real (x::int) < real y) = (x < y)"
paulson@14378
   607
by (simp add: real_of_int_def) 
paulson@14365
   608
chaieb@27668
   609
lemma real_of_int_le_iff [simp, presburger]: "(real (x::int) \<le> real y) = (x \<le> y)"
paulson@14378
   610
by (simp add: real_of_int_def) 
paulson@14365
   611
chaieb@27668
   612
lemma real_of_int_gt_zero_cancel_iff [simp, presburger]: "(0 < real (n::int)) = (0 < n)"
avigad@16819
   613
by (simp add: real_of_int_def) 
avigad@16819
   614
chaieb@27668
   615
lemma real_of_int_ge_zero_cancel_iff [simp, presburger]: "(0 <= real (n::int)) = (0 <= n)"
avigad@16819
   616
by (simp add: real_of_int_def) 
avigad@16819
   617
chaieb@27668
   618
lemma real_of_int_lt_zero_cancel_iff [simp, presburger]: "(real (n::int) < 0) = (n < 0)" 
avigad@16819
   619
by (simp add: real_of_int_def)
avigad@16819
   620
chaieb@27668
   621
lemma real_of_int_le_zero_cancel_iff [simp, presburger]: "(real (n::int) <= 0) = (n <= 0)"
avigad@16819
   622
by (simp add: real_of_int_def)
avigad@16819
   623
avigad@16888
   624
lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
avigad@16888
   625
by (auto simp add: abs_if)
avigad@16888
   626
avigad@16819
   627
lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
avigad@16819
   628
  apply (subgoal_tac "real n + 1 = real (n + 1)")
avigad@16819
   629
  apply (simp del: real_of_int_add)
avigad@16819
   630
  apply auto
avigad@16819
   631
done
avigad@16819
   632
avigad@16819
   633
lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
avigad@16819
   634
  apply (subgoal_tac "real m + 1 = real (m + 1)")
avigad@16819
   635
  apply (simp del: real_of_int_add)
avigad@16819
   636
  apply simp
avigad@16819
   637
done
avigad@16819
   638
avigad@16819
   639
lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
avigad@16819
   640
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
   641
proof -
avigad@16819
   642
  assume "d ~= 0"
avigad@16819
   643
  have "x = (x div d) * d + x mod d"
avigad@16819
   644
    by auto
avigad@16819
   645
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
   646
    by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
avigad@16819
   647
  then have "real x / real d = ... / real d"
avigad@16819
   648
    by simp
avigad@16819
   649
  then show ?thesis
nipkow@29667
   650
    by (auto simp add: add_divide_distrib algebra_simps prems)
avigad@16819
   651
qed
avigad@16819
   652
avigad@16819
   653
lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
avigad@16819
   654
    real(n div d) = real n / real d"
avigad@16819
   655
  apply (frule real_of_int_div_aux [of d n])
avigad@16819
   656
  apply simp
nipkow@30042
   657
  apply (simp add: dvd_eq_mod_eq_0)
avigad@16819
   658
done
avigad@16819
   659
avigad@16819
   660
lemma real_of_int_div2:
avigad@16819
   661
  "0 <= real (n::int) / real (x) - real (n div x)"
avigad@16819
   662
  apply (case_tac "x = 0")
avigad@16819
   663
  apply simp
avigad@16819
   664
  apply (case_tac "0 < x")
nipkow@29667
   665
  apply (simp add: algebra_simps)
avigad@16819
   666
  apply (subst real_of_int_div_aux)
avigad@16819
   667
  apply simp
avigad@16819
   668
  apply simp
avigad@16819
   669
  apply (subst zero_le_divide_iff)
avigad@16819
   670
  apply auto
nipkow@29667
   671
  apply (simp add: algebra_simps)
avigad@16819
   672
  apply (subst real_of_int_div_aux)
avigad@16819
   673
  apply simp
avigad@16819
   674
  apply simp
avigad@16819
   675
  apply (subst zero_le_divide_iff)
avigad@16819
   676
  apply auto
avigad@16819
   677
done
avigad@16819
   678
avigad@16819
   679
lemma real_of_int_div3:
avigad@16819
   680
  "real (n::int) / real (x) - real (n div x) <= 1"
avigad@16819
   681
  apply(case_tac "x = 0")
avigad@16819
   682
  apply simp
nipkow@29667
   683
  apply (simp add: algebra_simps)
avigad@16819
   684
  apply (subst real_of_int_div_aux)
avigad@16819
   685
  apply assumption
avigad@16819
   686
  apply simp
avigad@16819
   687
  apply (subst divide_le_eq)
avigad@16819
   688
  apply clarsimp
avigad@16819
   689
  apply (rule conjI)
avigad@16819
   690
  apply (rule impI)
avigad@16819
   691
  apply (rule order_less_imp_le)
avigad@16819
   692
  apply simp
avigad@16819
   693
  apply (rule impI)
avigad@16819
   694
  apply (rule order_less_imp_le)
avigad@16819
   695
  apply simp
avigad@16819
   696
done
avigad@16819
   697
avigad@16819
   698
lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
nipkow@27964
   699
by (insert real_of_int_div2 [of n x], simp)
nipkow@27964
   700
nipkow@27964
   701
paulson@14365
   702
subsection{*Embedding the Naturals into the Reals*}
paulson@14365
   703
paulson@14334
   704
lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
paulson@14365
   705
by (simp add: real_of_nat_def)
paulson@14334
   706
huffman@30082
   707
lemma real_of_nat_1 [simp]: "real (1::nat) = 1"
huffman@30082
   708
by (simp add: real_of_nat_def)
huffman@30082
   709
paulson@14334
   710
lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
paulson@14365
   711
by (simp add: real_of_nat_def)
paulson@14334
   712
paulson@14365
   713
lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
paulson@14378
   714
by (simp add: real_of_nat_def)
paulson@14334
   715
paulson@14334
   716
(*Not for addsimps: often the LHS is used to represent a positive natural*)
paulson@14334
   717
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
paulson@14378
   718
by (simp add: real_of_nat_def)
paulson@14334
   719
paulson@14334
   720
lemma real_of_nat_less_iff [iff]: 
paulson@14334
   721
     "(real (n::nat) < real m) = (n < m)"
paulson@14365
   722
by (simp add: real_of_nat_def)
paulson@14334
   723
paulson@14334
   724
lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
paulson@14378
   725
by (simp add: real_of_nat_def)
paulson@14334
   726
paulson@14334
   727
lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
paulson@14378
   728
by (simp add: real_of_nat_def zero_le_imp_of_nat)
paulson@14334
   729
paulson@14365
   730
lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
paulson@14378
   731
by (simp add: real_of_nat_def del: of_nat_Suc)
paulson@14365
   732
paulson@14334
   733
lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
huffman@23431
   734
by (simp add: real_of_nat_def of_nat_mult)
paulson@14334
   735
avigad@16819
   736
lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
avigad@16819
   737
    (SUM x:A. real(f x))"
avigad@16819
   738
  apply (subst real_eq_of_nat)+
avigad@16819
   739
  apply (rule of_nat_setsum)
avigad@16819
   740
done
avigad@16819
   741
avigad@16819
   742
lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
avigad@16819
   743
    (PROD x:A. real(f x))"
avigad@16819
   744
  apply (subst real_eq_of_nat)+
avigad@16819
   745
  apply (rule of_nat_setprod)
avigad@16819
   746
done
avigad@16819
   747
avigad@16819
   748
lemma real_of_card: "real (card A) = setsum (%x.1) A"
avigad@16819
   749
  apply (subst card_eq_setsum)
avigad@16819
   750
  apply (subst real_of_nat_setsum)
avigad@16819
   751
  apply simp
avigad@16819
   752
done
avigad@16819
   753
paulson@14334
   754
lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
paulson@14378
   755
by (simp add: real_of_nat_def)
paulson@14334
   756
paulson@14387
   757
lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
paulson@14378
   758
by (simp add: real_of_nat_def)
paulson@14334
   759
paulson@14365
   760
lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
huffman@23438
   761
by (simp add: add: real_of_nat_def of_nat_diff)
paulson@14334
   762
nipkow@25162
   763
lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
nipkow@25140
   764
by (auto simp: real_of_nat_def)
paulson@14365
   765
paulson@14365
   766
lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
paulson@14378
   767
by (simp add: add: real_of_nat_def)
paulson@14334
   768
paulson@14365
   769
lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
paulson@14378
   770
by (simp add: add: real_of_nat_def)
paulson@14334
   771
nipkow@25140
   772
lemma real_of_nat_ge_zero_cancel_iff [simp]: "(0 \<le> real (n::nat))"
paulson@14378
   773
by (simp add: add: real_of_nat_def)
paulson@14334
   774
avigad@16819
   775
lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
avigad@16819
   776
  apply (subgoal_tac "real n + 1 = real (Suc n)")
avigad@16819
   777
  apply simp
avigad@16819
   778
  apply (auto simp add: real_of_nat_Suc)
avigad@16819
   779
done
avigad@16819
   780
avigad@16819
   781
lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
avigad@16819
   782
  apply (subgoal_tac "real m + 1 = real (Suc m)")
avigad@16819
   783
  apply (simp add: less_Suc_eq_le)
avigad@16819
   784
  apply (simp add: real_of_nat_Suc)
avigad@16819
   785
done
avigad@16819
   786
avigad@16819
   787
lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
avigad@16819
   788
    real (x div d) + (real (x mod d)) / (real d)"
avigad@16819
   789
proof -
avigad@16819
   790
  assume "0 < d"
avigad@16819
   791
  have "x = (x div d) * d + x mod d"
avigad@16819
   792
    by auto
avigad@16819
   793
  then have "real x = real (x div d) * real d + real(x mod d)"
avigad@16819
   794
    by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
avigad@16819
   795
  then have "real x / real d = \<dots> / real d"
avigad@16819
   796
    by simp
avigad@16819
   797
  then show ?thesis
nipkow@29667
   798
    by (auto simp add: add_divide_distrib algebra_simps prems)
avigad@16819
   799
qed
avigad@16819
   800
avigad@16819
   801
lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
avigad@16819
   802
    real(n div d) = real n / real d"
avigad@16819
   803
  apply (frule real_of_nat_div_aux [of d n])
avigad@16819
   804
  apply simp
avigad@16819
   805
  apply (subst dvd_eq_mod_eq_0 [THEN sym])
avigad@16819
   806
  apply assumption
avigad@16819
   807
done
avigad@16819
   808
avigad@16819
   809
lemma real_of_nat_div2:
avigad@16819
   810
  "0 <= real (n::nat) / real (x) - real (n div x)"
nipkow@25134
   811
apply(case_tac "x = 0")
nipkow@25134
   812
 apply (simp)
nipkow@29667
   813
apply (simp add: algebra_simps)
nipkow@25134
   814
apply (subst real_of_nat_div_aux)
nipkow@25134
   815
 apply simp
nipkow@25134
   816
apply simp
nipkow@25134
   817
apply (subst zero_le_divide_iff)
nipkow@25134
   818
apply simp
avigad@16819
   819
done
avigad@16819
   820
avigad@16819
   821
lemma real_of_nat_div3:
avigad@16819
   822
  "real (n::nat) / real (x) - real (n div x) <= 1"
nipkow@25134
   823
apply(case_tac "x = 0")
nipkow@25134
   824
apply (simp)
nipkow@29667
   825
apply (simp add: algebra_simps)
nipkow@25134
   826
apply (subst real_of_nat_div_aux)
nipkow@25134
   827
 apply simp
nipkow@25134
   828
apply simp
avigad@16819
   829
done
avigad@16819
   830
avigad@16819
   831
lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
nipkow@29667
   832
by (insert real_of_nat_div2 [of n x], simp)
avigad@16819
   833
paulson@14365
   834
lemma real_of_int_real_of_nat: "real (int n) = real n"
paulson@14378
   835
by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)
paulson@14378
   836
paulson@14426
   837
lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
paulson@14426
   838
by (simp add: real_of_int_def real_of_nat_def)
paulson@14334
   839
avigad@16819
   840
lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
avigad@16819
   841
  apply (subgoal_tac "real(int(nat x)) = real(nat x)")
avigad@16819
   842
  apply force
avigad@16819
   843
  apply (simp only: real_of_int_real_of_nat)
avigad@16819
   844
done
paulson@14387
   845
nipkow@28001
   846
nipkow@28001
   847
subsection{* Rationals *}
nipkow@28001
   848
nipkow@28091
   849
lemma Rats_real_nat[simp]: "real(n::nat) \<in> \<rat>"
nipkow@28091
   850
by (simp add: real_eq_of_nat)
nipkow@28091
   851
nipkow@28091
   852
nipkow@28001
   853
lemma Rats_eq_int_div_int:
nipkow@28091
   854
  "\<rat> = { real(i::int)/real(j::int) |i j. j \<noteq> 0}" (is "_ = ?S")
nipkow@28001
   855
proof
nipkow@28091
   856
  show "\<rat> \<subseteq> ?S"
nipkow@28001
   857
  proof
nipkow@28091
   858
    fix x::real assume "x : \<rat>"
nipkow@28001
   859
    then obtain r where "x = of_rat r" unfolding Rats_def ..
nipkow@28001
   860
    have "of_rat r : ?S"
nipkow@28001
   861
      by (cases r)(auto simp add:of_rat_rat real_eq_of_int)
nipkow@28001
   862
    thus "x : ?S" using `x = of_rat r` by simp
nipkow@28001
   863
  qed
nipkow@28001
   864
next
nipkow@28091
   865
  show "?S \<subseteq> \<rat>"
nipkow@28001
   866
  proof(auto simp:Rats_def)
nipkow@28001
   867
    fix i j :: int assume "j \<noteq> 0"
nipkow@28001
   868
    hence "real i / real j = of_rat(Fract i j)"
nipkow@28001
   869
      by (simp add:of_rat_rat real_eq_of_int)
nipkow@28001
   870
    thus "real i / real j \<in> range of_rat" by blast
nipkow@28001
   871
  qed
nipkow@28001
   872
qed
nipkow@28001
   873
nipkow@28001
   874
lemma Rats_eq_int_div_nat:
nipkow@28091
   875
  "\<rat> = { real(i::int)/real(n::nat) |i n. n \<noteq> 0}"
nipkow@28001
   876
proof(auto simp:Rats_eq_int_div_int)
nipkow@28001
   877
  fix i j::int assume "j \<noteq> 0"
nipkow@28001
   878
  show "EX (i'::int) (n::nat). real i/real j = real i'/real n \<and> 0<n"
nipkow@28001
   879
  proof cases
nipkow@28001
   880
    assume "j>0"
nipkow@28001
   881
    hence "real i/real j = real i/real(nat j) \<and> 0<nat j"
nipkow@28001
   882
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
   883
    thus ?thesis by blast
nipkow@28001
   884
  next
nipkow@28001
   885
    assume "~ j>0"
nipkow@28001
   886
    hence "real i/real j = real(-i)/real(nat(-j)) \<and> 0<nat(-j)" using `j\<noteq>0`
nipkow@28001
   887
      by (simp add: real_eq_of_int real_eq_of_nat of_nat_nat)
nipkow@28001
   888
    thus ?thesis by blast
nipkow@28001
   889
  qed
nipkow@28001
   890
next
nipkow@28001
   891
  fix i::int and n::nat assume "0 < n"
nipkow@28001
   892
  hence "real i/real n = real i/real(int n) \<and> int n \<noteq> 0" by simp
nipkow@28001
   893
  thus "\<exists>(i'::int) j::int. real i/real n = real i'/real j \<and> j \<noteq> 0" by blast
nipkow@28001
   894
qed
nipkow@28001
   895
nipkow@28001
   896
lemma Rats_abs_nat_div_natE:
nipkow@28001
   897
  assumes "x \<in> \<rat>"
huffman@31706
   898
  obtains m n :: nat
huffman@31706
   899
  where "n \<noteq> 0" and "\<bar>x\<bar> = real m / real n" and "gcd m n = 1"
nipkow@28001
   900
proof -
nipkow@28001
   901
  from `x \<in> \<rat>` obtain i::int and n::nat where "n \<noteq> 0" and "x = real i / real n"
nipkow@28001
   902
    by(auto simp add: Rats_eq_int_div_nat)
nipkow@28001
   903
  hence "\<bar>x\<bar> = real(nat(abs i)) / real n" by simp
nipkow@28001
   904
  then obtain m :: nat where x_rat: "\<bar>x\<bar> = real m / real n" by blast
nipkow@28001
   905
  let ?gcd = "gcd m n"
huffman@31706
   906
  from `n\<noteq>0` have gcd: "?gcd \<noteq> 0" by simp
nipkow@28001
   907
  let ?k = "m div ?gcd"
nipkow@28001
   908
  let ?l = "n div ?gcd"
nipkow@28001
   909
  let ?gcd' = "gcd ?k ?l"
nipkow@28001
   910
  have "?gcd dvd m" .. then have gcd_k: "?gcd * ?k = m"
nipkow@28001
   911
    by (rule dvd_mult_div_cancel)
nipkow@28001
   912
  have "?gcd dvd n" .. then have gcd_l: "?gcd * ?l = n"
nipkow@28001
   913
    by (rule dvd_mult_div_cancel)
nipkow@28001
   914
  from `n\<noteq>0` and gcd_l have "?l \<noteq> 0" by (auto iff del: neq0_conv)
nipkow@28001
   915
  moreover
nipkow@28001
   916
  have "\<bar>x\<bar> = real ?k / real ?l"
nipkow@28001
   917
  proof -
nipkow@28001
   918
    from gcd have "real ?k / real ?l =
nipkow@28001
   919
        real (?gcd * ?k) / real (?gcd * ?l)" by simp
nipkow@28001
   920
    also from gcd_k and gcd_l have "\<dots> = real m / real n" by simp
nipkow@28001
   921
    also from x_rat have "\<dots> = \<bar>x\<bar>" ..
nipkow@28001
   922
    finally show ?thesis ..
nipkow@28001
   923
  qed
nipkow@28001
   924
  moreover
nipkow@28001
   925
  have "?gcd' = 1"
nipkow@28001
   926
  proof -
nipkow@28001
   927
    have "?gcd * ?gcd' = gcd (?gcd * ?k) (?gcd * ?l)"
huffman@31706
   928
      by (rule nat_gcd_mult_distrib)
nipkow@28001
   929
    with gcd_k gcd_l have "?gcd * ?gcd' = ?gcd" by simp
huffman@31706
   930
    with gcd show ?thesis by auto
nipkow@28001
   931
  qed
nipkow@28001
   932
  ultimately show ?thesis ..
nipkow@28001
   933
qed
nipkow@28001
   934
nipkow@28001
   935
paulson@14387
   936
subsection{*Numerals and Arithmetic*}
paulson@14387
   937
haftmann@25571
   938
instantiation real :: number_ring
haftmann@25571
   939
begin
haftmann@25571
   940
haftmann@25571
   941
definition
haftmann@28562
   942
  real_number_of_def [code del]: "number_of w = real_of_int w"
haftmann@25571
   943
haftmann@25571
   944
instance
haftmann@24198
   945
  by intro_classes (simp add: real_number_of_def)
paulson@14387
   946
haftmann@25571
   947
end
haftmann@25571
   948
haftmann@25965
   949
lemma [code unfold, symmetric, code post]:
haftmann@24198
   950
  "number_of k = real_of_int (number_of k)"
haftmann@24198
   951
  unfolding number_of_is_id real_number_of_def ..
paulson@14387
   952
paulson@14387
   953
paulson@14387
   954
text{*Collapse applications of @{term real} to @{term number_of}*}
paulson@14387
   955
lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
paulson@14387
   956
by (simp add:  real_of_int_def of_int_number_of_eq)
paulson@14387
   957
paulson@14387
   958
lemma real_of_nat_number_of [simp]:
paulson@14387
   959
     "real (number_of v :: nat) =  
paulson@14387
   960
        (if neg (number_of v :: int) then 0  
paulson@14387
   961
         else (number_of v :: real))"
paulson@14387
   962
by (simp add: real_of_int_real_of_nat [symmetric] int_nat_number_of)
paulson@14387
   963
haftmann@31100
   964
declaration {*
haftmann@31100
   965
  K (Lin_Arith.add_inj_thms [@{thm real_of_nat_le_iff} RS iffD2, @{thm real_of_nat_inject} RS iffD2]
haftmann@31100
   966
    (* not needed because x < (y::nat) can be rewritten as Suc x <= y: real_of_nat_less_iff RS iffD2 *)
haftmann@31100
   967
  #> Lin_Arith.add_inj_thms [@{thm real_of_int_le_iff} RS iffD2, @{thm real_of_int_inject} RS iffD2]
haftmann@31100
   968
    (* not needed because x < (y::int) can be rewritten as x + 1 <= y: real_of_int_less_iff RS iffD2 *)
haftmann@31100
   969
  #> Lin_Arith.add_simps [@{thm real_of_nat_zero}, @{thm real_of_nat_Suc}, @{thm real_of_nat_add},
haftmann@31100
   970
      @{thm real_of_nat_mult}, @{thm real_of_int_zero}, @{thm real_of_one},
haftmann@31100
   971
      @{thm real_of_int_add}, @{thm real_of_int_minus}, @{thm real_of_int_diff},
haftmann@31100
   972
      @{thm real_of_int_mult}, @{thm real_of_int_of_nat_eq},
haftmann@31100
   973
      @{thm real_of_nat_number_of}, @{thm real_number_of}]
haftmann@31100
   974
  #> Lin_Arith.add_inj_const (@{const_name real}, HOLogic.natT --> HOLogic.realT)
haftmann@31100
   975
  #> Lin_Arith.add_inj_const (@{const_name real}, HOLogic.intT --> HOLogic.realT))
haftmann@31100
   976
*}
paulson@14387
   977
kleing@19023
   978
paulson@14387
   979
subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
paulson@14387
   980
paulson@14387
   981
text{*Needed in this non-standard form by Hyperreal/Transcendental*}
paulson@14387
   982
lemma real_0_le_divide_iff:
paulson@14387
   983
     "((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
paulson@14387
   984
by (simp add: real_divide_def zero_le_mult_iff, auto)
paulson@14387
   985
paulson@14387
   986
lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
paulson@14387
   987
by arith
paulson@14387
   988
paulson@15085
   989
lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
paulson@14387
   990
by auto
paulson@14387
   991
paulson@15085
   992
lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
paulson@14387
   993
by auto
paulson@14387
   994
paulson@15085
   995
lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
paulson@14387
   996
by auto
paulson@14387
   997
paulson@15085
   998
lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
paulson@14387
   999
by auto
paulson@14387
  1000
paulson@15085
  1001
lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
paulson@14387
  1002
by auto
paulson@14387
  1003
paulson@14387
  1004
paulson@14387
  1005
(*
paulson@14387
  1006
FIXME: we should have this, as for type int, but many proofs would break.
paulson@14387
  1007
It replaces x+-y by x-y.
paulson@15086
  1008
declare real_diff_def [symmetric, simp]
paulson@14387
  1009
*)
paulson@14387
  1010
paulson@14387
  1011
subsubsection{*Density of the Reals*}
paulson@14387
  1012
paulson@14387
  1013
lemma real_lbound_gt_zero:
paulson@14387
  1014
     "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
paulson@14387
  1015
apply (rule_tac x = " (min d1 d2) /2" in exI)
paulson@14387
  1016
apply (simp add: min_def)
paulson@14387
  1017
done
paulson@14387
  1018
paulson@14387
  1019
paulson@14387
  1020
text{*Similar results are proved in @{text Ring_and_Field}*}
paulson@14387
  1021
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
paulson@14387
  1022
  by auto
paulson@14387
  1023
paulson@14387
  1024
lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
paulson@14387
  1025
  by auto
paulson@14387
  1026
paulson@14387
  1027
paulson@14387
  1028
subsection{*Absolute Value Function for the Reals*}
paulson@14387
  1029
paulson@14387
  1030
lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
paulson@15003
  1031
by (simp add: abs_if)
paulson@14387
  1032
huffman@23289
  1033
(* FIXME: redundant, but used by Integration/RealRandVar.thy in AFP *)
paulson@14387
  1034
lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
obua@14738
  1035
by (force simp add: OrderedGroup.abs_le_iff)
paulson@14387
  1036
paulson@14387
  1037
lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
paulson@15003
  1038
by (simp add: abs_if)
paulson@14387
  1039
paulson@14387
  1040
lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
huffman@22958
  1041
by (rule abs_of_nonneg [OF real_of_nat_ge_zero])
paulson@14387
  1042
paulson@14387
  1043
lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
webertj@20217
  1044
by simp
paulson@14387
  1045
 
paulson@14387
  1046
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
webertj@20217
  1047
by simp
paulson@14387
  1048
haftmann@26732
  1049
instance real :: lordered_ring
haftmann@26732
  1050
proof
haftmann@26732
  1051
  fix a::real
haftmann@26732
  1052
  show "abs a = sup a (-a)"
haftmann@26732
  1053
    by (auto simp add: real_abs_def sup_real_def)
haftmann@26732
  1054
qed
haftmann@26732
  1055
berghofe@24534
  1056
haftmann@27544
  1057
subsection {* Implementation of rational real numbers *}
berghofe@24534
  1058
haftmann@27544
  1059
definition Ratreal :: "rat \<Rightarrow> real" where
haftmann@27544
  1060
  [simp]: "Ratreal = of_rat"
berghofe@24534
  1061
haftmann@24623
  1062
code_datatype Ratreal
berghofe@24534
  1063
haftmann@27544
  1064
lemma Ratreal_number_collapse [code post]:
haftmann@27544
  1065
  "Ratreal 0 = 0"
haftmann@27544
  1066
  "Ratreal 1 = 1"
haftmann@27544
  1067
  "Ratreal (number_of k) = number_of k"
haftmann@27544
  1068
by simp_all
berghofe@24534
  1069
berghofe@24534
  1070
lemma zero_real_code [code, code unfold]:
haftmann@27544
  1071
  "0 = Ratreal 0"
haftmann@27544
  1072
by simp
berghofe@24534
  1073
berghofe@24534
  1074
lemma one_real_code [code, code unfold]:
haftmann@27544
  1075
  "1 = Ratreal 1"
haftmann@27544
  1076
by simp
haftmann@27544
  1077
haftmann@27544
  1078
lemma number_of_real_code [code unfold]:
haftmann@27544
  1079
  "number_of k = Ratreal (number_of k)"
haftmann@27544
  1080
by simp
haftmann@27544
  1081
haftmann@27544
  1082
lemma Ratreal_number_of_quotient [code post]:
haftmann@27544
  1083
  "Ratreal (number_of r) / Ratreal (number_of s) = number_of r / number_of s"
haftmann@27544
  1084
by simp
haftmann@27544
  1085
haftmann@27544
  1086
lemma Ratreal_number_of_quotient2 [code post]:
haftmann@27544
  1087
  "Ratreal (number_of r / number_of s) = number_of r / number_of s"
haftmann@27544
  1088
unfolding Ratreal_number_of_quotient [symmetric] Ratreal_def of_rat_divide ..
berghofe@24534
  1089
haftmann@26513
  1090
instantiation real :: eq
haftmann@26513
  1091
begin
haftmann@26513
  1092
haftmann@27544
  1093
definition "eq_class.eq (x\<Colon>real) y \<longleftrightarrow> x - y = 0"
haftmann@26513
  1094
haftmann@26513
  1095
instance by default (simp add: eq_real_def)
berghofe@24534
  1096
haftmann@27544
  1097
lemma real_eq_code [code]: "eq_class.eq (Ratreal x) (Ratreal y) \<longleftrightarrow> eq_class.eq x y"
haftmann@27544
  1098
  by (simp add: eq_real_def eq)
haftmann@26513
  1099
haftmann@28351
  1100
lemma real_eq_refl [code nbe]:
haftmann@28351
  1101
  "eq_class.eq (x::real) x \<longleftrightarrow> True"
haftmann@28351
  1102
  by (rule HOL.eq_refl)
haftmann@28351
  1103
haftmann@26513
  1104
end
berghofe@24534
  1105
haftmann@27544
  1106
lemma real_less_eq_code [code]: "Ratreal x \<le> Ratreal y \<longleftrightarrow> x \<le> y"
haftmann@27652
  1107
  by (simp add: of_rat_less_eq)
berghofe@24534
  1108
haftmann@27544
  1109
lemma real_less_code [code]: "Ratreal x < Ratreal y \<longleftrightarrow> x < y"
haftmann@27652
  1110
  by (simp add: of_rat_less)
berghofe@24534
  1111
haftmann@27544
  1112
lemma real_plus_code [code]: "Ratreal x + Ratreal y = Ratreal (x + y)"
haftmann@27544
  1113
  by (simp add: of_rat_add)
berghofe@24534
  1114
haftmann@27544
  1115
lemma real_times_code [code]: "Ratreal x * Ratreal y = Ratreal (x * y)"
haftmann@27544
  1116
  by (simp add: of_rat_mult)
haftmann@27544
  1117
haftmann@27544
  1118
lemma real_uminus_code [code]: "- Ratreal x = Ratreal (- x)"
haftmann@27544
  1119
  by (simp add: of_rat_minus)
berghofe@24534
  1120
haftmann@27544
  1121
lemma real_minus_code [code]: "Ratreal x - Ratreal y = Ratreal (x - y)"
haftmann@27544
  1122
  by (simp add: of_rat_diff)
berghofe@24534
  1123
haftmann@27544
  1124
lemma real_inverse_code [code]: "inverse (Ratreal x) = Ratreal (inverse x)"
haftmann@27544
  1125
  by (simp add: of_rat_inverse)
haftmann@27544
  1126
 
haftmann@27544
  1127
lemma real_divide_code [code]: "Ratreal x / Ratreal y = Ratreal (x / y)"
haftmann@27544
  1128
  by (simp add: of_rat_divide)
berghofe@24534
  1129
haftmann@31203
  1130
definition (in term_syntax)
haftmann@31203
  1131
  valterm_ratreal :: "rat \<times> (unit \<Rightarrow> Code_Eval.term) \<Rightarrow> real \<times> (unit \<Rightarrow> Code_Eval.term)" where
haftmann@31203
  1132
  [code inline]: "valterm_ratreal k = Code_Eval.valtermify Ratreal {\<cdot>} k"
haftmann@31203
  1133
haftmann@31203
  1134
notation fcomp (infixl "o>" 60)
haftmann@31203
  1135
notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31203
  1136
haftmann@31203
  1137
instantiation real :: random
haftmann@31203
  1138
begin
haftmann@31203
  1139
haftmann@31203
  1140
definition
haftmann@31641
  1141
  "Quickcheck.random i = Quickcheck.random i o\<rightarrow> (\<lambda>r. Pair (valterm_ratreal r))"
haftmann@31203
  1142
haftmann@31203
  1143
instance ..
haftmann@31203
  1144
haftmann@31203
  1145
end
haftmann@31203
  1146
haftmann@31203
  1147
no_notation fcomp (infixl "o>" 60)
haftmann@31203
  1148
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@31203
  1149
haftmann@24623
  1150
text {* Setup for SML code generator *}
nipkow@23031
  1151
nipkow@23031
  1152
types_code
berghofe@24534
  1153
  real ("(int */ int)")
nipkow@23031
  1154
attach (term_of) {*
berghofe@24534
  1155
fun term_of_real (p, q) =
haftmann@24623
  1156
  let
haftmann@24623
  1157
    val rT = HOLogic.realT
berghofe@24534
  1158
  in
berghofe@24534
  1159
    if q = 1 orelse p = 0 then HOLogic.mk_number rT p
haftmann@24623
  1160
    else @{term "op / \<Colon> real \<Rightarrow> real \<Rightarrow> real"} $
berghofe@24534
  1161
      HOLogic.mk_number rT p $ HOLogic.mk_number rT q
berghofe@24534
  1162
  end;
nipkow@23031
  1163
*}
nipkow@23031
  1164
attach (test) {*
nipkow@23031
  1165
fun gen_real i =
berghofe@24534
  1166
  let
berghofe@24534
  1167
    val p = random_range 0 i;
berghofe@24534
  1168
    val q = random_range 1 (i + 1);
berghofe@24534
  1169
    val g = Integer.gcd p q;
wenzelm@24630
  1170
    val p' = p div g;
wenzelm@24630
  1171
    val q' = q div g;
berghofe@25885
  1172
    val r = (if one_of [true, false] then p' else ~ p',
berghofe@25885
  1173
      if p' = 0 then 0 else q')
berghofe@24534
  1174
  in
berghofe@25885
  1175
    (r, fn () => term_of_real r)
berghofe@24534
  1176
  end;
nipkow@23031
  1177
*}
nipkow@23031
  1178
nipkow@23031
  1179
consts_code
haftmann@24623
  1180
  Ratreal ("(_)")
berghofe@24534
  1181
berghofe@24534
  1182
consts_code
berghofe@24534
  1183
  "of_int :: int \<Rightarrow> real" ("\<module>real'_of'_int")
berghofe@24534
  1184
attach {*
berghofe@24534
  1185
fun real_of_int 0 = (0, 0)
berghofe@24534
  1186
  | real_of_int i = (i, 1);
berghofe@24534
  1187
*}
berghofe@24534
  1188
paulson@5588
  1189
end