- use TableFun instead of homebrew binary tree in am_interpreter.ML
authorobua
Tue Jul 12 21:49:38 2005 +0200 (2005-07-12)
changeset 16782b214f21ae396
parent 16781 663235466562
child 16783 26fccaaf9cb4
- use TableFun instead of homebrew binary tree in am_interpreter.ML
- add Floats to HOL/Real
src/HOL/IsaMakefile
src/HOL/Real/Float.ML
src/HOL/Real/Float.thy
src/HOL/Real/ROOT.ML
src/Pure/Tools/am_compiler.ML
src/Pure/Tools/am_interpreter.ML
     1.1 --- a/src/HOL/IsaMakefile	Tue Jul 12 19:29:52 2005 +0200
     1.2 +++ b/src/HOL/IsaMakefile	Tue Jul 12 21:49:38 2005 +0200
     1.3 @@ -143,6 +143,7 @@
     1.4    Real/Rational.thy Real/PReal.thy Real/RComplete.thy				\
     1.5    Real/ROOT.ML Real/Real.thy Real/real_arith.ML Real/RealDef.thy		\
     1.6    Real/RealPow.thy Real/document/root.tex					\
     1.7 +  Real/Float.thy Real/Float.ML                                                  \
     1.8    Hyperreal/EvenOdd.thy Hyperreal/Fact.thy Hyperreal/HLog.thy			\
     1.9    Hyperreal/Filter.thy Hyperreal/HSeries.thy					\
    1.10    Hyperreal/HTranscendental.thy Hyperreal/HyperArith.thy			\
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Real/Float.ML	Tue Jul 12 21:49:38 2005 +0200
     2.3 @@ -0,0 +1,519 @@
     2.4 +(*  Title: HOL/Real/Float.ML
     2.5 +    ID:    $Id$
     2.6 +    Author: Steven Obua
     2.7 +*)
     2.8 +
     2.9 +structure ExactFloatingPoint :
    2.10 +sig
    2.11 +    exception Destruct_floatstr of string
    2.12 +    val destruct_floatstr : (char -> bool) -> (char -> bool) -> string -> bool * string * string * bool * string
    2.13 +									  
    2.14 +    exception Floating_point of string
    2.15 +				
    2.16 +    type floatrep = IntInf.int * IntInf.int
    2.17 +    val approx_dec_by_bin : IntInf.int -> floatrep -> floatrep * floatrep
    2.18 +    val approx_decstr_by_bin : int -> string -> floatrep * floatrep
    2.19 +end 
    2.20 +=
    2.21 +struct
    2.22 +
    2.23 +fun fst (a,b) = a
    2.24 +fun snd (a,b) = b
    2.25 +
    2.26 +val filter = List.filter;
    2.27 +
    2.28 +exception Destruct_floatstr of string;
    2.29 +
    2.30 +fun destruct_floatstr isDigit isExp number = 
    2.31 +    let
    2.32 +	val numlist = filter (not o Char.isSpace) (String.explode number)
    2.33 +	
    2.34 +	fun countsigns ((#"+")::cs) = countsigns cs
    2.35 +	  | countsigns ((#"-")::cs) = 
    2.36 +	    let	
    2.37 +		val (positive, rest) = countsigns cs 
    2.38 +	    in
    2.39 +		(not positive, rest)
    2.40 +	    end
    2.41 +	  | countsigns cs = (true, cs)
    2.42 +
    2.43 +	fun readdigits [] = ([], [])
    2.44 +	  | readdigits (q as c::cs) = 
    2.45 +	    if (isDigit c) then 
    2.46 +		let
    2.47 +		    val (digits, rest) = readdigits cs
    2.48 +		in
    2.49 +		    (c::digits, rest)
    2.50 +		end
    2.51 +	    else
    2.52 +		([], q)		
    2.53 +
    2.54 +	fun readfromexp_helper cs =
    2.55 +	    let
    2.56 +		val (positive, rest) = countsigns cs
    2.57 +		val (digits, rest') = readdigits rest
    2.58 +	    in
    2.59 +		case rest' of
    2.60 +		    [] => (positive, digits)
    2.61 +		  | _ => raise (Destruct_floatstr number)
    2.62 +	    end	    
    2.63 +
    2.64 +	fun readfromexp [] = (true, [])
    2.65 +	  | readfromexp (c::cs) = 
    2.66 +	    if isExp c then
    2.67 +		readfromexp_helper cs
    2.68 +	    else 
    2.69 +		raise (Destruct_floatstr number)		
    2.70 +
    2.71 +	fun readfromdot [] = ([], readfromexp [])
    2.72 +	  | readfromdot ((#".")::cs) = 
    2.73 +	    let		
    2.74 +		val (digits, rest) = readdigits cs
    2.75 +		val exp = readfromexp rest
    2.76 +	    in
    2.77 +		(digits, exp)
    2.78 +	    end		
    2.79 +	  | readfromdot cs = readfromdot ((#".")::cs)
    2.80 +			    
    2.81 +	val (positive, numlist) = countsigns numlist				 
    2.82 +	val (digits1, numlist) = readdigits numlist				 
    2.83 + 	val (digits2, exp) = readfromdot numlist
    2.84 +    in
    2.85 +	(positive, String.implode digits1, String.implode digits2, fst exp, String.implode (snd exp))
    2.86 +    end
    2.87 +
    2.88 +type floatrep = IntInf.int * IntInf.int
    2.89 +
    2.90 +exception Floating_point of string;
    2.91 +
    2.92 +val ln2_10 = (Math.ln 10.0)/(Math.ln 2.0)
    2.93 +	
    2.94 +fun intmul a b = IntInf.* (a,b)
    2.95 +fun intsub a b = IntInf.- (a,b)	
    2.96 +fun intadd a b = IntInf.+ (a,b) 		 
    2.97 +fun intpow a b = IntInf.pow (a, IntInf.toInt b);
    2.98 +fun intle a b = IntInf.<= (a, b);
    2.99 +fun intless a b = IntInf.< (a, b);
   2.100 +fun intneg a = IntInf.~ a;
   2.101 +val zero = IntInf.fromInt 0;
   2.102 +val one = IntInf.fromInt 1;
   2.103 +val two = IntInf.fromInt 2;
   2.104 +val ten = IntInf.fromInt 10;
   2.105 +val five = IntInf.fromInt 5;
   2.106 +
   2.107 +fun find_most_significant q r = 
   2.108 +    let 
   2.109 +	fun int2real i = 
   2.110 +	    case Real.fromString (IntInf.toString i) of 
   2.111 +		SOME r => r 
   2.112 +	      | NONE => raise (Floating_point "int2real")	
   2.113 +	fun subtract (q, r) (q', r') = 
   2.114 +	    if intle r r' then
   2.115 +		(intsub q (intmul q' (intpow ten (intsub r' r))), r)
   2.116 +	    else
   2.117 +		(intsub (intmul q (intpow ten (intsub r r'))) q', r')
   2.118 +	fun bin2dec d =
   2.119 +	    if intle zero d then 
   2.120 +		(intpow two d, zero)
   2.121 +	    else
   2.122 +		(intpow five (intneg d), d)				
   2.123 +		
   2.124 +	val L = IntInf.fromInt (Real.floor (int2real (IntInf.fromInt (IntInf.log2 q)) + (int2real r) * ln2_10))	
   2.125 +	val L1 = intadd L one
   2.126 +
   2.127 +	val (q1, r1) = subtract (q, r) (bin2dec L1) 		
   2.128 +    in
   2.129 +	if intle zero q1 then 
   2.130 +	    let
   2.131 +		val (q2, r2) = subtract (q, r) (bin2dec (intadd L1 one))
   2.132 +	    in
   2.133 +		if intle zero q2 then 
   2.134 +		    raise (Floating_point "find_most_significant")
   2.135 +		else
   2.136 +		    (L1, (q1, r1))
   2.137 +	    end
   2.138 +	else
   2.139 +	    let
   2.140 +		val (q0, r0) = subtract (q, r) (bin2dec L)
   2.141 +	    in
   2.142 +		if intle zero q0 then
   2.143 +		    (L, (q0, r0))
   2.144 +		else
   2.145 +		    raise (Floating_point "find_most_significant")
   2.146 +	    end		    
   2.147 +    end
   2.148 +
   2.149 +fun approx_dec_by_bin n (q,r) =
   2.150 +    let	
   2.151 +	fun addseq acc d' [] = acc
   2.152 +	  | addseq acc d' (d::ds) = addseq (intadd acc (intpow two (intsub d d'))) d' ds
   2.153 +
   2.154 +	fun seq2bin [] = (zero, zero)
   2.155 +	  | seq2bin (d::ds) = (intadd (addseq zero d ds) one, d)
   2.156 +
   2.157 +	fun approx d_seq d0 precision (q,r) = 
   2.158 +	    if q = zero then 
   2.159 +		let val x = seq2bin d_seq in
   2.160 +		    (x, x)
   2.161 +		end
   2.162 +	    else    
   2.163 +		let 
   2.164 +		    val (d, (q', r')) = find_most_significant q r
   2.165 +		in	
   2.166 +		    if intless precision (intsub d0 d) then 
   2.167 +			let 
   2.168 +			    val d' = intsub d0 precision
   2.169 +			    val x1 = seq2bin (d_seq)
   2.170 +			    val x2 = (intadd (intmul (fst x1) (intpow two (intsub (snd x1) d'))) one,  d') (* = seq2bin (d'::d_seq) *) 
   2.171 +			in
   2.172 +			    (x1, x2)
   2.173 +			end
   2.174 +		    else
   2.175 +			approx (d::d_seq) d0 precision (q', r') 						    		
   2.176 +		end		
   2.177 +	
   2.178 +	fun approx_start precision (q, r) =
   2.179 +	    if q = zero then 
   2.180 +		((zero, zero), (zero, zero))
   2.181 +	    else
   2.182 +		let 
   2.183 +		    val (d, (q', r')) = find_most_significant q r
   2.184 +		in	
   2.185 +		    if intle precision zero then 
   2.186 +			let
   2.187 +			    val x1 = seq2bin [d]
   2.188 +			in
   2.189 +			    if q' = zero then 
   2.190 +				(x1, x1)
   2.191 +			    else
   2.192 +				(x1, seq2bin [intadd d one])
   2.193 +			end
   2.194 +		    else
   2.195 +			approx [d] d precision (q', r')
   2.196 +		end		
   2.197 +    in
   2.198 +	if intle zero q then 
   2.199 +	    approx_start n (q,r)
   2.200 +	else
   2.201 +	    let 
   2.202 +		val ((a1,b1), (a2, b2)) = approx_start n (intneg q, r) 
   2.203 +	    in
   2.204 +		((intneg a2, b2), (intneg a1, b1))
   2.205 +	    end					
   2.206 +    end
   2.207 +
   2.208 +fun approx_decstr_by_bin n decstr =
   2.209 +    let 
   2.210 +	fun str2int s = case IntInf.fromString s of SOME x => x | NONE => zero 
   2.211 +	fun signint p x = if p then x else intneg x
   2.212 +
   2.213 +	val (p, d1, d2, ep, e) = destruct_floatstr Char.isDigit (fn e => e = #"e" orelse e = #"E") decstr
   2.214 +	val s = IntInf.fromInt (size d2)
   2.215 +		
   2.216 +	val q = signint p (intadd (intmul (str2int d1) (intpow ten s)) (str2int d2))
   2.217 +	val r = intsub (signint ep (str2int e)) s
   2.218 +    in
   2.219 +	approx_dec_by_bin (IntInf.fromInt n) (q,r)
   2.220 +    end
   2.221 +
   2.222 +end;
   2.223 +
   2.224 +structure FloatArith = 
   2.225 +struct
   2.226 +
   2.227 +type float = IntInf.int * IntInf.int 
   2.228 +
   2.229 +val izero = IntInf.fromInt 0
   2.230 +val ione = IntInf.fromInt 1
   2.231 +val imone = IntInf.fromInt ~1
   2.232 +val itwo = IntInf.fromInt 2
   2.233 +fun imul a b = IntInf.* (a,b)
   2.234 +fun isub a b = IntInf.- (a,b)
   2.235 +fun iadd a b = IntInf.+ (a,b)
   2.236 +
   2.237 +val floatzero = (izero, izero)
   2.238 +
   2.239 +fun positive_part (a,b) = 
   2.240 +    (if IntInf.< (a,izero) then izero else a, b)
   2.241 +
   2.242 +fun negative_part (a,b) = 
   2.243 +    (if IntInf.< (a,izero) then a else izero, b)
   2.244 +
   2.245 +fun is_negative (a,b) = 
   2.246 +    if IntInf.< (a, izero) then true else false
   2.247 +
   2.248 +fun is_positive (a,b) = 
   2.249 +    if IntInf.< (izero, a) then true else false
   2.250 +
   2.251 +fun is_zero (a,b) = 
   2.252 +    if a = izero then true else false
   2.253 +
   2.254 +fun ipow2 a = IntInf.pow ((IntInf.fromInt 2), IntInf.toInt a)
   2.255 +
   2.256 +fun add (a1, b1) (a2, b2) = 
   2.257 +    if IntInf.< (b1, b2) then
   2.258 +	(iadd a1 (imul a2 (ipow2 (isub b2 b1))), b1)
   2.259 +    else
   2.260 +	(iadd (imul a1 (ipow2 (isub b1 b2))) a2, b2)
   2.261 +
   2.262 +fun sub (a1, b1) (a2, b2) = 
   2.263 +    if IntInf.< (b1, b2) then
   2.264 +	(isub a1 (imul a2 (ipow2 (isub b2 b1))), b1)
   2.265 +    else
   2.266 +	(isub (imul a1 (ipow2 (isub b1 b2))) a2, b2)
   2.267 +
   2.268 +fun neg (a, b) = (IntInf.~ a, b)
   2.269 +
   2.270 +fun is_equal a b = is_zero (sub a b)
   2.271 +
   2.272 +fun is_less a b = is_negative (sub a b)
   2.273 +
   2.274 +fun max a b = if is_less a b then b else a
   2.275 +
   2.276 +fun min a b = if is_less a b then a else b
   2.277 +
   2.278 +fun abs a = if is_negative a then neg a else a
   2.279 +
   2.280 +fun mul (a1, b1) (a2, b2) = (imul a1 a2, iadd b1 b2)
   2.281 +
   2.282 +end;
   2.283 +
   2.284 +
   2.285 +structure Float:
   2.286 +sig
   2.287 +    type float = FloatArith.float
   2.288 +    type floatfunc = float * float -> float * float
   2.289 +
   2.290 +    val mk_intinf : typ -> IntInf.int -> term
   2.291 +    val mk_float : float -> term
   2.292 +
   2.293 +    exception Dest_intinf;
   2.294 +    val dest_intinf : term -> IntInf.int
   2.295 +    val dest_nat : term -> IntInf.int
   2.296 +    
   2.297 +    exception Dest_float;
   2.298 +    val dest_float : term -> float
   2.299 +
   2.300 +    val float_const : term
   2.301 +
   2.302 +    val float_add_const : term
   2.303 +    val float_diff_const : term
   2.304 +    val float_uminus_const : term
   2.305 +    val float_pprt_const : term
   2.306 +    val float_nprt_const : term
   2.307 +    val float_abs_const : term
   2.308 +    val float_mult_const : term 
   2.309 +    val float_le_const : term
   2.310 +
   2.311 +    val nat_le_const : term
   2.312 +    val nat_less_const : term
   2.313 +    val nat_eq_const : term
   2.314 +
   2.315 +    val approx_float : int -> floatfunc -> string -> term * term
   2.316 +
   2.317 +    val sign_term : term -> cterm
   2.318 +
   2.319 +(*    exception Float_op_oracle_data of term
   2.320 +    exception Nat_op_oracle_data of term
   2.321 +
   2.322 +    val float_op_oracle : Sign.sg * exn -> term
   2.323 +    val nat_op_oracle : Sign.sg * exn -> term
   2.324 +
   2.325 +    val invoke_float_op : term -> thm
   2.326 +    val invoke_nat_op : term -> thm*)
   2.327 +end 
   2.328 += 
   2.329 +struct
   2.330 +
   2.331 +structure Inttab = TableFun(type key = int val ord = (rev_order o int_ord));
   2.332 +
   2.333 +type float = IntInf.int*IntInf.int
   2.334 +type floatfunc = float*float -> float*float
   2.335 +
   2.336 +val th = theory "Float"
   2.337 +val sg = sign_of th
   2.338 +		
   2.339 +val float_const = Const ("Float.float", HOLogic.mk_prodT (HOLogic.intT, HOLogic.intT) --> HOLogic.realT)
   2.340 +
   2.341 +val float_add_const = Const ("op +", HOLogic.realT --> HOLogic.realT --> HOLogic.realT)
   2.342 +val float_diff_const = Const ("op -", HOLogic.realT --> HOLogic.realT --> HOLogic.realT)
   2.343 +val float_mult_const = Const ("op *", HOLogic.realT --> HOLogic.realT --> HOLogic.realT)
   2.344 +val float_uminus_const = Const ("uminus", HOLogic.realT --> HOLogic.realT)
   2.345 +val float_abs_const = Const ("HOL.abs", HOLogic.realT --> HOLogic.realT)
   2.346 +val float_le_const = Const ("op <=", HOLogic.realT --> HOLogic.realT --> HOLogic.boolT)
   2.347 +val float_pprt_const = Const ("OrderedGroup.pprt", HOLogic.realT --> HOLogic.realT)
   2.348 +val float_nprt_const = Const ("OrderedGroup.nprt", HOLogic.realT --> HOLogic.realT)
   2.349 +
   2.350 +val nat_le_const = Const ("op <=", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT)
   2.351 +val nat_less_const = Const ("op <", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT)
   2.352 +val nat_eq_const = Const ("op =", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT)
   2.353 + 		  
   2.354 +val zero = FloatArith.izero
   2.355 +val minus_one = FloatArith.imone
   2.356 +val two = FloatArith.itwo
   2.357 +	  
   2.358 +exception Dest_intinf;
   2.359 +exception Dest_float;
   2.360 +
   2.361 +fun mk_intinf ty n =
   2.362 +    let
   2.363 +	fun mk_bit n = if n = zero then HOLogic.false_const else HOLogic.true_const
   2.364 +								 
   2.365 +	fun bin_of n = 
   2.366 +	    if n = zero then HOLogic.pls_const
   2.367 +	    else if n = minus_one then HOLogic.min_const
   2.368 +	    else 
   2.369 +		let 
   2.370 +		    (*val (q,r) = IntInf.divMod (n, two): doesn't work in SML 10.0.7, but in newer versions!!!*)
   2.371 +	            val q = IntInf.div (n, two)
   2.372 +		    val r = IntInf.mod (n, two)
   2.373 +		in
   2.374 +		    HOLogic.bit_const $ bin_of q $ mk_bit r
   2.375 +		end
   2.376 +    in 
   2.377 +	HOLogic.number_of_const ty $ (bin_of n)
   2.378 +    end
   2.379 +
   2.380 +fun dest_intinf n = 
   2.381 +    let
   2.382 +	fun dest_bit n = 
   2.383 +	    case n of 
   2.384 +		Const ("False", _) => FloatArith.izero
   2.385 +	      | Const ("True", _) => FloatArith.ione
   2.386 +	      | _ => raise Dest_intinf
   2.387 +			 
   2.388 +	fun int_of n = 
   2.389 +	    case n of
   2.390 +		Const ("Numeral.Pls", _) => FloatArith.izero
   2.391 +	      | Const ("Numeral.Min", _) => FloatArith.imone
   2.392 +	      | Const ("Numeral.Bit", _) $ q $ r => FloatArith.iadd (FloatArith.imul (int_of q) FloatArith.itwo) (dest_bit r)
   2.393 +	      | _ => raise Dest_intinf
   2.394 +    in
   2.395 +	case n of 
   2.396 +	    Const ("Numeral.number_of", _) $ n' => int_of n'
   2.397 +	  | Const ("Numeral0", _) => FloatArith.izero
   2.398 +	  | Const ("Numeral1", _) => FloatArith.ione    
   2.399 +	  | _ => raise Dest_intinf
   2.400 +    end
   2.401 +
   2.402 +fun mk_float (a,b) = 
   2.403 +    float_const $ (HOLogic.mk_prod ((mk_intinf HOLogic.intT a), (mk_intinf HOLogic.intT b)))
   2.404 +
   2.405 +fun dest_float f = 
   2.406 +    case f of 
   2.407 +	(Const ("Float.float", _) $ (Const ("Pair", _) $ a $ b)) => (dest_intinf a, dest_intinf b)
   2.408 +      | Const ("Numeral.number_of",_) $ a => (dest_intinf f, 0)
   2.409 +      | Const ("Numeral0", _) => (FloatArith.izero, FloatArith.izero)
   2.410 +      | Const ("Numeral1", _) => (FloatArith.ione, FloatArith.izero)
   2.411 +      | _ => raise Dest_float
   2.412 +
   2.413 +fun dest_nat n = 
   2.414 +    let 
   2.415 +	val v = dest_intinf n
   2.416 +    in
   2.417 +	if IntInf.< (v, FloatArith.izero) then
   2.418 +	    FloatArith.izero
   2.419 +	else
   2.420 +	    v
   2.421 +    end
   2.422 +
   2.423 +fun approx_float prec f value = 
   2.424 +    let
   2.425 +	val interval = ExactFloatingPoint.approx_decstr_by_bin prec value
   2.426 +	val (flower, fupper) = f interval
   2.427 +    in
   2.428 +	(mk_float flower, mk_float fupper)
   2.429 +    end
   2.430 +
   2.431 +fun sign_term t = cterm_of sg t
   2.432 +
   2.433 +(*exception Float_op_oracle_data of term;
   2.434 +
   2.435 +fun float_op_oracle (sg, exn as Float_op_oracle_data t) =
   2.436 +    Logic.mk_equals (t,
   2.437 +		     case t of 
   2.438 +			 f $ a $ b => 
   2.439 +			 let 
   2.440 +			     val a' = dest_float a 
   2.441 +			     val b' = dest_float b
   2.442 +			 in
   2.443 +			     if f = float_add_const then
   2.444 +				 mk_float (FloatArith.add a' b')	
   2.445 +			     else if f = float_diff_const then
   2.446 +				 mk_float (FloatArith.sub a' b')
   2.447 +			     else if f = float_mult_const then
   2.448 +				 mk_float (FloatArith.mul a' b')		
   2.449 +			     else if f = float_le_const then
   2.450 +				 (if FloatArith.is_less b' a' then
   2.451 +				     HOLogic.false_const
   2.452 +				 else
   2.453 +				     HOLogic.true_const)
   2.454 +			     else raise exn	    		    	       
   2.455 +			 end
   2.456 +		       | f $ a => 
   2.457 +			 let
   2.458 +			     val a' = dest_float a
   2.459 +			 in
   2.460 +			     if f = float_uminus_const then
   2.461 +				 mk_float (FloatArith.neg a')
   2.462 +			     else if f = float_abs_const then
   2.463 +				 mk_float (FloatArith.abs a')
   2.464 +			     else if f = float_pprt_const then
   2.465 +				 mk_float (FloatArith.positive_part a')
   2.466 +			     else if f = float_nprt_const then
   2.467 +				 mk_float (FloatArith.negative_part a')
   2.468 +			     else
   2.469 +				 raise exn
   2.470 +			 end
   2.471 +		       | _ => raise exn
   2.472 +		    )
   2.473 +val th = ref ([]: Theory.theory list)
   2.474 +val sg = ref ([]: Sign.sg list)
   2.475 +
   2.476 +fun invoke_float_op c = 
   2.477 +    let
   2.478 +	val th = (if length(!th) = 0 then th := [theory "MatrixLP"] else (); hd (!th))
   2.479 +	val sg = (if length(!sg) = 0 then sg := [sign_of th] else (); hd (!sg))
   2.480 +    in
   2.481 +	invoke_oracle th "float_op" (sg, Float_op_oracle_data c)
   2.482 +    end
   2.483 +
   2.484 +exception Nat_op_oracle_data of term;
   2.485 +
   2.486 +fun nat_op_oracle (sg, exn as Nat_op_oracle_data t) =
   2.487 +    Logic.mk_equals (t,
   2.488 +		     case t of 
   2.489 +			 f $ a $ b => 
   2.490 +			 let 
   2.491 +			     val a' = dest_nat a 
   2.492 +			     val b' = dest_nat b
   2.493 +			 in
   2.494 +			     if f = nat_le_const then
   2.495 +				 (if IntInf.<= (a', b') then
   2.496 +				     HOLogic.true_const
   2.497 +				 else
   2.498 +				     HOLogic.false_const)
   2.499 +			     else if f = nat_eq_const then
   2.500 +				 (if a' = b' then 
   2.501 +				      HOLogic.true_const
   2.502 +				  else
   2.503 +				      HOLogic.false_const)
   2.504 +			     else if f = nat_less_const then
   2.505 +				 (if IntInf.< (a', b') then
   2.506 +				      HOLogic.true_const
   2.507 +				  else
   2.508 +				      HOLogic.false_const)
   2.509 +			     else 
   2.510 +				 raise exn	    	
   2.511 +			 end
   2.512 +		       | _ => raise exn)
   2.513 +
   2.514 +fun invoke_nat_op c = 
   2.515 +    let
   2.516 +	val th = (if length (!th) = 0 then th := [theory "MatrixLP"] else (); hd (!th))
   2.517 +	val sg = (if length (!sg) = 0 then sg := [sign_of th] else (); hd (!sg))
   2.518 +    in
   2.519 +	invoke_oracle th "nat_op" (sg, Nat_op_oracle_data c)
   2.520 +    end
   2.521 +*)
   2.522 +end;
   2.523 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Real/Float.thy	Tue Jul 12 21:49:38 2005 +0200
     3.3 @@ -0,0 +1,525 @@
     3.4 +(*  Title: HOL/Real/Float.thy
     3.5 +    ID:    $Id$
     3.6 +    Author: Steven Obua
     3.7 +*)
     3.8 +
     3.9 +theory Float = Real:
    3.10 +
    3.11 +constdefs  
    3.12 +  pow2 :: "int \<Rightarrow> real"
    3.13 +  "pow2 a == if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a))))" 
    3.14 +  float :: "int * int \<Rightarrow> real"
    3.15 +  "float x == (real (fst x)) * (pow2 (snd x))"
    3.16 +
    3.17 +lemma pow2_0[simp]: "pow2 0 = 1"
    3.18 +by (simp add: pow2_def)
    3.19 +
    3.20 +lemma pow2_1[simp]: "pow2 1 = 2"
    3.21 +by (simp add: pow2_def)
    3.22 +
    3.23 +lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
    3.24 +by (simp add: pow2_def)
    3.25 +
    3.26 +lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)" 
    3.27 +proof -
    3.28 +  have h: "! n. nat (2 + int n) - Suc 0 = nat (1 + int n)" by arith
    3.29 +  have g: "! a b. a - -1 = a + (1::int)" by arith
    3.30 +  have pos: "! n. pow2 (int n + 1) = 2 * pow2 (int n)"
    3.31 +    apply (auto, induct_tac n)
    3.32 +    apply (simp_all add: pow2_def)
    3.33 +    apply (rule_tac m1="2" and n1="nat (2 + int na)" in ssubst[OF realpow_num_eq_if])
    3.34 +    apply (auto simp add: h)
    3.35 +    apply arith
    3.36 +    done  
    3.37 +  show ?thesis
    3.38 +  proof (induct a)
    3.39 +    case (1 n)
    3.40 +    from pos show ?case by (simp add: ring_eq_simps)
    3.41 +  next
    3.42 +    case (2 n)
    3.43 +    show ?case
    3.44 +      apply (auto)
    3.45 +      apply (subst pow2_neg[of "- int n"])
    3.46 +      apply (subst pow2_neg[of "-1 - int n"])
    3.47 +      apply (auto simp add: g pos)
    3.48 +      done
    3.49 +  qed  
    3.50 +qed
    3.51 +  
    3.52 +lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
    3.53 +proof (induct b)
    3.54 +  case (1 n) 
    3.55 +  show ?case
    3.56 +  proof (induct n)
    3.57 +    case 0
    3.58 +    show ?case by simp
    3.59 +  next
    3.60 +    case (Suc m)
    3.61 +    show ?case by (auto simp add: ring_eq_simps pow2_add1 prems)
    3.62 +  qed
    3.63 +next
    3.64 +  case (2 n)
    3.65 +  show ?case 
    3.66 +  proof (induct n)
    3.67 +    case 0
    3.68 +    show ?case 
    3.69 +      apply (auto)
    3.70 +      apply (subst pow2_neg[of "a + -1"])
    3.71 +      apply (subst pow2_neg[of "-1"])
    3.72 +      apply (simp)
    3.73 +      apply (insert pow2_add1[of "-a"])
    3.74 +      apply (simp add: ring_eq_simps)
    3.75 +      apply (subst pow2_neg[of "-a"])
    3.76 +      apply (simp)
    3.77 +      done
    3.78 +    case (Suc m)
    3.79 +    have a: "int m - (a + -2) =  1 + (int m - a + 1)" by arith	
    3.80 +    have b: "int m - -2 = 1 + (int m + 1)" by arith
    3.81 +    show ?case
    3.82 +      apply (auto)
    3.83 +      apply (subst pow2_neg[of "a + (-2 - int m)"])
    3.84 +      apply (subst pow2_neg[of "-2 - int m"])
    3.85 +      apply (auto simp add: ring_eq_simps)
    3.86 +      apply (subst a)
    3.87 +      apply (subst b)
    3.88 +      apply (simp only: pow2_add1)
    3.89 +      apply (subst pow2_neg[of "int m - a + 1"])
    3.90 +      apply (subst pow2_neg[of "int m + 1"])
    3.91 +      apply auto
    3.92 +      apply (insert prems)
    3.93 +      apply (auto simp add: ring_eq_simps)
    3.94 +      done
    3.95 +  qed
    3.96 +qed
    3.97 +
    3.98 +lemma "float (a, e) + float (b, e) = float (a + b, e)"  
    3.99 +by (simp add: float_def ring_eq_simps)
   3.100 +
   3.101 +constdefs 
   3.102 +  int_of_real :: "real \<Rightarrow> int"
   3.103 +  "int_of_real x == SOME y. real y = x"  
   3.104 +  real_is_int :: "real \<Rightarrow> bool"
   3.105 +  "real_is_int x == ? (u::int). x = real u" 
   3.106 +
   3.107 +lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
   3.108 +by (auto simp add: real_is_int_def int_of_real_def)
   3.109 +
   3.110 +lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
   3.111 +by (simp add: float_def real_is_int_def2 pow2_add[symmetric])
   3.112 +
   3.113 +lemma pow2_int: "pow2 (int c) = (2::real)^c"
   3.114 +by (simp add: pow2_def)
   3.115 +
   3.116 +lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)" 
   3.117 +by (simp add: float_def pow2_int[symmetric] pow2_add[symmetric])
   3.118 +
   3.119 +lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
   3.120 +by (auto simp add: real_is_int_def int_of_real_def)
   3.121 +
   3.122 +lemma int_of_real_real[simp]: "int_of_real (real x) = x"
   3.123 +by (simp add: int_of_real_def)
   3.124 +
   3.125 +lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
   3.126 +by (auto simp add: int_of_real_def real_is_int_def)
   3.127 +
   3.128 +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)"
   3.129 +by (auto simp add: int_of_real_def real_is_int_def)
   3.130 +
   3.131 +lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
   3.132 +apply (subst real_is_int_def2)
   3.133 +apply (simp add: real_is_int_add_int_of_real real_int_of_real)
   3.134 +done
   3.135 +
   3.136 +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)"
   3.137 +by (auto simp add: int_of_real_def real_is_int_def)
   3.138 +
   3.139 +lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
   3.140 +apply (subst real_is_int_def2)
   3.141 +apply (simp add: int_of_real_sub real_int_of_real)
   3.142 +done
   3.143 +
   3.144 +lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
   3.145 +by (auto simp add: real_is_int_def)
   3.146 +
   3.147 +lemma int_of_real_mult: 
   3.148 +  assumes "real_is_int a" "real_is_int b"
   3.149 +  shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
   3.150 +proof -
   3.151 +  from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
   3.152 +  from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
   3.153 +  from a obtain a'::int where a':"a = real a'" by auto
   3.154 +  from b obtain b'::int where b':"b = real b'" by auto
   3.155 +  have r: "real a' * real b' = real (a' * b')" by auto
   3.156 +  show ?thesis
   3.157 +    apply (simp add: a' b')
   3.158 +    apply (subst r)
   3.159 +    apply (simp only: int_of_real_real)
   3.160 +    done
   3.161 +qed
   3.162 +
   3.163 +lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
   3.164 +apply (subst real_is_int_def2)
   3.165 +apply (simp add: int_of_real_mult)
   3.166 +done
   3.167 +
   3.168 +lemma real_is_int_0[simp]: "real_is_int (0::real)"
   3.169 +by (simp add: real_is_int_def int_of_real_def)
   3.170 +
   3.171 +lemma real_is_int_1[simp]: "real_is_int (1::real)"
   3.172 +proof -
   3.173 +  have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
   3.174 +  also have "\<dots> = True" by (simp only: real_is_int_real)
   3.175 +  ultimately show ?thesis by auto
   3.176 +qed
   3.177 +
   3.178 +lemma real_is_int_n1: "real_is_int (-1::real)"
   3.179 +proof -
   3.180 +  have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
   3.181 +  also have "\<dots> = True" by (simp only: real_is_int_real)
   3.182 +  ultimately show ?thesis by auto
   3.183 +qed
   3.184 +
   3.185 +lemma real_is_int_number_of[simp]: "real_is_int ((number_of::bin\<Rightarrow>real) x)"
   3.186 +proof -
   3.187 +  have neg1: "real_is_int (-1::real)"
   3.188 +  proof -
   3.189 +    have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
   3.190 +    also have "\<dots> = True" by (simp only: real_is_int_real)
   3.191 +    ultimately show ?thesis by auto
   3.192 +  qed
   3.193 +  
   3.194 +  { 
   3.195 +    fix x::int
   3.196 +    have "!! y. real_is_int ((number_of::bin\<Rightarrow>real) (Abs_Bin x))"
   3.197 +      apply (simp add: number_of_eq)
   3.198 +      apply (subst Abs_Bin_inverse)
   3.199 +      apply (simp add: Bin_def)
   3.200 +      apply (induct x)
   3.201 +      apply (induct_tac n)
   3.202 +      apply (simp)
   3.203 +      apply (simp)
   3.204 +      apply (induct_tac n)
   3.205 +      apply (simp add: neg1)
   3.206 +    proof -
   3.207 +      fix n :: nat
   3.208 +      assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
   3.209 +      have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
   3.210 +      show "real_is_int (of_int (- (int (Suc (Suc n)))))"
   3.211 +	apply (simp only: s of_int_add)
   3.212 +	apply (rule real_is_int_add)
   3.213 +	apply (simp add: neg1)
   3.214 +	apply (simp only: rn)
   3.215 +	done
   3.216 +    qed
   3.217 +  }
   3.218 +  note Abs_Bin = this
   3.219 +  {
   3.220 +    fix x :: bin
   3.221 +    have "? u. x = Abs_Bin u"
   3.222 +      apply (rule exI[where x = "Rep_Bin x"])
   3.223 +      apply (simp add: Rep_Bin_inverse)
   3.224 +      done
   3.225 +  }
   3.226 +  then obtain u::int where "x = Abs_Bin u" by auto
   3.227 +  with Abs_Bin show ?thesis by auto
   3.228 +qed
   3.229 +
   3.230 +lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
   3.231 +by (simp add: int_of_real_def)
   3.232 +
   3.233 +lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
   3.234 +proof - 
   3.235 +  have 1: "(1::real) = real (1::int)" by auto
   3.236 +  show ?thesis by (simp only: 1 int_of_real_real)
   3.237 +qed
   3.238 +
   3.239 +lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
   3.240 +proof -
   3.241 +  have "real_is_int (number_of b)" by simp
   3.242 +  then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
   3.243 +  then obtain u::int where u:"number_of b = real u" by auto
   3.244 +  have "number_of b = real ((number_of b)::int)" 
   3.245 +    by (simp add: number_of_eq real_of_int_def)
   3.246 +  have ub: "number_of b = real ((number_of b)::int)" 
   3.247 +    by (simp add: number_of_eq real_of_int_def)
   3.248 +  from uu u ub have unb: "u = number_of b"
   3.249 +    by blast
   3.250 +  have "int_of_real (number_of b) = u" by (simp add: u)
   3.251 +  with unb show ?thesis by simp
   3.252 +qed
   3.253 +
   3.254 +lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
   3.255 +  apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
   3.256 +  apply (simp_all add: pow2_def even_def real_is_int_def ring_eq_simps)
   3.257 +  apply (auto)
   3.258 +proof -
   3.259 +  fix q::int
   3.260 +  have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
   3.261 +  show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))" 
   3.262 +    by (simp add: a)
   3.263 +qed
   3.264 +    
   3.265 +consts
   3.266 +  norm_float :: "int*int \<Rightarrow> int*int"
   3.267 +
   3.268 +lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
   3.269 +apply (subst split_div, auto)
   3.270 +apply (subst split_zdiv, auto)
   3.271 +apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)
   3.272 +apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
   3.273 +done
   3.274 +
   3.275 +lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
   3.276 +apply (subst split_mod, auto)
   3.277 +apply (subst split_zmod, auto)
   3.278 +apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia in IntDiv.unique_remainder)
   3.279 +apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
   3.280 +done
   3.281 +
   3.282 +lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
   3.283 +by arith
   3.284 +
   3.285 +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>"
   3.286 +apply (auto)
   3.287 +apply (rule abs_div_2_less)
   3.288 +apply (auto)
   3.289 +done
   3.290 +
   3.291 +ML {* simp_depth_limit := 2 *} 
   3.292 +recdef norm_float "measure (% (a,b). nat (abs a))"
   3.293 +  "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)))"
   3.294 +(hints simp: terminating_norm_float)
   3.295 +ML {* simp_depth_limit := 1000 *}
   3.296 +
   3.297 +lemma norm_float: "float x = float (norm_float x)"
   3.298 +proof -
   3.299 +  {
   3.300 +    fix a b :: int 
   3.301 +    have norm_float_pair: "float (a,b) = float (norm_float (a,b))" 
   3.302 +    proof (induct a b rule: norm_float.induct)
   3.303 +      case (1 u v)
   3.304 +      show ?case 
   3.305 +      proof cases
   3.306 +	assume u: "u \<noteq> 0 \<and> even u"
   3.307 +	with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2, v + 1))" by auto
   3.308 +	with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even) 
   3.309 +	then show ?thesis
   3.310 +	  apply (subst norm_float.simps)
   3.311 +	  apply (simp add: ind)
   3.312 +	  done
   3.313 +      next
   3.314 +	assume "~(u \<noteq> 0 \<and> even u)"
   3.315 +	then show ?thesis
   3.316 +	  by (simp add: prems float_def)
   3.317 +      qed
   3.318 +    qed
   3.319 +  }
   3.320 +  note helper = this
   3.321 +  have "? a b. x = (a,b)" by auto
   3.322 +  then obtain a b where "x = (a, b)" by blast
   3.323 +  then show ?thesis by (simp only: helper)
   3.324 +qed
   3.325 +
   3.326 +lemma pow2_int: "pow2 (int n) = 2^n"
   3.327 +  by (simp add: pow2_def)
   3.328 +
   3.329 +lemma float_add: 
   3.330 +  "float (a1, e1) + float (a2, e2) = 
   3.331 +  (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1) 
   3.332 +  else float (a1*2^(nat (e1-e2))+a2, e2))"
   3.333 +  apply (simp add: float_def ring_eq_simps)
   3.334 +  apply (auto simp add: pow2_int[symmetric] pow2_add[symmetric])
   3.335 +  done
   3.336 +
   3.337 +lemma float_mult:
   3.338 +  "float (a1, e1) * float (a2, e2) = 
   3.339 +  (float (a1 * a2, e1 + e2))"
   3.340 +  by (simp add: float_def pow2_add)
   3.341 +
   3.342 +lemma float_minus:
   3.343 +  "- (float (a,b)) = float (-a, b)"
   3.344 +  by (simp add: float_def)
   3.345 +
   3.346 +lemma zero_less_pow2:
   3.347 +  "0 < pow2 x"
   3.348 +proof -
   3.349 +  {
   3.350 +    fix y
   3.351 +    have "0 <= y \<Longrightarrow> 0 < pow2 y"    
   3.352 +      by (induct y, induct_tac n, simp_all add: pow2_add)
   3.353 +  }
   3.354 +  note helper=this
   3.355 +  show ?thesis
   3.356 +    apply (case_tac "0 <= x")
   3.357 +    apply (simp add: helper)
   3.358 +    apply (subst pow2_neg)
   3.359 +    apply (simp add: helper)
   3.360 +    done
   3.361 +qed
   3.362 +
   3.363 +lemma zero_le_float:
   3.364 +  "(0 <= float (a,b)) = (0 <= a)"
   3.365 +  apply (auto simp add: float_def)
   3.366 +  apply (auto simp add: zero_le_mult_iff zero_less_pow2) 
   3.367 +  apply (insert zero_less_pow2[of b])
   3.368 +  apply (simp_all)
   3.369 +  done
   3.370 +
   3.371 +lemma float_le_zero:
   3.372 +  "(float (a,b) <= 0) = (a <= 0)"
   3.373 +  apply (auto simp add: float_def)
   3.374 +  apply (auto simp add: mult_le_0_iff)
   3.375 +  apply (insert zero_less_pow2[of b])
   3.376 +  apply auto
   3.377 +  done
   3.378 +
   3.379 +lemma float_abs:
   3.380 +  "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
   3.381 +  apply (auto simp add: abs_if)
   3.382 +  apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
   3.383 +  done
   3.384 +
   3.385 +lemma float_zero:
   3.386 +  "float (0, b) = 0"
   3.387 +  by (simp add: float_def)
   3.388 +
   3.389 +lemma float_pprt:
   3.390 +  "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
   3.391 +  by (auto simp add: zero_le_float float_le_zero float_zero)
   3.392 +
   3.393 +lemma float_nprt:
   3.394 +  "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
   3.395 +  by (auto simp add: zero_le_float float_le_zero float_zero)
   3.396 +
   3.397 +lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
   3.398 +  by auto
   3.399 +  
   3.400 +lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
   3.401 +  by simp
   3.402 +
   3.403 +lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
   3.404 +  by simp
   3.405 +
   3.406 +lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
   3.407 +  by simp
   3.408 +
   3.409 +lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
   3.410 +  by simp
   3.411 +
   3.412 +lemma int_pow_0: "(a::int)^(Numeral0) = 1"
   3.413 +  by simp
   3.414 +
   3.415 +lemma int_pow_1: "(a::int)^(Numeral1) = a"
   3.416 +  by simp
   3.417 +
   3.418 +lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
   3.419 +  by simp
   3.420 +
   3.421 +lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
   3.422 +  by simp
   3.423 +
   3.424 +lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
   3.425 +  by simp
   3.426 +
   3.427 +lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
   3.428 +  by simp
   3.429 +
   3.430 +lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
   3.431 +  by simp
   3.432 +
   3.433 +lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
   3.434 +proof -
   3.435 +  have 1:"((-1)::nat) = 0"
   3.436 +    by simp
   3.437 +  show ?thesis by (simp add: 1)
   3.438 +qed
   3.439 +
   3.440 +lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
   3.441 +  by simp
   3.442 +
   3.443 +lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
   3.444 +  by simp
   3.445 +
   3.446 +lemma lift_bool: "x \<Longrightarrow> x=True"
   3.447 +  by simp
   3.448 +
   3.449 +lemma nlift_bool: "~x \<Longrightarrow> x=False"
   3.450 +  by simp
   3.451 +
   3.452 +lemma not_false_eq_true: "(~ False) = True" by simp
   3.453 +
   3.454 +lemma not_true_eq_false: "(~ True) = False" by simp
   3.455 +
   3.456 +
   3.457 +lemmas binarith = 
   3.458 +  Pls_0_eq Min_1_eq
   3.459 +  bin_pred_Pls bin_pred_Min bin_pred_1 bin_pred_0     
   3.460 +  bin_succ_Pls bin_succ_Min bin_succ_1 bin_succ_0
   3.461 +  bin_add_Pls bin_add_Min bin_add_BIT_0 bin_add_BIT_10
   3.462 +  bin_add_BIT_11 bin_minus_Pls bin_minus_Min bin_minus_1 
   3.463 +  bin_minus_0 bin_mult_Pls bin_mult_Min bin_mult_1 bin_mult_0 
   3.464 +  bin_add_Pls_right bin_add_Min_right
   3.465 +
   3.466 +lemma int_eq_number_of_eq: "(((number_of v)::int)=(number_of w)) = iszero ((number_of (bin_add v (bin_minus w)))::int)"
   3.467 +  by simp
   3.468 +
   3.469 +lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)" 
   3.470 +  by (simp only: iszero_number_of_Pls)
   3.471 +
   3.472 +lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
   3.473 +  by simp
   3.474 +
   3.475 +lemma int_iszero_number_of_0: "iszero ((number_of (w BIT bit.B0))::int) = iszero ((number_of w)::int)"
   3.476 +  by simp
   3.477 +
   3.478 +lemma int_iszero_number_of_1: "\<not> iszero ((number_of (w BIT bit.B1))::int)" 
   3.479 +  by simp
   3.480 +
   3.481 +lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (bin_add x (bin_minus y)))::int)"
   3.482 +  by simp
   3.483 +
   3.484 +lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))" 
   3.485 +  by simp
   3.486 +
   3.487 +lemma int_neg_number_of_Min: "neg (-1::int)"
   3.488 +  by simp
   3.489 +
   3.490 +lemma int_neg_number_of_BIT: "neg ((number_of (w BIT x))::int) = neg ((number_of w)::int)"
   3.491 +  by simp
   3.492 +
   3.493 +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))"
   3.494 +  by simp
   3.495 +
   3.496 +lemmas intarithrel = 
   3.497 +  int_eq_number_of_eq 
   3.498 +  lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_0 
   3.499 +  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]
   3.500 +  int_neg_number_of_BIT int_le_number_of_eq
   3.501 +
   3.502 +lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (bin_add v w)"
   3.503 +  by simp
   3.504 +
   3.505 +lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (bin_add v (bin_minus w))"
   3.506 +  by simp
   3.507 +
   3.508 +lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (bin_mult v w)"
   3.509 +  by simp
   3.510 +
   3.511 +lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (bin_minus v)"
   3.512 +  by simp
   3.513 +
   3.514 +lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
   3.515 +
   3.516 +lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
   3.517 +
   3.518 +lemmas powerarith = nat_number_of zpower_number_of_even 
   3.519 +  zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]   
   3.520 +  zpower_Pls zpower_Min
   3.521 +
   3.522 +lemmas floatarith[simplified norm_0_1] = float_add float_mult float_minus float_abs zero_le_float float_pprt float_nprt
   3.523 +
   3.524 +(* for use with the compute oracle *)
   3.525 +lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
   3.526 +
   3.527 +end
   3.528 + 
     4.1 --- a/src/HOL/Real/ROOT.ML	Tue Jul 12 19:29:52 2005 +0200
     4.2 +++ b/src/HOL/Real/ROOT.ML	Tue Jul 12 21:49:38 2005 +0200
     4.3 @@ -8,3 +8,4 @@
     4.4  *)
     4.5  
     4.6  time_use_thy "Real";
     4.7 +use_thy "Float";
     4.8 \ No newline at end of file
     5.1 --- a/src/Pure/Tools/am_compiler.ML	Tue Jul 12 19:29:52 2005 +0200
     5.2 +++ b/src/Pure/Tools/am_compiler.ML	Tue Jul 12 21:49:38 2005 +0200
     5.3 @@ -11,10 +11,15 @@
     5.4  		     | CApp of closure * closure | CAbs of closure | Closure of (closure list) * closure
     5.5  
     5.6      val set_compiled_rewriter : (term -> closure) -> unit
     5.7 +    val list_nth : 'a list * int -> 'a
     5.8 +    val list_map : ('a -> 'b) -> 'a list -> 'b list
     5.9  end
    5.10  
    5.11  structure AM_Compiler :> COMPILING_AM = struct
    5.12  
    5.13 +val list_nth = List.nth;
    5.14 +val list_map = map;
    5.15 +
    5.16  datatype term = Var of int | Const of int | App of term * term | Abs of term
    5.17  
    5.18  datatype pattern = PVar | PConst of int * (pattern list)
    5.19 @@ -133,7 +138,7 @@
    5.20  		"and weak stack (Closure (e, App (a, b))) = weak (SAppL (Closure (e, b), stack)) (Closure (e, a))",
    5.21  		"  | weak (SAppL (b, stack)) (Closure (e, Abs m)) =  weak stack (Closure (b::e, m))",
    5.22  		"  | weak stack (clos as Closure (_, Abs _)) = weak_last stack clos",
    5.23 -		"  | weak stack (Closure (e, Var n)) = weak stack (List.nth (e, n) handle Subscript => (Var (n-(length e))))",
    5.24 +		"  | weak stack (Closure (e, Var n)) = weak stack ("^sname^".list_nth (e, n) handle _ => (Var (n-(length e))))",
    5.25  		"  | weak stack (Closure (e, c)) = weak stack c",
    5.26  		"  | weak stack clos = lookup stack clos",
    5.27  		"and weak_last (SAppR (a, stack)) b = weak stack (App(a, b))",
    5.28 @@ -177,7 +182,7 @@
    5.29  		"  | exportTerm (Const c) = "^sname^".CConst c",
    5.30  		"  | exportTerm (App (a,b)) = "^sname^".CApp (exportTerm a, exportTerm b)",
    5.31  		"  | exportTerm (Abs m) = "^sname^".CAbs (exportTerm m)",
    5.32 -		"  | exportTerm (Closure (closlist, clos)) = "^sname^".Closure (map exportTerm closlist, exportTerm clos)"]
    5.33 +		"  | exportTerm (Closure (closlist, clos)) = "^sname^".Closure ("^sname^".list_map exportTerm closlist, exportTerm clos)"]
    5.34  	val _ = writelist (map ec constants)
    5.35  		
    5.36  	val _ = writelist [
    5.37 @@ -199,9 +204,9 @@
    5.38  
    5.39  	val _ = 
    5.40  	    let
    5.41 -		val fout = TextIO.openOut "gen_code.ML"
    5.42 +		(*val fout = TextIO.openOut "gen_code.ML"
    5.43  		val _ = TextIO.output (fout, !buffer)
    5.44 -		val _  = TextIO.closeOut fout
    5.45 +		val _  = TextIO.closeOut fout*)
    5.46  	    in
    5.47  		()
    5.48  	    end
     6.1 --- a/src/Pure/Tools/am_interpreter.ML	Tue Jul 12 19:29:52 2005 +0200
     6.2 +++ b/src/Pure/Tools/am_interpreter.ML	Tue Jul 12 21:49:38 2005 +0200
     6.3 @@ -18,66 +18,6 @@
     6.4  
     6.5  end
     6.6  
     6.7 -signature BIN_TREE_KEY =
     6.8 -sig
     6.9 -  type key
    6.10 -  val less : key * key -> bool
    6.11 -  val eq : key * key -> bool
    6.12 -end
    6.13 -
    6.14 -signature BIN_TREE = 
    6.15 -sig
    6.16 -    type key    
    6.17 -    type 'a t
    6.18 -    val tree_of_list : (key * 'a list -> 'b) -> (key * 'a) list -> 'b t
    6.19 -    val lookup : 'a t -> key -> 'a Option.option
    6.20 -    val empty : 'a t
    6.21 -end
    6.22 -
    6.23 -functor BinTreeFun(Key: BIN_TREE_KEY) : BIN_TREE  =
    6.24 -struct
    6.25 -
    6.26 -type key = Key.key
    6.27 -
    6.28 -datatype 'a t = Empty | Node of key * 'a * 'a t * 'a t 
    6.29 -
    6.30 -val empty = Empty
    6.31 -
    6.32 -fun insert (k, a) [] = [(k, a)]
    6.33 -  | insert (k, a) ((l,b)::x') = 
    6.34 -    if Key.less (k, l) then (k, a)::(l,b)::x'
    6.35 -    else if Key.eq (k, l) then (k, a@b)::x'
    6.36 -    else (l,b)::(insert (k, a) x')
    6.37 -
    6.38 -fun sort ((k, a)::x) = insert (k, a) (sort x)
    6.39 -  | sort [] = []
    6.40 -
    6.41 -fun tree_of_sorted_list [] = Empty
    6.42 -  | tree_of_sorted_list l = 
    6.43 -    let
    6.44 -	val len = length l
    6.45 -	val leftlen = (len - 1) div 2
    6.46 -	val left = tree_of_sorted_list (List.take (l, leftlen))
    6.47 -	val rightl = List.drop (l, leftlen)
    6.48 -	val (k, x) = hd rightl
    6.49 -    in
    6.50 -	Node (k, x, left, tree_of_sorted_list (tl rightl))
    6.51 -    end
    6.52 -	
    6.53 -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)))
    6.54 -		 
    6.55 -fun lookup Empty key = NONE
    6.56 -  | lookup (Node (k, x, left, right)) key =
    6.57 -    if Key.less (key, k) then
    6.58 -	lookup left key
    6.59 -    else if Key.less (k, key) then
    6.60 -	lookup right key
    6.61 -    else
    6.62 -	SOME x
    6.63 -end;
    6.64 -
    6.65 -structure IntBinTree = BinTreeFun (type key = int val less = (op <) val eq = (op = : int * int -> bool));
    6.66 -
    6.67  structure AM_Interpreter :> ABSTRACT_MACHINE = struct
    6.68  
    6.69  datatype term = Var of int | Const of int | App of term * term | Abs of term
    6.70 @@ -88,16 +28,9 @@
    6.71  		 | CApp of closure * closure | CAbs of closure 
    6.72  		 | Closure of (closure list) * closure 
    6.73  
    6.74 -structure IntPairKey = 
    6.75 -struct
    6.76 -type key = int * int
    6.77 -fun less ((x1, y1), (x2, y2)) = x1 < x2 orelse (x1 = x2 andalso y1 < y2)
    6.78 -fun eq (k1, k2) = (k1 = k2)
    6.79 -end
    6.80 +structure prog_struct = TableFun(type key = int*int val ord = prod_ord int_ord int_ord);
    6.81  
    6.82 -structure prog_struct = BinTreeFun (IntPairKey)
    6.83 -
    6.84 -type program = ((pattern * closure) list) prog_struct.t
    6.85 +type program = ((pattern * closure) list) prog_struct.table
    6.86  
    6.87  datatype stack = SEmpty | SAppL of closure * stack | SAppR of closure * stack | SAbs of stack
    6.88  
    6.89 @@ -160,7 +93,7 @@
    6.90  	val eqs = map (fn (p, r) => (check_freevars (count_patternvars p) r; 
    6.91  				     (pattern_key p, (p, clos_of_term r)))) eqs
    6.92      in
    6.93 -	prog_struct.tree_of_list (fn (key, rules) => rules) eqs
    6.94 +	prog_struct.make (map (fn (k, a) => (k, [a])) eqs)
    6.95      end	
    6.96  
    6.97  fun match_rules n [] clos = NONE
    6.98 @@ -172,7 +105,7 @@
    6.99  fun match_closure prog clos = 
   6.100      case len_head_of_closure 0 clos of
   6.101  	(len, CConst c) =>
   6.102 -	(case prog_struct.lookup prog (c, len) of
   6.103 +	(case prog_struct.lookup (prog, (c, len)) of
   6.104  	    NONE => NONE
   6.105  	  | SOME rules => match_rules 0 rules clos)
   6.106        | _ => NONE