src/HOL/Real/RealDef.thy
author haftmann
Fri Mar 16 21:32:12 2007 +0100 (2007-03-16)
changeset 22456 6070e48ecb78
parent 21404 eb85850d3eb7
child 22958 b3a5569a81e5
permissions -rw-r--r--
added lattice definitions
     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 instance real :: distrib_lattice
   438   "inf x y \<equiv> min x y"
   439   "sup x y \<equiv> max x y"
   440   by default (auto simp add: inf_real_def sup_real_def min_max.sup_inf_distrib1)
   441 
   442 
   443 subsection{*The Reals Form an Ordered Field*}
   444 
   445 instance real :: ordered_field
   446 proof
   447   fix x y z :: real
   448   show "x \<le> y ==> z + x \<le> z + y" by (rule real_add_left_mono)
   449   show "x < y ==> 0 < z ==> z * x < z * y" by (simp add: real_mult_less_mono2)
   450   show "\<bar>x\<bar> = (if x < 0 then -x else x)"
   451     by (auto dest: order_le_less_trans simp add: real_abs_def linorder_not_le)
   452 qed
   453 
   454 text{*The function @{term real_of_preal} requires many proofs, but it seems
   455 to be essential for proving completeness of the reals from that of the
   456 positive reals.*}
   457 
   458 lemma real_of_preal_add:
   459      "real_of_preal ((x::preal) + y) = real_of_preal x + real_of_preal y"
   460 by (simp add: real_of_preal_def real_add preal_add_mult_distrib preal_mult_1 
   461               preal_add_ac)
   462 
   463 lemma real_of_preal_mult:
   464      "real_of_preal ((x::preal) * y) = real_of_preal x* real_of_preal y"
   465 by (simp add: real_of_preal_def real_mult preal_add_mult_distrib2
   466               preal_mult_1 preal_mult_1_right preal_add_ac preal_mult_ac)
   467 
   468 
   469 text{*Gleason prop 9-4.4 p 127*}
   470 lemma real_of_preal_trichotomy:
   471       "\<exists>m. (x::real) = real_of_preal m | x = 0 | x = -(real_of_preal m)"
   472 apply (simp add: real_of_preal_def real_zero_def, cases x)
   473 apply (auto simp add: real_minus preal_add_ac)
   474 apply (cut_tac x = x and y = y in linorder_less_linear)
   475 apply (auto dest!: less_add_left_Ex simp add: preal_add_assoc [symmetric])
   476 done
   477 
   478 lemma real_of_preal_leD:
   479       "real_of_preal m1 \<le> real_of_preal m2 ==> m1 \<le> m2"
   480 by (simp add: real_of_preal_def real_le preal_cancels)
   481 
   482 lemma real_of_preal_lessI: "m1 < m2 ==> real_of_preal m1 < real_of_preal m2"
   483 by (auto simp add: real_of_preal_leD linorder_not_le [symmetric])
   484 
   485 lemma real_of_preal_lessD:
   486       "real_of_preal m1 < real_of_preal m2 ==> m1 < m2"
   487 by (simp add: real_of_preal_def real_le linorder_not_le [symmetric] 
   488               preal_cancels) 
   489 
   490 
   491 lemma real_of_preal_less_iff [simp]:
   492      "(real_of_preal m1 < real_of_preal m2) = (m1 < m2)"
   493 by (blast intro: real_of_preal_lessI real_of_preal_lessD)
   494 
   495 lemma real_of_preal_le_iff:
   496      "(real_of_preal m1 \<le> real_of_preal m2) = (m1 \<le> m2)"
   497 by (simp add: linorder_not_less [symmetric]) 
   498 
   499 lemma real_of_preal_zero_less: "0 < real_of_preal m"
   500 apply (auto simp add: real_zero_def real_of_preal_def real_less_def real_le_def
   501             preal_add_ac preal_cancels)
   502 apply (simp_all add: preal_add_assoc [symmetric] preal_cancels)
   503 apply (blast intro: preal_self_less_add_left order_less_imp_le)
   504 apply (insert preal_not_eq_self [of "preal_of_rat 1" m]) 
   505 apply (simp add: preal_add_ac) 
   506 done
   507 
   508 lemma real_of_preal_minus_less_zero: "- real_of_preal m < 0"
   509 by (simp add: real_of_preal_zero_less)
   510 
   511 lemma real_of_preal_not_minus_gt_zero: "~ 0 < - real_of_preal m"
   512 proof -
   513   from real_of_preal_minus_less_zero
   514   show ?thesis by (blast dest: order_less_trans)
   515 qed
   516 
   517 
   518 subsection{*Theorems About the Ordering*}
   519 
   520 text{*obsolete but used a lot*}
   521 
   522 lemma real_not_refl2: "x < y ==> x \<noteq> (y::real)"
   523 by blast 
   524 
   525 lemma real_le_imp_less_or_eq: "!!(x::real). x \<le> y ==> x < y | x = y"
   526 by (simp add: order_le_less)
   527 
   528 lemma real_gt_zero_preal_Ex: "(0 < x) = (\<exists>y. x = real_of_preal y)"
   529 apply (auto simp add: real_of_preal_zero_less)
   530 apply (cut_tac x = x in real_of_preal_trichotomy)
   531 apply (blast elim!: real_of_preal_not_minus_gt_zero [THEN notE])
   532 done
   533 
   534 lemma real_gt_preal_preal_Ex:
   535      "real_of_preal z < x ==> \<exists>y. x = real_of_preal y"
   536 by (blast dest!: real_of_preal_zero_less [THEN order_less_trans]
   537              intro: real_gt_zero_preal_Ex [THEN iffD1])
   538 
   539 lemma real_ge_preal_preal_Ex:
   540      "real_of_preal z \<le> x ==> \<exists>y. x = real_of_preal y"
   541 by (blast dest: order_le_imp_less_or_eq real_gt_preal_preal_Ex)
   542 
   543 lemma real_less_all_preal: "y \<le> 0 ==> \<forall>x. y < real_of_preal x"
   544 by (auto elim: order_le_imp_less_or_eq [THEN disjE] 
   545             intro: real_of_preal_zero_less [THEN [2] order_less_trans] 
   546             simp add: real_of_preal_zero_less)
   547 
   548 lemma real_less_all_real2: "~ 0 < y ==> \<forall>x. y < real_of_preal x"
   549 by (blast intro!: real_less_all_preal linorder_not_less [THEN iffD1])
   550 
   551 lemma real_add_less_le_mono: "[| w'<w; z'\<le>z |] ==> w' + z' < w + (z::real)"
   552   by (rule OrderedGroup.add_less_le_mono)
   553 
   554 lemma real_add_le_less_mono:
   555      "!!z z'::real. [| w'\<le>w; z'<z |] ==> w' + z' < w + z"
   556   by (rule OrderedGroup.add_le_less_mono)
   557 
   558 lemma real_le_square [simp]: "(0::real) \<le> x*x"
   559  by (rule Ring_and_Field.zero_le_square)
   560 
   561 
   562 subsection{*More Lemmas*}
   563 
   564 lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
   565 by auto
   566 
   567 lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
   568 by auto
   569 
   570 text{*The precondition could be weakened to @{term "0\<le>x"}*}
   571 lemma real_mult_less_mono:
   572      "[| u<v;  x<y;  (0::real) < v;  0 < x |] ==> u*x < v* y"
   573  by (simp add: Ring_and_Field.mult_strict_mono order_less_imp_le)
   574 
   575 lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
   576   by (force elim: order_less_asym
   577             simp add: Ring_and_Field.mult_less_cancel_right)
   578 
   579 lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
   580 apply (simp add: mult_le_cancel_right)
   581 apply (blast intro: elim: order_less_asym) 
   582 done
   583 
   584 lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
   585 by(simp add:mult_commute)
   586 
   587 text{*Only two uses?*}
   588 lemma real_mult_less_mono':
   589      "[| x < y;  r1 < r2;  (0::real) \<le> r1;  0 \<le> x|] ==> r1 * x < r2 * y"
   590  by (rule Ring_and_Field.mult_strict_mono')
   591 
   592 text{*FIXME: delete or at least combine the next two lemmas*}
   593 lemma real_sum_squares_cancel: "x * x + y * y = 0 ==> x = (0::real)"
   594 apply (drule OrderedGroup.equals_zero_I [THEN sym])
   595 apply (cut_tac x = y in real_le_square) 
   596 apply (auto, drule order_antisym, auto)
   597 done
   598 
   599 lemma real_sum_squares_cancel2: "x * x + y * y = 0 ==> y = (0::real)"
   600 apply (rule_tac y = x in real_sum_squares_cancel)
   601 apply (simp add: add_commute)
   602 done
   603 
   604 lemma real_add_order: "[| 0 < x; 0 < y |] ==> (0::real) < x + y"
   605 by (drule add_strict_mono [of concl: 0 0], assumption, simp)
   606 
   607 lemma real_le_add_order: "[| 0 \<le> x; 0 \<le> y |] ==> (0::real) \<le> x + y"
   608 apply (drule order_le_imp_less_or_eq)+
   609 apply (auto intro: real_add_order order_less_imp_le)
   610 done
   611 
   612 lemma real_inverse_unique: "x*y = (1::real) ==> y = inverse x"
   613 apply (case_tac "x \<noteq> 0")
   614 apply (rule_tac c1 = x in real_mult_left_cancel [THEN iffD1], auto)
   615 done
   616 
   617 lemma real_inverse_gt_one: "[| (0::real) < x; x < 1 |] ==> 1 < inverse x"
   618 by (auto dest: less_imp_inverse_less)
   619 
   620 lemma real_mult_self_sum_ge_zero: "(0::real) \<le> x*x + y*y"
   621 proof -
   622   have "0 + 0 \<le> x*x + y*y" by (blast intro: add_mono zero_le_square)
   623   thus ?thesis by simp
   624 qed
   625 
   626 
   627 subsection{*Embedding the Integers into the Reals*}
   628 
   629 defs (overloaded)
   630   real_of_nat_def: "real z == of_nat z"
   631   real_of_int_def: "real z == of_int z"
   632 
   633 lemma real_eq_of_nat: "real = of_nat"
   634   apply (rule ext)
   635   apply (unfold real_of_nat_def)
   636   apply (rule refl)
   637   done
   638 
   639 lemma real_eq_of_int: "real = of_int"
   640   apply (rule ext)
   641   apply (unfold real_of_int_def)
   642   apply (rule refl)
   643   done
   644 
   645 lemma real_of_int_zero [simp]: "real (0::int) = 0"  
   646 by (simp add: real_of_int_def) 
   647 
   648 lemma real_of_one [simp]: "real (1::int) = (1::real)"
   649 by (simp add: real_of_int_def) 
   650 
   651 lemma real_of_int_add [simp]: "real(x + y) = real (x::int) + real y"
   652 by (simp add: real_of_int_def) 
   653 
   654 lemma real_of_int_minus [simp]: "real(-x) = -real (x::int)"
   655 by (simp add: real_of_int_def) 
   656 
   657 lemma real_of_int_diff [simp]: "real(x - y) = real (x::int) - real y"
   658 by (simp add: real_of_int_def) 
   659 
   660 lemma real_of_int_mult [simp]: "real(x * y) = real (x::int) * real y"
   661 by (simp add: real_of_int_def) 
   662 
   663 lemma real_of_int_setsum [simp]: "real ((SUM x:A. f x)::int) = (SUM x:A. real(f x))"
   664   apply (subst real_eq_of_int)+
   665   apply (rule of_int_setsum)
   666 done
   667 
   668 lemma real_of_int_setprod [simp]: "real ((PROD x:A. f x)::int) = 
   669     (PROD x:A. real(f x))"
   670   apply (subst real_eq_of_int)+
   671   apply (rule of_int_setprod)
   672 done
   673 
   674 lemma real_of_int_zero_cancel [simp]: "(real x = 0) = (x = (0::int))"
   675 by (simp add: real_of_int_def) 
   676 
   677 lemma real_of_int_inject [iff]: "(real (x::int) = real y) = (x = y)"
   678 by (simp add: real_of_int_def) 
   679 
   680 lemma real_of_int_less_iff [iff]: "(real (x::int) < real y) = (x < y)"
   681 by (simp add: real_of_int_def) 
   682 
   683 lemma real_of_int_le_iff [simp]: "(real (x::int) \<le> real y) = (x \<le> y)"
   684 by (simp add: real_of_int_def) 
   685 
   686 lemma real_of_int_gt_zero_cancel_iff [simp]: "(0 < real (n::int)) = (0 < n)"
   687 by (simp add: real_of_int_def) 
   688 
   689 lemma real_of_int_ge_zero_cancel_iff [simp]: "(0 <= real (n::int)) = (0 <= n)"
   690 by (simp add: real_of_int_def) 
   691 
   692 lemma real_of_int_lt_zero_cancel_iff [simp]: "(real (n::int) < 0) = (n < 0)"
   693 by (simp add: real_of_int_def)
   694 
   695 lemma real_of_int_le_zero_cancel_iff [simp]: "(real (n::int) <= 0) = (n <= 0)"
   696 by (simp add: real_of_int_def)
   697 
   698 lemma real_of_int_abs [simp]: "real (abs x) = abs(real (x::int))"
   699 by (auto simp add: abs_if)
   700 
   701 lemma int_less_real_le: "((n::int) < m) = (real n + 1 <= real m)"
   702   apply (subgoal_tac "real n + 1 = real (n + 1)")
   703   apply (simp del: real_of_int_add)
   704   apply auto
   705 done
   706 
   707 lemma int_le_real_less: "((n::int) <= m) = (real n < real m + 1)"
   708   apply (subgoal_tac "real m + 1 = real (m + 1)")
   709   apply (simp del: real_of_int_add)
   710   apply simp
   711 done
   712 
   713 lemma real_of_int_div_aux: "d ~= 0 ==> (real (x::int)) / (real d) = 
   714     real (x div d) + (real (x mod d)) / (real d)"
   715 proof -
   716   assume "d ~= 0"
   717   have "x = (x div d) * d + x mod d"
   718     by auto
   719   then have "real x = real (x div d) * real d + real(x mod d)"
   720     by (simp only: real_of_int_mult [THEN sym] real_of_int_add [THEN sym])
   721   then have "real x / real d = ... / real d"
   722     by simp
   723   then show ?thesis
   724     by (auto simp add: add_divide_distrib ring_eq_simps prems)
   725 qed
   726 
   727 lemma real_of_int_div: "(d::int) ~= 0 ==> d dvd n ==>
   728     real(n div d) = real n / real d"
   729   apply (frule real_of_int_div_aux [of d n])
   730   apply simp
   731   apply (simp add: zdvd_iff_zmod_eq_0)
   732 done
   733 
   734 lemma real_of_int_div2:
   735   "0 <= real (n::int) / real (x) - real (n div x)"
   736   apply (case_tac "x = 0")
   737   apply simp
   738   apply (case_tac "0 < x")
   739   apply (simp add: compare_rls)
   740   apply (subst real_of_int_div_aux)
   741   apply simp
   742   apply simp
   743   apply (subst zero_le_divide_iff)
   744   apply auto
   745   apply (simp add: compare_rls)
   746   apply (subst real_of_int_div_aux)
   747   apply simp
   748   apply simp
   749   apply (subst zero_le_divide_iff)
   750   apply auto
   751 done
   752 
   753 lemma real_of_int_div3:
   754   "real (n::int) / real (x) - real (n div x) <= 1"
   755   apply(case_tac "x = 0")
   756   apply simp
   757   apply (simp add: compare_rls)
   758   apply (subst real_of_int_div_aux)
   759   apply assumption
   760   apply simp
   761   apply (subst divide_le_eq)
   762   apply clarsimp
   763   apply (rule conjI)
   764   apply (rule impI)
   765   apply (rule order_less_imp_le)
   766   apply simp
   767   apply (rule impI)
   768   apply (rule order_less_imp_le)
   769   apply simp
   770 done
   771 
   772 lemma real_of_int_div4: "real (n div x) <= real (n::int) / real x" 
   773   by (insert real_of_int_div2 [of n x], simp)
   774 
   775 subsection{*Embedding the Naturals into the Reals*}
   776 
   777 lemma real_of_nat_zero [simp]: "real (0::nat) = 0"
   778 by (simp add: real_of_nat_def)
   779 
   780 lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"
   781 by (simp add: real_of_nat_def)
   782 
   783 lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"
   784 by (simp add: real_of_nat_def)
   785 
   786 (*Not for addsimps: often the LHS is used to represent a positive natural*)
   787 lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"
   788 by (simp add: real_of_nat_def)
   789 
   790 lemma real_of_nat_less_iff [iff]: 
   791      "(real (n::nat) < real m) = (n < m)"
   792 by (simp add: real_of_nat_def)
   793 
   794 lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"
   795 by (simp add: real_of_nat_def)
   796 
   797 lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"
   798 by (simp add: real_of_nat_def zero_le_imp_of_nat)
   799 
   800 lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
   801 by (simp add: real_of_nat_def del: of_nat_Suc)
   802 
   803 lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"
   804 by (simp add: real_of_nat_def)
   805 
   806 lemma real_of_nat_setsum [simp]: "real ((SUM x:A. f x)::nat) = 
   807     (SUM x:A. real(f x))"
   808   apply (subst real_eq_of_nat)+
   809   apply (rule of_nat_setsum)
   810 done
   811 
   812 lemma real_of_nat_setprod [simp]: "real ((PROD x:A. f x)::nat) = 
   813     (PROD x:A. real(f x))"
   814   apply (subst real_eq_of_nat)+
   815   apply (rule of_nat_setprod)
   816 done
   817 
   818 lemma real_of_card: "real (card A) = setsum (%x.1) A"
   819   apply (subst card_eq_setsum)
   820   apply (subst real_of_nat_setsum)
   821   apply simp
   822 done
   823 
   824 lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"
   825 by (simp add: real_of_nat_def)
   826 
   827 lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"
   828 by (simp add: real_of_nat_def)
   829 
   830 lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"
   831 by (simp add: add: real_of_nat_def) 
   832 
   833 lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"
   834 by (simp add: add: real_of_nat_def) 
   835 
   836 lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"
   837 by (simp add: add: real_of_nat_def)
   838 
   839 lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"
   840 by (simp add: add: real_of_nat_def)
   841 
   842 lemma real_of_nat_ge_zero_cancel_iff [simp]: "(0 \<le> real (n::nat)) = (0 \<le> n)"
   843 by (simp add: add: real_of_nat_def)
   844 
   845 lemma nat_less_real_le: "((n::nat) < m) = (real n + 1 <= real m)"
   846   apply (subgoal_tac "real n + 1 = real (Suc n)")
   847   apply simp
   848   apply (auto simp add: real_of_nat_Suc)
   849 done
   850 
   851 lemma nat_le_real_less: "((n::nat) <= m) = (real n < real m + 1)"
   852   apply (subgoal_tac "real m + 1 = real (Suc m)")
   853   apply (simp add: less_Suc_eq_le)
   854   apply (simp add: real_of_nat_Suc)
   855 done
   856 
   857 lemma real_of_nat_div_aux: "0 < d ==> (real (x::nat)) / (real d) = 
   858     real (x div d) + (real (x mod d)) / (real d)"
   859 proof -
   860   assume "0 < d"
   861   have "x = (x div d) * d + x mod d"
   862     by auto
   863   then have "real x = real (x div d) * real d + real(x mod d)"
   864     by (simp only: real_of_nat_mult [THEN sym] real_of_nat_add [THEN sym])
   865   then have "real x / real d = \<dots> / real d"
   866     by simp
   867   then show ?thesis
   868     by (auto simp add: add_divide_distrib ring_eq_simps prems)
   869 qed
   870 
   871 lemma real_of_nat_div: "0 < (d::nat) ==> d dvd n ==>
   872     real(n div d) = real n / real d"
   873   apply (frule real_of_nat_div_aux [of d n])
   874   apply simp
   875   apply (subst dvd_eq_mod_eq_0 [THEN sym])
   876   apply assumption
   877 done
   878 
   879 lemma real_of_nat_div2:
   880   "0 <= real (n::nat) / real (x) - real (n div x)"
   881   apply(case_tac "x = 0")
   882   apply simp
   883   apply (simp add: compare_rls)
   884   apply (subst real_of_nat_div_aux)
   885   apply assumption
   886   apply simp
   887   apply (subst zero_le_divide_iff)
   888   apply simp
   889 done
   890 
   891 lemma real_of_nat_div3:
   892   "real (n::nat) / real (x) - real (n div x) <= 1"
   893   apply(case_tac "x = 0")
   894   apply simp
   895   apply (simp add: compare_rls)
   896   apply (subst real_of_nat_div_aux)
   897   apply assumption
   898   apply simp
   899 done
   900 
   901 lemma real_of_nat_div4: "real (n div x) <= real (n::nat) / real x" 
   902   by (insert real_of_nat_div2 [of n x], simp)
   903 
   904 lemma real_of_int_real_of_nat: "real (int n) = real n"
   905 by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)
   906 
   907 lemma real_of_int_of_nat_eq [simp]: "real (of_nat n :: int) = real n"
   908 by (simp add: real_of_int_def real_of_nat_def)
   909 
   910 lemma real_nat_eq_real [simp]: "0 <= x ==> real(nat x) = real x"
   911   apply (subgoal_tac "real(int(nat x)) = real(nat x)")
   912   apply force
   913   apply (simp only: real_of_int_real_of_nat)
   914 done
   915 
   916 subsection{*Numerals and Arithmetic*}
   917 
   918 instance real :: number ..
   919 
   920 defs (overloaded)
   921   real_number_of_def: "(number_of w :: real) == of_int w"
   922     --{*the type constraint is essential!*}
   923 
   924 instance real :: number_ring
   925 by (intro_classes, simp add: real_number_of_def) 
   926 
   927 text{*Collapse applications of @{term real} to @{term number_of}*}
   928 lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"
   929 by (simp add:  real_of_int_def of_int_number_of_eq)
   930 
   931 lemma real_of_nat_number_of [simp]:
   932      "real (number_of v :: nat) =  
   933         (if neg (number_of v :: int) then 0  
   934          else (number_of v :: real))"
   935 by (simp add: real_of_int_real_of_nat [symmetric] int_nat_number_of)
   936  
   937 
   938 use "real_arith.ML"
   939 
   940 setup real_arith_setup
   941 
   942 
   943 lemma real_diff_mult_distrib:
   944   fixes a::real
   945   shows "a * (b - c) = a * b - a * c" 
   946 proof -
   947   have "a * (b - c) = a * (b + -c)" by simp
   948   also have "\<dots> = (b + -c) * a" by simp
   949   also have "\<dots> = b*a + (-c)*a" by (rule real_add_mult_distrib)
   950   also have "\<dots> = a*b - a*c" by simp
   951   finally show ?thesis .
   952 qed
   953 
   954 
   955 subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}
   956 
   957 text{*Needed in this non-standard form by Hyperreal/Transcendental*}
   958 lemma real_0_le_divide_iff:
   959      "((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
   960 by (simp add: real_divide_def zero_le_mult_iff, auto)
   961 
   962 lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)" 
   963 by arith
   964 
   965 lemma real_add_eq_0_iff: "(x+y = (0::real)) = (y = -x)"
   966 by auto
   967 
   968 lemma real_add_less_0_iff: "(x+y < (0::real)) = (y < -x)"
   969 by auto
   970 
   971 lemma real_0_less_add_iff: "((0::real) < x+y) = (-x < y)"
   972 by auto
   973 
   974 lemma real_add_le_0_iff: "(x+y \<le> (0::real)) = (y \<le> -x)"
   975 by auto
   976 
   977 lemma real_0_le_add_iff: "((0::real) \<le> x+y) = (-x \<le> y)"
   978 by auto
   979 
   980 
   981 (*
   982 FIXME: we should have this, as for type int, but many proofs would break.
   983 It replaces x+-y by x-y.
   984 declare real_diff_def [symmetric, simp]
   985 *)
   986 
   987 
   988 subsubsection{*Density of the Reals*}
   989 
   990 lemma real_lbound_gt_zero:
   991      "[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
   992 apply (rule_tac x = " (min d1 d2) /2" in exI)
   993 apply (simp add: min_def)
   994 done
   995 
   996 
   997 text{*Similar results are proved in @{text Ring_and_Field}*}
   998 lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
   999   by auto
  1000 
  1001 lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
  1002   by auto
  1003 
  1004 
  1005 subsection{*Absolute Value Function for the Reals*}
  1006 
  1007 lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
  1008 by (simp add: abs_if)
  1009 
  1010 lemma abs_interval_iff: "(abs x < r) = (-r < x & x < (r::real))"
  1011 by (force simp add: Ring_and_Field.abs_less_iff)
  1012 
  1013 lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"
  1014 by (force simp add: OrderedGroup.abs_le_iff)
  1015 
  1016 lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
  1017 by (simp add: abs_if)
  1018 
  1019 lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
  1020 by (simp add: real_of_nat_ge_zero)
  1021 
  1022 lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
  1023 by simp
  1024  
  1025 lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
  1026 by simp
  1027 
  1028 end