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