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