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