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