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