author | huffman |
Thu, 10 May 2007 03:00:15 +0200 | |
changeset 22912 | c477862c566d |
parent 22578 | b0eb5652f210 |
child 22951 | dfafcd6223ad |
permissions | -rw-r--r-- |
20767 | 1 |
(* Title: HOL/Real/Float.ML |
2 |
ID: $Id$ |
|
3 |
Author: Steven Obua |
|
4 |
*) |
|
5 |
||
6 |
structure ExactFloatingPoint : |
|
7 |
sig |
|
8 |
exception Destruct_floatstr of string |
|
9 |
val destruct_floatstr : (char -> bool) -> (char -> bool) -> string -> bool * string * string * bool * string |
|
10 |
||
11 |
exception Floating_point of string |
|
12 |
||
13 |
type floatrep = IntInf.int * IntInf.int |
|
14 |
val approx_dec_by_bin : IntInf.int -> floatrep -> floatrep * floatrep |
|
15 |
val approx_decstr_by_bin : int -> string -> floatrep * floatrep |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
16 |
end |
20767 | 17 |
= |
18 |
struct |
|
19 |
||
20 |
exception Destruct_floatstr of string; |
|
21 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
22 |
fun destruct_floatstr isDigit isExp number = |
20767 | 23 |
let |
24 |
val numlist = filter (not o Char.isSpace) (String.explode number) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
25 |
|
20767 | 26 |
fun countsigns ((#"+")::cs) = countsigns cs |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
27 |
| countsigns ((#"-")::cs) = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
28 |
let |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
29 |
val (positive, rest) = countsigns cs |
20767 | 30 |
in |
31 |
(not positive, rest) |
|
32 |
end |
|
33 |
| countsigns cs = (true, cs) |
|
34 |
||
35 |
fun readdigits [] = ([], []) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
36 |
| readdigits (q as c::cs) = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
37 |
if (isDigit c) then |
20767 | 38 |
let |
39 |
val (digits, rest) = readdigits cs |
|
40 |
in |
|
41 |
(c::digits, rest) |
|
42 |
end |
|
43 |
else |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
44 |
([], q) |
20767 | 45 |
|
46 |
fun readfromexp_helper cs = |
|
47 |
let |
|
48 |
val (positive, rest) = countsigns cs |
|
49 |
val (digits, rest') = readdigits rest |
|
50 |
in |
|
51 |
case rest' of |
|
52 |
[] => (positive, digits) |
|
53 |
| _ => raise (Destruct_floatstr number) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
54 |
end |
20767 | 55 |
|
56 |
fun readfromexp [] = (true, []) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
57 |
| readfromexp (c::cs) = |
20767 | 58 |
if isExp c then |
59 |
readfromexp_helper cs |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
60 |
else |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
61 |
raise (Destruct_floatstr number) |
20767 | 62 |
|
63 |
fun readfromdot [] = ([], readfromexp []) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
64 |
| readfromdot ((#".")::cs) = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
65 |
let |
20767 | 66 |
val (digits, rest) = readdigits cs |
67 |
val exp = readfromexp rest |
|
68 |
in |
|
69 |
(digits, exp) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
70 |
end |
20767 | 71 |
| readfromdot cs = readfromdot ((#".")::cs) |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
72 |
|
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
73 |
val (positive, numlist) = countsigns numlist |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
74 |
val (digits1, numlist) = readdigits numlist |
20767 | 75 |
val (digits2, exp) = readfromdot numlist |
76 |
in |
|
77 |
(positive, String.implode digits1, String.implode digits2, fst exp, String.implode (snd exp)) |
|
78 |
end |
|
79 |
||
80 |
type floatrep = IntInf.int * IntInf.int |
|
81 |
||
82 |
exception Floating_point of string; |
|
83 |
||
84 |
val ln2_10 = (Math.ln 10.0)/(Math.ln 2.0) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
85 |
|
20767 | 86 |
fun intmul a b = IntInf.* (a,b) |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
87 |
fun intsub a b = IntInf.- (a,b) |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
88 |
fun intadd a b = IntInf.+ (a,b) |
20767 | 89 |
fun intpow a b = IntInf.pow (a, IntInf.toInt b); |
90 |
fun intle a b = IntInf.<= (a, b); |
|
91 |
fun intless a b = IntInf.< (a, b); |
|
92 |
fun intneg a = IntInf.~ a; |
|
93 |
val zero = IntInf.fromInt 0; |
|
94 |
val one = IntInf.fromInt 1; |
|
95 |
val two = IntInf.fromInt 2; |
|
96 |
val ten = IntInf.fromInt 10; |
|
97 |
val five = IntInf.fromInt 5; |
|
98 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
99 |
fun find_most_significant q r = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
100 |
let |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
101 |
fun int2real i = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
102 |
case Real.fromString (IntInf.toString i) of |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
103 |
SOME r => r |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
104 |
| NONE => raise (Floating_point "int2real") |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
105 |
fun subtract (q, r) (q', r') = |
20767 | 106 |
if intle r r' then |
107 |
(intsub q (intmul q' (intpow ten (intsub r' r))), r) |
|
108 |
else |
|
109 |
(intsub (intmul q (intpow ten (intsub r r'))) q', r') |
|
110 |
fun bin2dec d = |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
111 |
if intle zero d then |
20767 | 112 |
(intpow two d, zero) |
113 |
else |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
114 |
(intpow five (intneg d), d) |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
115 |
|
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
116 |
val L = IntInf.fromInt (Real.floor (int2real (IntInf.fromInt (IntInf.log2 q)) + (int2real r) * ln2_10)) |
20767 | 117 |
val L1 = intadd L one |
118 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
119 |
val (q1, r1) = subtract (q, r) (bin2dec L1) |
20767 | 120 |
in |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
121 |
if intle zero q1 then |
20767 | 122 |
let |
123 |
val (q2, r2) = subtract (q, r) (bin2dec (intadd L1 one)) |
|
124 |
in |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
125 |
if intle zero q2 then |
20767 | 126 |
raise (Floating_point "find_most_significant") |
127 |
else |
|
128 |
(L1, (q1, r1)) |
|
129 |
end |
|
130 |
else |
|
131 |
let |
|
132 |
val (q0, r0) = subtract (q, r) (bin2dec L) |
|
133 |
in |
|
134 |
if intle zero q0 then |
|
135 |
(L, (q0, r0)) |
|
136 |
else |
|
137 |
raise (Floating_point "find_most_significant") |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
138 |
end |
20767 | 139 |
end |
140 |
||
141 |
fun approx_dec_by_bin n (q,r) = |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
142 |
let |
20767 | 143 |
fun addseq acc d' [] = acc |
144 |
| addseq acc d' (d::ds) = addseq (intadd acc (intpow two (intsub d d'))) d' ds |
|
145 |
||
146 |
fun seq2bin [] = (zero, zero) |
|
147 |
| seq2bin (d::ds) = (intadd (addseq zero d ds) one, d) |
|
148 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
149 |
fun approx d_seq d0 precision (q,r) = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
150 |
if q = zero then |
20767 | 151 |
let val x = seq2bin d_seq in |
152 |
(x, x) |
|
153 |
end |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
154 |
else |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
155 |
let |
20767 | 156 |
val (d, (q', r')) = find_most_significant q r |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
157 |
in |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
158 |
if intless precision (intsub d0 d) then |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
159 |
let |
20767 | 160 |
val d' = intsub d0 precision |
161 |
val x1 = seq2bin (d_seq) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
162 |
val x2 = (intadd (intmul (fst x1) (intpow two (intsub (snd x1) d'))) one, d') (* = seq2bin (d'::d_seq) *) |
20767 | 163 |
in |
164 |
(x1, x2) |
|
165 |
end |
|
166 |
else |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
167 |
approx (d::d_seq) d0 precision (q', r') |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
168 |
end |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
169 |
|
20767 | 170 |
fun approx_start precision (q, r) = |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
171 |
if q = zero then |
20767 | 172 |
((zero, zero), (zero, zero)) |
173 |
else |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
174 |
let |
20767 | 175 |
val (d, (q', r')) = find_most_significant q r |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
176 |
in |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
177 |
if intle precision zero then |
20767 | 178 |
let |
179 |
val x1 = seq2bin [d] |
|
180 |
in |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
181 |
if q' = zero then |
20767 | 182 |
(x1, x1) |
183 |
else |
|
184 |
(x1, seq2bin [intadd d one]) |
|
185 |
end |
|
186 |
else |
|
187 |
approx [d] d precision (q', r') |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
188 |
end |
20767 | 189 |
in |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
190 |
if intle zero q then |
20767 | 191 |
approx_start n (q,r) |
192 |
else |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
193 |
let |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
194 |
val ((a1,b1), (a2, b2)) = approx_start n (intneg q, r) |
20767 | 195 |
in |
196 |
((intneg a2, b2), (intneg a1, b1)) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
197 |
end |
20767 | 198 |
end |
199 |
||
200 |
fun approx_decstr_by_bin n decstr = |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
201 |
let |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
202 |
fun str2int s = case IntInf.fromString s of SOME x => x | NONE => zero |
20767 | 203 |
fun signint p x = if p then x else intneg x |
204 |
||
205 |
val (p, d1, d2, ep, e) = destruct_floatstr Char.isDigit (fn e => e = #"e" orelse e = #"E") decstr |
|
206 |
val s = IntInf.fromInt (size d2) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
207 |
|
20767 | 208 |
val q = signint p (intadd (intmul (str2int d1) (intpow ten s)) (str2int d2)) |
209 |
val r = intsub (signint ep (str2int e)) s |
|
210 |
in |
|
211 |
approx_dec_by_bin (IntInf.fromInt n) (q,r) |
|
212 |
end |
|
213 |
||
214 |
end; |
|
215 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
216 |
structure FloatArith = |
20767 | 217 |
struct |
218 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
219 |
type float = IntInf.int * IntInf.int |
20767 | 220 |
|
221 |
val izero = IntInf.fromInt 0 |
|
222 |
val ione = IntInf.fromInt 1 |
|
223 |
val imone = IntInf.fromInt ~1 |
|
224 |
val itwo = IntInf.fromInt 2 |
|
225 |
fun imul a b = IntInf.* (a,b) |
|
226 |
fun isub a b = IntInf.- (a,b) |
|
227 |
fun iadd a b = IntInf.+ (a,b) |
|
228 |
||
229 |
val floatzero = (izero, izero) |
|
230 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
231 |
fun positive_part (a,b) = |
20767 | 232 |
(if IntInf.< (a,izero) then izero else a, b) |
233 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
234 |
fun negative_part (a,b) = |
20767 | 235 |
(if IntInf.< (a,izero) then a else izero, b) |
236 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
237 |
fun is_negative (a,b) = |
20767 | 238 |
if IntInf.< (a, izero) then true else false |
239 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
240 |
fun is_positive (a,b) = |
20767 | 241 |
if IntInf.< (izero, a) then true else false |
242 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
243 |
fun is_zero (a,b) = |
20767 | 244 |
if a = izero then true else false |
245 |
||
246 |
fun ipow2 a = IntInf.pow ((IntInf.fromInt 2), IntInf.toInt a) |
|
247 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
248 |
fun add (a1, b1) (a2, b2) = |
20767 | 249 |
if IntInf.< (b1, b2) then |
250 |
(iadd a1 (imul a2 (ipow2 (isub b2 b1))), b1) |
|
251 |
else |
|
252 |
(iadd (imul a1 (ipow2 (isub b1 b2))) a2, b2) |
|
253 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
254 |
fun sub (a1, b1) (a2, b2) = |
20767 | 255 |
if IntInf.< (b1, b2) then |
256 |
(isub a1 (imul a2 (ipow2 (isub b2 b1))), b1) |
|
257 |
else |
|
258 |
(isub (imul a1 (ipow2 (isub b1 b2))) a2, b2) |
|
259 |
||
260 |
fun neg (a, b) = (IntInf.~ a, b) |
|
261 |
||
262 |
fun is_equal a b = is_zero (sub a b) |
|
263 |
||
264 |
fun is_less a b = is_negative (sub a b) |
|
265 |
||
266 |
fun max a b = if is_less a b then b else a |
|
267 |
||
268 |
fun min a b = if is_less a b then a else b |
|
269 |
||
270 |
fun abs a = if is_negative a then neg a else a |
|
271 |
||
272 |
fun mul (a1, b1) (a2, b2) = (imul a1 a2, iadd b1 b2) |
|
273 |
||
274 |
end; |
|
275 |
||
276 |
||
277 |
structure Float: |
|
278 |
sig |
|
279 |
type float = FloatArith.float |
|
280 |
type floatfunc = float * float -> float * float |
|
281 |
||
282 |
val mk_intinf : typ -> IntInf.int -> term |
|
283 |
val mk_float : float -> term |
|
284 |
||
285 |
exception Dest_intinf; |
|
286 |
val dest_intinf : term -> IntInf.int |
|
287 |
val dest_nat : term -> IntInf.int |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
288 |
|
20767 | 289 |
exception Dest_float; |
290 |
val dest_float : term -> float |
|
291 |
||
292 |
val float_const : term |
|
293 |
||
294 |
val float_add_const : term |
|
295 |
val float_diff_const : term |
|
296 |
val float_uminus_const : term |
|
297 |
val float_pprt_const : term |
|
298 |
val float_nprt_const : term |
|
299 |
val float_abs_const : term |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
300 |
val float_mult_const : term |
20767 | 301 |
val float_le_const : term |
302 |
||
303 |
val nat_le_const : term |
|
304 |
val nat_less_const : term |
|
305 |
val nat_eq_const : term |
|
306 |
||
307 |
val approx_float : int -> floatfunc -> string -> term * term |
|
308 |
||
309 |
(* exception Float_op_oracle_data of term |
|
310 |
exception Nat_op_oracle_data of term |
|
311 |
||
312 |
val float_op_oracle : Sign.sg * exn -> term |
|
313 |
val nat_op_oracle : Sign.sg * exn -> term |
|
314 |
||
315 |
val invoke_float_op : term -> thm |
|
316 |
val invoke_nat_op : term -> thm*) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
317 |
end |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
318 |
= |
20767 | 319 |
struct |
320 |
||
321 |
structure Inttab = TableFun(type key = int val ord = (rev_order o int_ord)); |
|
322 |
||
323 |
type float = IntInf.int*IntInf.int |
|
324 |
type floatfunc = float*float -> float*float |
|
325 |
||
326 |
val float_const = Const ("Float.float", HOLogic.mk_prodT (HOLogic.intT, HOLogic.intT) --> HOLogic.realT) |
|
327 |
||
328 |
val float_add_const = Const ("HOL.plus", HOLogic.realT --> HOLogic.realT --> HOLogic.realT) |
|
329 |
val float_diff_const = Const ("HOL.minus", HOLogic.realT --> HOLogic.realT --> HOLogic.realT) |
|
330 |
val float_mult_const = Const ("HOL.times", HOLogic.realT --> HOLogic.realT --> HOLogic.realT) |
|
331 |
val float_uminus_const = Const ("HOL.uminus", HOLogic.realT --> HOLogic.realT) |
|
332 |
val float_abs_const = Const ("HOL.abs", HOLogic.realT --> HOLogic.realT) |
|
333 |
val float_le_const = Const ("Orderings.less_eq", HOLogic.realT --> HOLogic.realT --> HOLogic.boolT) |
|
334 |
val float_pprt_const = Const ("OrderedGroup.pprt", HOLogic.realT --> HOLogic.realT) |
|
335 |
val float_nprt_const = Const ("OrderedGroup.nprt", HOLogic.realT --> HOLogic.realT) |
|
336 |
||
337 |
val nat_le_const = Const ("Orderings.less_eq", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT) |
|
338 |
val nat_less_const = Const ("Orderings.less", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT) |
|
339 |
val nat_eq_const = Const ("op =", HOLogic.natT --> HOLogic.natT --> HOLogic.boolT) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
340 |
|
20767 | 341 |
val zero = FloatArith.izero |
342 |
val minus_one = FloatArith.imone |
|
343 |
val two = FloatArith.itwo |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
344 |
|
20767 | 345 |
exception Dest_intinf; |
346 |
exception Dest_float; |
|
347 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
348 |
fun mk_intinf ty n = HOLogic.number_of_const ty $ HOLogic.mk_numeral n; |
20767 | 349 |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
350 |
val dest_intinf = snd o HOLogic.dest_number |
20767 | 351 |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
352 |
fun mk_float (a,b) = |
20767 | 353 |
float_const $ (HOLogic.mk_prod ((mk_intinf HOLogic.intT a), (mk_intinf HOLogic.intT b))) |
354 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
355 |
fun dest_float f = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
356 |
case f of |
20767 | 357 |
(Const ("Float.float", _) $ (Const ("Pair", _) $ a $ b)) => (dest_intinf a, dest_intinf b) |
358 |
| Const ("Numeral.number_of",_) $ a => (dest_intinf f, 0) |
|
359 |
| Const ("Numeral0", _) => (FloatArith.izero, FloatArith.izero) |
|
360 |
| Const ("Numeral1", _) => (FloatArith.ione, FloatArith.izero) |
|
361 |
| _ => raise Dest_float |
|
362 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
363 |
fun dest_nat n = |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
364 |
let |
20767 | 365 |
val v = dest_intinf n |
366 |
in |
|
367 |
if IntInf.< (v, FloatArith.izero) then |
|
368 |
FloatArith.izero |
|
369 |
else |
|
370 |
v |
|
371 |
end |
|
372 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
373 |
fun approx_float prec f value = |
20767 | 374 |
let |
375 |
val interval = ExactFloatingPoint.approx_decstr_by_bin prec value |
|
376 |
val (flower, fupper) = f interval |
|
377 |
in |
|
378 |
(mk_float flower, mk_float fupper) |
|
379 |
end |
|
380 |
||
381 |
(*exception Float_op_oracle_data of term; |
|
382 |
||
383 |
fun float_op_oracle (sg, exn as Float_op_oracle_data t) = |
|
384 |
Logic.mk_equals (t, |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
385 |
case t of |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
386 |
f $ a $ b => |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
387 |
let |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
388 |
val a' = dest_float a |
20767 | 389 |
val b' = dest_float b |
390 |
in |
|
391 |
if f = float_add_const then |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
392 |
mk_float (FloatArith.add a' b') |
20767 | 393 |
else if f = float_diff_const then |
394 |
mk_float (FloatArith.sub a' b') |
|
395 |
else if f = float_mult_const then |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
396 |
mk_float (FloatArith.mul a' b') |
20767 | 397 |
else if f = float_le_const then |
398 |
(if FloatArith.is_less b' a' then |
|
399 |
HOLogic.false_const |
|
400 |
else |
|
401 |
HOLogic.true_const) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
402 |
else raise exn |
20767 | 403 |
end |
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
404 |
| f $ a => |
20767 | 405 |
let |
406 |
val a' = dest_float a |
|
407 |
in |
|
408 |
if f = float_uminus_const then |
|
409 |
mk_float (FloatArith.neg a') |
|
410 |
else if f = float_abs_const then |
|
411 |
mk_float (FloatArith.abs a') |
|
412 |
else if f = float_pprt_const then |
|
413 |
mk_float (FloatArith.positive_part a') |
|
414 |
else if f = float_nprt_const then |
|
415 |
mk_float (FloatArith.negative_part a') |
|
416 |
else |
|
417 |
raise exn |
|
418 |
end |
|
419 |
| _ => raise exn |
|
420 |
) |
|
421 |
val th = ref ([]: theory list) |
|
422 |
val sg = ref ([]: Sign.sg list) |
|
423 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
424 |
fun invoke_float_op c = |
20767 | 425 |
let |
426 |
val th = (if length(!th) = 0 then th := [theory "MatrixLP"] else (); hd (!th)) |
|
22578 | 427 |
val sg = (if length(!sg) = 0 then sg := [th] else (); hd (!sg)) |
20767 | 428 |
in |
429 |
invoke_oracle th "float_op" (sg, Float_op_oracle_data c) |
|
430 |
end |
|
431 |
||
432 |
exception Nat_op_oracle_data of term; |
|
433 |
||
434 |
fun nat_op_oracle (sg, exn as Nat_op_oracle_data t) = |
|
435 |
Logic.mk_equals (t, |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
436 |
case t of |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
437 |
f $ a $ b => |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
438 |
let |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
439 |
val a' = dest_nat a |
20767 | 440 |
val b' = dest_nat b |
441 |
in |
|
442 |
if f = nat_le_const then |
|
443 |
(if IntInf.<= (a', b') then |
|
444 |
HOLogic.true_const |
|
445 |
else |
|
446 |
HOLogic.false_const) |
|
447 |
else if f = nat_eq_const then |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
448 |
(if a' = b' then |
20767 | 449 |
HOLogic.true_const |
450 |
else |
|
451 |
HOLogic.false_const) |
|
452 |
else if f = nat_less_const then |
|
453 |
(if IntInf.< (a', b') then |
|
454 |
HOLogic.true_const |
|
455 |
else |
|
456 |
HOLogic.false_const) |
|
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
457 |
else |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
458 |
raise exn |
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
459 |
|
20767 | 460 |
end |
461 |
| _ => raise exn) |
|
462 |
||
21820
2f2b6a965ccc
introduced mk/dest_numeral/number for mk/dest_binum etc.
haftmann
parents:
20767
diff
changeset
|
463 |
fun invoke_nat_op c = |
20767 | 464 |
let |
465 |
val th = (if length (!th) = 0 then th := [theory "MatrixLP"] else (); hd (!th)) |
|
22578 | 466 |
val sg = (if length (!sg) = 0 then sg := [th] else (); hd (!sg)) |
20767 | 467 |
in |
468 |
invoke_oracle th "nat_op" (sg, Nat_op_oracle_data c) |
|
469 |
end |
|
470 |
*) |
|
471 |
end; |