| 1 |
(* ========================================================================== *) |
| 2 |
(* Formal verification of nonlinear inequalities in HOL Light *) |
| 3 |
(* *) |
| 4 |
(* Copyright (c) 2014 Alexey Solovyev *) |
| 5 |
(* *) |
| 6 |
(* This file is distributed under the terms of the MIT licence *) |
| 7 |
(* ========================================================================== *) |
| 8 |
|
| 9 |
(* -------------------------------------------------------------------------- *) |
| 10 |
(* Formal interval evaluation of functions: exp *) |
| 11 |
(* -------------------------------------------------------------------------- *) |
| 12 |
|
| 13 |
needs "Formal_ineqs/trig/exp_log.hl";; |
| 14 |
needs "Formal_ineqs/trig/poly_eval.hl";; |
| 15 |
needs "Formal_ineqs/arith/float_pow.hl";; |
| 16 |
|
| 17 |
module type Exp_eval_sig = |
| 18 |
sig |
| 19 |
val float_exp_hi : int -> term -> thm |
| 20 |
val float_exp_lo : int -> term -> thm |
| 21 |
val float_interval_exp : int -> thm -> thm |
| 22 |
end;; |
| 23 |
|
| 24 |
module Exp_eval : Exp_eval_sig = struct |
| 25 |
|
| 26 |
open Misc_functions;; |
| 27 |
open Exp_log;; |
| 28 |
open More_list;; |
| 29 |
open Poly;; |
| 30 |
open Poly_eval;; |
| 31 |
open Interval_arith;; |
| 32 |
open Float_theory;; |
| 33 |
open Arith_nat;; |
| 34 |
open Arith_float;; |
| 35 |
open More_float;; |
| 36 |
open Float_pow;; |
| 37 |
open Misc_vars;; |
| 38 |
|
| 39 |
prioritize_real();; |
| 40 |
|
| 41 |
(* Defines the maximum x for which a poly approximation is computed directly. *) |
| 42 |
(* Should be in (0, 1]. *) |
| 43 |
let exp_max_x = 1.0;; |
| 44 |
|
| 45 |
let th_rule = UNDISCH_ALL o PURE_REWRITE_RULE[GSYM IMP_IMP] o SPEC_ALL;; |
| 46 |
|
| 47 |
(* ----------------- *) |
| 48 |
(* Theorems *) |
| 49 |
(* ----------------- *) |
| 50 |
|
| 51 |
let exp_interval = (th_rule o prove) |
| 52 |
(`interval_arith x (a, b) /\ |
| 53 |
lo <= exp a /\ |
| 54 |
exp b <= hi |
| 55 |
==> interval_arith (exp x) (lo, hi)`, |
| 56 |
REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THENL [ |
| 57 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `exp a` THEN |
| 58 |
ASM_REWRITE_TAC[REAL_EXP_MONO_LE]; |
| 59 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `exp b` THEN |
| 60 |
ASM_REWRITE_TAC[REAL_EXP_MONO_LE] |
| 61 |
]);; |
| 62 |
|
| 63 |
let bound_pos_low_trans = prove |
| 64 |
(`a <= e ==> |
| 65 |
interval_arith r (&0, a) ==> |
| 66 |
r <= e`, |
| 67 |
REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC);; |
| 68 |
|
| 69 |
let bound_neg_low_trans = prove(`p1 - x * p2 <= e ==> |
| 70 |
interval_arith a (&0, p1) /\ |
| 71 |
interval_arith p2 (&0, b) /\ |
| 72 |
&0 <= x /\ |
| 73 |
x * b <= n /\ |
| 74 |
r <= a - n |
| 75 |
==> r <= e`, |
| 76 |
REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN |
| 77 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `a - n:real` THEN ASM_REWRITE_TAC[] THEN |
| 78 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `p1 - x * p2:real` THEN ASM_REWRITE_TAC[] THEN |
| 79 |
REWRITE_TAC[real_sub] THEN MATCH_MP_TAC REAL_LE_ADD2 THEN ASM_REWRITE_TAC[REAL_LE_NEG2] THEN |
| 80 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `x * b:real` THEN ASM_REWRITE_TAC[] THEN |
| 81 |
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[REAL_LE_REFL]);; |
| 82 |
|
| 83 |
let bound_neg_high_trans = prove(`e <= p1 - x * p2 ==> |
| 84 |
interval_arith p1 (&0, a) /\ |
| 85 |
interval_arith b (&0, p2) /\ |
| 86 |
&0 <= x /\ |
| 87 |
n <= x * b /\ |
| 88 |
a - n <= r |
| 89 |
==> e <= r`, |
| 90 |
REWRITE_TAC[interval_arith] THEN REPEAT STRIP_TAC THEN |
| 91 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `a - n:real` THEN ASM_REWRITE_TAC[] THEN |
| 92 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `p1 - x * p2:real` THEN ASM_REWRITE_TAC[] THEN |
| 93 |
REWRITE_TAC[real_sub] THEN MATCH_MP_TAC REAL_LE_ADD2 THEN ASM_REWRITE_TAC[REAL_LE_NEG2] THEN |
| 94 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `x * b:real` THEN ASM_REWRITE_TAC[] THEN |
| 95 |
MATCH_MP_TAC REAL_LE_MUL2 THEN ASM_REWRITE_TAC[REAL_LE_REFL]);; |
| 96 |
|
| 97 |
(* Argument reduction *) |
| 98 |
|
| 99 |
let exp_neg_low = (th_rule o prove) |
| 100 |
(`r <= exp (-- float_num F n e) |
| 101 |
==> r <= exp (float_num T n e)`, |
| 102 |
REWRITE_TAC[FLOAT_NEG]);; |
| 103 |
|
| 104 |
let exp_neg_high = (th_rule o prove) |
| 105 |
(`exp (-- float_num F n e) <= r |
| 106 |
==> exp (float_num T n e) <= r`, |
| 107 |
REWRITE_TAC[FLOAT_NEG]);; |
| 108 |
|
| 109 |
let exp_pos_high = (th_rule o prove) |
| 110 |
(`&0 < t /\ t <= exp (--x) /\ inv t <= r |
| 111 |
==> exp x <= r`, |
| 112 |
REPEAT STRIP_TAC THEN |
| 113 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `inv t` THEN ASM_REWRITE_TAC[] THEN |
| 114 |
GEN_REWRITE_TAC (LAND_CONV o RAND_CONV) [GSYM REAL_NEG_NEG] THEN |
| 115 |
ONCE_REWRITE_TAC[REAL_EXP_NEG] THEN |
| 116 |
MATCH_MP_TAC REAL_LE_INV2 THEN ASM_REWRITE_TAC[]);; |
| 117 |
|
| 118 |
let exp_reduce_low = (th_rule o prove) |
| 119 |
(`&0 < t /\ t = &k /\ y <= x / t /\ lo <= exp y /\ low <= lo pow k /\ &0 <= lo |
| 120 |
==> low <= exp x`, |
| 121 |
REPEAT STRIP_TAC THEN |
| 122 |
UNDISCH_TAC `t = &k` THEN DISCH_THEN (ASSUME_TAC o SYM) THEN |
| 123 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `lo pow k` THEN ASM_REWRITE_TAC[] THEN |
| 124 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `exp (x / t) pow k` THEN |
| 125 |
CONJ_TAC THENL [ |
| 126 |
MATCH_MP_TAC REAL_POW_LE2 THEN ASM_REWRITE_TAC[] THEN |
| 127 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `exp y` THEN |
| 128 |
ASM_REWRITE_TAC[REAL_EXP_MONO_LE]; |
| 129 |
ASM_REWRITE_TAC[GSYM REAL_EXP_N; REAL_ARITH `a * b / a = b * (a / a)`] THEN |
| 130 |
SUBGOAL_THEN `~(t = &0)` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN |
| 131 |
ASM_SIMP_TAC[REAL_DIV_REFL; REAL_MUL_RID; REAL_LE_REFL] |
| 132 |
]);; |
| 133 |
|
| 134 |
let exp_reduce_high = (th_rule o prove) |
| 135 |
(`&0 < t /\ t = &k /\ x / t <= y /\ exp y <= hi /\ hi pow k <= high |
| 136 |
==> exp x <= high`, |
| 137 |
REPEAT STRIP_TAC THEN |
| 138 |
UNDISCH_TAC `t = &k` THEN DISCH_THEN (ASSUME_TAC o SYM) THEN |
| 139 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `hi pow k` THEN ASM_REWRITE_TAC[] THEN |
| 140 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `exp (x / t) pow k` THEN |
| 141 |
CONJ_TAC THENL [ |
| 142 |
ASM_REWRITE_TAC[GSYM REAL_EXP_N; REAL_ARITH `a * b / a = b * (a / a)`] THEN |
| 143 |
SUBGOAL_THEN `~(t = &0)` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN |
| 144 |
ASM_SIMP_TAC[REAL_DIV_REFL; REAL_MUL_RID; REAL_LE_REFL]; |
| 145 |
MATCH_MP_TAC REAL_POW_LE2 THEN REWRITE_TAC[REAL_EXP_POS_LE] THEN |
| 146 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `exp y` THEN |
| 147 |
ASM_REWRITE_TAC[REAL_EXP_MONO_LE] |
| 148 |
]);; |
| 149 |
|
| 150 |
|
| 151 |
(* ----------------- *) |
| 152 |
(* Functions *) |
| 153 |
(* ----------------- *) |
| 154 |
|
| 155 |
let mk_exp_bound_tables exp_bound_th = |
| 156 |
let exp_bound = (SPEC_ALL o |
| 157 |
REWRITE_RULE[poly_f_even; poly_f_odd; GSYM REAL_POW_2; REAL_POW_POW] o |
| 158 |
REWRITE_RULE[sum_eq_poly_f] o |
| 159 |
REWRITE_RULE[alt_sum_eq_poly_f; real_div]) exp_bound_th in |
| 160 |
let reduce_rule = CONV_RULE (DEPTH_CONV |
| 161 |
(FIRST_CONV [NUM_SUC_CONV; NUM_ADD_CONV; NUM_MULT_CONV])) in |
| 162 |
let find_poly_f = rev o find_terms (fun tm -> try (rator o rator) tm = `poly_f` |
| 163 |
with Failure _ -> false) in |
| 164 |
fun pp n -> |
| 165 |
let n_tm = mk_small_numeral n in |
| 166 |
let th0 = INST[n_tm, n_var_num] exp_bound in |
| 167 |
let th1 = CONV_RULE NUM_REDUCE_CONV th0 in |
| 168 |
let poly_tms0 = find_poly_f (concl th1) in |
| 169 |
let ns_th = map (REDEPTH_CONV num_CONV o rand o rand o rator) poly_tms0 in |
| 170 |
let th2 = REWRITE_RULE(ns_th @ [TABLE_SUC; TABLE_0; APPEND]) th1 in |
| 171 |
let bound_th = reduce_rule th2 in |
| 172 |
let poly_tms = find_poly_f (concl bound_th) in |
| 173 |
let cs_tms = map (rand o rator) poly_tms in |
| 174 |
let cs_lists = map (mk_poly_coeffs pp) cs_tms in |
| 175 |
bound_th, zip cs_tms cs_lists;; |
| 176 |
|
| 177 |
|
| 178 |
(* Computes x^k / k! *) |
| 179 |
let rec x_pow_over_fact x k = |
| 180 |
if k <= 0 then 1. else |
| 181 |
x /. (float_of_int k) *. x_pow_over_fact x (k - 1);; |
| 182 |
|
| 183 |
(* Computes i such that x^i / i! <= base^(-(p + 1)) and cond(i) *) |
| 184 |
let n_of_p_exp x pp cond = |
| 185 |
let t = (float_of_int Arith_num.arith_base) ** (float_of_int (-pp - 1)) in |
| 186 |
let rec try_i i = |
| 187 |
let _ = if i > 50 then failwith "n_of_p_exp: cannot find i" else () in |
| 188 |
if cond i then |
| 189 |
let r = x_pow_over_fact x i in |
| 190 |
if r <= t then i else try_i (i + 1) |
| 191 |
else |
| 192 |
try_i (i + 1) |
| 193 |
in |
| 194 |
try_i 0;; |
| 195 |
|
| 196 |
(* Makes the table of exp coefficients assuming the interval [0, exp_max_x] *) |
| 197 |
let mk_exp_pos_lower pp = |
| 198 |
let n = n_of_p_exp exp_max_x pp (fun i -> true) in |
| 199 |
let bound_th0, css = mk_exp_bound_tables exp_pos_poly_lower_bound (pp + 1) n in |
| 200 |
let cs1 = hd css in |
| 201 |
let RULE2 = UNDISCH_ALL o REWRITE_RULE[GSYM IMP_IMP] o SPEC_ALL in |
| 202 |
let bound_th = (RULE2 o MATCH_MP bound_pos_low_trans o UNDISCH_ALL) bound_th0 in |
| 203 |
bound_th, cs1;; |
| 204 |
|
| 205 |
(* Makes the table of exp coefficients assuming the interval [0, exp_max_x] *) |
| 206 |
let mk_exp_neg_lower pp = |
| 207 |
let n = n_of_p_exp exp_max_x pp (fun i -> i land 1 = 1) in |
| 208 |
let bound_th0, css = mk_exp_bound_tables exp_neg_poly_lower_bound (pp + 1) n in |
| 209 |
let cs1, cs2 = pair_of_list css in |
| 210 |
let RULE2 = UNDISCH_ALL o REWRITE_RULE[GSYM IMP_IMP] o SPEC_ALL in |
| 211 |
let bound_th = (RULE2 o MATCH_MP bound_neg_low_trans o th_rule o |
| 212 |
PURE_REWRITE_RULE[GSYM float1_eq]) bound_th0 in |
| 213 |
bound_th, cs1, cs2;; |
| 214 |
|
| 215 |
(* Makes the table of exp coefficients assuming the interval [0, exp_max_x] *) |
| 216 |
let mk_exp_neg_upper pp = |
| 217 |
let n = n_of_p_exp exp_max_x pp (fun i -> i land 1 = 0) in |
| 218 |
let bound_th0, css = mk_exp_bound_tables exp_neg_poly_upper_bound (pp + 1) n in |
| 219 |
let cs1, cs2 = pair_of_list css in |
| 220 |
let RULE2 = UNDISCH_ALL o REWRITE_RULE[GSYM IMP_IMP] o SPEC_ALL in |
| 221 |
let bound_th = (RULE2 o MATCH_MP bound_neg_high_trans o th_rule o |
| 222 |
PURE_REWRITE_RULE[GSYM float1_eq]) bound_th0 in |
| 223 |
bound_th, cs1, cs2;; |
| 224 |
|
| 225 |
(* Computes a lower bound of exp at the given non-negative floating-point number. *) |
| 226 |
(* The given number should be relatively small (|x| <= exp_max_x) to get a good results. *) |
| 227 |
let float_exp_pos_low_raw = |
| 228 |
let exp_table = Array.init 21 mk_exp_pos_lower in |
| 229 |
let pos_lemma = (th_rule o prove) |
| 230 |
(`&0 <= x ==> interval_arith x (&0, x)`, |
| 231 |
REWRITE_TAC[interval_arith] THEN REAL_ARITH_TAC) in |
| 232 |
fun pp x_tm -> |
| 233 |
let bound_th, cs1 = exp_table.(pp) in |
| 234 |
let pp = pp + 1 in |
| 235 |
let x_pos = EQT_ELIM (float_ge0 x_tm) in |
| 236 |
let pos_th = MY_PROVE_HYP x_pos (INST[x_tm, x_var_real] pos_lemma) in |
| 237 |
let p1_low_th = eval_low_poly_f_pos_pos pp cs1 pos_th in |
| 238 |
let r_tm = rand (rator (concl p1_low_th)) in |
| 239 |
let th0 = INST[r_tm, r_var_real; x_tm, x_var_real] bound_th in |
| 240 |
MY_PROVE_HYP x_pos (MY_PROVE_HYP p1_low_th th0);; |
| 241 |
|
| 242 |
(* Computes a lower bound of exp(-x) for the given floating-point number 0 <= x <= 1. *) |
| 243 |
(* The given number should be relatively small (|x| <= exp_max_x) to get a good results. *) |
| 244 |
let float_exp_neg_low_raw = |
| 245 |
let exp_table = Array.init 21 mk_exp_neg_lower in |
| 246 |
fun pp x_tm -> |
| 247 |
let bound_th, cs1, cs2 = exp_table.(pp) in |
| 248 |
let pp = pp + 1 in |
| 249 |
let x_pow2_high = eval_pow2_high pp x_tm and |
| 250 |
x_pow2_low = eval_pow2_low pp x_tm in |
| 251 |
let p1_low_th = eval_low_poly_f_pos_pos pp cs1 x_pow2_low and |
| 252 |
p2_high_th = eval_high_poly_f_pos_pos pp cs2 x_pow2_high in |
| 253 |
let a_tm = (rand o rator o concl) p1_low_th in |
| 254 |
let b_tm = (rand o rand o concl) p2_high_th in |
| 255 |
let xb_le_n = float_mul_hi pp x_tm b_tm in |
| 256 |
let n_tm = (rand o concl) xb_le_n in |
| 257 |
let r_le_an = float_sub_lo pp a_tm n_tm in |
| 258 |
let r_tm = (rand o rator o concl) r_le_an in |
| 259 |
let cmp_1 = EQT_ELIM (float_le x_tm one_float) in |
| 260 |
let cmp_0 = EQT_ELIM (float_ge0 x_tm) in |
| 261 |
let th0 = INST[a_tm, a_var_real; b_tm, b_var_real; n_tm, n_var_real; |
| 262 |
r_tm, r_var_real; x_tm, x_var_real] bound_th in |
| 263 |
itlist MY_PROVE_HYP [p1_low_th; p2_high_th; xb_le_n; r_le_an; cmp_0; cmp_1] th0;; |
| 264 |
|
| 265 |
(* Computes an upper bound of exp(-x) for the given floating-point number 0 <= x <= 1. *) |
| 266 |
(* The given number should be relatively small (|x| <= exp_max_x) to get a good results. *) |
| 267 |
let float_exp_neg_high_raw = |
| 268 |
let exp_table = Array.init 21 mk_exp_neg_upper in |
| 269 |
fun pp x_tm -> |
| 270 |
let bound_th, cs1, cs2 = exp_table.(pp) in |
| 271 |
let pp = pp + 1 in |
| 272 |
let x_pow2_high = eval_pow2_high pp x_tm and |
| 273 |
x_pow2_low = eval_pow2_low pp x_tm in |
| 274 |
let p1_high_th = eval_high_poly_f_pos_pos pp cs1 x_pow2_high in |
| 275 |
let p2_low_th = eval_low_poly_f_pos_pos pp cs2 x_pow2_low in |
| 276 |
let a_tm = (rand o rand o concl) p1_high_th in |
| 277 |
let b_tm = (rand o rator o concl) p2_low_th in |
| 278 |
let n_le_xb = float_mul_lo pp x_tm b_tm in |
| 279 |
let n_tm = (rand o rator o concl) n_le_xb in |
| 280 |
let an_le_r = float_sub_hi pp a_tm n_tm in |
| 281 |
let r_tm = (rand o concl) an_le_r in |
| 282 |
let cmp_1 = EQT_ELIM (float_le x_tm one_float) in |
| 283 |
let cmp_0 = EQT_ELIM (float_ge0 x_tm) in |
| 284 |
let th0 = INST[a_tm, a_var_real; b_tm, b_var_real; n_tm, n_var_real; |
| 285 |
r_tm, r_var_real; x_tm, x_var_real] bound_th in |
| 286 |
itlist MY_PROVE_HYP [p1_high_th; p2_low_th; n_le_xb; an_le_r; cmp_1; cmp_0] th0;; |
| 287 |
|
| 288 |
(* TODO: move to arith_float.hl *) |
| 289 |
let float_inv_hi = |
| 290 |
let div_lemma = (th_rule o PURE_REWRITE_RULE[GSYM float1_eq] o prove) |
| 291 |
(`&1 / x <= hi ==> inv x <= hi`, |
| 292 |
REWRITE_TAC[real_div; REAL_MUL_LID]) in |
| 293 |
fun pp tm -> |
| 294 |
let div_th = float_div_hi pp one_float tm in |
| 295 |
let hi_tm = rand (concl div_th) in |
| 296 |
let th0 = INST[tm, x_var_real; hi_tm, hi_var_real] div_lemma in |
| 297 |
MY_PROVE_HYP div_th th0;; |
| 298 |
|
| 299 |
(* x should be in [-1, infty), |x| should be small to get a good result *) |
| 300 |
let float_exp_low_raw pp x_tm = |
| 301 |
try |
| 302 |
let s, n_tm, e_tm = dest_float x_tm in |
| 303 |
if s = "F" then |
| 304 |
float_exp_pos_low_raw pp x_tm |
| 305 |
else |
| 306 |
let x_pos = make_float "F" n_tm e_tm in |
| 307 |
let low_th = float_exp_neg_low_raw pp x_pos in |
| 308 |
let r_tm = rand (rator (concl low_th)) in |
| 309 |
let th0 = INST[r_tm, r_var_real; n_tm, n_var_num; e_tm, e_var_num] exp_neg_low in |
| 310 |
MY_PROVE_HYP low_th th0 |
| 311 |
with Failure msg -> |
| 312 |
error ("float_exp_low_raw: " ^ msg) [x_tm] [];; |
| 313 |
|
| 314 |
(* x should be in [-1, 1], |x| should be small to get a good result *) |
| 315 |
let float_exp_high_raw pp x_tm = |
| 316 |
try |
| 317 |
let s, n_tm, e_tm = dest_float x_tm in |
| 318 |
if s = "F" then |
| 319 |
let low_th = float_exp_neg_low_raw pp x_tm in |
| 320 |
let t_tm = rand (rator (concl low_th)) in |
| 321 |
let t_pos = EQT_ELIM (float_gt0 t_tm) in |
| 322 |
let inv_t = float_inv_hi pp t_tm in |
| 323 |
let r_tm = rand (concl inv_t) in |
| 324 |
let th0 = INST[t_tm, t_var_real; r_tm, r_var_real; x_tm, x_var_real] exp_pos_high in |
| 325 |
MY_PROVE_HYP t_pos (MY_PROVE_HYP low_th (MY_PROVE_HYP inv_t th0)) |
| 326 |
else |
| 327 |
let x_pos = make_float "F" n_tm e_tm in |
| 328 |
let high_th = float_exp_neg_high_raw pp x_pos in |
| 329 |
let r_tm = rand (concl high_th) in |
| 330 |
let th0 = INST[r_tm, r_var_real; n_tm, n_var_num; e_tm, e_var_num] exp_neg_high in |
| 331 |
MY_PROVE_HYP high_th th0 |
| 332 |
with Failure msg -> |
| 333 |
error ("float_exp_high_raw: " ^ msg) [x_tm] [];; |
| 334 |
|
| 335 |
let raw_num_to_numeral tm = |
| 336 |
let th0 = INST[tm, n_var_num] (SYM Arith_num.NUM_THM) in |
| 337 |
let th1 = NUM_TO_NUMERAL_CONV (mk_comb (Arith_num.num_const, tm)) in |
| 338 |
TRANS th0 th1;; |
| 339 |
|
| 340 |
(* Returns the theorem |- ##n = &n where the rhs is a HOL Light numeral *) |
| 341 |
let mk_float_eq = |
| 342 |
let eq_lemma = (SYM o NUMERALS_TO_NUM o REWRITE_RULE[min_exp_def] o SPEC_ALL) FLOAT_OF_NUM in |
| 343 |
let inj = `(&):num->real` in |
| 344 |
fun n -> |
| 345 |
let th0 = INST[rand (mk_small_numeral_array n), n_var_num] eq_lemma in |
| 346 |
let th1 = raw_num_to_numeral (rand (rand (concl th0))) in |
| 347 |
TRANS th0 (AP_TERM inj th1);; |
| 348 |
|
| 349 |
(* Finds an integer k such that x / k <= exp_max_x *) |
| 350 |
let reduce_float x_tm = |
| 351 |
let f = abs_float (float_of_float_tm x_tm) in |
| 352 |
if f <= exp_max_x then |
| 353 |
false, 0, TRUTH |
| 354 |
else |
| 355 |
let k = int_of_float (floor (f /. exp_max_x)) + 1 in |
| 356 |
true, k, mk_float_eq k;; |
| 357 |
|
| 358 |
(* exp upper bound *) |
| 359 |
let float_exp_hi pp x_tm = |
| 360 |
let flag, k, k_eq = reduce_float x_tm in |
| 361 |
if not flag then |
| 362 |
float_exp_high_raw pp x_tm |
| 363 |
else |
| 364 |
let ltm, rtm = dest_comb (concl k_eq) in |
| 365 |
let k_tm = rand rtm and |
| 366 |
t_tm = rand ltm in |
| 367 |
let t_gt0 = EQT_ELIM (float_gt0 t_tm) in |
| 368 |
let div_th = float_div_hi (pp + 1) x_tm t_tm in |
| 369 |
let y_tm = rand (concl div_th) in |
| 370 |
let exp_y = float_exp_high_raw (pp + 1) y_tm in |
| 371 |
let hi_tm = rand (concl exp_y) in |
| 372 |
let hi_pow = float_pow_hi pp k hi_tm in |
| 373 |
let high_tm = rand (concl hi_pow) in |
| 374 |
let th0 = INST[t_tm, t_var_real; k_tm, k_var_num; |
| 375 |
y_tm, y_var_real; hi_tm, hi_var_real; |
| 376 |
high_tm, high_var_real; x_tm, x_var_real] exp_reduce_high in |
| 377 |
itlist MY_PROVE_HYP [t_gt0; k_eq; div_th; exp_y; hi_pow] th0;; |
| 378 |
|
| 379 |
(* exp lower bound *) |
| 380 |
let float_exp_lo pp x_tm = |
| 381 |
let flag, k, k_eq = reduce_float x_tm in |
| 382 |
if not flag then |
| 383 |
float_exp_low_raw pp x_tm |
| 384 |
else |
| 385 |
let ltm, rtm = dest_comb (concl k_eq) in |
| 386 |
let k_tm = rand rtm and |
| 387 |
t_tm = rand ltm in |
| 388 |
let t_gt0 = EQT_ELIM (float_gt0 t_tm) in |
| 389 |
let div_th = float_div_lo (pp + 1) x_tm t_tm in |
| 390 |
let y_tm = rand (rator (concl div_th)) in |
| 391 |
let exp_y = float_exp_low_raw (pp + 1) y_tm in |
| 392 |
let lo_tm = rand (rator (concl exp_y)) in |
| 393 |
let lo_pow = float_pow_lo pp k lo_tm in |
| 394 |
let low_tm = rand (rator (concl lo_pow)) in |
| 395 |
let lo_ge0 = EQT_ELIM (float_ge0 lo_tm) in |
| 396 |
let th0 = INST[t_tm, t_var_real; k_tm, k_var_num; |
| 397 |
y_tm, y_var_real; lo_tm, lo_var_real; |
| 398 |
low_tm, low_var_real; x_tm, x_var_real] exp_reduce_low in |
| 399 |
itlist MY_PROVE_HYP [t_gt0; k_eq; div_th; exp_y; lo_pow; lo_ge0] th0;; |
| 400 |
|
| 401 |
(* exp interval *) |
| 402 |
let float_interval_exp pp x_th = |
| 403 |
let x_tm, a_tm, b_tm = dest_float_interval (concl x_th) in |
| 404 |
let exp_lo = float_exp_lo pp a_tm and |
| 405 |
exp_hi = float_exp_hi pp b_tm in |
| 406 |
let lo_tm = rand (rator (concl exp_lo)) and |
| 407 |
hi_tm = rand (concl exp_hi) in |
| 408 |
let th0 = INST[x_tm, x_var_real; a_tm, a_var_real; b_tm, b_var_real; |
| 409 |
lo_tm, lo_var_real; hi_tm, hi_var_real] exp_interval in |
| 410 |
MY_PROVE_HYP x_th (MY_PROVE_HYP exp_lo (MY_PROVE_HYP exp_hi th0));; |
| 411 |
|
| 412 |
end;; |
| 413 |
|
| 414 |
|
| 415 |
(* |
| 416 |
let x_th = float_interval_const 4 `&1`;; |
| 417 |
float_interval_exp 4 x_th;; |
| 418 |
float_interval_exp 20 x_th;; |
| 419 |
*) |
| 420 |
|