- use TableFun instead of homebrew binary tree in am_interpreter.ML
- add Floats to HOL/Real
--- a/src/HOL/IsaMakefile Tue Jul 12 19:29:52 2005 +0200
+++ b/src/HOL/IsaMakefile Tue Jul 12 21:49:38 2005 +0200
@@ -143,6 +143,7 @@
Real/Rational.thy Real/PReal.thy Real/RComplete.thy \
Real/ROOT.ML Real/Real.thy Real/real_arith.ML Real/RealDef.thy \
Real/RealPow.thy Real/document/root.tex \
+ Real/Float.thy Real/Float.ML \
Hyperreal/EvenOdd.thy Hyperreal/Fact.thy Hyperreal/HLog.thy \
Hyperreal/Filter.thy Hyperreal/HSeries.thy \
Hyperreal/HTranscendental.thy Hyperreal/HyperArith.thy \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Real/Float.ML Tue Jul 12 21:49:38 2005 +0200
@@ -0,0 +1,519 @@
+(* Title: HOL/Real/Float.ML
+ ID: $Id$
+ Author: Steven Obua
+*)
+
+structure ExactFloatingPoint :
+sig
+ exception Destruct_floatstr of string
+ val destruct_floatstr : (char -> bool) -> (char -> bool) -> string -> bool * string * string * bool * string
+
+ exception Floating_point of string
+
+ type floatrep = IntInf.int * IntInf.int
+ val approx_dec_by_bin : IntInf.int -> floatrep -> floatrep * floatrep
+ val approx_decstr_by_bin : int -> string -> floatrep * floatrep
+end
+=
+struct
+
+fun fst (a,b) = a
+fun snd (a,b) = b
+
+val filter = List.filter;
+
+exception Destruct_floatstr of string;
+
+fun destruct_floatstr isDigit isExp number =
+ let
+ val numlist = filter (not o Char.isSpace) (String.explode number)
+
+ fun countsigns ((#"+")::cs) = countsigns cs
+ | countsigns ((#"-")::cs) =
+ let
+ val (positive, rest) = countsigns cs
+ in
+ (not positive, rest)
+ end
+ | countsigns cs = (true, cs)
+
+ fun readdigits [] = ([], [])
+ | readdigits (q as c::cs) =
+ if (isDigit c) then
+ let
+ val (digits, rest) = readdigits cs
+ in
+ (c::digits, rest)
+ end
+ else
+ ([], q)
+
+ fun readfromexp_helper cs =
+ let
+ val (positive, rest) = countsigns cs
+ val (digits, rest') = readdigits rest
+ in
+ case rest' of
+ [] => (positive, digits)
+ | _ => raise (Destruct_floatstr number)
+ end
+
+ fun readfromexp [] = (true, [])
+ | readfromexp (c::cs) =
+ if isExp c then
+ readfromexp_helper cs
+ else
+ raise (Destruct_floatstr number)
+
+ fun readfromdot [] = ([], readfromexp [])
+ | readfromdot ((#".")::cs) =
+ let
+ val (digits, rest) = readdigits cs
+ val exp = readfromexp rest
+ in
+ (digits, exp)
+ end
+ | readfromdot cs = readfromdot ((#".")::cs)
+
+ val (positive, numlist) = countsigns numlist
+ val (digits1, numlist) = readdigits numlist
+ val (digits2, exp) = readfromdot numlist
+ in
+ (positive, String.implode digits1, String.implode digits2, fst exp, String.implode (snd exp))
+ end
+
+type floatrep = IntInf.int * IntInf.int
+
+exception Floating_point of string;
+
+val ln2_10 = (Math.ln 10.0)/(Math.ln 2.0)
+
+fun intmul a b = IntInf.* (a,b)
+fun intsub a b = IntInf.- (a,b)
+fun intadd a b = IntInf.+ (a,b)
+fun intpow a b = IntInf.pow (a, IntInf.toInt b);
+fun intle a b = IntInf.<= (a, b);
+fun intless a b = IntInf.< (a, b);
+fun intneg a = IntInf.~ a;
+val zero = IntInf.fromInt 0;
+val one = IntInf.fromInt 1;
+val two = IntInf.fromInt 2;
+val ten = IntInf.fromInt 10;
+val five = IntInf.fromInt 5;
+
+fun find_most_significant q r =
+ let
+ fun int2real i =
+ case Real.fromString (IntInf.toString i) of
+ SOME r => r
+ | NONE => raise (Floating_point "int2real")
+ fun subtract (q, r) (q', r') =
+ if intle r r' then
+ (intsub q (intmul q' (intpow ten (intsub r' r))), r)
+ else
+ (intsub (intmul q (intpow ten (intsub r r'))) q', r')
+ fun bin2dec d =
+ if intle zero d then
+ (intpow two d, zero)
+ else
+ (intpow five (intneg d), d)
+
+ val L = IntInf.fromInt (Real.floor (int2real (IntInf.fromInt (IntInf.log2 q)) + (int2real r) * ln2_10))
+ val L1 = intadd L one
+
+ val (q1, r1) = subtract (q, r) (bin2dec L1)
+ in
+ if intle zero q1 then
+ let
+ val (q2, r2) = subtract (q, r) (bin2dec (intadd L1 one))
+ in
+ if intle zero q2 then
+ raise (Floating_point "find_most_significant")
+ else
+ (L1, (q1, r1))
+ end
+ else
+ let
+ val (q0, r0) = subtract (q, r) (bin2dec L)
+ in
+ if intle zero q0 then
+ (L, (q0, r0))
+ else
+ raise (Floating_point "find_most_significant")
+ end
+ end
+
+fun approx_dec_by_bin n (q,r) =
+ let
+ fun addseq acc d' [] = acc
+ | addseq acc d' (d::ds) = addseq (intadd acc (intpow two (intsub d d'))) d' ds
+
+ fun seq2bin [] = (zero, zero)
+ | seq2bin (d::ds) = (intadd (addseq zero d ds) one, d)
+
+ fun approx d_seq d0 precision (q,r) =
+ if q = zero then
+ let val x = seq2bin d_seq in
+ (x, x)
+ end
+ else
+ let
+ val (d, (q', r')) = find_most_significant q r
+ in
+ if intless precision (intsub d0 d) then
+ let
+ val d' = intsub d0 precision
+ val x1 = seq2bin (d_seq)
+ val x2 = (intadd (intmul (fst x1) (intpow two (intsub (snd x1) d'))) one, d') (* = seq2bin (d'::d_seq) *)
+ in
+ (x1, x2)
+ end
+ else
+ approx (d::d_seq) d0 precision (q', r')
+ end
+
+ fun approx_start precision (q, r) =
+ if q = zero then
+ ((zero, zero), (zero, zero))
+ else
+ let
+ val (d, (q', r')) = find_most_significant q r
+ in
+ if intle precision zero then
+ let
+ val x1 = seq2bin [d]
+ in
+ if q' = zero then
+ (x1, x1)
+ else
+ (x1, seq2bin [intadd d one])
+ end
+ else
+ approx [d] d precision (q', r')
+ end
+ in
+ if intle zero q then
+ approx_start n (q,r)
+ else
+ let
+ val ((a1,b1), (a2, b2)) = approx_start n (intneg q, r)
+ in
+ ((intneg a2, b2), (intneg a1, b1))
+ end
+ end
+
+fun approx_decstr_by_bin n decstr =
+ let
+ fun str2int s = case IntInf.fromString s of SOME x => x | NONE => zero
+ fun signint p x = if p then x else intneg x
+
+ val (p, d1, d2, ep, e) = destruct_floatstr Char.isDigit (fn e => e = #"e" orelse e = #"E") decstr
+ val s = IntInf.fromInt (size d2)
+
+ val q = signint p (intadd (intmul (str2int d1) (intpow ten s)) (str2int d2))
+ val r = intsub (signint ep (str2int e)) s
+ in
+ approx_dec_by_bin (IntInf.fromInt n) (q,r)
+ end
+
+end;
+
+structure FloatArith =
+struct
+
+type float = IntInf.int * IntInf.int
+
+val izero = IntInf.fromInt 0
+val ione = IntInf.fromInt 1
+val imone = IntInf.fromInt ~1
+val itwo = IntInf.fromInt 2
+fun imul a b = IntInf.* (a,b)
+fun isub a b = IntInf.- (a,b)
+fun iadd a b = IntInf.+ (a,b)
+
+val floatzero = (izero, izero)
+
+fun positive_part (a,b) =
+ (if IntInf.< (a,izero) then izero else a, b)
+
+fun negative_part (a,b) =
+ (if IntInf.< (a,izero) then a else izero, b)
+
+fun is_negative (a,b) =
+ if IntInf.< (a, izero) then true else false
+
+fun is_positive (a,b) =
+ if IntInf.< (izero, a) then true else false
+
+fun is_zero (a,b) =
+ if a = izero then true else false
+
+fun ipow2 a = IntInf.pow ((IntInf.fromInt 2), IntInf.toInt a)
+
+fun add (a1, b1) (a2, b2) =
+ if IntInf.< (b1, b2) then
+ (iadd a1 (imul a2 (ipow2 (isub b2 b1))), b1)
+ else
+ (iadd (imul a1 (ipow2 (isub b1 b2))) a2, b2)
+
+fun sub (a1, b1) (a2, b2) =
+ if IntInf.< (b1, b2) then
+ (isub a1 (imul a2 (ipow2 (isub b2 b1))), b1)
+ else
+ (isub (imul a1 (ipow2 (isub b1 b2))) a2, b2)
+
+fun neg (a, b) = (IntInf.~ a, b)
+
+fun is_equal a b = is_zero (sub a b)
+
+fun is_less a b = is_negative (sub a b)
+
+fun max a b = if is_less a b then b else a
+
+fun min a b = if is_less a b then a else b
+
+fun abs a = if is_negative a then neg a else a
+
+fun mul (a1, b1) (a2, b2) = (imul a1 a2, iadd b1 b2)
+
+end;
+
+
+structure Float:
+sig
+ type float = FloatArith.float
+ type floatfunc = float * float -> float * float
+
+ val mk_intinf : typ -> IntInf.int -> term
+ val mk_float : float -> term
+
+ exception Dest_intinf;
+ val dest_intinf : term -> IntInf.int
+ val dest_nat : term -> IntInf.int
+
+ exception Dest_float;
+ val dest_float : term -> float
+
+ val float_const : term
+
+ val float_add_const : term
+ val float_diff_const : term
+ val float_uminus_const : term
+ val float_pprt_const : term
+ val float_nprt_const : term
+ val float_abs_const : term
+ val float_mult_const : term
+ val float_le_const : term
+
+ val nat_le_const : term
+ val nat_less_const : term
+ val nat_eq_const : term
+
+ val approx_float : int -> floatfunc -> string -> term * term
+
+ val sign_term : term -> cterm
+
+(* exception Float_op_oracle_data of term
+ exception Nat_op_oracle_data of term
+
+ val float_op_oracle : Sign.sg * exn -> term
+ val nat_op_oracle : Sign.sg * exn -> term
+
+ val invoke_float_op : term -> thm
+ val invoke_nat_op : term -> thm*)
+end
+=
+struct
+
+structure Inttab = TableFun(type key = int val ord = (rev_order o int_ord));
+
+type float = IntInf.int*IntInf.int
+type floatfunc = float*float -> float*float
+
+val th = theory "Float"
+val sg = sign_of th
+
+val float_const = Const ("Float.float", HOLogic.mk_prodT (HOLogic.intT, HOLogic.intT) --> HOLogic.realT)
+
+val float_add_const = Const ("op +", HOLogic.realT --> HOLogic.realT --> HOLogic.realT)
+val float_diff_const = Const ("op -", HOLogic.realT --> HOLogic.realT --> HOLogic.realT)
+val float_mult_const = Const ("op *", HOLogic.realT --> HOLogic.realT --> HOLogic.realT)
+val float_uminus_const = Const ("uminus", HOLogic.realT --> HOLogic.realT)
+val float_abs_const = Const ("HOL.abs", HOLogic.realT --> HOLogic.realT)
+val float_le_const = Const ("op <=", HOLogic.realT --> HOLogic.realT --> HOLogic.boolT)
+val float_pprt_const = Const ("OrderedGroup.pprt", HOLogic.realT --> HOLogic.realT)
+val float_nprt_const = Const ("OrderedGroup.nprt", HOLogic.realT --> HOLogic.realT)
+
+val nat_le_const = Const ("op <=", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT)
+val nat_less_const = Const ("op <", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT)
+val nat_eq_const = Const ("op =", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT)
+
+val zero = FloatArith.izero
+val minus_one = FloatArith.imone
+val two = FloatArith.itwo
+
+exception Dest_intinf;
+exception Dest_float;
+
+fun mk_intinf ty n =
+ let
+ fun mk_bit n = if n = zero then HOLogic.false_const else HOLogic.true_const
+
+ fun bin_of n =
+ if n = zero then HOLogic.pls_const
+ else if n = minus_one then HOLogic.min_const
+ else
+ let
+ (*val (q,r) = IntInf.divMod (n, two): doesn't work in SML 10.0.7, but in newer versions!!!*)
+ val q = IntInf.div (n, two)
+ val r = IntInf.mod (n, two)
+ in
+ HOLogic.bit_const $ bin_of q $ mk_bit r
+ end
+ in
+ HOLogic.number_of_const ty $ (bin_of n)
+ end
+
+fun dest_intinf n =
+ let
+ fun dest_bit n =
+ case n of
+ Const ("False", _) => FloatArith.izero
+ | Const ("True", _) => FloatArith.ione
+ | _ => raise Dest_intinf
+
+ fun int_of n =
+ case n of
+ Const ("Numeral.Pls", _) => FloatArith.izero
+ | Const ("Numeral.Min", _) => FloatArith.imone
+ | Const ("Numeral.Bit", _) $ q $ r => FloatArith.iadd (FloatArith.imul (int_of q) FloatArith.itwo) (dest_bit r)
+ | _ => raise Dest_intinf
+ in
+ case n of
+ Const ("Numeral.number_of", _) $ n' => int_of n'
+ | Const ("Numeral0", _) => FloatArith.izero
+ | Const ("Numeral1", _) => FloatArith.ione
+ | _ => raise Dest_intinf
+ end
+
+fun mk_float (a,b) =
+ float_const $ (HOLogic.mk_prod ((mk_intinf HOLogic.intT a), (mk_intinf HOLogic.intT b)))
+
+fun dest_float f =
+ case f of
+ (Const ("Float.float", _) $ (Const ("Pair", _) $ a $ b)) => (dest_intinf a, dest_intinf b)
+ | Const ("Numeral.number_of",_) $ a => (dest_intinf f, 0)
+ | Const ("Numeral0", _) => (FloatArith.izero, FloatArith.izero)
+ | Const ("Numeral1", _) => (FloatArith.ione, FloatArith.izero)
+ | _ => raise Dest_float
+
+fun dest_nat n =
+ let
+ val v = dest_intinf n
+ in
+ if IntInf.< (v, FloatArith.izero) then
+ FloatArith.izero
+ else
+ v
+ end
+
+fun approx_float prec f value =
+ let
+ val interval = ExactFloatingPoint.approx_decstr_by_bin prec value
+ val (flower, fupper) = f interval
+ in
+ (mk_float flower, mk_float fupper)
+ end
+
+fun sign_term t = cterm_of sg t
+
+(*exception Float_op_oracle_data of term;
+
+fun float_op_oracle (sg, exn as Float_op_oracle_data t) =
+ Logic.mk_equals (t,
+ case t of
+ f $ a $ b =>
+ let
+ val a' = dest_float a
+ val b' = dest_float b
+ in
+ if f = float_add_const then
+ mk_float (FloatArith.add a' b')
+ else if f = float_diff_const then
+ mk_float (FloatArith.sub a' b')
+ else if f = float_mult_const then
+ mk_float (FloatArith.mul a' b')
+ else if f = float_le_const then
+ (if FloatArith.is_less b' a' then
+ HOLogic.false_const
+ else
+ HOLogic.true_const)
+ else raise exn
+ end
+ | f $ a =>
+ let
+ val a' = dest_float a
+ in
+ if f = float_uminus_const then
+ mk_float (FloatArith.neg a')
+ else if f = float_abs_const then
+ mk_float (FloatArith.abs a')
+ else if f = float_pprt_const then
+ mk_float (FloatArith.positive_part a')
+ else if f = float_nprt_const then
+ mk_float (FloatArith.negative_part a')
+ else
+ raise exn
+ end
+ | _ => raise exn
+ )
+val th = ref ([]: Theory.theory list)
+val sg = ref ([]: Sign.sg list)
+
+fun invoke_float_op c =
+ let
+ val th = (if length(!th) = 0 then th := [theory "MatrixLP"] else (); hd (!th))
+ val sg = (if length(!sg) = 0 then sg := [sign_of th] else (); hd (!sg))
+ in
+ invoke_oracle th "float_op" (sg, Float_op_oracle_data c)
+ end
+
+exception Nat_op_oracle_data of term;
+
+fun nat_op_oracle (sg, exn as Nat_op_oracle_data t) =
+ Logic.mk_equals (t,
+ case t of
+ f $ a $ b =>
+ let
+ val a' = dest_nat a
+ val b' = dest_nat b
+ in
+ if f = nat_le_const then
+ (if IntInf.<= (a', b') then
+ HOLogic.true_const
+ else
+ HOLogic.false_const)
+ else if f = nat_eq_const then
+ (if a' = b' then
+ HOLogic.true_const
+ else
+ HOLogic.false_const)
+ else if f = nat_less_const then
+ (if IntInf.< (a', b') then
+ HOLogic.true_const
+ else
+ HOLogic.false_const)
+ else
+ raise exn
+ end
+ | _ => raise exn)
+
+fun invoke_nat_op c =
+ let
+ val th = (if length (!th) = 0 then th := [theory "MatrixLP"] else (); hd (!th))
+ val sg = (if length (!sg) = 0 then sg := [sign_of th] else (); hd (!sg))
+ in
+ invoke_oracle th "nat_op" (sg, Nat_op_oracle_data c)
+ end
+*)
+end;
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/HOL/Real/Float.thy Tue Jul 12 21:49:38 2005 +0200
@@ -0,0 +1,525 @@
+(* Title: HOL/Real/Float.thy
+ ID: $Id$
+ Author: Steven Obua
+*)
+
+theory Float = Real:
+
+constdefs
+ pow2 :: "int \<Rightarrow> real"
+ "pow2 a == if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a))))"
+ float :: "int * int \<Rightarrow> real"
+ "float x == (real (fst x)) * (pow2 (snd x))"
+
+lemma pow2_0[simp]: "pow2 0 = 1"
+by (simp add: pow2_def)
+
+lemma pow2_1[simp]: "pow2 1 = 2"
+by (simp add: pow2_def)
+
+lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
+by (simp add: pow2_def)
+
+lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
+proof -
+ have h: "! n. nat (2 + int n) - Suc 0 = nat (1 + int n)" by arith
+ have g: "! a b. a - -1 = a + (1::int)" by arith
+ have pos: "! n. pow2 (int n + 1) = 2 * pow2 (int n)"
+ apply (auto, induct_tac n)
+ apply (simp_all add: pow2_def)
+ apply (rule_tac m1="2" and n1="nat (2 + int na)" in ssubst[OF realpow_num_eq_if])
+ apply (auto simp add: h)
+ apply arith
+ done
+ show ?thesis
+ proof (induct a)
+ case (1 n)
+ from pos show ?case by (simp add: ring_eq_simps)
+ next
+ case (2 n)
+ show ?case
+ apply (auto)
+ apply (subst pow2_neg[of "- int n"])
+ apply (subst pow2_neg[of "-1 - int n"])
+ apply (auto simp add: g pos)
+ done
+ qed
+qed
+
+lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
+proof (induct b)
+ case (1 n)
+ show ?case
+ proof (induct n)
+ case 0
+ show ?case by simp
+ next
+ case (Suc m)
+ show ?case by (auto simp add: ring_eq_simps pow2_add1 prems)
+ qed
+next
+ case (2 n)
+ show ?case
+ proof (induct n)
+ case 0
+ show ?case
+ apply (auto)
+ apply (subst pow2_neg[of "a + -1"])
+ apply (subst pow2_neg[of "-1"])
+ apply (simp)
+ apply (insert pow2_add1[of "-a"])
+ apply (simp add: ring_eq_simps)
+ apply (subst pow2_neg[of "-a"])
+ apply (simp)
+ done
+ case (Suc m)
+ have a: "int m - (a + -2) = 1 + (int m - a + 1)" by arith
+ have b: "int m - -2 = 1 + (int m + 1)" by arith
+ show ?case
+ apply (auto)
+ apply (subst pow2_neg[of "a + (-2 - int m)"])
+ apply (subst pow2_neg[of "-2 - int m"])
+ apply (auto simp add: ring_eq_simps)
+ apply (subst a)
+ apply (subst b)
+ apply (simp only: pow2_add1)
+ apply (subst pow2_neg[of "int m - a + 1"])
+ apply (subst pow2_neg[of "int m + 1"])
+ apply auto
+ apply (insert prems)
+ apply (auto simp add: ring_eq_simps)
+ done
+ qed
+qed
+
+lemma "float (a, e) + float (b, e) = float (a + b, e)"
+by (simp add: float_def ring_eq_simps)
+
+constdefs
+ int_of_real :: "real \<Rightarrow> int"
+ "int_of_real x == SOME y. real y = x"
+ real_is_int :: "real \<Rightarrow> bool"
+ "real_is_int x == ? (u::int). x = real u"
+
+lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
+by (auto simp add: real_is_int_def int_of_real_def)
+
+lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
+by (simp add: float_def real_is_int_def2 pow2_add[symmetric])
+
+lemma pow2_int: "pow2 (int c) = (2::real)^c"
+by (simp add: pow2_def)
+
+lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)"
+by (simp add: float_def pow2_int[symmetric] pow2_add[symmetric])
+
+lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
+by (auto simp add: real_is_int_def int_of_real_def)
+
+lemma int_of_real_real[simp]: "int_of_real (real x) = x"
+by (simp add: int_of_real_def)
+
+lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
+by (auto simp add: int_of_real_def real_is_int_def)
+
+lemma real_is_int_add_int_of_real: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a+b)) = (int_of_real a) + (int_of_real b)"
+by (auto simp add: int_of_real_def real_is_int_def)
+
+lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
+apply (subst real_is_int_def2)
+apply (simp add: real_is_int_add_int_of_real real_int_of_real)
+done
+
+lemma int_of_real_sub: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a-b)) = (int_of_real a) - (int_of_real b)"
+by (auto simp add: int_of_real_def real_is_int_def)
+
+lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
+apply (subst real_is_int_def2)
+apply (simp add: int_of_real_sub real_int_of_real)
+done
+
+lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
+by (auto simp add: real_is_int_def)
+
+lemma int_of_real_mult:
+ assumes "real_is_int a" "real_is_int b"
+ shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
+proof -
+ from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
+ from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
+ from a obtain a'::int where a':"a = real a'" by auto
+ from b obtain b'::int where b':"b = real b'" by auto
+ have r: "real a' * real b' = real (a' * b')" by auto
+ show ?thesis
+ apply (simp add: a' b')
+ apply (subst r)
+ apply (simp only: int_of_real_real)
+ done
+qed
+
+lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
+apply (subst real_is_int_def2)
+apply (simp add: int_of_real_mult)
+done
+
+lemma real_is_int_0[simp]: "real_is_int (0::real)"
+by (simp add: real_is_int_def int_of_real_def)
+
+lemma real_is_int_1[simp]: "real_is_int (1::real)"
+proof -
+ have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
+ also have "\<dots> = True" by (simp only: real_is_int_real)
+ ultimately show ?thesis by auto
+qed
+
+lemma real_is_int_n1: "real_is_int (-1::real)"
+proof -
+ have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
+ also have "\<dots> = True" by (simp only: real_is_int_real)
+ ultimately show ?thesis by auto
+qed
+
+lemma real_is_int_number_of[simp]: "real_is_int ((number_of::bin\<Rightarrow>real) x)"
+proof -
+ have neg1: "real_is_int (-1::real)"
+ proof -
+ have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
+ also have "\<dots> = True" by (simp only: real_is_int_real)
+ ultimately show ?thesis by auto
+ qed
+
+ {
+ fix x::int
+ have "!! y. real_is_int ((number_of::bin\<Rightarrow>real) (Abs_Bin x))"
+ apply (simp add: number_of_eq)
+ apply (subst Abs_Bin_inverse)
+ apply (simp add: Bin_def)
+ apply (induct x)
+ apply (induct_tac n)
+ apply (simp)
+ apply (simp)
+ apply (induct_tac n)
+ apply (simp add: neg1)
+ proof -
+ fix n :: nat
+ assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
+ have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
+ show "real_is_int (of_int (- (int (Suc (Suc n)))))"
+ apply (simp only: s of_int_add)
+ apply (rule real_is_int_add)
+ apply (simp add: neg1)
+ apply (simp only: rn)
+ done
+ qed
+ }
+ note Abs_Bin = this
+ {
+ fix x :: bin
+ have "? u. x = Abs_Bin u"
+ apply (rule exI[where x = "Rep_Bin x"])
+ apply (simp add: Rep_Bin_inverse)
+ done
+ }
+ then obtain u::int where "x = Abs_Bin u" by auto
+ with Abs_Bin show ?thesis by auto
+qed
+
+lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
+by (simp add: int_of_real_def)
+
+lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
+proof -
+ have 1: "(1::real) = real (1::int)" by auto
+ show ?thesis by (simp only: 1 int_of_real_real)
+qed
+
+lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
+proof -
+ have "real_is_int (number_of b)" by simp
+ then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
+ then obtain u::int where u:"number_of b = real u" by auto
+ have "number_of b = real ((number_of b)::int)"
+ by (simp add: number_of_eq real_of_int_def)
+ have ub: "number_of b = real ((number_of b)::int)"
+ by (simp add: number_of_eq real_of_int_def)
+ from uu u ub have unb: "u = number_of b"
+ by blast
+ have "int_of_real (number_of b) = u" by (simp add: u)
+ with unb show ?thesis by simp
+qed
+
+lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
+ apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
+ apply (simp_all add: pow2_def even_def real_is_int_def ring_eq_simps)
+ apply (auto)
+proof -
+ fix q::int
+ have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
+ show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))"
+ by (simp add: a)
+qed
+
+consts
+ norm_float :: "int*int \<Rightarrow> int*int"
+
+lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
+apply (subst split_div, auto)
+apply (subst split_zdiv, auto)
+apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)
+apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
+done
+
+lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
+apply (subst split_mod, auto)
+apply (subst split_zmod, auto)
+apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia in IntDiv.unique_remainder)
+apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
+done
+
+lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
+by arith
+
+lemma terminating_norm_float: "\<forall>a. (a::int) \<noteq> 0 \<and> even a \<longrightarrow> a \<noteq> 0 \<and> \<bar>a div 2\<bar> < \<bar>a\<bar>"
+apply (auto)
+apply (rule abs_div_2_less)
+apply (auto)
+done
+
+ML {* simp_depth_limit := 2 *}
+recdef norm_float "measure (% (a,b). nat (abs a))"
+ "norm_float (a,b) = (if (a \<noteq> 0) & (even a) then norm_float (a div 2, b+1) else (if a=0 then (0,0) else (a,b)))"
+(hints simp: terminating_norm_float)
+ML {* simp_depth_limit := 1000 *}
+
+lemma norm_float: "float x = float (norm_float x)"
+proof -
+ {
+ fix a b :: int
+ have norm_float_pair: "float (a,b) = float (norm_float (a,b))"
+ proof (induct a b rule: norm_float.induct)
+ case (1 u v)
+ show ?case
+ proof cases
+ assume u: "u \<noteq> 0 \<and> even u"
+ with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2, v + 1))" by auto
+ with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even)
+ then show ?thesis
+ apply (subst norm_float.simps)
+ apply (simp add: ind)
+ done
+ next
+ assume "~(u \<noteq> 0 \<and> even u)"
+ then show ?thesis
+ by (simp add: prems float_def)
+ qed
+ qed
+ }
+ note helper = this
+ have "? a b. x = (a,b)" by auto
+ then obtain a b where "x = (a, b)" by blast
+ then show ?thesis by (simp only: helper)
+qed
+
+lemma pow2_int: "pow2 (int n) = 2^n"
+ by (simp add: pow2_def)
+
+lemma float_add:
+ "float (a1, e1) + float (a2, e2) =
+ (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1)
+ else float (a1*2^(nat (e1-e2))+a2, e2))"
+ apply (simp add: float_def ring_eq_simps)
+ apply (auto simp add: pow2_int[symmetric] pow2_add[symmetric])
+ done
+
+lemma float_mult:
+ "float (a1, e1) * float (a2, e2) =
+ (float (a1 * a2, e1 + e2))"
+ by (simp add: float_def pow2_add)
+
+lemma float_minus:
+ "- (float (a,b)) = float (-a, b)"
+ by (simp add: float_def)
+
+lemma zero_less_pow2:
+ "0 < pow2 x"
+proof -
+ {
+ fix y
+ have "0 <= y \<Longrightarrow> 0 < pow2 y"
+ by (induct y, induct_tac n, simp_all add: pow2_add)
+ }
+ note helper=this
+ show ?thesis
+ apply (case_tac "0 <= x")
+ apply (simp add: helper)
+ apply (subst pow2_neg)
+ apply (simp add: helper)
+ done
+qed
+
+lemma zero_le_float:
+ "(0 <= float (a,b)) = (0 <= a)"
+ apply (auto simp add: float_def)
+ apply (auto simp add: zero_le_mult_iff zero_less_pow2)
+ apply (insert zero_less_pow2[of b])
+ apply (simp_all)
+ done
+
+lemma float_le_zero:
+ "(float (a,b) <= 0) = (a <= 0)"
+ apply (auto simp add: float_def)
+ apply (auto simp add: mult_le_0_iff)
+ apply (insert zero_less_pow2[of b])
+ apply auto
+ done
+
+lemma float_abs:
+ "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
+ apply (auto simp add: abs_if)
+ apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
+ done
+
+lemma float_zero:
+ "float (0, b) = 0"
+ by (simp add: float_def)
+
+lemma float_pprt:
+ "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
+ by (auto simp add: zero_le_float float_le_zero float_zero)
+
+lemma float_nprt:
+ "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
+ by (auto simp add: zero_le_float float_le_zero float_zero)
+
+lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
+ by auto
+
+lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
+ by simp
+
+lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
+ by simp
+
+lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
+ by simp
+
+lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
+ by simp
+
+lemma int_pow_0: "(a::int)^(Numeral0) = 1"
+ by simp
+
+lemma int_pow_1: "(a::int)^(Numeral1) = a"
+ by simp
+
+lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
+ by simp
+
+lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
+ by simp
+
+lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
+ by simp
+
+lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
+ by simp
+
+lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
+ by simp
+
+lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
+proof -
+ have 1:"((-1)::nat) = 0"
+ by simp
+ show ?thesis by (simp add: 1)
+qed
+
+lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
+ by simp
+
+lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
+ by simp
+
+lemma lift_bool: "x \<Longrightarrow> x=True"
+ by simp
+
+lemma nlift_bool: "~x \<Longrightarrow> x=False"
+ by simp
+
+lemma not_false_eq_true: "(~ False) = True" by simp
+
+lemma not_true_eq_false: "(~ True) = False" by simp
+
+
+lemmas binarith =
+ Pls_0_eq Min_1_eq
+ bin_pred_Pls bin_pred_Min bin_pred_1 bin_pred_0
+ bin_succ_Pls bin_succ_Min bin_succ_1 bin_succ_0
+ bin_add_Pls bin_add_Min bin_add_BIT_0 bin_add_BIT_10
+ bin_add_BIT_11 bin_minus_Pls bin_minus_Min bin_minus_1
+ bin_minus_0 bin_mult_Pls bin_mult_Min bin_mult_1 bin_mult_0
+ bin_add_Pls_right bin_add_Min_right
+
+lemma int_eq_number_of_eq: "(((number_of v)::int)=(number_of w)) = iszero ((number_of (bin_add v (bin_minus w)))::int)"
+ by simp
+
+lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
+ by (simp only: iszero_number_of_Pls)
+
+lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
+ by simp
+
+lemma int_iszero_number_of_0: "iszero ((number_of (w BIT bit.B0))::int) = iszero ((number_of w)::int)"
+ by simp
+
+lemma int_iszero_number_of_1: "\<not> iszero ((number_of (w BIT bit.B1))::int)"
+ by simp
+
+lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (bin_add x (bin_minus y)))::int)"
+ by simp
+
+lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
+ by simp
+
+lemma int_neg_number_of_Min: "neg (-1::int)"
+ by simp
+
+lemma int_neg_number_of_BIT: "neg ((number_of (w BIT x))::int) = neg ((number_of w)::int)"
+ by simp
+
+lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (bin_add y (bin_minus x)))::int))"
+ by simp
+
+lemmas intarithrel =
+ int_eq_number_of_eq
+ lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_0
+ lift_bool[OF int_iszero_number_of_1] int_less_number_of_eq_neg nlift_bool[OF int_not_neg_number_of_Pls] lift_bool[OF int_neg_number_of_Min]
+ int_neg_number_of_BIT int_le_number_of_eq
+
+lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (bin_add v w)"
+ by simp
+
+lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (bin_add v (bin_minus w))"
+ by simp
+
+lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (bin_mult v w)"
+ by simp
+
+lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (bin_minus v)"
+ by simp
+
+lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
+
+lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
+
+lemmas powerarith = nat_number_of zpower_number_of_even
+ zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
+ zpower_Pls zpower_Min
+
+lemmas floatarith[simplified norm_0_1] = float_add float_mult float_minus float_abs zero_le_float float_pprt float_nprt
+
+(* for use with the compute oracle *)
+lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
+
+end
+
--- a/src/HOL/Real/ROOT.ML Tue Jul 12 19:29:52 2005 +0200
+++ b/src/HOL/Real/ROOT.ML Tue Jul 12 21:49:38 2005 +0200
@@ -8,3 +8,4 @@
*)
time_use_thy "Real";
+use_thy "Float";
\ No newline at end of file
--- a/src/Pure/Tools/am_compiler.ML Tue Jul 12 19:29:52 2005 +0200
+++ b/src/Pure/Tools/am_compiler.ML Tue Jul 12 21:49:38 2005 +0200
@@ -11,10 +11,15 @@
| CApp of closure * closure | CAbs of closure | Closure of (closure list) * closure
val set_compiled_rewriter : (term -> closure) -> unit
+ val list_nth : 'a list * int -> 'a
+ val list_map : ('a -> 'b) -> 'a list -> 'b list
end
structure AM_Compiler :> COMPILING_AM = struct
+val list_nth = List.nth;
+val list_map = map;
+
datatype term = Var of int | Const of int | App of term * term | Abs of term
datatype pattern = PVar | PConst of int * (pattern list)
@@ -133,7 +138,7 @@
"and weak stack (Closure (e, App (a, b))) = weak (SAppL (Closure (e, b), stack)) (Closure (e, a))",
" | weak (SAppL (b, stack)) (Closure (e, Abs m)) = weak stack (Closure (b::e, m))",
" | weak stack (clos as Closure (_, Abs _)) = weak_last stack clos",
- " | weak stack (Closure (e, Var n)) = weak stack (List.nth (e, n) handle Subscript => (Var (n-(length e))))",
+ " | weak stack (Closure (e, Var n)) = weak stack ("^sname^".list_nth (e, n) handle _ => (Var (n-(length e))))",
" | weak stack (Closure (e, c)) = weak stack c",
" | weak stack clos = lookup stack clos",
"and weak_last (SAppR (a, stack)) b = weak stack (App(a, b))",
@@ -177,7 +182,7 @@
" | exportTerm (Const c) = "^sname^".CConst c",
" | exportTerm (App (a,b)) = "^sname^".CApp (exportTerm a, exportTerm b)",
" | exportTerm (Abs m) = "^sname^".CAbs (exportTerm m)",
- " | exportTerm (Closure (closlist, clos)) = "^sname^".Closure (map exportTerm closlist, exportTerm clos)"]
+ " | exportTerm (Closure (closlist, clos)) = "^sname^".Closure ("^sname^".list_map exportTerm closlist, exportTerm clos)"]
val _ = writelist (map ec constants)
val _ = writelist [
@@ -199,9 +204,9 @@
val _ =
let
- val fout = TextIO.openOut "gen_code.ML"
+ (*val fout = TextIO.openOut "gen_code.ML"
val _ = TextIO.output (fout, !buffer)
- val _ = TextIO.closeOut fout
+ val _ = TextIO.closeOut fout*)
in
()
end
--- a/src/Pure/Tools/am_interpreter.ML Tue Jul 12 19:29:52 2005 +0200
+++ b/src/Pure/Tools/am_interpreter.ML Tue Jul 12 21:49:38 2005 +0200
@@ -18,66 +18,6 @@
end
-signature BIN_TREE_KEY =
-sig
- type key
- val less : key * key -> bool
- val eq : key * key -> bool
-end
-
-signature BIN_TREE =
-sig
- type key
- type 'a t
- val tree_of_list : (key * 'a list -> 'b) -> (key * 'a) list -> 'b t
- val lookup : 'a t -> key -> 'a Option.option
- val empty : 'a t
-end
-
-functor BinTreeFun(Key: BIN_TREE_KEY) : BIN_TREE =
-struct
-
-type key = Key.key
-
-datatype 'a t = Empty | Node of key * 'a * 'a t * 'a t
-
-val empty = Empty
-
-fun insert (k, a) [] = [(k, a)]
- | insert (k, a) ((l,b)::x') =
- if Key.less (k, l) then (k, a)::(l,b)::x'
- else if Key.eq (k, l) then (k, a@b)::x'
- else (l,b)::(insert (k, a) x')
-
-fun sort ((k, a)::x) = insert (k, a) (sort x)
- | sort [] = []
-
-fun tree_of_sorted_list [] = Empty
- | tree_of_sorted_list l =
- let
- val len = length l
- val leftlen = (len - 1) div 2
- val left = tree_of_sorted_list (List.take (l, leftlen))
- val rightl = List.drop (l, leftlen)
- val (k, x) = hd rightl
- in
- Node (k, x, left, tree_of_sorted_list (tl rightl))
- end
-
-fun tree_of_list f l = tree_of_sorted_list (map (fn (k, a) => (k, f (k,a))) (sort (map (fn (k, a) => (k, [a])) l)))
-
-fun lookup Empty key = NONE
- | lookup (Node (k, x, left, right)) key =
- if Key.less (key, k) then
- lookup left key
- else if Key.less (k, key) then
- lookup right key
- else
- SOME x
-end;
-
-structure IntBinTree = BinTreeFun (type key = int val less = (op <) val eq = (op = : int * int -> bool));
-
structure AM_Interpreter :> ABSTRACT_MACHINE = struct
datatype term = Var of int | Const of int | App of term * term | Abs of term
@@ -88,16 +28,9 @@
| CApp of closure * closure | CAbs of closure
| Closure of (closure list) * closure
-structure IntPairKey =
-struct
-type key = int * int
-fun less ((x1, y1), (x2, y2)) = x1 < x2 orelse (x1 = x2 andalso y1 < y2)
-fun eq (k1, k2) = (k1 = k2)
-end
+structure prog_struct = TableFun(type key = int*int val ord = prod_ord int_ord int_ord);
-structure prog_struct = BinTreeFun (IntPairKey)
-
-type program = ((pattern * closure) list) prog_struct.t
+type program = ((pattern * closure) list) prog_struct.table
datatype stack = SEmpty | SAppL of closure * stack | SAppR of closure * stack | SAbs of stack
@@ -160,7 +93,7 @@
val eqs = map (fn (p, r) => (check_freevars (count_patternvars p) r;
(pattern_key p, (p, clos_of_term r)))) eqs
in
- prog_struct.tree_of_list (fn (key, rules) => rules) eqs
+ prog_struct.make (map (fn (k, a) => (k, [a])) eqs)
end
fun match_rules n [] clos = NONE
@@ -172,7 +105,7 @@
fun match_closure prog clos =
case len_head_of_closure 0 clos of
(len, CConst c) =>
- (case prog_struct.lookup prog (c, len) of
+ (case prog_struct.lookup (prog, (c, len)) of
NONE => NONE
| SOME rules => match_rules 0 rules clos)
| _ => NONE