| 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: log *) |
| 11 |
(* -------------------------------------------------------------------------- *) |
| 12 |
|
| 13 |
needs "Formal_ineqs/trig/exp_log.hl";; |
| 14 |
needs "Formal_ineqs/trig/poly_eval.hl";; |
| 15 |
needs "Formal_ineqs/trig/exp_eval.hl";; |
| 16 |
|
| 17 |
module type Log_eval_sig = |
| 18 |
sig |
| 19 |
val float_log_hi : int -> term -> thm |
| 20 |
val float_log_lo : int -> term -> thm |
| 21 |
val float_interval_log : int -> thm -> thm |
| 22 |
end;; |
| 23 |
|
| 24 |
module Log_eval : Log_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_float;; |
| 34 |
open More_float;; |
| 35 |
open Exp_eval;; |
| 36 |
open Misc_vars;; |
| 37 |
|
| 38 |
prioritize_real();; |
| 39 |
|
| 40 |
(* |
| 41 |
(* Defines the maximum x for which a poly approximation is computed directly. *) |
| 42 |
(* Should be in (0, 1). *) |
| 43 |
let log_max_x = 0.01;; |
| 44 |
*) |
| 45 |
|
| 46 |
let th_rule = UNDISCH_ALL o PURE_REWRITE_RULE[GSYM IMP_IMP] o SPEC_ALL;; |
| 47 |
|
| 48 |
(* ----------------- *) |
| 49 |
(* Theorems *) |
| 50 |
(* ----------------- *) |
| 51 |
|
| 52 |
let log_interval = (th_rule o prove) |
| 53 |
(`interval_arith x (a, b) /\ |
| 54 |
&0 < a /\ |
| 55 |
lo <= log a /\ |
| 56 |
log b <= hi |
| 57 |
==> interval_arith (log x) (lo, hi)`, |
| 58 |
REWRITE_TAC[interval_arith] THEN STRIP_TAC THEN |
| 59 |
SUBGOAL_THEN `&0 < b /\ &0 < x` ASSUME_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN |
| 60 |
CONJ_TAC THEN MATCH_MP_TAC REAL_LE_TRANS THENL [ |
| 61 |
EXISTS_TAC `log a` THEN ASM_SIMP_TAC[LOG_MONO_LE]; |
| 62 |
EXISTS_TAC `log b` THEN ASM_SIMP_TAC[LOG_MONO_LE] |
| 63 |
]);; |
| 64 |
|
| 65 |
(* Argument reduction *) |
| 66 |
|
| 67 |
let log_reduce = prove |
| 68 |
(`!x k. &0 < x ==> log x = log (&1 + (x / exp k - &1)) + k`, |
| 69 |
REPEAT STRIP_TAC THEN REWRITE_TAC[REAL_ARITH `a + (b - a) = b`] THEN |
| 70 |
ASM_SIMP_TAC[LOG_DIV; REAL_EXP_POS_LT; LOG_EXP] THEN REAL_ARITH_TAC);; |
| 71 |
|
| 72 |
let log_reduce_exp_high = prove |
| 73 |
(`t <= exp k /\ x / t <= r /\ r - &1 <= a /\ |
| 74 |
&0 < x /\ &0 < t /\ |
| 75 |
log (&1 + a) <= hi /\ hi + k <= high |
| 76 |
==> log x <= high`, |
| 77 |
REPEAT STRIP_TAC THEN ASM_SIMP_TAC[log_reduce] THEN |
| 78 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `hi + k` THEN |
| 79 |
ASM_REWRITE_TAC[REAL_LE_RADD] THEN |
| 80 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `log (&1 + a)` THEN |
| 81 |
ASM_REWRITE_TAC[] THEN |
| 82 |
SUBGOAL_THEN `&0 < &1 + a /\ &0 < &1 + x / exp k - &1` ASSUME_TAC THENL [ |
| 83 |
CONJ_TAC THENL [ |
| 84 |
SUBGOAL_THEN `&0 < r` MP_TAC THENL [ |
| 85 |
MATCH_MP_TAC REAL_LTE_TRANS THEN EXISTS_TAC `x / t` THEN |
| 86 |
ASM_SIMP_TAC[REAL_LT_DIV]; |
| 87 |
ALL_TAC |
| 88 |
] THEN |
| 89 |
ASM_ARITH_TAC; |
| 90 |
REWRITE_TAC[REAL_ARITH `a + b - a = b`] THEN |
| 91 |
ASM_SIMP_TAC[REAL_LT_DIV; REAL_EXP_POS_LT] |
| 92 |
]; |
| 93 |
ALL_TAC |
| 94 |
] THEN |
| 95 |
ASM_SIMP_TAC[LOG_MONO_LE; REAL_LE_LADD] THEN |
| 96 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `r - &1` THEN |
| 97 |
ASM_REWRITE_TAC[real_sub; REAL_LE_RADD] THEN |
| 98 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `x / t` THEN |
| 99 |
ASM_SIMP_TAC[real_div; REAL_LE_LMUL_EQ] THEN |
| 100 |
MATCH_MP_TAC REAL_LE_INV2 THEN ASM_REWRITE_TAC[]);; |
| 101 |
|
| 102 |
let log_reduce_exp_low = prove |
| 103 |
(`exp k <= t /\ r <= x / t /\ a <= r - &1 /\ |
| 104 |
&0 < x /\ -- &1 < a /\ |
| 105 |
lo <= log (&1 + a) /\ low <= lo + k |
| 106 |
==> low <= log x`, |
| 107 |
REPEAT STRIP_TAC THEN ASM_SIMP_TAC[log_reduce] THEN |
| 108 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `lo + k` THEN |
| 109 |
ASM_REWRITE_TAC[REAL_LE_RADD] THEN |
| 110 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `log (&1 + a)` THEN |
| 111 |
ASM_REWRITE_TAC[] THEN |
| 112 |
SUBGOAL_THEN `&0 < &1 + a /\ &0 < &1 + x / exp k - &1` ASSUME_TAC THENL [ |
| 113 |
CONJ_TAC THENL [ ASM_ARITH_TAC; ALL_TAC ] THEN |
| 114 |
REWRITE_TAC[REAL_ARITH `a + b - a = b`] THEN |
| 115 |
ASM_SIMP_TAC[REAL_LT_DIV; REAL_EXP_POS_LT]; |
| 116 |
ALL_TAC |
| 117 |
] THEN |
| 118 |
ASM_SIMP_TAC[LOG_MONO_LE; REAL_LE_LADD] THEN |
| 119 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `r - &1` THEN |
| 120 |
ASM_REWRITE_TAC[real_sub; REAL_LE_RADD] THEN |
| 121 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `x / t` THEN |
| 122 |
ASM_SIMP_TAC[real_div; REAL_LE_LMUL_EQ] THEN |
| 123 |
MATCH_MP_TAC REAL_LE_INV2 THEN ASM_REWRITE_TAC[REAL_EXP_POS_LT]);; |
| 124 |
|
| 125 |
let log_reduce_simple_high = (th_rule o REWRITE_RULE[GSYM float1_eq] o prove) |
| 126 |
(`t <= exp b /\ x / t <= r /\ r - &1 <= a /\ |
| 127 |
&0 < x /\ &0 < t /\ |
| 128 |
a + b <= high |
| 129 |
==> log x <= high`, |
| 130 |
REPEAT STRIP_TAC THEN MATCH_MP_TAC (GEN_ALL log_reduce_exp_high) THEN |
| 131 |
MAP_EVERY EXISTS_TAC [`r:real`; `t:real`; `a:real`; `a:real`; `b:real`] THEN |
| 132 |
ASM_REWRITE_TAC[] THEN MATCH_MP_TAC LOG_LE_STRONG THEN |
| 133 |
SUBGOAL_THEN `&0 < r` MP_TAC THENL [ |
| 134 |
MATCH_MP_TAC REAL_LTE_TRANS THEN EXISTS_TAC `x / t` THEN |
| 135 |
ASM_SIMP_TAC[REAL_LT_DIV]; |
| 136 |
ALL_TAC |
| 137 |
] THEN |
| 138 |
ASM_ARITH_TAC);; |
| 139 |
|
| 140 |
let log_reduce_simple_low = (th_rule o prove) |
| 141 |
(`inv x <= t /\ log t <= r /\ |
| 142 |
low = --r /\ &0 < x |
| 143 |
==> low <= log x`, |
| 144 |
REPEAT STRIP_TAC THEN ONCE_REWRITE_TAC[REAL_ARITH `a <= b <=> --b <= --a`] THEN |
| 145 |
ASM_SIMP_TAC[GSYM LOG_INV; REAL_NEG_NEG] THEN |
| 146 |
MATCH_MP_TAC REAL_LE_TRANS THEN EXISTS_TAC `log t` THEN |
| 147 |
SUBGOAL_THEN `&0 < inv x /\ &0 < t` ASSUME_TAC THENL [ |
| 148 |
MP_TAC (SPEC `x:real` REAL_LT_INV) THEN ASM_SIMP_TAC[] THEN |
| 149 |
ASM_ARITH_TAC; |
| 150 |
ALL_TAC |
| 151 |
] THEN |
| 152 |
ASM_SIMP_TAC[LOG_MONO_LE]);; |
| 153 |
|
| 154 |
|
| 155 |
(* ----------------- *) |
| 156 |
(* Functions *) |
| 157 |
(* ----------------- *) |
| 158 |
|
| 159 |
let float_log_hi pp x_tm = |
| 160 |
let pp = pp + 1 in |
| 161 |
let x_gt0 = EQT_ELIM (float_gt0 x_tm) in |
| 162 |
let f = log (float_of_float_tm x_tm) in |
| 163 |
let b_tm = float_tm_of_float pp f in |
| 164 |
let exp_lo = float_exp_lo pp b_tm in |
| 165 |
let t_tm = rand (rator (concl exp_lo)) in |
| 166 |
let t_gt0 = EQT_ELIM (float_gt0 t_tm) in |
| 167 |
let xt_le_r = float_div_hi pp x_tm t_tm in |
| 168 |
let r_tm = rand (concl xt_le_r) in |
| 169 |
let r1_le_a = float_sub_hi pp r_tm one_float in |
| 170 |
let a_tm = rand (concl r1_le_a) in |
| 171 |
let ab_le_high = float_add_hi pp a_tm b_tm in |
| 172 |
let high_tm = rand (concl ab_le_high) in |
| 173 |
let th0 = INST[x_tm, x_var_real; x_tm, x_var_real; |
| 174 |
t_tm, t_var_real; r_tm, r_var_real; b_tm, b_var_real; |
| 175 |
a_tm, a_var_real; high_tm, high_var_real] log_reduce_simple_high in |
| 176 |
itlist MY_PROVE_HYP [exp_lo; xt_le_r; r1_le_a; ab_le_high; x_gt0; t_gt0] th0;; |
| 177 |
|
| 178 |
let float_log_lo pp x_tm = |
| 179 |
let x_gt0 = EQT_ELIM (float_gt0 x_tm) in |
| 180 |
let inv_x = float_inv_hi (pp + 1) x_tm in |
| 181 |
let t_tm = rand (concl inv_x) in |
| 182 |
let log_t = float_log_hi pp t_tm in |
| 183 |
let r_tm = rand (concl log_t) in |
| 184 |
let low_eq = float_neg r_tm in |
| 185 |
let low_tm = rand (rator (concl low_eq)) in |
| 186 |
let th0 = INST[x_tm, x_var_real; t_tm, t_var_real; |
| 187 |
r_tm, r_var_real; low_tm, low_var_real] log_reduce_simple_low in |
| 188 |
itlist MY_PROVE_HYP [inv_x; log_t; low_eq; x_gt0] th0;; |
| 189 |
|
| 190 |
(* log interval *) |
| 191 |
let float_interval_log pp x_th = |
| 192 |
let x_tm, a_tm, b_tm = dest_float_interval (concl x_th) in |
| 193 |
let a_gt0 = EQT_ELIM (float_gt0 a_tm) in |
| 194 |
let log_lo = float_log_lo pp a_tm and |
| 195 |
log_hi = float_log_hi pp b_tm in |
| 196 |
let lo_tm = rand (rator (concl log_lo)) and |
| 197 |
hi_tm = rand (concl log_hi) in |
| 198 |
let th0 = INST[x_tm, x_var_real; a_tm, a_var_real; b_tm, b_var_real; |
| 199 |
lo_tm, lo_var_real; hi_tm, hi_var_real] log_interval in |
| 200 |
itlist MY_PROVE_HYP [x_th; log_lo; log_hi; a_gt0] th0;; |
| 201 |
|
| 202 |
end;; |
| 203 |
|
| 204 |
(* |
| 205 |
|
| 206 |
let x_th = float_interval_const 4 `&100000`;; |
| 207 |
float_interval_exp 20 x_th;; |
| 208 |
float_interval_log 18 x_th;; |
| 209 |
float_interval_exp 20 x_th;; |
| 210 |
|
| 211 |
*) |