src/HOL/Real/RealDef.thy
 author ballarin Thu, 19 Feb 2004 15:57:34 +0100 changeset 14398 c5c47703f763 parent 14387 e96d5c42c4b0 child 14421 ee97b6463cb4 permissions -rw-r--r--
Efficient, graph-based reasoner for linear and partial orders. + Setup as solver in the HOL simplifier.
```
(*  Title       : Real/RealDef.thy
ID          : \$Id\$
Author      : Jacques D. Fleuriot
Copyright   : 1998  University of Cambridge
Conversion to Isar and new proofs by Lawrence C Paulson, 2003/4
*)

header{*Defining the Reals from the Positive Reals*}

theory RealDef = PReal
files ("real_arith.ML"):

constdefs
realrel   ::  "((preal * preal) * (preal * preal)) set"
"realrel == {p. \<exists>x1 y1 x2 y2. p = ((x1,y1),(x2,y2)) & x1+y2 = x2+y1}"

typedef (REAL)  real = "UNIV//realrel"

instance real :: ord ..
instance real :: zero ..
instance real :: one ..
instance real :: plus ..
instance real :: times ..
instance real :: minus ..
instance real :: inverse ..

consts
(*Overloaded constant denoting the Real subset of enclosing
types such as hypreal and complex*)
Reals :: "'a set"

(*overloaded constant for injecting other types into "real"*)
real :: "'a => real"

real_zero_def:
"0 == Abs_REAL(realrel``{(preal_of_rat 1, preal_of_rat 1)})"

real_one_def:
"1 == Abs_REAL(realrel``
{(preal_of_rat 1 + preal_of_rat 1,
preal_of_rat 1)})"

real_minus_def:
"- R ==  Abs_REAL(UN (x,y):Rep_REAL(R). realrel``{(y,x)})"

real_diff_def:
"R - (S::real) == R + - S"

real_inverse_def:
"inverse (R::real) == (SOME S. (R = 0 & S = 0) | S * R = 1)"

real_divide_def:
"R / (S::real) == R * inverse S"

constdefs

(** these don't use the overloaded "real" function: users don't see them **)

real_of_preal :: "preal => real"
"real_of_preal m     ==
Abs_REAL(realrel``{(m + preal_of_rat 1, preal_of_rat 1)})"

"P+Q == Abs_REAL(\<Union>p1\<in>Rep_REAL(P). \<Union>p2\<in>Rep_REAL(Q).
(%(x1,y1). (%(x2,y2). realrel``{(x1+x2, y1+y2)}) p2) p1)"

real_mult_def:
"P*Q == Abs_REAL(\<Union>p1\<in>Rep_REAL(P). \<Union>p2\<in>Rep_REAL(Q).
(%(x1,y1). (%(x2,y2). realrel``{(x1*x2+y1*y2,x1*y2+x2*y1)})
p2) p1)"

real_less_def: "(x < (y::real)) == (x \<le> y & x \<noteq> y)"

real_le_def:
"P \<le> (Q::real) == \<exists>x1 y1 x2 y2. x1 + y2 \<le> x2 + y1 &
(x1,y1) \<in> Rep_REAL(P) & (x2,y2) \<in> Rep_REAL(Q)"

real_abs_def:  "abs (r::real) == (if 0 \<le> r then r else -r)"

syntax (xsymbols)
Reals     :: "'a set"                   ("\<real>")

subsection{*Proving that realrel is an equivalence relation*}

lemma preal_trans_lemma:
assumes "x + y1 = x1 + y"
and "x + y2 = x2 + y"
shows "x1 + y2 = x2 + (y1::preal)"
proof -
have "(x1 + y2) + x = (x + y2) + x1" by (simp add: preal_add_ac)
also have "... = (x2 + y) + x1"  by (simp add: prems)
also have "... = x2 + (x1 + y)"  by (simp add: preal_add_ac)
also have "... = x2 + (x + y1)"  by (simp add: prems)
also have "... = (x2 + y1) + x"  by (simp add: preal_add_ac)
finally have "(x1 + y2) + x = (x2 + y1) + x" .
qed

lemma realrel_iff [simp]: "(((x1,y1),(x2,y2)): realrel) = (x1 + y2 = x2 + y1)"
by (unfold realrel_def, blast)

lemma realrel_refl: "(x,x): realrel"
apply (case_tac "x")
done

lemma equiv_realrel: "equiv UNIV realrel"
apply (auto simp add: equiv_def refl_def sym_def trans_def realrel_def)
apply (blast dest: preal_trans_lemma)
done

(* (realrel `` {x} = realrel `` {y}) = ((x,y) : realrel) *)
lemmas equiv_realrel_iff =
eq_equiv_class_iff [OF equiv_realrel UNIV_I UNIV_I]

declare equiv_realrel_iff [simp]

lemma realrel_in_real [simp]: "realrel``{(x,y)}: REAL"
by (unfold REAL_def realrel_def quotient_def, blast)

lemma inj_on_Abs_REAL: "inj_on Abs_REAL REAL"
apply (rule inj_on_inverseI)
apply (erule Abs_REAL_inverse)
done

declare inj_on_Abs_REAL [THEN inj_on_iff, simp]
declare Abs_REAL_inverse [simp]

lemmas eq_realrelD = equiv_realrel [THEN [2] eq_equiv_class]

lemma inj_Rep_REAL: "inj Rep_REAL"
apply (rule inj_on_inverseI)
apply (rule Rep_REAL_inverse)
done

(** real_of_preal: the injection from preal to real **)
lemma inj_real_of_preal: "inj(real_of_preal)"
apply (rule inj_onI)
apply (unfold real_of_preal_def)
apply (drule inj_on_Abs_REAL [THEN inj_onD])
apply (rule realrel_in_real)+
apply (drule eq_equiv_class)
apply (rule equiv_realrel, blast)
done

lemma eq_Abs_REAL:
"(!!x y. z = Abs_REAL(realrel``{(x,y)}) ==> P) ==> P"
apply (rule_tac x1 = z in Rep_REAL [unfolded REAL_def, THEN quotientE])
apply (drule_tac f = Abs_REAL in arg_cong)
apply (case_tac "x")
done

"[|a + ba = aa + b; ab + bc = ac + bb|]
==> a + ab + (ba + bc) = aa + ac + (b + (bb::preal))"
apply (rule preal_add_left_commute [of ab, THEN ssubst])
done

"Abs_REAL(realrel``{(x1,y1)}) + Abs_REAL(realrel``{(x2,y2)}) =
Abs_REAL(realrel``{(x1+x2, y1+y2)})"
apply (subst equiv_realrel [THEN UN_equiv_class2])
done

lemma real_add_commute: "(z::real) + w = w + z"
apply (rule eq_Abs_REAL [of z])
apply (rule eq_Abs_REAL [of w])
done

lemma real_add_assoc: "((z1::real) + z2) + z3 = z1 + (z2 + z3)"
apply (rule eq_Abs_REAL [of z1])
apply (rule eq_Abs_REAL [of z2])
apply (rule eq_Abs_REAL [of z3])
done

lemma real_add_zero_left: "(0::real) + z = z"
apply (unfold real_of_preal_def real_zero_def)
apply (rule eq_Abs_REAL [of z])
done

lemma real_add_zero_right: "z + (0::real) = z"

instance real :: plus_ac0
by (intro_classes,
(assumption |

lemma real_minus_congruent:
"congruent realrel (%p. (%(x,y). realrel``{(y,x)}) p)"
apply (unfold congruent_def, clarify)
done

lemma real_minus:
"- (Abs_REAL(realrel``{(x,y)})) = Abs_REAL(realrel `` {(y,x)})"
apply (unfold real_minus_def)
apply (rule_tac f = Abs_REAL in arg_cong)
apply (simp add: realrel_in_real [THEN Abs_REAL_inverse]
UN_equiv_class [OF equiv_realrel real_minus_congruent])
done

lemma real_add_minus_left: "(-z) + z = (0::real)"
apply (unfold real_zero_def)
apply (rule eq_Abs_REAL [of z])
done

subsection{*Congruence property for multiplication*}

lemma real_mult_congruent2_lemma:
"!!(x1::preal). [| x1 + y2 = x2 + y1 |] ==>
x * x1 + y * y1 + (x * y2 + x2 * y) =
x * x2 + y * y2 + (x * y1 + x1 * y)"
apply (rule preal_mult_commute [THEN subst])
apply (rule_tac y1 = x2 in preal_mult_commute [THEN subst])
done

lemma real_mult_congruent2:
"congruent2 realrel (%p1 p2.
(%(x1,y1). (%(x2,y2). realrel``{(x1*x2 + y1*y2, x1*y2+x2*y1)}) p2) p1)"
apply (rule equiv_realrel [THEN congruent2_commuteI], clarify)
apply (unfold split_def)
done

lemma real_mult:
"Abs_REAL((realrel``{(x1,y1)})) * Abs_REAL((realrel``{(x2,y2)})) =
Abs_REAL(realrel `` {(x1*x2+y1*y2,x1*y2+x2*y1)})"
apply (unfold real_mult_def)
apply (simp add: equiv_realrel [THEN UN_equiv_class2] real_mult_congruent2)
done

lemma real_mult_commute: "(z::real) * w = w * z"
apply (rule eq_Abs_REAL [of z])
apply (rule eq_Abs_REAL [of w])
done

lemma real_mult_assoc: "((z1::real) * z2) * z3 = z1 * (z2 * z3)"
apply (rule eq_Abs_REAL [of z1])
apply (rule eq_Abs_REAL [of z2])
apply (rule eq_Abs_REAL [of z3])
done

lemma real_mult_1: "(1::real) * z = z"
apply (unfold real_one_def)
apply (rule eq_Abs_REAL [of z])
done

lemma real_add_mult_distrib: "((z1::real) + z2) * w = (z1 * w) + (z2 * w)"
apply (rule eq_Abs_REAL [of z1])
apply (rule eq_Abs_REAL [of z2])
apply (rule eq_Abs_REAL [of w])
done

text{*one and zero are distinct*}
lemma real_zero_not_eq_one: "0 \<noteq> (1::real)"
apply (subgoal_tac "preal_of_rat 1 < preal_of_rat 1 + preal_of_rat 1")
apply (unfold real_zero_def real_one_def)
done

subsection{*existence of inverse*}

lemma real_zero_iff: "Abs_REAL (realrel `` {(x, x)}) = 0"
apply (unfold real_zero_def)
done

text{*Instead of using an existential quantifier and constructing the inverse
within the proof, we could define the inverse explicitly.*}

lemma real_mult_inverse_left_ex: "x \<noteq> 0 ==> \<exists>y. y*x = (1::real)"
apply (unfold real_zero_def real_one_def)
apply (rule eq_Abs_REAL [of x])
apply (cut_tac x = xa and y = y in linorder_less_linear)
apply (rule_tac
x = "Abs_REAL (realrel `` { (preal_of_rat 1,
inverse (D) + preal_of_rat 1)}) "
in exI)
apply (rule_tac [2]
x = "Abs_REAL (realrel `` { (inverse (D) + preal_of_rat 1,
preal_of_rat 1)})"
in exI)
apply (auto simp add: real_mult preal_mult_1_right
done

lemma real_mult_inverse_left: "x \<noteq> 0 ==> inverse(x)*x = (1::real)"
apply (unfold real_inverse_def)
apply (frule real_mult_inverse_left_ex, safe)
apply (rule someI2, auto)
done

subsection{*The Real Numbers form a Field*}

instance real :: field
proof
fix x y z :: real
show "(x + y) + z = x + (y + z)" by (rule real_add_assoc)
show "x + y = y + x" by (rule real_add_commute)
show "0 + x = x" by simp
show "- x + x = 0" by (rule real_add_minus_left)
show "x - y = x + (-y)" by (simp add: real_diff_def)
show "(x * y) * z = x * (y * z)" by (rule real_mult_assoc)
show "x * y = y * x" by (rule real_mult_commute)
show "1 * x = x" by (rule real_mult_1)
show "(x + y) * z = x * z + y * z" by (simp add: real_add_mult_distrib)
show "0 \<noteq> (1::real)" by (rule real_zero_not_eq_one)
show "x \<noteq> 0 ==> inverse x * x = 1" by (rule real_mult_inverse_left)
show "y \<noteq> 0 ==> x / y = x * inverse y" by (simp add: real_divide_def)
assume eq: "z+x = z+y"
hence "(-z + z) + x = (-z + z) + y" by (simp only: eq real_add_assoc)
qed

text{*Inverse of zero!  Useful to simplify certain equations*}

lemma INVERSE_ZERO: "inverse 0 = (0::real)"
apply (unfold real_inverse_def)
apply (rule someI2)
done

lemma DIVISION_BY_ZERO: "a / (0::real) = 0"

instance real :: division_by_zero
proof
fix x :: real
show "inverse 0 = (0::real)" by (rule INVERSE_ZERO)
show "x/0 = 0" by (rule DIVISION_BY_ZERO)
qed

(*Pull negations out*)
declare minus_mult_right [symmetric, simp]
minus_mult_left [symmetric, simp]

lemma real_mult_1_right: "z * (1::real) = z"
by (rule Ring_and_Field.mult_1_right)

subsection{*The @{text "\<le>"} Ordering*}

lemma real_le_refl: "w \<le> (w::real)"
apply (rule eq_Abs_REAL [of w])
done

text{*The arithmetic decision procedure is not set up for type preal.
This lemma is currently unused, but it could simplify the proofs of the
following two lemmas.*}
lemma preal_eq_le_imp_le:
assumes eq: "a+b = c+d" and le: "c \<le> a"
shows "b \<le> (d::preal)"
proof -
have "c+d \<le> a+d" by (simp add: prems preal_cancels)
hence "a+b \<le> a+d" by (simp add: prems)
thus "b \<le> d" by (simp add: preal_cancels)
qed

lemma real_le_lemma:
assumes l: "u1 + v2 \<le> u2 + v1"
and "x1 + v1 = u1 + y1"
and "x2 + v2 = u2 + y2"
shows "x1 + y2 \<le> x2 + (y1::preal)"
proof -
have "(x1+v1) + (u2+y2) = (u1+y1) + (x2+v2)" by (simp add: prems)
hence "(x1+y2) + (u2+v1) = (x2+y1) + (u1+v2)" by (simp add: preal_add_ac)
also have "... \<le> (x2+y1) + (u2+v1)"
qed

lemma real_le:
"(Abs_REAL(realrel``{(x1,y1)}) \<le> Abs_REAL(realrel``{(x2,y2)})) =
(x1 + y2 \<le> x2 + y1)"
apply (auto intro: real_le_lemma)
done

lemma real_le_anti_sym: "[| z \<le> w; w \<le> z |] ==> z = (w::real)"
apply (rule eq_Abs_REAL [of z])
apply (rule eq_Abs_REAL [of w])
done

lemma real_trans_lemma:
assumes "x + v \<le> u + y"
and "u + v' \<le> u' + v"
and "x2 + v2 = u2 + y2"
shows "x + v' \<le> u' + (y::preal)"
proof -
have "(x+v') + (u+v) = (x+v) + (u+v')" by (simp add: preal_add_ac)
also have "... \<le> (u+y) + (u+v')"
also have "... \<le> (u+y) + (u'+v)"
qed

lemma real_le_trans: "[| i \<le> j; j \<le> k |] ==> i \<le> (k::real)"
apply (rule eq_Abs_REAL [of i])
apply (rule eq_Abs_REAL [of j])
apply (rule eq_Abs_REAL [of k])
apply (blast intro: real_trans_lemma)
done

(* Axiom 'order_less_le' of class 'order': *)
lemma real_less_le: "((w::real) < z) = (w \<le> z & w \<noteq> z)"

instance real :: order
proof qed
(assumption |
rule real_le_refl real_le_trans real_le_anti_sym real_less_le)+

(* Axiom 'linorder_linear' of class 'linorder': *)
lemma real_le_linear: "(z::real) \<le> w | w \<le> z"
apply (rule eq_Abs_REAL [of z])
apply (rule eq_Abs_REAL [of w])
done

instance real :: linorder
by (intro_classes, rule real_le_linear)

lemma real_le_eq_diff: "(x \<le> y) = (x-y \<le> (0::real))"
apply (rule eq_Abs_REAL [of x])
apply (rule eq_Abs_REAL [of y])
done

lemma real_add_left_mono: "x \<le> y ==> z + x \<le> z + (y::real)"
apply (auto simp add: real_le_eq_diff [of x] real_le_eq_diff [of "z+x"])
apply (subgoal_tac "z + x - (z + y) = (z + -z) + (x - y)")
done

lemma real_sum_gt_zero_less: "(0 < S + (-W::real)) ==> (W < S)"
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)

lemma real_less_sum_gt_zero: "(W < S) ==> (0 < S + (-W::real))"
by (simp add: linorder_not_le [symmetric] real_le_eq_diff [of S] diff_minus)

lemma real_mult_order: "[| 0 < x; 0 < y |] ==> (0::real) < x * y"
apply (rule eq_Abs_REAL [of x])
apply (rule eq_Abs_REAL [of y])
apply (simp add: linorder_not_le [where 'a = real, symmetric]
linorder_not_le [where 'a = preal]
real_zero_def real_le real_mult)
--{*Reduce to the (simpler) @{text "\<le>"} relation *}
done

lemma real_mult_less_mono2: "[| (0::real) < z; x < y |] ==> z * x < z * y"
apply (rule real_sum_gt_zero_less)
apply (drule real_less_sum_gt_zero [of x y])
apply (drule real_mult_order, assumption)
done

text{*lemma for proving @{term "0<(1::real)"}*}
lemma real_zero_le_one: "0 \<le> (1::real)"
by (simp add: real_zero_def real_one_def real_le

subsection{*The Reals Form an Ordered Field*}

instance real :: ordered_field
proof
fix x y z :: real
show "0 < (1::real)"
by (simp add: real_less_def real_zero_le_one real_zero_not_eq_one)
show "x \<le> y ==> z + x \<le> z + y" by (rule real_add_left_mono)
show "x < y ==> 0 < z ==> z * x < z * y" by (simp add: real_mult_less_mono2)
show "\<bar>x\<bar> = (if x < 0 then -x else x)"
by (auto dest: order_le_less_trans simp add: real_abs_def linorder_not_le)
qed

text{*The function @{term real_of_preal} requires many proofs, but it seems
to be essential for proving completeness of the reals from that of the
positive reals.*}

"real_of_preal ((x::preal) + y) = real_of_preal x + real_of_preal y"

lemma real_of_preal_mult:
"real_of_preal ((x::preal) * y) = real_of_preal x* real_of_preal y"

text{*Gleason prop 9-4.4 p 127*}
lemma real_of_preal_trichotomy:
"\<exists>m. (x::real) = real_of_preal m | x = 0 | x = -(real_of_preal m)"
apply (unfold real_of_preal_def real_zero_def)
apply (rule eq_Abs_REAL [of x])
apply (cut_tac x = x and y = y in linorder_less_linear)
done

lemma real_of_preal_leD:
"real_of_preal m1 \<le> real_of_preal m2 ==> m1 \<le> m2"
apply (unfold real_of_preal_def)
done

lemma real_of_preal_lessI: "m1 < m2 ==> real_of_preal m1 < real_of_preal m2"
by (auto simp add: real_of_preal_leD linorder_not_le [symmetric])

lemma real_of_preal_lessD:
"real_of_preal m1 < real_of_preal m2 ==> m1 < m2"
apply (drule real_of_preal_leD)
done

lemma real_of_preal_less_iff [simp]:
"(real_of_preal m1 < real_of_preal m2) = (m1 < m2)"
by (blast intro: real_of_preal_lessI real_of_preal_lessD)

lemma real_of_preal_le_iff:
"(real_of_preal m1 \<le> real_of_preal m2) = (m1 \<le> m2)"

lemma real_of_preal_zero_less: "0 < real_of_preal m"
apply (auto simp add: real_zero_def real_of_preal_def real_less_def real_le_def
apply (insert preal_not_eq_self [of "preal_of_rat 1" m])
done

lemma real_of_preal_minus_less_zero: "- real_of_preal m < 0"

lemma real_of_preal_not_minus_gt_zero: "~ 0 < - real_of_preal m"
apply (cut_tac real_of_preal_minus_less_zero)
apply (fast dest: order_less_trans)
done

text{*obsolete but used a lot*}

lemma real_not_refl2: "x < y ==> x \<noteq> (y::real)"
by blast

lemma real_le_imp_less_or_eq: "!!(x::real). x \<le> y ==> x < y | x = y"

lemma real_gt_zero_preal_Ex: "(0 < x) = (\<exists>y. x = real_of_preal y)"
apply (cut_tac x = x in real_of_preal_trichotomy)
apply (blast elim!: real_of_preal_not_minus_gt_zero [THEN notE])
done

lemma real_gt_preal_preal_Ex:
"real_of_preal z < x ==> \<exists>y. x = real_of_preal y"
by (blast dest!: real_of_preal_zero_less [THEN order_less_trans]
intro: real_gt_zero_preal_Ex [THEN iffD1])

lemma real_ge_preal_preal_Ex:
"real_of_preal z \<le> x ==> \<exists>y. x = real_of_preal y"
by (blast dest: order_le_imp_less_or_eq real_gt_preal_preal_Ex)

lemma real_less_all_preal: "y \<le> 0 ==> \<forall>x. y < real_of_preal x"
by (auto elim: order_le_imp_less_or_eq [THEN disjE]
intro: real_of_preal_zero_less [THEN [2] order_less_trans]

lemma real_less_all_real2: "~ 0 < y ==> \<forall>x. y < real_of_preal x"
by (blast intro!: real_less_all_preal linorder_not_less [THEN iffD1])

lemma real_add_less_le_mono: "[| w'<w; z'\<le>z |] ==> w' + z' < w + (z::real)"

"!!z z'::real. [| w'\<le>w; z'<z |] ==> w' + z' < w + z"

lemma real_zero_less_one: "0 < (1::real)"
by (rule Ring_and_Field.zero_less_one)

lemma real_le_square [simp]: "(0::real) \<le> x*x"
by (rule Ring_and_Field.zero_le_square)

subsection{*More Lemmas*}

lemma real_mult_left_cancel: "(c::real) \<noteq> 0 ==> (c*a=c*b) = (a=b)"
by auto

lemma real_mult_right_cancel: "(c::real) \<noteq> 0 ==> (a*c=b*c) = (a=b)"
by auto

text{*The precondition could be weakened to @{term "0\<le>x"}*}
lemma real_mult_less_mono:
"[| u<v;  x<y;  (0::real) < v;  0 < x |] ==> u*x < v* y"

lemma real_mult_less_iff1 [simp]: "(0::real) < z ==> (x*z < y*z) = (x < y)"
by (force elim: order_less_asym

lemma real_mult_le_cancel_iff1 [simp]: "(0::real) < z ==> (x*z \<le> y*z) = (x\<le>y)"
apply (blast intro: elim: order_less_asym)
done

lemma real_mult_le_cancel_iff2 [simp]: "(0::real) < z ==> (z*x \<le> z*y) = (x\<le>y)"
by (force elim: order_less_asym

text{*Only two uses?*}
lemma real_mult_less_mono':
"[| x < y;  r1 < r2;  (0::real) \<le> r1;  0 \<le> x|] ==> r1 * x < r2 * y"
by (rule Ring_and_Field.mult_strict_mono')

text{*FIXME: delete or at least combine the next two lemmas*}
lemma real_sum_squares_cancel: "x * x + y * y = 0 ==> x = (0::real)"
apply (drule Ring_and_Field.equals_zero_I [THEN sym])
apply (cut_tac x = y in real_le_square)
apply (auto, drule real_le_anti_sym, auto)
done

lemma real_sum_squares_cancel2: "x * x + y * y = 0 ==> y = (0::real)"
apply (rule_tac y = x in real_sum_squares_cancel)
done

lemma real_add_order: "[| 0 < x; 0 < y |] ==> (0::real) < x + y"
by (drule add_strict_mono [of concl: 0 0], assumption, simp)

lemma real_le_add_order: "[| 0 \<le> x; 0 \<le> y |] ==> (0::real) \<le> x + y"
apply (drule order_le_imp_less_or_eq)+
done

lemma real_inverse_unique: "x*y = (1::real) ==> y = inverse x"
apply (case_tac "x \<noteq> 0")
apply (rule_tac c1 = x in real_mult_left_cancel [THEN iffD1], auto)
done

lemma real_inverse_gt_one: "[| (0::real) < x; x < 1 |] ==> 1 < inverse x"
by (auto dest: less_imp_inverse_less)

lemma real_mult_self_sum_ge_zero: "(0::real) \<le> x*x + y*y"
proof -
have "0 + 0 \<le> x*x + y*y" by (blast intro: add_mono zero_le_square)
thus ?thesis by simp
qed

subsection{*Embedding the Integers into the Reals*}

real_of_nat_def: "real z == of_nat z"
real_of_int_def: "real z == of_int z"

lemma real_of_int_zero [simp]: "real (0::int) = 0"

lemma real_of_one [simp]: "real (1::int) = (1::real)"

lemma real_of_int_add: "real (x::int) + real y = real (x + y)"

lemma real_of_int_minus: "-real (x::int) = real (-x)"
declare real_of_int_minus [symmetric, simp]

lemma real_of_int_diff: "real (x::int) - real y = real (x - y)"
declare real_of_int_diff [symmetric, simp]

lemma real_of_int_mult: "real (x::int) * real y = real (x * y)"
declare real_of_int_mult [symmetric, simp]

lemma real_of_int_zero_cancel [simp]: "(real x = 0) = (x = (0::int))"

lemma real_of_int_inject [iff]: "(real (x::int) = real y) = (x = y)"

lemma real_of_int_less_iff [iff]: "(real (x::int) < real y) = (x < y)"

lemma real_of_int_le_iff [simp]: "(real (x::int) \<le> real y) = (x \<le> y)"

subsection{*Embedding the Naturals into the Reals*}

lemma real_of_nat_zero [simp]: "real (0::nat) = 0"

lemma real_of_nat_one [simp]: "real (Suc 0) = (1::real)"

lemma real_of_nat_add [simp]: "real (m + n) = real (m::nat) + real n"

(*Not for addsimps: often the LHS is used to represent a positive natural*)
lemma real_of_nat_Suc: "real (Suc n) = real n + (1::real)"

lemma real_of_nat_less_iff [iff]:
"(real (n::nat) < real m) = (n < m)"

lemma real_of_nat_le_iff [iff]: "(real (n::nat) \<le> real m) = (n \<le> m)"

lemma real_of_nat_ge_zero [iff]: "0 \<le> real (n::nat)"

lemma real_of_nat_Suc_gt_zero: "0 < real (Suc n)"
by (simp add: real_of_nat_def del: of_nat_Suc)

lemma real_of_nat_mult [simp]: "real (m * n) = real (m::nat) * real n"

lemma real_of_nat_inject [iff]: "(real (n::nat) = real m) = (n = m)"

lemma real_of_nat_zero_iff [iff]: "(real (n::nat) = 0) = (n = 0)"

lemma real_of_nat_diff: "n \<le> m ==> real (m - n) = real (m::nat) - real n"

lemma real_of_nat_gt_zero_cancel_iff [simp]: "(0 < real (n::nat)) = (0 < n)"

lemma real_of_nat_le_zero_cancel_iff [simp]: "(real (n::nat) \<le> 0) = (n = 0)"

lemma not_real_of_nat_less_zero [simp]: "~ real (n::nat) < 0"

lemma real_of_nat_ge_zero_cancel_iff [simp]: "(0 \<le> real (n::nat)) = (0 \<le> n)"

lemma real_of_int_real_of_nat: "real (int n) = real n"
by (simp add: real_of_nat_def real_of_int_def int_eq_of_nat)

text{*Still needed for binary arithmetic*}
lemma real_of_nat_real_of_int: "~neg z ==> real (nat z) = real z"
proof (simp add: not_neg_eq_ge_0 real_of_nat_def real_of_int_def)
assume "0 \<le> z"
hence eq: "of_nat (nat z) = z"
have "of_nat (nat z) = of_int (of_nat (nat z))" by simp
also have "... = of_int z" by (simp add: eq)
finally show "of_nat (nat z) = of_int z" .
qed

subsection{*Numerals and Arithmetic*}

instance real :: number ..

primrec (*the type constraint is essential!*)
number_of_Pls: "number_of bin.Pls = 0"
number_of_Min: "number_of bin.Min = - (1::real)"
number_of_BIT: "number_of(w BIT x) = (if x then 1 else 0) +
(number_of w) + (number_of w)"

declare number_of_Pls [simp del]
number_of_Min [simp del]
number_of_BIT [simp del]

instance real :: number_ring
proof
show "Numeral0 = (0::real)" by (rule number_of_Pls)
show "-1 = - (1::real)" by (rule number_of_Min)
fix w :: bin and x :: bool
show "(number_of (w BIT x) :: real) =
(if x then 1 else 0) + number_of w + number_of w"
by (rule number_of_BIT)
qed

text{*Collapse applications of @{term real} to @{term number_of}*}
lemma real_number_of [simp]: "real (number_of v :: int) = number_of v"

lemma real_of_nat_number_of [simp]:
"real (number_of v :: nat) =
(if neg (number_of v :: int) then 0
else (number_of v :: real))"
by (simp add: real_of_int_real_of_nat [symmetric] int_nat_number_of)

use "real_arith.ML"

setup real_arith_setup

subsection{* Simprules combining x+y and 0: ARE THEY NEEDED?*}

text{*Needed in this non-standard form by Hyperreal/Transcendental*}
lemma real_0_le_divide_iff:
"((0::real) \<le> x/y) = ((x \<le> 0 | 0 \<le> y) & (0 \<le> x | y \<le> 0))"
by (simp add: real_divide_def zero_le_mult_iff, auto)

lemma real_add_minus_iff [simp]: "(x + - a = (0::real)) = (x=a)"
by arith

lemma real_add_eq_0_iff [iff]: "(x+y = (0::real)) = (y = -x)"
by auto

lemma real_add_less_0_iff [iff]: "(x+y < (0::real)) = (y < -x)"
by auto

lemma real_0_less_add_iff [iff]: "((0::real) < x+y) = (-x < y)"
by auto

lemma real_add_le_0_iff [iff]: "(x+y \<le> (0::real)) = (y \<le> -x)"
by auto

lemma real_0_le_add_iff [iff]: "((0::real) \<le> x+y) = (-x \<le> y)"
by auto

(** Simprules combining x-y and 0 (needed??) **)

lemma real_0_less_diff_iff [iff]: "((0::real) < x-y) = (y < x)"
by auto

lemma real_0_le_diff_iff [iff]: "((0::real) \<le> x-y) = (y \<le> x)"
by auto

(*
FIXME: we should have this, as for type int, but many proofs would break.
It replaces x+-y by x-y.
*)

subsubsection{*Density of the Reals*}

lemma real_lbound_gt_zero:
"[| (0::real) < d1; 0 < d2 |] ==> \<exists>e. 0 < e & e < d1 & e < d2"
apply (rule_tac x = " (min d1 d2) /2" in exI)
done

text{*Similar results are proved in @{text Ring_and_Field}*}
lemma real_less_half_sum: "x < y ==> x < (x+y) / (2::real)"
by auto

lemma real_gt_half_sum: "x < y ==> (x+y)/(2::real) < y"
by auto

lemma real_dense: "x < y ==> \<exists>r::real. x < r & r < y"
by (rule Ring_and_Field.dense)

subsection{*Absolute Value Function for the Reals*}

text{*FIXME: these should go!*}
lemma abs_eqI1: "(0::real)\<le>x ==> abs x = x"
by (unfold real_abs_def, simp)

lemma abs_eqI2: "(0::real) < x ==> abs x = x"
by (unfold real_abs_def, simp)

lemma abs_minus_eqI2: "x < (0::real) ==> abs x = -x"
by (simp add: real_abs_def linorder_not_less [symmetric])

lemma abs_minus_add_cancel: "abs(x + (-y)) = abs (y + (-(x::real)))"
by (unfold real_abs_def, simp)

lemma abs_minus_one [simp]: "abs (-1) = (1::real)"
by (unfold real_abs_def, simp)

lemma abs_interval_iff: "(abs x < r) = (-r < x & x < (r::real))"

lemma abs_le_interval_iff: "(abs x \<le> r) = (-r\<le>x & x\<le>(r::real))"

lemma abs_add_one_gt_zero [simp]: "(0::real) < 1 + abs(x)"
by (unfold real_abs_def, auto)

lemma abs_real_of_nat_cancel [simp]: "abs (real x) = real (x::nat)"
by (auto intro: abs_eqI1 simp add: real_of_nat_ge_zero)

lemma abs_add_one_not_less_self [simp]: "~ abs(x) + (1::real) < x"
apply (auto intro: abs_ge_self [THEN order_trans])
done

text{*Used only in Hyperreal/Lim.ML*}
lemma abs_sum_triangle_ineq: "abs ((x::real) + y + (-l + -m)) \<le> abs(x + -l) + abs(y + -m)"
apply (rule_tac a1 = y in add_left_commute [THEN ssubst])
apply (rule abs_triangle_ineq)
done

ML
{*
val real_0_le_divide_iff = thm"real_0_le_divide_iff";
val real_0_less_diff_iff = thm"real_0_less_diff_iff";
val real_0_le_diff_iff = thm"real_0_le_diff_iff";
val real_lbound_gt_zero = thm"real_lbound_gt_zero";
val real_less_half_sum = thm"real_less_half_sum";
val real_gt_half_sum = thm"real_gt_half_sum";
val real_dense = thm"real_dense";

val abs_eqI1 = thm"abs_eqI1";
val abs_eqI2 = thm"abs_eqI2";
val abs_minus_eqI2 = thm"abs_minus_eqI2";
val abs_ge_zero = thm"abs_ge_zero";
val abs_idempotent = thm"abs_idempotent";
val abs_zero_iff = thm"abs_zero_iff";
val abs_ge_self = thm"abs_ge_self";
val abs_ge_minus_self = thm"abs_ge_minus_self";
val abs_mult = thm"abs_mult";
val abs_inverse = thm"abs_inverse";
val abs_triangle_ineq = thm"abs_triangle_ineq";
val abs_minus_cancel = thm"abs_minus_cancel";
val abs_minus_one = thm"abs_minus_one";
val abs_interval_iff = thm"abs_interval_iff";
val abs_le_interval_iff = thm"abs_le_interval_iff";