hoelzl@40881: (* Author: Johannes Hoelzl, TU Muenchen hoelzl@40881: Coercions removed by Dmitriy Traytel *) wenzelm@30122: wenzelm@30886: header {* Prove Real Valued Inequalities by Computation *} wenzelm@30122: wenzelm@40892: theory Approximation wenzelm@41413: imports wenzelm@41413: Complex_Main wenzelm@41413: "~~/src/HOL/Library/Float" wenzelm@51544: Dense_Linear_Order haftmann@51143: "~~/src/HOL/Library/Code_Target_Numeral" haftmann@56923: keywords "approximate" :: diag hoelzl@29805: begin hoelzl@29805: haftmann@54489: declare powr_one [simp] haftmann@54489: declare powr_numeral [simp] haftmann@54489: declare powr_neg_one [simp] haftmann@54489: declare powr_neg_numeral [simp] hoelzl@47600: hoelzl@29805: section "Horner Scheme" hoelzl@29805: hoelzl@29805: subsection {* Define auxiliary helper @{text horner} function *} hoelzl@29805: hoelzl@31098: primrec horner :: "(nat \ nat) \ (nat \ nat \ nat) \ nat \ nat \ nat \ real \ real" where hoelzl@29805: "horner F G 0 i k x = 0" | hoelzl@40881: "horner F G (Suc n) i k x = 1 / k - x * horner F G n (F i) (G i k) x" hoelzl@29805: wenzelm@49351: lemma horner_schema': wenzelm@49351: fixes x :: real and a :: "nat \ real" hoelzl@29805: shows "a 0 - x * (\ i=0.. i=0..i. - (x * ((-1)^i * a (Suc i) * x ^ i)) = (-1)^(Suc i) * a (Suc i) * x ^ (Suc i)" wenzelm@49351: by auto wenzelm@49351: show ?thesis haftmann@54230: unfolding setsum_right_distrib shift_pow uminus_add_conv_diff [symmetric] setsum_negf[symmetric] wenzelm@49351: setsum_head_upt_Suc[OF zero_less_Suc] haftmann@57418: setsum.reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\ n. (-1)^n *a n * x^n"] by auto hoelzl@29805: qed hoelzl@29805: wenzelm@49351: lemma horner_schema: wenzelm@49351: fixes f :: "nat \ nat" and G :: "nat \ nat \ nat" and F :: "nat \ nat" haftmann@30971: assumes f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" hoelzl@40881: shows "horner F G n ((F ^^ j') s) (f j') x = (\ j = 0..< n. -1 ^ j * (1 / (f (j' + j))) * x ^ j)" wenzelm@45129: proof (induct n arbitrary: j') wenzelm@45129: case 0 wenzelm@45129: then show ?case by auto wenzelm@45129: next hoelzl@29805: case (Suc n) hoelzl@29805: show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc] hoelzl@40881: using horner_schema'[of "\ j. 1 / (f (j' + j))"] by auto wenzelm@45129: qed hoelzl@29805: hoelzl@29805: lemma horner_bounds': hoelzl@40881: fixes lb :: "nat \ nat \ nat \ float \ float" and ub :: "nat \ nat \ nat \ float \ float" hoelzl@31098: assumes "0 \ real x" and f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" wenzelm@49351: and lb_0: "\ i k x. lb 0 i k x = 0" wenzelm@49351: and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k - x * (ub n (F i) (G i k) x)" wenzelm@49351: and ub_0: "\ i k x. ub 0 i k x = 0" wenzelm@49351: and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k - x * (lb n (F i) (G i k) x)" hoelzl@40881: shows "(lb n ((F ^^ j') s) (f j') x) \ horner F G n ((F ^^ j') s) (f j') x \ hoelzl@40881: horner F G n ((F ^^ j') s) (f j') x \ (ub n ((F ^^ j') s) (f j') x)" hoelzl@29805: (is "?lb n j' \ ?horner n j' \ ?horner n j' \ ?ub n j'") hoelzl@29805: proof (induct n arbitrary: j') wenzelm@49351: case 0 wenzelm@49351: thus ?case unfolding lb_0 ub_0 horner.simps by auto hoelzl@29805: next hoelzl@29805: case (Suc n) hoelzl@47599: thus ?case using lapprox_rat[of prec 1 "f j'"] using rapprox_rat[of 1 "f j'" prec] hoelzl@47599: Suc[where j'="Suc j'"] `0 \ real x` hoelzl@47599: by (auto intro!: add_mono mult_left_mono simp add: lb_Suc ub_Suc field_simps f_Suc) hoelzl@29805: qed hoelzl@29805: hoelzl@29805: subsection "Theorems for floating point functions implementing the horner scheme" hoelzl@29805: hoelzl@29805: text {* hoelzl@29805: hoelzl@29805: Here @{term_type "f :: nat \ nat"} is the sequence defining the Taylor series, the coefficients are hoelzl@29805: all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}. hoelzl@29805: hoelzl@29805: *} hoelzl@29805: wenzelm@49351: lemma horner_bounds: wenzelm@49351: fixes F :: "nat \ nat" and G :: "nat \ nat \ nat" hoelzl@31098: assumes "0 \ real x" and f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" wenzelm@49351: and lb_0: "\ i k x. lb 0 i k x = 0" wenzelm@49351: and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k - x * (ub n (F i) (G i k) x)" wenzelm@49351: and ub_0: "\ i k x. ub 0 i k x = 0" wenzelm@49351: and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k - x * (lb n (F i) (G i k) x)" hoelzl@40881: shows "(lb n ((F ^^ j') s) (f j') x) \ (\j=0..j=0.. (ub n ((F ^^ j') s) (f j') x)" (is "?ub") hoelzl@29805: proof - hoelzl@31809: have "?lb \ ?ub" hoelzl@31098: using horner_bounds'[where lb=lb, OF `0 \ real x` f_Suc lb_0 lb_Suc ub_0 ub_Suc] hoelzl@29805: unfolding horner_schema[where f=f, OF f_Suc] . hoelzl@29805: thus "?lb" and "?ub" by auto hoelzl@29805: qed hoelzl@29805: wenzelm@49351: lemma horner_bounds_nonpos: wenzelm@49351: fixes F :: "nat \ nat" and G :: "nat \ nat \ nat" hoelzl@31098: assumes "real x \ 0" and f_Suc: "\n. f (Suc n) = G ((F ^^ n) s) (f n)" wenzelm@49351: and lb_0: "\ i k x. lb 0 i k x = 0" wenzelm@49351: and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 k + x * (ub n (F i) (G i k) x)" wenzelm@49351: and ub_0: "\ i k x. ub 0 i k x = 0" wenzelm@49351: and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 k + x * (lb n (F i) (G i k) x)" hoelzl@40881: shows "(lb n ((F ^^ j') s) (f j') x) \ (\j=0..j=0.. (ub n ((F ^^ j') s) (f j') x)" (is "?ub") hoelzl@29805: proof - hoelzl@47599: { fix x y z :: float have "x - y * z = x + - y * z" by simp } note diff_mult_minus = this hoelzl@40881: have sum_eq: "(\j=0..j = 0.. real (-x)" using assms by auto hoelzl@29805: from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec hoelzl@29805: and lb="\ n i k x. lb n i k (-x)" and ub="\ n i k x. ub n i k (-x)", unfolded lb_Suc ub_Suc diff_mult_minus, hoelzl@29805: OF this f_Suc lb_0 refl ub_0 refl] hoelzl@29805: show "?lb" and "?ub" unfolding minus_minus sum_eq hoelzl@29805: by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: subsection {* Selectors for next even or odd number *} hoelzl@29805: hoelzl@29805: text {* hoelzl@29805: hoelzl@29805: The horner scheme computes alternating series. To get the upper and lower bounds we need to hoelzl@29805: guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}. hoelzl@29805: hoelzl@29805: *} hoelzl@29805: hoelzl@29805: definition get_odd :: "nat \ nat" where hoelzl@29805: "get_odd n = (if odd n then n else (Suc n))" hoelzl@29805: hoelzl@29805: definition get_even :: "nat \ nat" where hoelzl@29805: "get_even n = (if even n then n else (Suc n))" hoelzl@29805: hoelzl@29805: lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto) hoelzl@29805: lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto) hoelzl@29805: lemma get_odd_ex: "\ k. Suc k = get_odd n \ odd (Suc k)" hoelzl@54269: by (auto simp: get_odd_def odd_pos intro!: exI[of _ "n - 1"]) hoelzl@29805: hoelzl@29805: lemma get_even_double: "\i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] . hoelzl@29805: lemma get_odd_double: "\i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto hoelzl@29805: hoelzl@29805: section "Power function" hoelzl@29805: hoelzl@29805: definition float_power_bnds :: "nat \ float \ float \ float * float" where hoelzl@29805: "float_power_bnds n l u = (if odd n \ 0 < l then (l ^ n, u ^ n) hoelzl@29805: else if u < 0 then (u ^ n, l ^ n) hoelzl@29805: else (0, (max (-l) u) ^ n))" hoelzl@29805: hoelzl@54269: lemma float_power_bnds: "(l1, u1) = float_power_bnds n l u \ x \ {l .. u} \ (x::real) ^ n \ {l1..u1}" hoelzl@54269: by (auto simp: float_power_bnds_def max_def split: split_if_asm hoelzl@54269: intro: power_mono_odd power_mono power_mono_even zero_le_even_power) hoelzl@29805: hoelzl@40881: lemma bnds_power: "\ (x::real) l u. (l1, u1) = float_power_bnds n l u \ x \ {l .. u} \ l1 \ x ^ n \ x ^ n \ u1" hoelzl@29805: using float_power_bnds by auto hoelzl@29805: hoelzl@29805: section "Square root" hoelzl@29805: hoelzl@29805: text {* hoelzl@29805: hoelzl@29805: The square root computation is implemented as newton iteration. As first first step we use the hoelzl@29805: nearest power of two greater than the square root. hoelzl@29805: hoelzl@29805: *} hoelzl@29805: hoelzl@29805: fun sqrt_iteration :: "nat \ nat \ float \ float" where hoelzl@47599: "sqrt_iteration prec 0 x = Float 1 ((bitlen \mantissa x\ + exponent x) div 2 + 1)" | hoelzl@31467: "sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x hoelzl@29805: in Float 1 -1 * (y + float_divr prec x y))" hoelzl@29805: hoelzl@47599: lemma compute_sqrt_iteration_base[code]: hoelzl@47599: shows "sqrt_iteration prec n (Float m e) = hoelzl@47599: (if n = 0 then Float 1 ((if m = 0 then 0 else bitlen \m\ + e) div 2 + 1) hoelzl@47599: else (let y = sqrt_iteration prec (n - 1) (Float m e) in hoelzl@47599: Float 1 -1 * (y + float_divr prec (Float m e) y)))" hoelzl@47599: using bitlen_Float by (cases n) simp_all hoelzl@47599: hoelzl@31467: function ub_sqrt lb_sqrt :: "nat \ float \ float" where hoelzl@31467: "ub_sqrt prec x = (if 0 < x then (sqrt_iteration prec prec x) hoelzl@31467: else if x < 0 then - lb_sqrt prec (- x) hoelzl@31467: else 0)" | hoelzl@31467: "lb_sqrt prec x = (if 0 < x then (float_divl prec x (sqrt_iteration prec prec x)) hoelzl@31467: else if x < 0 then - ub_sqrt prec (- x) hoelzl@31467: else 0)" hoelzl@31467: by pat_completeness auto blanchet@55414: termination by (relation "measure (\ v. let (prec, x) = case_sum id id v in (if x < 0 then 1 else 0))", auto) hoelzl@29805: hoelzl@31467: declare lb_sqrt.simps[simp del] hoelzl@31467: declare ub_sqrt.simps[simp del] hoelzl@29805: hoelzl@29805: lemma sqrt_ub_pos_pos_1: hoelzl@29805: assumes "sqrt x < b" and "0 < b" and "0 < x" hoelzl@29805: shows "sqrt x < (b + x / b)/2" hoelzl@29805: proof - wenzelm@53077: from assms have "0 < (b - sqrt x)\<^sup>2 " by simp wenzelm@53077: also have "\ = b\<^sup>2 - 2 * b * sqrt x + (sqrt x)\<^sup>2" by algebra wenzelm@53077: also have "\ = b\<^sup>2 - 2 * b * sqrt x + x" using assms by simp wenzelm@53077: finally have "0 < b\<^sup>2 - 2 * b * sqrt x + x" . hoelzl@29805: hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms hoelzl@29805: by (simp add: field_simps power2_eq_square) hoelzl@29805: thus ?thesis by (simp add: field_simps) hoelzl@29805: qed hoelzl@29805: hoelzl@31098: lemma sqrt_iteration_bound: assumes "0 < real x" hoelzl@54269: shows "sqrt x < sqrt_iteration prec n x" hoelzl@29805: proof (induct n) hoelzl@29805: case 0 hoelzl@29805: show ?case hoelzl@29805: proof (cases x) hoelzl@29805: case (Float m e) hoelzl@47599: hence "0 < m" using assms powr_gt_zero[of 2 e] by (auto simp: sign_simps) hoelzl@40881: hence "0 < sqrt m" by auto hoelzl@40881: hoelzl@47599: have int_nat_bl: "(nat (bitlen m)) = bitlen m" using bitlen_nonneg by auto hoelzl@47599: hoelzl@47599: have "x = (m / 2^nat (bitlen m)) * 2 powr (e + (nat (bitlen m)))" hoelzl@47599: unfolding Float by (auto simp: powr_realpow[symmetric] field_simps powr_add) hoelzl@47599: also have "\ < 1 * 2 powr (e + nat (bitlen m))" hoelzl@29805: proof (rule mult_strict_right_mono, auto) hoelzl@31467: show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2] wenzelm@32960: unfolding real_of_int_less_iff[of m, symmetric] by auto hoelzl@29805: qed hoelzl@47599: finally have "sqrt x < sqrt (2 powr (e + bitlen m))" unfolding int_nat_bl by auto hoelzl@47599: also have "\ \ 2 powr ((e + bitlen m) div 2 + 1)" hoelzl@29805: proof - hoelzl@29805: let ?E = "e + bitlen m" hoelzl@47599: have E_mod_pow: "2 powr (?E mod 2) < 4" hoelzl@29805: proof (cases "?E mod 2 = 1") wenzelm@32960: case True thus ?thesis by auto hoelzl@29805: next wenzelm@32960: case False wenzelm@32960: have "0 \ ?E mod 2" by auto wenzelm@32960: have "?E mod 2 < 2" by auto wenzelm@32960: from this[THEN zless_imp_add1_zle] wenzelm@32960: have "?E mod 2 \ 0" using False by auto wenzelm@32960: from xt1(5)[OF `0 \ ?E mod 2` this] wenzelm@32960: show ?thesis by auto hoelzl@29805: qed hoelzl@56889: hence "sqrt (2 powr (?E mod 2)) < sqrt (2 * 2)" hoelzl@56889: by (auto simp del: real_sqrt_four) hoelzl@56889: hence E_mod_pow: "sqrt (2 powr (?E mod 2)) < 2" by auto hoelzl@47599: hoelzl@47599: have E_eq: "2 powr ?E = 2 powr (?E div 2 + ?E div 2 + ?E mod 2)" by auto hoelzl@47599: have "sqrt (2 powr ?E) = sqrt (2 powr (?E div 2) * 2 powr (?E div 2) * 2 powr (?E mod 2))" hoelzl@47599: unfolding E_eq unfolding powr_add[symmetric] by (simp add: int_of_reals del: real_of_ints) hoelzl@47599: also have "\ = 2 powr (?E div 2) * sqrt (2 powr (?E mod 2))" hoelzl@47599: unfolding real_sqrt_mult[of _ "2 powr (?E mod 2)"] real_sqrt_abs2 by auto hoelzl@47599: also have "\ < 2 powr (?E div 2) * 2 powr 1" wenzelm@32960: by (rule mult_strict_left_mono, auto intro: E_mod_pow) haftmann@57512: also have "\ = 2 powr (?E div 2 + 1)" unfolding add.commute[of _ 1] powr_add[symmetric] hoelzl@47599: by simp hoelzl@29805: finally show ?thesis by auto hoelzl@29805: qed hoelzl@47599: finally show ?thesis using `0 < m` hoelzl@47599: unfolding Float hoelzl@47600: by (subst compute_sqrt_iteration_base) (simp add: ac_simps) hoelzl@29805: qed hoelzl@29805: next hoelzl@29805: case (Suc n) hoelzl@29805: let ?b = "sqrt_iteration prec n x" hoelzl@40881: have "0 < sqrt x" using `0 < real x` by auto hoelzl@31098: also have "\ < real ?b" using Suc . hoelzl@40881: finally have "sqrt x < (?b + x / ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < real x`] by auto hoelzl@40881: also have "\ \ (?b + (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr) hoelzl@47599: also have "\ = (Float 1 -1) * (?b + (float_divr prec x ?b))" by simp webertj@49962: finally show ?case unfolding sqrt_iteration.simps Let_def distrib_left . hoelzl@29805: qed hoelzl@29805: hoelzl@31098: lemma sqrt_iteration_lower_bound: assumes "0 < real x" hoelzl@31098: shows "0 < real (sqrt_iteration prec n x)" (is "0 < ?sqrt") hoelzl@29805: proof - hoelzl@40881: have "0 < sqrt x" using assms by auto hoelzl@29805: also have "\ < ?sqrt" using sqrt_iteration_bound[OF assms] . hoelzl@29805: finally show ?thesis . hoelzl@29805: qed hoelzl@29805: hoelzl@31098: lemma lb_sqrt_lower_bound: assumes "0 \ real x" hoelzl@31467: shows "0 \ real (lb_sqrt prec x)" hoelzl@29805: proof (cases "0 < x") hoelzl@47600: case True hence "0 < real x" and "0 \ x" using `0 \ real x` by auto hoelzl@47600: hence "0 < sqrt_iteration prec prec x" using sqrt_iteration_lower_bound by auto hoelzl@47599: hence "0 \ real (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \ x`] unfolding less_eq_float_def by auto hoelzl@31467: thus ?thesis unfolding lb_sqrt.simps using True by auto hoelzl@29805: next hoelzl@47600: case False with `0 \ real x` have "real x = 0" by auto hoelzl@47600: thus ?thesis unfolding lb_sqrt.simps by auto hoelzl@29805: qed hoelzl@29805: wenzelm@49351: lemma bnds_sqrt': "sqrt x \ {(lb_sqrt prec x) .. (ub_sqrt prec x)}" hoelzl@31467: proof - hoelzl@31467: { fix x :: float assume "0 < x" hoelzl@47600: hence "0 < real x" and "0 \ real x" by auto hoelzl@40881: hence sqrt_gt0: "0 < sqrt x" by auto hoelzl@40881: hence sqrt_ub: "sqrt x < sqrt_iteration prec prec x" using sqrt_iteration_bound by auto hoelzl@40881: hoelzl@40881: have "(float_divl prec x (sqrt_iteration prec prec x)) \ hoelzl@40881: x / (sqrt_iteration prec prec x)" by (rule float_divl) hoelzl@40881: also have "\ < x / sqrt x" hoelzl@31467: by (rule divide_strict_left_mono[OF sqrt_ub `0 < real x` hoelzl@31467: mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]]) hoelzl@40881: also have "\ = sqrt x" hoelzl@40881: unfolding inverse_eq_iff_eq[of _ "sqrt x", symmetric] wenzelm@32960: sqrt_divide_self_eq[OF `0 \ real x`, symmetric] by auto hoelzl@40881: finally have "lb_sqrt prec x \ sqrt x" hoelzl@31467: unfolding lb_sqrt.simps if_P[OF `0 < x`] by auto } hoelzl@31467: note lb = this hoelzl@31467: hoelzl@31467: { fix x :: float assume "0 < x" hoelzl@47600: hence "0 < real x" by auto hoelzl@40881: hence "0 < sqrt x" by auto hoelzl@40881: hence "sqrt x < sqrt_iteration prec prec x" hoelzl@31467: using sqrt_iteration_bound by auto hoelzl@40881: hence "sqrt x \ ub_sqrt prec x" hoelzl@31467: unfolding ub_sqrt.simps if_P[OF `0 < x`] by auto } hoelzl@31467: note ub = this hoelzl@31467: hoelzl@31467: show ?thesis hoelzl@54269: using lb[of "-x"] ub[of "-x"] lb[of x] ub[of x] hoelzl@54269: by (auto simp add: lb_sqrt.simps ub_sqrt.simps real_sqrt_minus) hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma bnds_sqrt: "\ (x::real) lx ux. (l, u) = (lb_sqrt prec lx, ub_sqrt prec ux) \ x \ {lx .. ux} \ l \ sqrt x \ sqrt x \ u" hoelzl@31467: proof ((rule allI) +, rule impI, erule conjE, rule conjI) hoelzl@40881: fix x :: real fix lx ux hoelzl@31467: assume "(l, u) = (lb_sqrt prec lx, ub_sqrt prec ux)" hoelzl@40881: and x: "x \ {lx .. ux}" hoelzl@31467: hence l: "l = lb_sqrt prec lx " and u: "u = ub_sqrt prec ux" by auto hoelzl@29805: hoelzl@40881: have "sqrt lx \ sqrt x" using x by auto hoelzl@31467: from order_trans[OF _ this] hoelzl@40881: show "l \ sqrt x" unfolding l using bnds_sqrt'[of lx prec] by auto hoelzl@40881: hoelzl@40881: have "sqrt x \ sqrt ux" using x by auto hoelzl@31467: from order_trans[OF this] hoelzl@40881: show "sqrt x \ u" unfolding u using bnds_sqrt'[of ux prec] by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: section "Arcus tangens and \" hoelzl@29805: hoelzl@29805: subsection "Compute arcus tangens series" hoelzl@29805: hoelzl@29805: text {* hoelzl@29805: hoelzl@29805: As first step we implement the computation of the arcus tangens series. This is only valid in the range hoelzl@29805: @{term "{-1 :: real .. 1}"}. This is used to compute \ and then the entire arcus tangens. hoelzl@29805: hoelzl@29805: *} hoelzl@29805: hoelzl@29805: fun ub_arctan_horner :: "nat \ nat \ nat \ float \ float" hoelzl@29805: and lb_arctan_horner :: "nat \ nat \ nat \ float \ float" where hoelzl@29805: "ub_arctan_horner prec 0 k x = 0" hoelzl@31809: | "ub_arctan_horner prec (Suc n) k x = hoelzl@40881: (rapprox_rat prec 1 k) - x * (lb_arctan_horner prec n (k + 2) x)" hoelzl@29805: | "lb_arctan_horner prec 0 k x = 0" hoelzl@31809: | "lb_arctan_horner prec (Suc n) k x = hoelzl@40881: (lapprox_rat prec 1 k) - x * (ub_arctan_horner prec n (k + 2) x)" hoelzl@29805: wenzelm@49351: lemma arctan_0_1_bounds': wenzelm@49351: assumes "0 \ real x" "real x \ 1" and "even n" hoelzl@40881: shows "arctan x \ {(x * lb_arctan_horner prec n 1 (x * x)) .. (x * ub_arctan_horner prec (Suc n) 1 (x * x))}" hoelzl@29805: proof - hoelzl@54269: let ?c = "\i. -1^i * (1 / (i * 2 + (1::nat)) * real x ^ (i * 2 + 1))" hoelzl@54269: let ?S = "\n. \ i=0.. real (x * x)" by auto hoelzl@29805: from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto hoelzl@31809: hoelzl@40881: have "arctan x \ { ?S n .. ?S (Suc n) }" hoelzl@31098: proof (cases "real x = 0") hoelzl@29805: case False hoelzl@31098: hence "0 < real x" using `0 \ real x` by auto hoelzl@40881: hence prem: "0 < 1 / (0 * 2 + (1::nat)) * real x ^ (0 * 2 + 1)" by auto hoelzl@29805: hoelzl@31098: have "\ real x \ \ 1" using `0 \ real x` `real x \ 1` by auto hoelzl@29805: from mp[OF summable_Leibniz(2)[OF zeroseq_arctan_series[OF this] monoseq_arctan_series[OF this]] prem, THEN spec, of m, unfolded `2 * m = n`] huffman@56195: show ?thesis unfolding arctan_series[OF `\ real x \ \ 1`] Suc_eq_plus1 atLeast0LessThan . hoelzl@29805: qed auto hoelzl@29805: note arctan_bounds = this[unfolded atLeastAtMost_iff] hoelzl@29805: hoelzl@29805: have F: "\n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto hoelzl@29805: hoelzl@31809: note bounds = horner_bounds[where s=1 and f="\i. 2 * i + 1" and j'=0 hoelzl@29805: and lb="\n i k x. lb_arctan_horner prec n k x" hoelzl@31809: and ub="\n i k x. ub_arctan_horner prec n k x", hoelzl@31098: OF `0 \ real (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps] hoelzl@29805: hoelzl@40881: { have "(x * lb_arctan_horner prec n 1 (x*x)) \ ?S n" hoelzl@31098: using bounds(1) `0 \ real x` haftmann@57512: unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric] haftmann@57512: unfolding mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"] hoelzl@29805: by (auto intro!: mult_left_mono) hoelzl@40881: also have "\ \ arctan x" using arctan_bounds .. hoelzl@40881: finally have "(x * lb_arctan_horner prec n 1 (x*x)) \ arctan x" . } hoelzl@29805: moreover hoelzl@40881: { have "arctan x \ ?S (Suc n)" using arctan_bounds .. hoelzl@40881: also have "\ \ (x * ub_arctan_horner prec (Suc n) 1 (x*x))" hoelzl@31098: using bounds(2)[of "Suc n"] `0 \ real x` haftmann@57512: unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric] haftmann@57512: unfolding mult.commute[where 'a=real] mult.commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"] hoelzl@29805: by (auto intro!: mult_left_mono) hoelzl@40881: finally have "arctan x \ (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . } hoelzl@29805: ultimately show ?thesis by auto hoelzl@29805: qed hoelzl@29805: hoelzl@31098: lemma arctan_0_1_bounds: assumes "0 \ real x" "real x \ 1" hoelzl@40881: shows "arctan x \ {(x * lb_arctan_horner prec (get_even n) 1 (x * x)) .. (x * ub_arctan_horner prec (get_odd n) 1 (x * x))}" hoelzl@54269: using hoelzl@54269: arctan_0_1_bounds'[OF assms, of n prec] hoelzl@54269: arctan_0_1_bounds'[OF assms, of "n + 1" prec] hoelzl@54269: arctan_0_1_bounds'[OF assms, of "n - 1" prec] hoelzl@54269: by (auto simp: get_even_def get_odd_def odd_pos simp del: ub_arctan_horner.simps lb_arctan_horner.simps) hoelzl@29805: hoelzl@29805: subsection "Compute \" hoelzl@29805: hoelzl@29805: definition ub_pi :: "nat \ float" where hoelzl@31809: "ub_pi prec = (let A = rapprox_rat prec 1 5 ; hoelzl@29805: B = lapprox_rat prec 1 239 hoelzl@31809: in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) - hoelzl@29805: B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))" hoelzl@29805: hoelzl@29805: definition lb_pi :: "nat \ float" where hoelzl@31809: "lb_pi prec = (let A = lapprox_rat prec 1 5 ; hoelzl@29805: B = rapprox_rat prec 1 239 hoelzl@31809: in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) - hoelzl@29805: B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))" hoelzl@29805: hoelzl@40881: lemma pi_boundaries: "pi \ {(lb_pi n) .. (ub_pi n)}" hoelzl@29805: proof - hoelzl@29805: have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto hoelzl@29805: hoelzl@29805: { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \ k" and "0 < k" and "1 \ k" by auto hoelzl@29805: let ?k = "rapprox_rat prec 1 k" hoelzl@29805: have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto hoelzl@31809: hoelzl@31098: have "0 \ real ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \ k`) hoelzl@47599: have "real ?k \ 1" hoelzl@47599: by (rule rapprox_rat_le1, auto simp add: `0 < k` `1 \ k`) hoelzl@29805: hoelzl@40881: have "1 / k \ ?k" using rapprox_rat[where x=1 and y=k] by auto hoelzl@40881: hence "arctan (1 / k) \ arctan ?k" by (rule arctan_monotone') hoelzl@40881: also have "\ \ (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" hoelzl@31098: using arctan_0_1_bounds[OF `0 \ real ?k` `real ?k \ 1`] by auto hoelzl@40881: finally have "arctan (1 / k) \ ?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k)" . hoelzl@29805: } note ub_arctan = this hoelzl@29805: hoelzl@29805: { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \ k" and "0 < k" by auto hoelzl@29805: let ?k = "lapprox_rat prec 1 k" hoelzl@29805: have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto hoelzl@40881: have "1 / k \ 1" using `1 < k` by auto hoelzl@47599: have "\n. 0 \ real ?k" using lapprox_rat_nonneg[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`) hoelzl@40881: have "\n. real ?k \ 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / k \ 1`) hoelzl@40881: hoelzl@40881: have "?k \ 1 / k" using lapprox_rat[where x=1 and y=k] by auto hoelzl@40881: hoelzl@40881: have "?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k) \ arctan ?k" hoelzl@31098: using arctan_0_1_bounds[OF `0 \ real ?k` `real ?k \ 1`] by auto hoelzl@40881: also have "\ \ arctan (1 / k)" using `?k \ 1 / k` by (rule arctan_monotone') hoelzl@40881: finally have "?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k) \ arctan (1 / k)" . hoelzl@29805: } note lb_arctan = this hoelzl@29805: hoelzl@54269: have "pi \ ub_pi n \ lb_pi n \ pi" hoelzl@54269: unfolding lb_pi_def ub_pi_def machin_pi Let_def unfolding Float_num hoelzl@54269: using lb_arctan[of 5] ub_arctan[of 239] lb_arctan[of 239] ub_arctan[of 5] powr_realpow[of 2 2] haftmann@54230: by (auto intro!: mult_left_mono add_mono simp add: uminus_add_conv_diff [symmetric] simp del: uminus_add_conv_diff) hoelzl@54269: then show ?thesis by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: subsection "Compute arcus tangens in the entire domain" hoelzl@29805: hoelzl@31467: function lb_arctan :: "nat \ float \ float" and ub_arctan :: "nat \ float \ float" where hoelzl@29805: "lb_arctan prec x = (let ub_horner = \ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ; hoelzl@29805: lb_horner = \ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) hoelzl@29805: in (if x < 0 then - ub_arctan prec (-x) else hoelzl@29805: if x \ Float 1 -1 then lb_horner x else hoelzl@31467: if x \ Float 1 1 then Float 1 1 * lb_horner (float_divl prec x (1 + ub_sqrt prec (1 + x * x))) hoelzl@31467: else (let inv = float_divr prec 1 x hoelzl@31467: in if inv > 1 then 0 hoelzl@29805: else lb_pi prec * Float 1 -1 - ub_horner inv)))" hoelzl@29805: hoelzl@29805: | "ub_arctan prec x = (let lb_horner = \ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ; hoelzl@29805: ub_horner = \ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) hoelzl@29805: in (if x < 0 then - lb_arctan prec (-x) else hoelzl@29805: if x \ Float 1 -1 then ub_horner x else hoelzl@31467: if x \ Float 1 1 then let y = float_divr prec x (1 + lb_sqrt prec (1 + x * x)) hoelzl@31467: in if y > 1 then ub_pi prec * Float 1 -1 hoelzl@31467: else Float 1 1 * ub_horner y hoelzl@29805: else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))" hoelzl@29805: by pat_completeness auto blanchet@55414: termination by (relation "measure (\ v. let (prec, x) = case_sum id id v in (if x < 0 then 1 else 0))", auto) hoelzl@29805: hoelzl@29805: declare ub_arctan_horner.simps[simp del] hoelzl@29805: declare lb_arctan_horner.simps[simp del] hoelzl@29805: hoelzl@31098: lemma lb_arctan_bound': assumes "0 \ real x" hoelzl@40881: shows "lb_arctan prec x \ arctan x" hoelzl@29805: proof - hoelzl@47600: have "\ x < 0" and "0 \ x" using `0 \ real x` by auto hoelzl@29805: let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" hoelzl@29805: and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" hoelzl@29805: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "x \ Float 1 -1") hoelzl@47600: case True hence "real x \ 1" by auto hoelzl@29805: show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_P[OF True] hoelzl@31098: using arctan_0_1_bounds[OF `0 \ real x` `real x \ 1`] by auto hoelzl@29805: next hoelzl@47600: case False hence "0 < real x" by auto hoelzl@31098: let ?R = "1 + sqrt (1 + real x * real x)" hoelzl@31467: let ?fR = "1 + ub_sqrt prec (1 + x * x)" hoelzl@29805: let ?DIV = "float_divl prec x ?fR" hoelzl@31467: hoelzl@31098: have sqr_ge0: "0 \ 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto hoelzl@29805: hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) hoelzl@29805: hoelzl@40881: have "sqrt (1 + x * x) \ ub_sqrt prec (1 + x * x)" hoelzl@31467: using bnds_sqrt'[of "1 + x * x"] by auto hoelzl@31467: hoelzl@40881: hence "?R \ ?fR" by auto hoelzl@47600: hence "0 < ?fR" and "0 < real ?fR" using `0 < ?R` by auto hoelzl@29805: hoelzl@40881: have monotone: "(float_divl prec x ?fR) \ x / ?R" hoelzl@29805: proof - hoelzl@40881: have "?DIV \ real x / ?fR" by (rule float_divl) hoelzl@40881: also have "\ \ x / ?R" by (rule divide_left_mono[OF `?R \ ?fR` `0 \ real x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R \ real ?fR`] divisor_gt0]]) hoelzl@29805: finally show ?thesis . hoelzl@29805: qed hoelzl@29805: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "x \ Float 1 1") hoelzl@29805: case True hoelzl@31467: hoelzl@40881: have "x \ sqrt (1 + x * x)" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto hoelzl@40881: also have "\ \ (ub_sqrt prec (1 + x * x))" wenzelm@32960: using bnds_sqrt'[of "1 + x * x"] by auto hoelzl@40881: finally have "real x \ ?fR" by auto hoelzl@40881: moreover have "?DIV \ real x / ?fR" by (rule float_divl) hoelzl@31098: ultimately have "real ?DIV \ 1" unfolding divide_le_eq_1_pos[OF `0 < real ?fR`, symmetric] by auto hoelzl@29805: immler@54782: have "0 \ real ?DIV" using float_divl_lower_bound[OF `0 \ x`] `0 < ?fR` unfolding less_eq_float_def by auto hoelzl@47599: hoelzl@47599: have "(Float 1 1 * ?lb_horner ?DIV) \ 2 * arctan (float_divl prec x ?fR)" wenzelm@32960: using arctan_0_1_bounds[OF `0 \ real ?DIV` `real ?DIV \ 1`] by auto hoelzl@40881: also have "\ \ 2 * arctan (x / ?R)" wenzelm@32960: using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) hoelzl@40881: also have "2 * arctan (x / ?R) = arctan x" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left . hoelzl@29805: finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF True] . hoelzl@29805: next hoelzl@29805: case False hoelzl@47600: hence "2 < real x" by auto hoelzl@31098: hence "1 \ real x" by auto hoelzl@29805: hoelzl@29805: let "?invx" = "float_divr prec 1 x" hoelzl@40881: have "0 \ arctan x" using arctan_monotone'[OF `0 \ real x`] using arctan_tan[of 0, unfolded tan_zero] by auto hoelzl@29805: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "1 < ?invx") wenzelm@32960: case True wenzelm@32960: show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF False] if_P[OF True] hoelzl@40881: using `0 \ arctan x` by auto hoelzl@29805: next wenzelm@32960: case False hoelzl@47600: hence "real ?invx \ 1" by auto wenzelm@32960: have "0 \ real ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \ real x`) wenzelm@32960: hoelzl@40881: have "1 / x \ 0" and "0 < 1 / x" using `0 < real x` by auto hoelzl@40881: hoelzl@47601: have "arctan (1 / x) \ arctan ?invx" unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone', rule float_divr) hoelzl@40881: also have "\ \ (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \ real ?invx` `real ?invx \ 1`] by auto hoelzl@40881: finally have "pi / 2 - (?ub_horner ?invx) \ arctan x" hoelzl@40881: using `0 \ arctan x` arctan_inverse[OF `1 / x \ 0`] wenzelm@32960: unfolding real_sgn_pos[OF `0 < 1 / real x`] le_diff_eq by auto wenzelm@32960: moreover hoelzl@47599: have "lb_pi prec * Float 1 -1 \ pi / 2" hoelzl@47599: unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by simp wenzelm@32960: ultimately wenzelm@32960: show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF `\ x \ Float 1 1`] if_not_P[OF False] wenzelm@32960: by auto hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@31098: lemma ub_arctan_bound': assumes "0 \ real x" hoelzl@40881: shows "arctan x \ ub_arctan prec x" hoelzl@29805: proof - hoelzl@47600: have "\ x < 0" and "0 \ x" using `0 \ real x` by auto hoelzl@29805: hoelzl@29805: let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" hoelzl@29805: and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" hoelzl@29805: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "x \ Float 1 -1") hoelzl@47600: case True hence "real x \ 1" by auto hoelzl@29805: show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_P[OF True] hoelzl@31098: using arctan_0_1_bounds[OF `0 \ real x` `real x \ 1`] by auto hoelzl@29805: next hoelzl@47600: case False hence "0 < real x" by auto hoelzl@31098: let ?R = "1 + sqrt (1 + real x * real x)" hoelzl@31467: let ?fR = "1 + lb_sqrt prec (1 + x * x)" hoelzl@29805: let ?DIV = "float_divr prec x ?fR" hoelzl@31467: hoelzl@31098: have sqr_ge0: "0 \ 1 + real x * real x" using sum_power2_ge_zero[of 1 "real x", unfolded numeral_2_eq_2] by auto hoelzl@31098: hence "0 \ real (1 + x*x)" by auto hoelzl@31467: hoelzl@29805: hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) hoelzl@29805: hoelzl@40881: have "lb_sqrt prec (1 + x * x) \ sqrt (1 + x * x)" hoelzl@31467: using bnds_sqrt'[of "1 + x * x"] by auto hoelzl@40881: hence "?fR \ ?R" by auto hoelzl@47599: have "0 < real ?fR" by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \ real (1 + x*x)`]) hoelzl@29805: hoelzl@40881: have monotone: "x / ?R \ (float_divr prec x ?fR)" hoelzl@29805: proof - hoelzl@40881: from divide_left_mono[OF `?fR \ ?R` `0 \ real x` mult_pos_pos[OF divisor_gt0 `0 < real ?fR`]] hoelzl@40881: have "x / ?R \ x / ?fR" . hoelzl@40881: also have "\ \ ?DIV" by (rule float_divr) hoelzl@29805: finally show ?thesis . hoelzl@29805: qed hoelzl@29805: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "x \ Float 1 1") hoelzl@29805: case True hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "?DIV > 1") wenzelm@32960: case True hoelzl@47599: have "pi / 2 \ ub_pi prec * Float 1 -1" unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto wenzelm@32960: from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le] wenzelm@32960: show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF `x \ Float 1 1`] if_P[OF True] . hoelzl@29805: next wenzelm@32960: case False hoelzl@47600: hence "real ?DIV \ 1" by auto wenzelm@32960: huffman@44349: have "0 \ x / ?R" using `0 \ real x` `0 < ?R` unfolding zero_le_divide_iff by auto wenzelm@32960: hence "0 \ real ?DIV" using monotone by (rule order_trans) wenzelm@32960: hoelzl@40881: have "arctan x = 2 * arctan (x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left . hoelzl@40881: also have "\ \ 2 * arctan (?DIV)" wenzelm@32960: using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) hoelzl@47599: also have "\ \ (Float 1 1 * ?ub_horner ?DIV)" unfolding Float_num wenzelm@32960: using arctan_0_1_bounds[OF `0 \ real ?DIV` `real ?DIV \ 1`] by auto wenzelm@32960: finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF `x \ Float 1 1`] if_not_P[OF False] . hoelzl@29805: qed hoelzl@29805: next hoelzl@29805: case False hoelzl@47600: hence "2 < real x" by auto hoelzl@31098: hence "1 \ real x" by auto hoelzl@31098: hence "0 < real x" by auto hoelzl@47600: hence "0 < x" by auto hoelzl@29805: hoelzl@29805: let "?invx" = "float_divl prec 1 x" hoelzl@40881: have "0 \ arctan x" using arctan_monotone'[OF `0 \ real x`] using arctan_tan[of 0, unfolded tan_zero] by auto hoelzl@29805: hoelzl@31098: have "real ?invx \ 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \ real x` divide_le_eq_1_pos[OF `0 < real x`]) hoelzl@47600: have "0 \ real ?invx" using `0 < x` by (intro float_divl_lower_bound) auto hoelzl@31467: hoelzl@40881: have "1 / x \ 0" and "0 < 1 / x" using `0 < real x` by auto hoelzl@40881: hoelzl@40881: have "(?lb_horner ?invx) \ arctan (?invx)" using arctan_0_1_bounds[OF `0 \ real ?invx` `real ?invx \ 1`] by auto hoelzl@47601: also have "\ \ arctan (1 / x)" unfolding one_float.rep_eq[symmetric] by (rule arctan_monotone', rule float_divl) hoelzl@40881: finally have "arctan x \ pi / 2 - (?lb_horner ?invx)" hoelzl@40881: using `0 \ arctan x` arctan_inverse[OF `1 / x \ 0`] hoelzl@40881: unfolding real_sgn_pos[OF `0 < 1 / x`] le_diff_eq by auto hoelzl@29805: moreover hoelzl@47599: have "pi / 2 \ ub_pi prec * Float 1 -1" unfolding Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto hoelzl@29805: ultimately haftmann@46545: show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`]if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF False] wenzelm@32960: by auto hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma arctan_boundaries: hoelzl@40881: "arctan x \ {(lb_arctan prec x) .. (ub_arctan prec x)}" hoelzl@29805: proof (cases "0 \ x") hoelzl@47600: case True hence "0 \ real x" by auto hoelzl@31098: show ?thesis using ub_arctan_bound'[OF `0 \ real x`] lb_arctan_bound'[OF `0 \ real x`] unfolding atLeastAtMost_iff by auto hoelzl@29805: next hoelzl@29805: let ?mx = "-x" hoelzl@47600: case False hence "x < 0" and "0 \ real ?mx" by auto hoelzl@40881: hence bounds: "lb_arctan prec ?mx \ arctan ?mx \ arctan ?mx \ ub_arctan prec ?mx" hoelzl@31098: using ub_arctan_bound'[OF `0 \ real ?mx`] lb_arctan_bound'[OF `0 \ real ?mx`] by auto hoelzl@47601: show ?thesis unfolding minus_float.rep_eq arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`] hoelzl@47601: unfolding atLeastAtMost_iff using bounds[unfolded minus_float.rep_eq arctan_minus] hoelzl@47599: by (simp add: arctan_minus) hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma bnds_arctan: "\ (x::real) lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \ x \ {lx .. ux} \ l \ arctan x \ arctan x \ u" hoelzl@29805: proof (rule allI, rule allI, rule allI, rule impI) hoelzl@40881: fix x :: real fix lx ux hoelzl@40881: assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \ x \ {lx .. ux}" hoelzl@40881: hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \ {lx .. ux}" by auto hoelzl@29805: hoelzl@29805: { from arctan_boundaries[of lx prec, unfolded l] hoelzl@40881: have "l \ arctan lx" by (auto simp del: lb_arctan.simps) hoelzl@29805: also have "\ \ arctan x" using x by (auto intro: arctan_monotone') hoelzl@40881: finally have "l \ arctan x" . hoelzl@29805: } moreover hoelzl@40881: { have "arctan x \ arctan ux" using x by (auto intro: arctan_monotone') hoelzl@40881: also have "\ \ u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps) hoelzl@40881: finally have "arctan x \ u" . hoelzl@40881: } ultimately show "l \ arctan x \ arctan x \ u" .. hoelzl@29805: qed hoelzl@29805: hoelzl@29805: section "Sinus and Cosinus" hoelzl@29805: hoelzl@29805: subsection "Compute the cosinus and sinus series" hoelzl@29805: hoelzl@29805: fun ub_sin_cos_aux :: "nat \ nat \ nat \ nat \ float \ float" hoelzl@29805: and lb_sin_cos_aux :: "nat \ nat \ nat \ nat \ float \ float" where hoelzl@29805: "ub_sin_cos_aux prec 0 i k x = 0" hoelzl@31809: | "ub_sin_cos_aux prec (Suc n) i k x = hoelzl@40881: (rapprox_rat prec 1 k) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" hoelzl@29805: | "lb_sin_cos_aux prec 0 i k x = 0" hoelzl@31809: | "lb_sin_cos_aux prec (Suc n) i k x = hoelzl@40881: (lapprox_rat prec 1 k) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" hoelzl@47601: hoelzl@29805: lemma cos_aux: hoelzl@40881: shows "(lb_sin_cos_aux prec n 1 1 (x * x)) \ (\ i=0.. i=0.. (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub") hoelzl@29805: proof - hoelzl@47599: have "0 \ real (x * x)" by auto hoelzl@29805: let "?f n" = "fact (2 * n)" hoelzl@29805: hoelzl@31809: { fix n wenzelm@45129: have F: "\m. ((\i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto haftmann@30971: have "?f (Suc n) = ?f n * ((\i. i + 2) ^^ n) 1 * (((\i. i + 2) ^^ n) 1 + 1)" hoelzl@29805: unfolding F by auto } note f_eq = this hoelzl@31809: hoelzl@31809: from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, hoelzl@31098: OF `0 \ real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] hoelzl@31098: show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "real x"]) hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma cos_boundaries: assumes "0 \ real x" and "x \ pi / 2" hoelzl@40881: shows "cos x \ {(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}" hoelzl@31098: proof (cases "real x = 0") hoelzl@31098: case False hence "real x \ 0" by auto hoelzl@47600: hence "0 < x" and "0 < real x" using `0 \ real x` by auto nipkow@56544: have "0 < x * x" using `0 < x` by simp hoelzl@29805: haftmann@30952: { fix x n have "(\ i=0.. i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * x ^ i)" (is "?sum = ?ifsum") hoelzl@29805: proof - hoelzl@29805: have "?sum = ?sum + (\ j = 0 ..< n. 0)" by auto hoelzl@31809: also have "\ = hoelzl@29805: (\ j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\ j = 0 ..< n. 0)" by auto hoelzl@29805: also have "\ = (\ i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)" huffman@56195: unfolding sum_split_even_odd atLeast0LessThan .. hoelzl@29805: also have "\ = (\ i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)" haftmann@57418: by (rule setsum.cong) auto hoelzl@29805: finally show ?thesis by assumption hoelzl@29805: qed } note morph_to_if_power = this hoelzl@29805: hoelzl@29805: hoelzl@29805: { fix n :: nat assume "0 < n" hoelzl@29805: hence "0 < 2 * n" by auto hoelzl@31098: obtain t where "0 < t" and "t < real x" and hoelzl@40881: cos_eq: "cos x = (\ i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * (real x) ^ i) hoelzl@40881: + (cos (t + 1/2 * (2 * n) * pi) / real (fact (2*n))) * (real x)^(2*n)" hoelzl@29805: (is "_ = ?SUM + ?rest / ?fact * ?pow") huffman@44306: using Maclaurin_cos_expansion2[OF `0 < real x` `0 < 2 * n`] huffman@56195: unfolding cos_coeff_def atLeast0LessThan by auto hoelzl@29805: hoelzl@40881: have "cos t * -1^n = cos t * cos (n * pi) + sin t * sin (n * pi)" by auto hoelzl@40881: also have "\ = cos (t + n * pi)" using cos_add by auto hoelzl@29805: also have "\ = ?rest" by auto hoelzl@29805: finally have "cos t * -1^n = ?rest" . hoelzl@29805: moreover hoelzl@40881: have "t \ pi / 2" using `t < real x` and `x \ pi / 2` by auto hoelzl@29805: hence "0 \ cos t" using `0 < t` and cos_ge_zero by auto hoelzl@29805: ultimately have even: "even n \ 0 \ ?rest" and odd: "odd n \ 0 \ - ?rest " by auto hoelzl@29805: hoelzl@29805: have "0 < ?fact" by auto hoelzl@31098: have "0 < ?pow" using `0 < real x` by auto hoelzl@29805: hoelzl@29805: { hoelzl@29805: assume "even n" hoelzl@40881: have "(lb_sin_cos_aux prec n 1 1 (x * x)) \ ?SUM" wenzelm@32960: unfolding morph_to_if_power[symmetric] using cos_aux by auto hoelzl@40881: also have "\ \ cos x" hoelzl@29805: proof - wenzelm@32960: from even[OF `even n`] `0 < ?fact` `0 < ?pow` hoelzl@56571: have "0 \ (?rest / ?fact) * ?pow" by simp wenzelm@32960: thus ?thesis unfolding cos_eq by auto hoelzl@29805: qed hoelzl@40881: finally have "(lb_sin_cos_aux prec n 1 1 (x * x)) \ cos x" . hoelzl@29805: } note lb = this hoelzl@29805: hoelzl@29805: { hoelzl@29805: assume "odd n" hoelzl@40881: have "cos x \ ?SUM" hoelzl@29805: proof - wenzelm@32960: from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] wenzelm@32960: have "0 \ (- ?rest) / ?fact * ?pow" wenzelm@32960: by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) wenzelm@32960: thus ?thesis unfolding cos_eq by auto hoelzl@29805: qed hoelzl@40881: also have "\ \ (ub_sin_cos_aux prec n 1 1 (x * x))" wenzelm@32960: unfolding morph_to_if_power[symmetric] using cos_aux by auto hoelzl@40881: finally have "cos x \ (ub_sin_cos_aux prec n 1 1 (x * x))" . hoelzl@29805: } note ub = this and lb hoelzl@29805: } note ub = this(1) and lb = this(2) hoelzl@29805: hoelzl@40881: have "cos x \ (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . hoelzl@40881: moreover have "(lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \ cos x" hoelzl@29805: proof (cases "0 < get_even n") hoelzl@29805: case True show ?thesis using lb[OF True get_even] . hoelzl@29805: next hoelzl@29805: case False hoelzl@29805: hence "get_even n = 0" by auto hoelzl@40881: have "- (pi / 2) \ x" by (rule order_trans[OF _ `0 < real x`[THEN less_imp_le]], auto) hoelzl@40881: with `x \ pi / 2` hoelzl@47601: show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps minus_float.rep_eq zero_float.rep_eq using cos_ge_zero by auto hoelzl@29805: qed hoelzl@29805: ultimately show ?thesis by auto hoelzl@29805: next hoelzl@29805: case True hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "n = 0") hoelzl@31809: case True hoelzl@47599: thus ?thesis unfolding `n = 0` get_even_def get_odd_def hoelzl@47599: using `real x = 0` lapprox_rat[where x="-1" and y=1] hoelzl@47621: by (auto simp: Float.compute_lapprox_rat Float.compute_rapprox_rat) hoelzl@29805: next hoelzl@29805: case False with not0_implies_Suc obtain m where "n = Suc m" by blast hoelzl@31098: thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto) hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@31098: lemma sin_aux: assumes "0 \ real x" hoelzl@40881: shows "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \ (\ i=0.. i=0.. (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub") hoelzl@29805: proof - hoelzl@47599: have "0 \ real (x * x)" by auto hoelzl@29805: let "?f n" = "fact (2 * n + 1)" hoelzl@29805: hoelzl@31809: { fix n wenzelm@45129: have F: "\m. ((\i. i + 2) ^^ n) m = m + 2 * n" by (induct n) auto haftmann@30971: have "?f (Suc n) = ?f n * ((\i. i + 2) ^^ n) 2 * (((\i. i + 2) ^^ n) 2 + 1)" hoelzl@29805: unfolding F by auto } note f_eq = this hoelzl@31809: hoelzl@29805: from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, hoelzl@31098: OF `0 \ real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] hoelzl@47599: show "?lb" and "?ub" using `0 \ real x` haftmann@57512: unfolding power_add power_one_right mult.assoc[symmetric] setsum_left_distrib[symmetric] haftmann@57512: unfolding mult.commute[where 'a=real] hoelzl@31098: by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real x"]) hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma sin_boundaries: assumes "0 \ real x" and "x \ pi / 2" hoelzl@40881: shows "sin x \ {(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}" hoelzl@31098: proof (cases "real x = 0") hoelzl@31098: case False hence "real x \ 0" by auto hoelzl@47600: hence "0 < x" and "0 < real x" using `0 \ real x` by auto nipkow@56544: have "0 < x * x" using `0 < x` by simp hoelzl@29805: hoelzl@29805: { fix x n have "(\ j = 0 ..< n. -1 ^ (((2 * j + 1) - Suc 0) div 2) / (real (fact (2 * j + 1))) * x ^(2 * j + 1)) hoelzl@29805: = (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * x ^ i)" (is "?SUM = _") hoelzl@29805: proof - hoelzl@29805: have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto hoelzl@29805: have "?SUM = (\ j = 0 ..< n. 0) + ?SUM" by auto hoelzl@29805: also have "\ = (\ i = 0 ..< 2 * n. if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i)) * x ^ i)" huffman@56195: unfolding sum_split_even_odd atLeast0LessThan .. hoelzl@29805: also have "\ = (\ i = 0 ..< 2 * n. (if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i))) * x ^ i)" haftmann@57418: by (rule setsum.cong) auto hoelzl@29805: finally show ?thesis by assumption hoelzl@29805: qed } note setsum_morph = this hoelzl@29805: hoelzl@29805: { fix n :: nat assume "0 < n" hoelzl@29805: hence "0 < 2 * n + 1" by auto hoelzl@31098: obtain t where "0 < t" and "t < real x" and hoelzl@40881: sin_eq: "sin x = (\ i = 0 ..< 2 * n + 1. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i) hoelzl@40881: + (sin (t + 1/2 * (2 * n + 1) * pi) / real (fact (2*n + 1))) * (real x)^(2*n + 1)" hoelzl@29805: (is "_ = ?SUM + ?rest / ?fact * ?pow") huffman@44306: using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < real x`] huffman@56195: unfolding sin_coeff_def atLeast0LessThan by auto hoelzl@29805: webertj@49962: have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add distrib_right distrib_left by auto hoelzl@29805: moreover hoelzl@40881: have "t \ pi / 2" using `t < real x` and `x \ pi / 2` by auto hoelzl@29805: hence "0 \ cos t" using `0 < t` and cos_ge_zero by auto hoelzl@29805: ultimately have even: "even n \ 0 \ ?rest" and odd: "odd n \ 0 \ - ?rest " by auto hoelzl@29805: huffman@44305: have "0 < ?fact" by (simp del: fact_Suc) hoelzl@31098: have "0 < ?pow" using `0 < real x` by (rule zero_less_power) hoelzl@29805: hoelzl@29805: { hoelzl@29805: assume "even n" hoelzl@40881: have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \ hoelzl@31098: (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)" wenzelm@32960: using sin_aux[OF `0 \ real x`] unfolding setsum_morph[symmetric] by auto hoelzl@29805: also have "\ \ ?SUM" by auto hoelzl@40881: also have "\ \ sin x" hoelzl@29805: proof - wenzelm@32960: from even[OF `even n`] `0 < ?fact` `0 < ?pow` hoelzl@56571: have "0 \ (?rest / ?fact) * ?pow" by simp wenzelm@32960: thus ?thesis unfolding sin_eq by auto hoelzl@29805: qed hoelzl@40881: finally have "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \ sin x" . hoelzl@29805: } note lb = this hoelzl@29805: hoelzl@29805: { hoelzl@29805: assume "odd n" hoelzl@40881: have "sin x \ ?SUM" hoelzl@29805: proof - wenzelm@32960: from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] wenzelm@32960: have "0 \ (- ?rest) / ?fact * ?pow" wenzelm@32960: by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) wenzelm@32960: thus ?thesis unfolding sin_eq by auto hoelzl@29805: qed hoelzl@31098: also have "\ \ (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (real x) ^ i)" wenzelm@32960: by auto hoelzl@40881: also have "\ \ (x * ub_sin_cos_aux prec n 2 1 (x * x))" wenzelm@32960: using sin_aux[OF `0 \ real x`] unfolding setsum_morph[symmetric] by auto hoelzl@40881: finally have "sin x \ (x * ub_sin_cos_aux prec n 2 1 (x * x))" . hoelzl@29805: } note ub = this and lb hoelzl@29805: } note ub = this(1) and lb = this(2) hoelzl@29805: hoelzl@40881: have "sin x \ (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . hoelzl@40881: moreover have "(x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \ sin x" hoelzl@29805: proof (cases "0 < get_even n") hoelzl@29805: case True show ?thesis using lb[OF True get_even] . hoelzl@29805: next hoelzl@29805: case False hoelzl@29805: hence "get_even n = 0" by auto hoelzl@40881: with `x \ pi / 2` `0 \ real x` hoelzl@47601: show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps minus_float.rep_eq using sin_ge_zero by auto hoelzl@29805: qed hoelzl@29805: ultimately show ?thesis by auto hoelzl@29805: next hoelzl@29805: case True hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "n = 0") hoelzl@31809: case True hoelzl@31098: thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `real x = 0` lapprox_rat[where x="-1" and y=1] by auto hoelzl@29805: next hoelzl@29805: case False with not0_implies_Suc obtain m where "n = Suc m" by blast hoelzl@31098: thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto) hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@29805: subsection "Compute the cosinus in the entire domain" hoelzl@29805: hoelzl@29805: definition lb_cos :: "nat \ float \ float" where hoelzl@29805: "lb_cos prec x = (let hoelzl@29805: horner = \ x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ; hoelzl@29805: half = \ x. if x < 0 then - 1 else Float 1 1 * x * x - 1 hoelzl@29805: in if x < Float 1 -1 then horner x hoelzl@29805: else if x < 1 then half (horner (x * Float 1 -1)) hoelzl@29805: else half (half (horner (x * Float 1 -2))))" hoelzl@29805: hoelzl@29805: definition ub_cos :: "nat \ float \ float" where hoelzl@29805: "ub_cos prec x = (let hoelzl@29805: horner = \ x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ; hoelzl@29805: half = \ x. Float 1 1 * x * x - 1 hoelzl@29805: in if x < Float 1 -1 then horner x hoelzl@29805: else if x < 1 then half (horner (x * Float 1 -1)) hoelzl@29805: else half (half (horner (x * Float 1 -2))))" hoelzl@29805: hoelzl@40881: lemma lb_cos: assumes "0 \ real x" and "x \ pi" hoelzl@40881: shows "cos x \ {(lb_cos prec x) .. (ub_cos prec x)}" (is "?cos x \ {(?lb x) .. (?ub x) }") hoelzl@29805: proof - hoelzl@29805: { fix x :: real hoelzl@29805: have "cos x = cos (x / 2 + x / 2)" by auto hoelzl@29805: also have "\ = cos (x / 2) * cos (x / 2) + sin (x / 2) * sin (x / 2) - sin (x / 2) * sin (x / 2) + cos (x / 2) * cos (x / 2) - 1" hoelzl@29805: unfolding cos_add by auto hoelzl@29805: also have "\ = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra hoelzl@29805: finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" . hoelzl@29805: } note x_half = this[symmetric] hoelzl@29805: hoelzl@47600: have "\ x < 0" using `0 \ real x` by auto hoelzl@29805: let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)" hoelzl@29805: let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)" hoelzl@29805: let "?ub_half x" = "Float 1 1 * x * x - 1" hoelzl@29805: let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1" hoelzl@29805: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "x < Float 1 -1") hoelzl@47600: case True hence "x \ pi / 2" using pi_ge_two by auto hoelzl@29805: show ?thesis unfolding lb_cos_def[where x=x] ub_cos_def[where x=x] if_not_P[OF `\ x < 0`] if_P[OF `x < Float 1 -1`] Let_def hoelzl@40881: using cos_boundaries[OF `0 \ real x` `x \ pi / 2`] . hoelzl@29805: next hoelzl@29805: case False hoelzl@40881: { fix y x :: float let ?x2 = "(x * Float 1 -1)" hoelzl@40881: assume "y \ cos ?x2" and "-pi \ x" and "x \ pi" hoelzl@47599: hence "- (pi / 2) \ ?x2" and "?x2 \ pi / 2" using pi_ge_two unfolding Float_num by auto hoelzl@29805: hence "0 \ cos ?x2" by (rule cos_ge_zero) hoelzl@31467: hoelzl@40881: have "(?lb_half y) \ cos x" hoelzl@29805: proof (cases "y < 0") wenzelm@32960: case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto hoelzl@29805: next wenzelm@32960: case False hoelzl@47600: hence "0 \ real y" by auto hoelzl@40881: from mult_mono[OF `y \ cos ?x2` `y \ cos ?x2` `0 \ cos ?x2` this] wenzelm@32960: have "real y * real y \ cos ?x2 * cos ?x2" . wenzelm@32960: hence "2 * real y * real y \ 2 * cos ?x2 * cos ?x2" by auto hoelzl@47599: hence "2 * real y * real y - 1 \ 2 * cos (x / 2) * cos (x / 2) - 1" unfolding Float_num by auto hoelzl@47599: thus ?thesis unfolding if_not_P[OF False] x_half Float_num by auto hoelzl@29805: qed hoelzl@29805: } note lb_half = this hoelzl@31467: hoelzl@40881: { fix y x :: float let ?x2 = "(x * Float 1 -1)" hoelzl@40881: assume ub: "cos ?x2 \ y" and "- pi \ x" and "x \ pi" hoelzl@47599: hence "- (pi / 2) \ ?x2" and "?x2 \ pi / 2" using pi_ge_two unfolding Float_num by auto hoelzl@29805: hence "0 \ cos ?x2" by (rule cos_ge_zero) hoelzl@31467: hoelzl@40881: have "cos x \ (?ub_half y)" hoelzl@29805: proof - wenzelm@32960: have "0 \ real y" using `0 \ cos ?x2` ub by (rule order_trans) wenzelm@32960: from mult_mono[OF ub ub this `0 \ cos ?x2`] wenzelm@32960: have "cos ?x2 * cos ?x2 \ real y * real y" . wenzelm@32960: hence "2 * cos ?x2 * cos ?x2 \ 2 * real y * real y" by auto hoelzl@47599: hence "2 * cos (x / 2) * cos (x / 2) - 1 \ 2 * real y * real y - 1" unfolding Float_num by auto hoelzl@47599: thus ?thesis unfolding x_half Float_num by auto hoelzl@29805: qed hoelzl@29805: } note ub_half = this hoelzl@31467: hoelzl@29805: let ?x2 = "x * Float 1 -1" hoelzl@29805: let ?x4 = "x * Float 1 -1 * Float 1 -1" hoelzl@31467: hoelzl@40881: have "-pi \ x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 \ real x` by (rule order_trans) hoelzl@31467: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "x < 1") hoelzl@47600: case True hence "real x \ 1" by auto hoelzl@47599: have "0 \ real ?x2" and "?x2 \ pi / 2" using pi_ge_two `0 \ real x` using assms by auto hoelzl@29805: from cos_boundaries[OF this] hoelzl@40881: have lb: "(?lb_horner ?x2) \ ?cos ?x2" and ub: "?cos ?x2 \ (?ub_horner ?x2)" by auto hoelzl@40881: hoelzl@40881: have "(?lb x) \ ?cos x" hoelzl@29805: proof - hoelzl@40881: from lb_half[OF lb `-pi \ x` `x \ pi`] wenzelm@32960: show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\ x < 0` `\ x < Float 1 -1` `x < 1` by auto hoelzl@29805: qed hoelzl@40881: moreover have "?cos x \ (?ub x)" hoelzl@29805: proof - hoelzl@40881: from ub_half[OF ub `-pi \ x` `x \ pi`] wenzelm@32960: show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\ x < 0` `\ x < Float 1 -1` `x < 1` by auto hoelzl@29805: qed hoelzl@29805: ultimately show ?thesis by auto hoelzl@29805: next hoelzl@29805: case False hoelzl@47599: have "0 \ real ?x4" and "?x4 \ pi / 2" using pi_ge_two `0 \ real x` `x \ pi` unfolding Float_num by auto hoelzl@29805: from cos_boundaries[OF this] hoelzl@40881: have lb: "(?lb_horner ?x4) \ ?cos ?x4" and ub: "?cos ?x4 \ (?ub_horner ?x4)" by auto hoelzl@31467: hoelzl@47600: have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by transfer simp hoelzl@31467: hoelzl@40881: have "(?lb x) \ ?cos x" hoelzl@29805: proof - hoelzl@47599: have "-pi \ ?x2" and "?x2 \ pi" using pi_ge_two `0 \ real x` `x \ pi` by auto hoelzl@40881: from lb_half[OF lb_half[OF lb this] `-pi \ x` `x \ pi`, unfolded eq_4] wenzelm@32960: show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `\ x < 0`] if_not_P[OF `\ x < Float 1 -1`] if_not_P[OF `\ x < 1`] Let_def . hoelzl@29805: qed hoelzl@40881: moreover have "?cos x \ (?ub x)" hoelzl@29805: proof - hoelzl@47599: have "-pi \ ?x2" and "?x2 \ pi" using pi_ge_two `0 \ real x` ` x \ pi` by auto hoelzl@40881: from ub_half[OF ub_half[OF ub this] `-pi \ x` `x \ pi`, unfolded eq_4] wenzelm@32960: show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `\ x < 0`] if_not_P[OF `\ x < Float 1 -1`] if_not_P[OF `\ x < 1`] Let_def . hoelzl@29805: qed hoelzl@29805: ultimately show ?thesis by auto hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma lb_cos_minus: assumes "-pi \ x" and "real x \ 0" hoelzl@40881: shows "cos (real(-x)) \ {(lb_cos prec (-x)) .. (ub_cos prec (-x))}" hoelzl@29805: proof - hoelzl@40881: have "0 \ real (-x)" and "(-x) \ pi" using `-pi \ x` `real x \ 0` by auto hoelzl@29805: from lb_cos[OF this] show ?thesis . hoelzl@29805: qed hoelzl@29805: hoelzl@31467: definition bnds_cos :: "nat \ float \ float \ float * float" where hoelzl@31467: "bnds_cos prec lx ux = (let hoelzl@47599: lpi = float_round_down prec (lb_pi prec) ; hoelzl@47599: upi = float_round_up prec (ub_pi prec) ; hoelzl@31467: k = floor_fl (float_divr prec (lx + lpi) (2 * lpi)) ; hoelzl@31467: lx = lx - k * 2 * (if k < 0 then lpi else upi) ; hoelzl@31467: ux = ux - k * 2 * (if k < 0 then upi else lpi) hoelzl@31467: in if - lpi \ lx \ ux \ 0 then (lb_cos prec (-lx), ub_cos prec (-ux)) hoelzl@31467: else if 0 \ lx \ ux \ lpi then (lb_cos prec ux, ub_cos prec lx) hoelzl@31467: else if - lpi \ lx \ ux \ lpi then (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0) hoelzl@31467: else if 0 \ lx \ ux \ 2 * lpi then (Float -1 0, max (ub_cos prec lx) (ub_cos prec (- (ux - 2 * lpi)))) hoelzl@31508: else if -2 * lpi \ lx \ ux \ 0 then (Float -1 0, max (ub_cos prec (lx + 2 * lpi)) (ub_cos prec (-ux))) hoelzl@31467: else (Float -1 0, Float 1 0))" hoelzl@29805: hoelzl@31467: lemma floor_int: hoelzl@40881: obtains k :: int where "real k = (floor_fl f)" hoelzl@47599: by (simp add: floor_fl_def) hoelzl@29805: hoelzl@40881: lemma cos_periodic_nat[simp]: fixes n :: nat shows "cos (x + n * (2 * pi)) = cos x" hoelzl@31467: proof (induct n arbitrary: x) hoelzl@31467: case (Suc n) hoelzl@40881: have split_pi_off: "x + (Suc n) * (2 * pi) = (x + n * (2 * pi)) + 2 * pi" webertj@49962: unfolding Suc_eq_plus1 real_of_nat_add real_of_one distrib_right by auto hoelzl@31467: show ?case unfolding split_pi_off using Suc by auto hoelzl@31467: qed auto hoelzl@31467: hoelzl@40881: lemma cos_periodic_int[simp]: fixes i :: int shows "cos (x + i * (2 * pi)) = cos x" hoelzl@31467: proof (cases "0 \ i") hoelzl@40881: case True hence i_nat: "real i = nat i" by auto hoelzl@31467: show ?thesis unfolding i_nat by auto hoelzl@31467: next hoelzl@40881: case False hence i_nat: "i = - real (nat (-i))" by auto hoelzl@40881: have "cos x = cos (x + i * (2 * pi) - i * (2 * pi))" by auto hoelzl@40881: also have "\ = cos (x + i * (2 * pi))" hoelzl@31467: unfolding i_nat mult_minus_left diff_minus_eq_add by (rule cos_periodic_nat) hoelzl@31467: finally show ?thesis by auto hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma bnds_cos: "\ (x::real) lx ux. (l, u) = bnds_cos prec lx ux \ x \ {lx .. ux} \ l \ cos x \ cos x \ u" hoelzl@31467: proof ((rule allI | rule impI | erule conjE) +) hoelzl@40881: fix x :: real fix lx ux hoelzl@40881: assume bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \ {lx .. ux}" hoelzl@31467: hoelzl@47599: let ?lpi = "float_round_down prec (lb_pi prec)" hoelzl@47599: let ?upi = "float_round_up prec (ub_pi prec)" hoelzl@31467: let ?k = "floor_fl (float_divr prec (lx + ?lpi) (2 * ?lpi))" hoelzl@31467: let ?lx = "lx - ?k * 2 * (if ?k < 0 then ?lpi else ?upi)" hoelzl@31467: let ?ux = "ux - ?k * 2 * (if ?k < 0 then ?upi else ?lpi)" hoelzl@31467: hoelzl@40881: obtain k :: int where k: "k = real ?k" using floor_int . hoelzl@40881: hoelzl@40881: have upi: "pi \ ?upi" and lpi: "?lpi \ pi" hoelzl@47599: using float_round_up[of "ub_pi prec" prec] pi_boundaries[of prec] hoelzl@47599: float_round_down[of prec "lb_pi prec"] by auto hoelzl@40881: hence "?lx \ x - k * (2 * pi) \ x - k * (2 * pi) \ ?ux" hoelzl@47599: using x unfolding k[symmetric] hoelzl@47599: by (cases "k = 0") hoelzl@47599: (auto intro!: add_mono haftmann@54230: simp add: k [symmetric] uminus_add_conv_diff [symmetric] haftmann@54230: simp del: float_of_numeral uminus_add_conv_diff) hoelzl@31467: note lx = this[THEN conjunct1] and ux = this[THEN conjunct2] hoelzl@40881: hence lx_less_ux: "?lx \ real ?ux" by (rule order_trans) hoelzl@40881: hoelzl@40881: { assume "- ?lpi \ ?lx" and x_le_0: "x - k * (2 * pi) \ 0" hoelzl@31467: with lpi[THEN le_imp_neg_le] lx hoelzl@40881: have pi_lx: "- pi \ ?lx" and lx_0: "real ?lx \ 0" hoelzl@47600: by simp_all hoelzl@29805: hoelzl@40881: have "(lb_cos prec (- ?lx)) \ cos (real (- ?lx))" hoelzl@31467: using lb_cos_minus[OF pi_lx lx_0] by simp hoelzl@40881: also have "\ \ cos (x + (-k) * (2 * pi))" hoelzl@31467: using cos_monotone_minus_pi_0'[OF pi_lx lx x_le_0] hoelzl@47601: by (simp only: uminus_float.rep_eq real_of_int_minus haftmann@54230: cos_minus mult_minus_left) simp hoelzl@40881: finally have "(lb_cos prec (- ?lx)) \ cos x" hoelzl@31467: unfolding cos_periodic_int . } hoelzl@31467: note negative_lx = this hoelzl@31467: hoelzl@40881: { assume "0 \ ?lx" and pi_x: "x - k * (2 * pi) \ pi" hoelzl@31467: with lx hoelzl@40881: have pi_lx: "?lx \ pi" and lx_0: "0 \ real ?lx" hoelzl@47600: by auto hoelzl@29805: hoelzl@40881: have "cos (x + (-k) * (2 * pi)) \ cos ?lx" hoelzl@31467: using cos_monotone_0_pi'[OF lx_0 lx pi_x] hoelzl@47599: by (simp only: real_of_int_minus haftmann@54230: cos_minus mult_minus_left) simp hoelzl@40881: also have "\ \ (ub_cos prec ?lx)" hoelzl@31467: using lb_cos[OF lx_0 pi_lx] by simp hoelzl@40881: finally have "cos x \ (ub_cos prec ?lx)" hoelzl@31467: unfolding cos_periodic_int . } hoelzl@31467: note positive_lx = this hoelzl@31467: hoelzl@40881: { assume pi_x: "- pi \ x - k * (2 * pi)" and "?ux \ 0" hoelzl@31467: with ux hoelzl@40881: have pi_ux: "- pi \ ?ux" and ux_0: "real ?ux \ 0" hoelzl@47600: by simp_all hoelzl@29805: hoelzl@40881: have "cos (x + (-k) * (2 * pi)) \ cos (real (- ?ux))" hoelzl@31467: using cos_monotone_minus_pi_0'[OF pi_x ux ux_0] hoelzl@47601: by (simp only: uminus_float.rep_eq real_of_int_minus haftmann@54230: cos_minus mult_minus_left) simp hoelzl@40881: also have "\ \ (ub_cos prec (- ?ux))" hoelzl@31467: using lb_cos_minus[OF pi_ux ux_0, of prec] by simp hoelzl@40881: finally have "cos x \ (ub_cos prec (- ?ux))" hoelzl@31467: unfolding cos_periodic_int . } hoelzl@31467: note negative_ux = this hoelzl@31467: hoelzl@40881: { assume "?ux \ ?lpi" and x_ge_0: "0 \ x - k * (2 * pi)" hoelzl@31467: with lpi ux hoelzl@40881: have pi_ux: "?ux \ pi" and ux_0: "0 \ real ?ux" hoelzl@47600: by simp_all hoelzl@31467: hoelzl@40881: have "(lb_cos prec ?ux) \ cos ?ux" hoelzl@31467: using lb_cos[OF ux_0 pi_ux] by simp hoelzl@40881: also have "\ \ cos (x + (-k) * (2 * pi))" hoelzl@31467: using cos_monotone_0_pi'[OF x_ge_0 ux pi_ux] hoelzl@47599: by (simp only: real_of_int_minus haftmann@54230: cos_minus mult_minus_left) simp hoelzl@40881: finally have "(lb_cos prec ?ux) \ cos x" hoelzl@31467: unfolding cos_periodic_int . } hoelzl@31467: note positive_ux = this hoelzl@31467: hoelzl@40881: show "l \ cos x \ cos x \ u" hoelzl@31467: proof (cases "- ?lpi \ ?lx \ ?ux \ 0") hoelzl@31467: case True with bnds hoelzl@31467: have l: "l = lb_cos prec (-?lx)" hoelzl@31467: and u: "u = ub_cos prec (-?ux)" hoelzl@31467: by (auto simp add: bnds_cos_def Let_def) hoelzl@29805: hoelzl@31467: from True lpi[THEN le_imp_neg_le] lx ux hoelzl@40881: have "- pi \ x - k * (2 * pi)" hoelzl@40881: and "x - k * (2 * pi) \ 0" hoelzl@47600: by auto hoelzl@31467: with True negative_ux negative_lx hoelzl@31467: show ?thesis unfolding l u by simp hoelzl@31467: next case False note 1 = this show ?thesis hoelzl@31467: proof (cases "0 \ ?lx \ ?ux \ ?lpi") hoelzl@31467: case True with bnds 1 hoelzl@31467: have l: "l = lb_cos prec ?ux" hoelzl@31467: and u: "u = ub_cos prec ?lx" hoelzl@31467: by (auto simp add: bnds_cos_def Let_def) hoelzl@29805: hoelzl@31467: from True lpi lx ux hoelzl@40881: have "0 \ x - k * (2 * pi)" hoelzl@40881: and "x - k * (2 * pi) \ pi" hoelzl@47600: by auto hoelzl@31467: with True positive_ux positive_lx hoelzl@31467: show ?thesis unfolding l u by simp hoelzl@31467: next case False note 2 = this show ?thesis hoelzl@31467: proof (cases "- ?lpi \ ?lx \ ?ux \ ?lpi") hoelzl@31467: case True note Cond = this with bnds 1 2 hoelzl@31467: have l: "l = min (lb_cos prec (-?lx)) (lb_cos prec ?ux)" hoelzl@31467: and u: "u = Float 1 0" hoelzl@31467: by (auto simp add: bnds_cos_def Let_def) hoelzl@29805: hoelzl@31467: show ?thesis unfolding u l using negative_lx positive_ux Cond hoelzl@47599: by (cases "x - k * (2 * pi) < 0") (auto simp add: real_of_float_min) hoelzl@47599: hoelzl@31467: next case False note 3 = this show ?thesis hoelzl@31467: proof (cases "0 \ ?lx \ ?ux \ 2 * ?lpi") hoelzl@31467: case True note Cond = this with bnds 1 2 3 hoelzl@31467: have l: "l = Float -1 0" hoelzl@31467: and u: "u = max (ub_cos prec ?lx) (ub_cos prec (- (?ux - 2 * ?lpi)))" hoelzl@31467: by (auto simp add: bnds_cos_def Let_def) hoelzl@31467: hoelzl@31467: have "cos x \ real u" hoelzl@40881: proof (cases "x - k * (2 * pi) < pi") hoelzl@40881: case True hence "x - k * (2 * pi) \ pi" by simp hoelzl@31467: from positive_lx[OF Cond[THEN conjunct1] this] hoelzl@31467: show ?thesis unfolding u by (simp add: real_of_float_max) hoelzl@29805: next hoelzl@40881: case False hence "pi \ x - k * (2 * pi)" by simp hoelzl@40881: hence pi_x: "- pi \ x - k * (2 * pi) - 2 * pi" by simp hoelzl@40881: hoelzl@47600: have "?ux \ 2 * pi" using Cond lpi by auto hoelzl@40881: hence "x - k * (2 * pi) - 2 * pi \ 0" using ux by simp hoelzl@31467: hoelzl@31467: have ux_0: "real (?ux - 2 * ?lpi) \ 0" hoelzl@47600: using Cond by auto hoelzl@31467: hoelzl@31467: from 2 and Cond have "\ ?ux \ ?lpi" by auto hoelzl@47600: hence "- ?lpi \ ?ux - 2 * ?lpi" by auto hoelzl@40881: hence pi_ux: "- pi \ (?ux - 2 * ?lpi)" hoelzl@47600: using lpi[THEN le_imp_neg_le] by auto hoelzl@31467: hoelzl@40881: have x_le_ux: "x - k * (2 * pi) - 2 * pi \ (?ux - 2 * ?lpi)" wenzelm@32960: using ux lpi by auto hoelzl@40881: have "cos x = cos (x + (-k) * (2 * pi) + (-1::int) * (2 * pi))" wenzelm@32960: unfolding cos_periodic_int .. hoelzl@40881: also have "\ \ cos ((?ux - 2 * ?lpi))" wenzelm@32960: using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0] haftmann@54489: by (simp only: minus_float.rep_eq real_of_int_minus real_of_one haftmann@54489: mult_minus_left mult_1_left) simp hoelzl@40881: also have "\ = cos ((- (?ux - 2 * ?lpi)))" hoelzl@47601: unfolding uminus_float.rep_eq cos_minus .. hoelzl@40881: also have "\ \ (ub_cos prec (- (?ux - 2 * ?lpi)))" wenzelm@32960: using lb_cos_minus[OF pi_ux ux_0] by simp hoelzl@31467: finally show ?thesis unfolding u by (simp add: real_of_float_max) hoelzl@29805: qed hoelzl@31467: thus ?thesis unfolding l by auto hoelzl@31508: next case False note 4 = this show ?thesis hoelzl@31508: proof (cases "-2 * ?lpi \ ?lx \ ?ux \ 0") hoelzl@31508: case True note Cond = this with bnds 1 2 3 4 hoelzl@31508: have l: "l = Float -1 0" hoelzl@31508: and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))" hoelzl@47600: by (auto simp add: bnds_cos_def Let_def) hoelzl@31508: hoelzl@40881: have "cos x \ u" hoelzl@40881: proof (cases "-pi < x - k * (2 * pi)") hoelzl@40881: case True hence "-pi \ x - k * (2 * pi)" by simp hoelzl@31508: from negative_ux[OF this Cond[THEN conjunct2]] hoelzl@31508: show ?thesis unfolding u by (simp add: real_of_float_max) hoelzl@31508: next hoelzl@40881: case False hence "x - k * (2 * pi) \ -pi" by simp hoelzl@40881: hence pi_x: "x - k * (2 * pi) + 2 * pi \ pi" by simp hoelzl@40881: hoelzl@47600: have "-2 * pi \ ?lx" using Cond lpi by auto hoelzl@40881: hoelzl@40881: hence "0 \ x - k * (2 * pi) + 2 * pi" using lx by simp hoelzl@31508: hoelzl@31508: have lx_0: "0 \ real (?lx + 2 * ?lpi)" hoelzl@47600: using Cond lpi by auto hoelzl@31508: hoelzl@31508: from 1 and Cond have "\ -?lpi \ ?lx" by auto hoelzl@47600: hence "?lx + 2 * ?lpi \ ?lpi" by auto hoelzl@40881: hence pi_lx: "(?lx + 2 * ?lpi) \ pi" hoelzl@47600: using lpi[THEN le_imp_neg_le] by auto hoelzl@31508: hoelzl@40881: have lx_le_x: "(?lx + 2 * ?lpi) \ x - k * (2 * pi) + 2 * pi" wenzelm@32960: using lx lpi by auto hoelzl@31508: hoelzl@40881: have "cos x = cos (x + (-k) * (2 * pi) + (1 :: int) * (2 * pi))" wenzelm@32960: unfolding cos_periodic_int .. hoelzl@40881: also have "\ \ cos ((?lx + 2 * ?lpi))" wenzelm@32960: using cos_monotone_0_pi'[OF lx_0 lx_le_x pi_x] hoelzl@47601: by (simp only: minus_float.rep_eq real_of_int_minus real_of_one haftmann@54489: mult_minus_left mult_1_left) simp hoelzl@40881: also have "\ \ (ub_cos prec (?lx + 2 * ?lpi))" wenzelm@32960: using lb_cos[OF lx_0 pi_lx] by simp hoelzl@31508: finally show ?thesis unfolding u by (simp add: real_of_float_max) hoelzl@31508: qed hoelzl@31508: thus ?thesis unfolding l by auto hoelzl@29805: next hoelzl@31508: case False with bnds 1 2 3 4 show ?thesis by (auto simp add: bnds_cos_def Let_def) hoelzl@31508: qed qed qed qed qed hoelzl@29805: qed hoelzl@29805: hoelzl@29805: section "Exponential function" hoelzl@29805: hoelzl@29805: subsection "Compute the series of the exponential function" hoelzl@29805: hoelzl@29805: fun ub_exp_horner :: "nat \ nat \ nat \ nat \ float \ float" and lb_exp_horner :: "nat \ nat \ nat \ nat \ float \ float" where hoelzl@29805: "ub_exp_horner prec 0 i k x = 0" | hoelzl@29805: "ub_exp_horner prec (Suc n) i k x = rapprox_rat prec 1 (int k) + x * lb_exp_horner prec n (i + 1) (k * i) x" | hoelzl@29805: "lb_exp_horner prec 0 i k x = 0" | hoelzl@29805: "lb_exp_horner prec (Suc n) i k x = lapprox_rat prec 1 (int k) + x * ub_exp_horner prec n (i + 1) (k * i) x" hoelzl@29805: hoelzl@31098: lemma bnds_exp_horner: assumes "real x \ 0" hoelzl@40881: shows "exp x \ { lb_exp_horner prec (get_even n) 1 1 x .. ub_exp_horner prec (get_odd n) 1 1 x }" hoelzl@29805: proof - hoelzl@29805: { fix n haftmann@30971: have F: "\ m. ((\i. i + 1) ^^ n) m = n + m" by (induct n, auto) haftmann@30971: have "fact (Suc n) = fact n * ((\i. i + 1) ^^ n) 1" unfolding F by auto } note f_eq = this hoelzl@31467: hoelzl@29805: note bounds = horner_bounds_nonpos[where f="fact" and lb="lb_exp_horner prec" and ub="ub_exp_horner prec" and j'=0 and s=1, hoelzl@29805: OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps] hoelzl@29805: hoelzl@40881: { have "lb_exp_horner prec (get_even n) 1 1 x \ (\j = 0.. \ exp x" hoelzl@29805: proof - hoelzl@40881: obtain t where "\t\ \ \real x\" and "exp x = (\m = 0.. exp t / real (fact (get_even n)) * (real x) ^ (get_even n)" hoelzl@56571: by (auto simp: zero_le_even_power) nipkow@56536: ultimately show ?thesis using get_odd exp_gt_zero by auto hoelzl@29805: qed hoelzl@40881: finally have "lb_exp_horner prec (get_even n) 1 1 x \ exp x" . hoelzl@29805: } moreover hoelzl@31809: { hoelzl@31098: have x_less_zero: "real x ^ get_odd n \ 0" hoelzl@31098: proof (cases "real x = 0") hoelzl@29805: case True hoelzl@29805: have "(get_odd n) \ 0" using get_odd[THEN odd_pos] by auto hoelzl@29805: thus ?thesis unfolding True power_0_left by auto hoelzl@29805: next hoelzl@31098: case False hence "real x < 0" using `real x \ 0` by auto haftmann@46545: show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq `real x < 0`) hoelzl@29805: qed hoelzl@29805: hoelzl@40881: obtain t where "\t\ \ \real x\" and "exp x = (\m = 0.. 0" haftmann@46545: by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero) hoelzl@40881: ultimately have "exp x \ (\j = 0.. \ ub_exp_horner prec (get_odd n) 1 1 x" hoelzl@29805: using bounds(2) by auto hoelzl@40881: finally have "exp x \ ub_exp_horner prec (get_odd n) 1 1 x" . hoelzl@29805: } ultimately show ?thesis by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: subsection "Compute the exponential function on the entire domain" hoelzl@29805: hoelzl@29805: function ub_exp :: "nat \ float \ float" and lb_exp :: "nat \ float \ float" where hoelzl@29805: "lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x)) hoelzl@31809: else let hoelzl@29805: horner = (\ x. let y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x in if y \ 0 then Float 1 -2 else y) hoelzl@47599: in if x < - 1 then (horner (float_divl prec x (- floor_fl x))) ^ nat (- int_floor_fl x) hoelzl@29805: else horner x)" | hoelzl@29805: "ub_exp prec x = (if 0 < x then float_divr prec 1 (lb_exp prec (-x)) hoelzl@47599: else if x < - 1 then ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- floor_fl x)) ^ (nat (- int_floor_fl x)) hoelzl@29805: else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)" hoelzl@29805: by pat_completeness auto blanchet@55414: termination by (relation "measure (\ v. let (prec, x) = case_sum id id v in (if 0 < x then 1 else 0))", auto) hoelzl@29805: hoelzl@29805: lemma exp_m1_ge_quarter: "(1 / 4 :: real) \ exp (- 1)" hoelzl@29805: proof - hoelzl@29805: have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto hoelzl@29805: hoelzl@40881: have "1 / 4 = (Float 1 -2)" unfolding Float_num by auto hoelzl@40881: also have "\ \ lb_exp_horner 1 (get_even 4) 1 1 (- 1)" hoelzl@31809: unfolding get_even_def eq4 hoelzl@47621: by (auto simp add: Float.compute_lapprox_rat Float.compute_rapprox_rat hoelzl@47621: Float.compute_lapprox_posrat Float.compute_rapprox_posrat rat_precision_def Float.compute_bitlen) hoelzl@40881: also have "\ \ exp (- 1 :: float)" using bnds_exp_horner[where x="- 1"] by auto hoelzl@47600: finally show ?thesis by simp hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lb_exp_pos: assumes "\ 0 < x" shows "0 < lb_exp prec x" hoelzl@29805: proof - hoelzl@29805: let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" hoelzl@29805: let "?horner x" = "let y = ?lb_horner x in if y \ 0 then Float 1 -2 else y" hoelzl@47600: have pos_horner: "\ x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \ 0", auto) hoelzl@29805: moreover { fix x :: float fix num :: nat hoelzl@47600: have "0 < real (?horner x) ^ num" using `0 < ?horner x` by simp hoelzl@47599: also have "\ = (?horner x) ^ num" by auto hoelzl@31098: finally have "0 < real ((?horner x) ^ num)" . hoelzl@29805: } hoelzl@29805: ultimately show ?thesis haftmann@30968: unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] Let_def hoelzl@47600: by (cases "floor_fl x", cases "x < - 1", auto) hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma exp_boundaries': assumes "x \ 0" hoelzl@40881: shows "exp x \ { (lb_exp prec x) .. (ub_exp prec x)}" hoelzl@29805: proof - hoelzl@29805: let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" hoelzl@29805: let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x" hoelzl@29805: hoelzl@47600: have "real x \ 0" and "\ x > 0" using `x \ 0` by auto hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "x < - 1") hoelzl@47600: case False hence "- 1 \ real x" by auto hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "?lb_exp_horner x \ 0") hoelzl@47600: from `\ x < - 1` have "- 1 \ real x" by auto hoelzl@40881: hence "exp (- 1) \ exp x" unfolding exp_le_cancel_iff . hoelzl@29805: from order_trans[OF exp_m1_ge_quarter this] hoelzl@40881: have "Float 1 -2 \ exp x" unfolding Float_num . hoelzl@29805: moreover case True hoelzl@31098: ultimately show ?thesis using bnds_exp_horner `real x \ 0` `\ x > 0` `\ x < - 1` by auto hoelzl@29805: next hoelzl@31098: case False thus ?thesis using bnds_exp_horner `real x \ 0` `\ x > 0` `\ x < - 1` by (auto simp add: Let_def) hoelzl@29805: qed hoelzl@29805: next hoelzl@29805: case True hoelzl@31809: hoelzl@47599: let ?num = "nat (- int_floor_fl x)" hoelzl@47599: hoelzl@47600: have "real (int_floor_fl x) < - 1" using int_floor_fl[of x] `x < - 1` hoelzl@47600: by simp hoelzl@47599: hence "real (int_floor_fl x) < 0" by simp hoelzl@47599: hence "int_floor_fl x < 0" by auto hoelzl@47599: hence "1 \ - int_floor_fl x" by auto hoelzl@47599: hence "0 < nat (- int_floor_fl x)" by auto hoelzl@47599: hence "0 < ?num" by auto hoelzl@29805: hence "real ?num \ 0" by auto hoelzl@47599: have num_eq: "real ?num = - int_floor_fl x" using `0 < nat (- int_floor_fl x)` by auto hoelzl@47599: have "0 < - int_floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] by simp hoelzl@47599: hence "real (int_floor_fl x) < 0" unfolding less_float_def by auto hoelzl@47599: have fl_eq: "real (- int_floor_fl x) = real (- floor_fl x)" hoelzl@47599: by (simp add: floor_fl_def int_floor_fl_def) hoelzl@47599: from `0 < - int_floor_fl x` have "0 < real (- floor_fl x)" hoelzl@47599: by (simp add: floor_fl_def int_floor_fl_def) hoelzl@47599: from `real (int_floor_fl x) < 0` have "real (floor_fl x) < 0" hoelzl@47599: by (simp add: floor_fl_def int_floor_fl_def) hoelzl@40881: have "exp x \ ub_exp prec x" hoelzl@29805: proof - hoelzl@31809: have div_less_zero: "real (float_divr prec x (- floor_fl x)) \ 0" hoelzl@47599: using float_divr_nonpos_pos_upper_bound[OF `real x \ 0` `0 < real (- floor_fl x)`] hoelzl@47601: unfolding less_eq_float_def zero_float.rep_eq . hoelzl@31809: hoelzl@56479: have "exp x = exp (?num * (x / ?num))" using `real ?num \ 0` by auto hoelzl@40881: also have "\ = exp (x / ?num) ^ ?num" unfolding exp_real_of_nat_mult .. hoelzl@47599: also have "\ \ exp (float_divr prec x (- floor_fl x)) ^ ?num" unfolding num_eq fl_eq wenzelm@32960: by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto hoelzl@47599: also have "\ \ (?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num" hoelzl@47599: unfolding real_of_float_power wenzelm@32960: by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto) hoelzl@47599: finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] floor_fl_def Let_def . hoelzl@29805: qed hoelzl@31809: moreover hoelzl@40881: have "lb_exp prec x \ exp x" hoelzl@29805: proof - hoelzl@47599: let ?divl = "float_divl prec x (- floor_fl x)" hoelzl@29805: let ?horner = "?lb_exp_horner ?divl" hoelzl@31809: hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "?horner \ 0") hoelzl@47600: case False hence "0 \ real ?horner" by auto wenzelm@32960: wenzelm@32960: have div_less_zero: "real (float_divl prec x (- floor_fl x)) \ 0" hoelzl@56479: using `real (floor_fl x) < 0` `real x \ 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg) hoelzl@56479: hoelzl@40881: have "(?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num \ hoelzl@47599: exp (float_divl prec x (- floor_fl x)) ^ ?num" hoelzl@47599: using `0 \ real ?horner`[unfolded floor_fl_def[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono) hoelzl@47599: also have "\ \ exp (x / ?num) ^ ?num" unfolding num_eq fl_eq hoelzl@47601: using float_divl by (auto intro!: power_mono simp del: uminus_float.rep_eq) hoelzl@40881: also have "\ = exp (?num * (x / ?num))" unfolding exp_real_of_nat_mult .. hoelzl@56479: also have "\ = exp x" using `real ?num \ 0` by auto wenzelm@32960: finally show ?thesis hoelzl@47599: unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_not_P[OF False] by auto hoelzl@29805: next wenzelm@32960: case True wenzelm@32960: have "real (floor_fl x) \ 0" and "real (floor_fl x) \ 0" using `real (floor_fl x) < 0` by auto wenzelm@32960: from divide_right_mono_neg[OF floor_fl[of x] `real (floor_fl x) \ 0`, unfolded divide_self[OF `real (floor_fl x) \ 0`]] hoelzl@47601: have "- 1 \ x / (- floor_fl x)" unfolding minus_float.rep_eq by auto wenzelm@32960: from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]] hoelzl@40881: have "Float 1 -2 \ exp (x / (- floor_fl x))" unfolding Float_num . hoelzl@40881: hence "real (Float 1 -2) ^ ?num \ exp (x / (- floor_fl x)) ^ ?num" haftmann@46545: by (auto intro!: power_mono) hoelzl@56479: also have "\ = exp x" unfolding num_eq fl_eq exp_real_of_nat_mult[symmetric] using `real (floor_fl x) \ 0` by auto wenzelm@32960: finally show ?thesis hoelzl@47599: unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_P[OF True] real_of_float_power . hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: ultimately show ?thesis by auto hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma exp_boundaries: "exp x \ { lb_exp prec x .. ub_exp prec x }" hoelzl@29805: proof - hoelzl@29805: show ?thesis hoelzl@29805: proof (cases "0 < x") hoelzl@47600: case False hence "x \ 0" by auto hoelzl@29805: from exp_boundaries'[OF this] show ?thesis . hoelzl@29805: next hoelzl@47600: case True hence "-x \ 0" by auto hoelzl@31809: hoelzl@40881: have "lb_exp prec x \ exp x" hoelzl@29805: proof - hoelzl@29805: from exp_boundaries'[OF `-x \ 0`] hoelzl@47601: have ub_exp: "exp (- real x) \ ub_exp prec (-x)" unfolding atLeastAtMost_iff minus_float.rep_eq by auto hoelzl@40881: hoelzl@40881: have "float_divl prec 1 (ub_exp prec (-x)) \ 1 / ub_exp prec (-x)" using float_divl[where x=1] by auto hoelzl@40881: also have "\ \ exp x" wenzelm@32960: using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]] wenzelm@32960: unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto hoelzl@29805: finally show ?thesis unfolding lb_exp.simps if_P[OF True] . hoelzl@29805: qed hoelzl@29805: moreover hoelzl@40881: have "exp x \ ub_exp prec x" hoelzl@29805: proof - hoelzl@47600: have "\ 0 < -x" using `0 < x` by auto hoelzl@31809: hoelzl@29805: from exp_boundaries'[OF `-x \ 0`] hoelzl@47601: have lb_exp: "lb_exp prec (-x) \ exp (- real x)" unfolding atLeastAtMost_iff minus_float.rep_eq by auto hoelzl@40881: hoelzl@40881: have "exp x \ (1 :: float) / lb_exp prec (-x)" hoelzl@47600: using lb_exp lb_exp_pos[OF `\ 0 < -x`, of prec] hoelzl@47600: by (simp del: lb_exp.simps add: exp_minus inverse_eq_divide field_simps) hoelzl@40881: also have "\ \ float_divr prec 1 (lb_exp prec (-x))" using float_divr . hoelzl@29805: finally show ?thesis unfolding ub_exp.simps if_P[OF True] . hoelzl@29805: qed hoelzl@29805: ultimately show ?thesis by auto hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma bnds_exp: "\ (x::real) lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) \ x \ {lx .. ux} \ l \ exp x \ exp x \ u" hoelzl@29805: proof (rule allI, rule allI, rule allI, rule impI) hoelzl@40881: fix x::real and lx ux hoelzl@40881: assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \ x \ {lx .. ux}" hoelzl@40881: hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \ {lx .. ux}" by auto hoelzl@29805: hoelzl@29805: { from exp_boundaries[of lx prec, unfolded l] hoelzl@40881: have "l \ exp lx" by (auto simp del: lb_exp.simps) hoelzl@29805: also have "\ \ exp x" using x by auto hoelzl@40881: finally have "l \ exp x" . hoelzl@29805: } moreover hoelzl@40881: { have "exp x \ exp ux" using x by auto hoelzl@40881: also have "\ \ u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps) hoelzl@40881: finally have "exp x \ u" . hoelzl@40881: } ultimately show "l \ exp x \ exp x \ u" .. hoelzl@29805: qed hoelzl@29805: hoelzl@29805: section "Logarithm" hoelzl@29805: hoelzl@29805: subsection "Compute the logarithm series" hoelzl@29805: hoelzl@31809: fun ub_ln_horner :: "nat \ nat \ nat \ float \ float" hoelzl@29805: and lb_ln_horner :: "nat \ nat \ nat \ float \ float" where hoelzl@29805: "ub_ln_horner prec 0 i x = 0" | hoelzl@29805: "ub_ln_horner prec (Suc n) i x = rapprox_rat prec 1 (int i) - x * lb_ln_horner prec n (Suc i) x" | hoelzl@29805: "lb_ln_horner prec 0 i x = 0" | hoelzl@29805: "lb_ln_horner prec (Suc n) i x = lapprox_rat prec 1 (int i) - x * ub_ln_horner prec n (Suc i) x" hoelzl@29805: hoelzl@29805: lemma ln_bounds: hoelzl@29805: assumes "0 \ x" and "x < 1" haftmann@30952: shows "(\i=0..<2*n. -1^i * (1 / real (i + 1)) * x ^ (Suc i)) \ ln (x + 1)" (is "?lb") haftmann@30952: and "ln (x + 1) \ (\i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x ^ (Suc i))" (is "?ub") hoelzl@29805: proof - haftmann@30952: let "?a n" = "(1/real (n +1)) * x ^ (Suc n)" hoelzl@29805: hoelzl@29805: have ln_eq: "(\ i. -1^i * ?a i) = ln (x + 1)" hoelzl@29805: using ln_series[of "x + 1"] `0 \ x` `x < 1` by auto hoelzl@29805: hoelzl@29805: have "norm x < 1" using assms by auto hoelzl@31809: have "?a ----> 0" unfolding Suc_eq_plus1[symmetric] inverse_eq_divide[symmetric] huffman@44568: using tendsto_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto nipkow@56536: { fix n have "0 \ ?a n" by (rule mult_nonneg_nonneg, auto simp: `0 \ x`) } hoelzl@29805: { fix n have "?a (Suc n) \ ?a n" unfolding inverse_eq_divide[symmetric] hoelzl@29805: proof (rule mult_mono) nipkow@56536: show "0 \ x ^ Suc (Suc n)" by (auto simp add: `0 \ x`) haftmann@57512: have "x ^ Suc (Suc n) \ x ^ Suc n * 1" unfolding power_Suc2 mult.assoc[symmetric] nipkow@56536: by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto simp: `0 \ x`) hoelzl@29805: thus "x ^ Suc (Suc n) \ x ^ Suc n" by auto hoelzl@29805: qed auto } hoelzl@29805: from summable_Leibniz'(2,4)[OF `?a ----> 0` `\n. 0 \ ?a n`, OF `\n. ?a (Suc n) \ ?a n`, unfolded ln_eq] huffman@56195: show "?lb" and "?ub" unfolding atLeast0LessThan by auto hoelzl@29805: qed hoelzl@29805: hoelzl@31809: lemma ln_float_bounds: hoelzl@31098: assumes "0 \ real x" and "real x < 1" hoelzl@40881: shows "x * lb_ln_horner prec (get_even n) 1 x \ ln (x + 1)" (is "?lb \ ?ln") hoelzl@40881: and "ln (x + 1) \ x * ub_ln_horner prec (get_odd n) 1 x" (is "?ln \ ?ub") hoelzl@29805: proof - hoelzl@29805: obtain ev where ev: "get_even n = 2 * ev" using get_even_double .. hoelzl@29805: obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double .. hoelzl@29805: hoelzl@31098: let "?s n" = "-1^n * (1 / real (1 + n)) * (real x)^(Suc n)" hoelzl@29805: haftmann@57512: have "?lb \ setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 mult.assoc[symmetric] times_float.rep_eq setsum_left_distrib[symmetric] unfolding mult.commute[of "real x"] ev hoelzl@29805: using horner_bounds(1)[where G="\ i k. Suc k" and F="\x. x" and f="\x. x" and lb="\n i k x. lb_ln_horner prec n k x" and ub="\n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev", hoelzl@31098: OF `0 \ real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \ real x` hoelzl@29805: by (rule mult_right_mono) hoelzl@31098: also have "\ \ ?ln" using ln_bounds(1)[OF `0 \ real x` `real x < 1`] by auto hoelzl@31809: finally show "?lb \ ?ln" . hoelzl@29805: hoelzl@31098: have "?ln \ setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \ real x` `real x < 1`] by auto haftmann@57512: also have "\ \ ?ub" unfolding power_Suc2 mult.assoc[symmetric] times_float.rep_eq setsum_left_distrib[symmetric] unfolding mult.commute[of "real x"] od hoelzl@29805: using horner_bounds(2)[where G="\ i k. Suc k" and F="\x. x" and f="\x. x" and lb="\n i k x. lb_ln_horner prec n k x" and ub="\n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1", hoelzl@31098: OF `0 \ real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \ real x` hoelzl@29805: by (rule mult_right_mono) hoelzl@31809: finally show "?ln \ ?ub" . hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)" hoelzl@29805: proof - hoelzl@29805: have "x \ 0" using assms by auto webertj@49962: have "x + y = x * (1 + y / x)" unfolding distrib_left times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \ 0`] by auto hoelzl@31809: moreover nipkow@56541: have "0 < y / x" using assms by auto hoelzl@29805: hence "0 < 1 + y / x" by auto hoelzl@29805: ultimately show ?thesis using ln_mult assms by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: subsection "Compute the logarithm of 2" hoelzl@29805: hoelzl@31809: definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3 hoelzl@31809: in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) + hoelzl@29805: (third * ub_ln_horner prec (get_odd prec) 1 third))" hoelzl@31809: definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3 hoelzl@31809: in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) + hoelzl@29805: (third * lb_ln_horner prec (get_even prec) 1 third))" hoelzl@29805: hoelzl@40881: lemma ub_ln2: "ln 2 \ ub_ln2 prec" (is "?ub_ln2") hoelzl@40881: and lb_ln2: "lb_ln2 prec \ ln 2" (is "?lb_ln2") hoelzl@29805: proof - hoelzl@29805: let ?uthird = "rapprox_rat (max prec 1) 1 3" hoelzl@29805: let ?lthird = "lapprox_rat prec 1 3" hoelzl@29805: hoelzl@29805: have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)" hoelzl@29805: using ln_add[of "3 / 2" "1 / 2"] by auto hoelzl@40881: have lb3: "?lthird \ 1 / 3" using lapprox_rat[of prec 1 3] by auto hoelzl@31098: hence lb3_ub: "real ?lthird < 1" by auto hoelzl@47599: have lb3_lb: "0 \ real ?lthird" using lapprox_rat_nonneg[of 1 3] by auto hoelzl@40881: have ub3: "1 / 3 \ ?uthird" using rapprox_rat[of 1 3] by auto hoelzl@31098: hence ub3_lb: "0 \ real ?uthird" by auto hoelzl@29805: hoelzl@31098: have lb2: "0 \ real (Float 1 -1)" and ub2: "real (Float 1 -1) < 1" unfolding Float_num by auto hoelzl@29805: hoelzl@29805: have "0 \ (1::int)" and "0 < (3::int)" by auto hoelzl@47621: have ub3_ub: "real ?uthird < 1" by (simp add: Float.compute_rapprox_rat rapprox_posrat_less1) hoelzl@29805: hoelzl@29805: have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto hoelzl@31098: have uthird_gt0: "0 < real ?uthird + 1" using ub3_lb by auto hoelzl@31098: have lthird_gt0: "0 < real ?lthird + 1" using lb3_lb by auto hoelzl@29805: hoelzl@47601: show ?ub_ln2 unfolding ub_ln2_def Let_def plus_float.rep_eq ln2_sum Float_num(4)[symmetric] hoelzl@29805: proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2]) hoelzl@31098: have "ln (1 / 3 + 1) \ ln (real ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto hoelzl@40881: also have "\ \ ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird" hoelzl@29805: using ln_float_bounds(2)[OF ub3_lb ub3_ub] . hoelzl@40881: finally show "ln (1 / 3 + 1) \ ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird" . hoelzl@29805: qed hoelzl@47601: show ?lb_ln2 unfolding lb_ln2_def Let_def plus_float.rep_eq ln2_sum Float_num(4)[symmetric] hoelzl@29805: proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2]) hoelzl@40881: have "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \ ln (real ?lthird + 1)" hoelzl@29805: using ln_float_bounds(1)[OF lb3_lb lb3_ub] . hoelzl@29805: also have "\ \ ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto hoelzl@40881: finally show "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \ ln (1 / 3 + 1)" . hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@29805: subsection "Compute the logarithm in the entire domain" hoelzl@29805: hoelzl@29805: function ub_ln :: "nat \ float \ float option" and lb_ln :: "nat \ float \ float option" where hoelzl@31468: "ub_ln prec x = (if x \ 0 then None hoelzl@31468: else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x))) hoelzl@31468: else let horner = \x. x * ub_ln_horner prec (get_odd prec) 1 x in hoelzl@31468: if x \ Float 3 -1 then Some (horner (x - 1)) hoelzl@31468: else if x < Float 1 1 then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1)) hoelzl@31468: else let l = bitlen (mantissa x) - 1 in hoelzl@47599: Some (ub_ln2 prec * (Float (exponent x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" | hoelzl@31468: "lb_ln prec x = (if x \ 0 then None hoelzl@31468: else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x))) hoelzl@31468: else let horner = \x. x * lb_ln_horner prec (get_even prec) 1 x in hoelzl@31468: if x \ Float 3 -1 then Some (horner (x - 1)) hoelzl@31468: else if x < Float 1 1 then Some (horner (Float 1 -1) + hoelzl@31468: horner (max (x * lapprox_rat prec 2 3 - 1) 0)) hoelzl@31468: else let l = bitlen (mantissa x) - 1 in hoelzl@47599: Some (lb_ln2 prec * (Float (exponent x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" hoelzl@29805: by pat_completeness auto hoelzl@29805: blanchet@55414: termination proof (relation "measure (\ v. let (prec, x) = case_sum id id v in (if x < 1 then 1 else 0))", auto) hoelzl@47600: fix prec and x :: float assume "\ real x \ 0" and "real x < 1" and "real (float_divl (max prec (Suc 0)) 1 x) < 1" hoelzl@47600: hence "0 < real x" "1 \ max prec (Suc 0)" "real x < 1" by auto hoelzl@47599: from float_divl_pos_less1_bound[OF `0 < real x` `real x < 1` `1 \ max prec (Suc 0)`] hoelzl@47600: show False using `real (float_divl (max prec (Suc 0)) 1 x) < 1` by auto hoelzl@29805: next hoelzl@47600: fix prec x assume "\ real x \ 0" and "real x < 1" and "real (float_divr prec 1 x) < 1" hoelzl@47600: hence "0 < x" by auto hoelzl@47600: from float_divr_pos_less1_lower_bound[OF `0 < x`, of prec] `real x < 1` hoelzl@47600: show False using `real (float_divr prec 1 x) < 1` by auto hoelzl@47599: qed hoelzl@47599: hoelzl@47599: lemma float_pos_eq_mantissa_pos: "x > 0 \ mantissa x > 0" hoelzl@47599: apply (subst Float_mantissa_exponent[of x, symmetric]) hoelzl@47599: apply (auto simp add: zero_less_mult_iff zero_float_def powr_gt_zero[of 2 "exponent x"] dest: less_zeroE) hoelzl@47599: using powr_gt_zero[of 2 "exponent x"] hoelzl@47599: apply simp hoelzl@47599: done hoelzl@47599: hoelzl@47599: lemma Float_pos_eq_mantissa_pos: "Float m e > 0 \ m > 0" hoelzl@47599: using powr_gt_zero[of 2 "e"] hoelzl@54269: by (auto simp add: zero_less_mult_iff zero_float_def simp del: powr_gt_zero dest: less_zeroE) hoelzl@47599: hoelzl@47599: lemma Float_representation_aux: hoelzl@47599: fixes m e hoelzl@47599: defines "x \ Float m e" hoelzl@47599: assumes "x > 0" hoelzl@47599: shows "Float (exponent x + (bitlen (mantissa x) - 1)) 0 = Float (e + (bitlen m - 1)) 0" (is ?th1) hoelzl@47599: and "Float (mantissa x) (- (bitlen (mantissa x) - 1)) = Float m ( - (bitlen m - 1))" (is ?th2) hoelzl@47599: proof - hoelzl@47599: from assms have mantissa_pos: "m > 0" "mantissa x > 0" hoelzl@47600: using Float_pos_eq_mantissa_pos[of m e] float_pos_eq_mantissa_pos[of x] by simp_all hoelzl@47600: thus ?th1 using bitlen_Float[of m e] assms by (auto simp add: zero_less_mult_iff intro!: arg_cong2[where f=Float]) hoelzl@47600: have "x \ float_of 0" hoelzl@47600: unfolding zero_float_def[symmetric] using `0 < x` by auto hoelzl@47599: from denormalize_shift[OF assms(1) this] guess i . note i = this hoelzl@47600: hoelzl@47599: have "2 powr (1 - (real (bitlen (mantissa x)) + real i)) = hoelzl@47599: 2 powr (1 - (real (bitlen (mantissa x)))) * inverse (2 powr (real i))" hoelzl@47599: by (simp add: powr_minus[symmetric] powr_add[symmetric] field_simps) hoelzl@47599: hence "real (mantissa x) * 2 powr (1 - real (bitlen (mantissa x))) = hoelzl@47599: (real (mantissa x) * 2 ^ i) * 2 powr (1 - real (bitlen (mantissa x * 2 ^ i)))" hoelzl@47599: using `mantissa x > 0` by (simp add: powr_realpow) hoelzl@47600: then show ?th2 hoelzl@47600: unfolding i by transfer auto hoelzl@47599: qed hoelzl@47599: hoelzl@47599: lemma compute_ln[code]: hoelzl@47599: fixes m e hoelzl@47599: defines "x \ Float m e" hoelzl@47599: shows "ub_ln prec x = (if x \ 0 then None hoelzl@47599: else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x))) hoelzl@47599: else let horner = \x. x * ub_ln_horner prec (get_odd prec) 1 x in hoelzl@47599: if x \ Float 3 -1 then Some (horner (x - 1)) hoelzl@47599: else if x < Float 1 1 then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1)) hoelzl@47599: else let l = bitlen m - 1 in hoelzl@47599: Some (ub_ln2 prec * (Float (e + l) 0) + horner (Float m (- l) - 1)))" hoelzl@47599: (is ?th1) hoelzl@47599: and "lb_ln prec x = (if x \ 0 then None hoelzl@47599: else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x))) hoelzl@47599: else let horner = \x. x * lb_ln_horner prec (get_even prec) 1 x in hoelzl@47599: if x \ Float 3 -1 then Some (horner (x - 1)) hoelzl@47599: else if x < Float 1 1 then Some (horner (Float 1 -1) + hoelzl@47599: horner (max (x * lapprox_rat prec 2 3 - 1) 0)) hoelzl@47599: else let l = bitlen m - 1 in hoelzl@47599: Some (lb_ln2 prec * (Float (e + l) 0) + horner (Float m (- l) - 1)))" hoelzl@47599: (is ?th2) hoelzl@47599: proof - hoelzl@47599: from assms Float_pos_eq_mantissa_pos have "x > 0 \ m > 0" by simp hoelzl@47599: thus ?th1 ?th2 using Float_representation_aux[of m e] unfolding x_def[symmetric] hoelzl@47600: by (auto dest: not_leE) hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma ln_shifted_float: assumes "0 < m" shows "ln (Float m e) = ln 2 * (e + (bitlen m - 1)) + ln (Float m (- (bitlen m - 1)))" hoelzl@29805: proof - hoelzl@29805: let ?B = "2^nat (bitlen m - 1)" hoelzl@47599: def bl \ "bitlen m - 1" hoelzl@29805: have "0 < real m" and "\X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \ 0" using assms by auto hoelzl@47599: hence "0 \ bl" by (simp add: bitlen_def bl_def) hoelzl@31468: show ?thesis hoelzl@29805: proof (cases "0 \ e") hoelzl@47599: case True hoelzl@47599: thus ?thesis hoelzl@47599: unfolding bl_def[symmetric] using `0 < real m` `0 \ bl` hoelzl@47599: apply (simp add: ln_mult) hoelzl@47599: apply (cases "e=0") hoelzl@47599: apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr) hoelzl@47599: apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr field_simps) hoelzl@47599: done hoelzl@29805: next hoelzl@29805: case False hence "0 < -e" by auto hoelzl@47599: have lne: "ln (2 powr real e) = ln (inverse (2 powr - e))" by (simp add: powr_minus) hoelzl@29805: hence pow_gt0: "(0::real) < 2^nat (-e)" by auto hoelzl@29805: hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto hoelzl@47599: show ?thesis using False unfolding bl_def[symmetric] using `0 < real m` `0 \ bl` hoelzl@56483: by (auto simp add: lne ln_mult ln_powr ln_div field_simps) hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma ub_ln_lb_ln_bounds': assumes "1 \ x" hoelzl@40881: shows "the (lb_ln prec x) \ ln x \ ln x \ the (ub_ln prec x)" hoelzl@29805: (is "?lb \ ?ln \ ?ln \ ?ub") hoelzl@29805: proof (cases "x < Float 1 1") hoelzl@31468: case True hoelzl@47600: hence "real (x - 1) < 1" and "real x < 2" by auto hoelzl@47600: have "\ x \ 0" and "\ x < 1" using `1 \ x` by auto hoelzl@47600: hence "0 \ real (x - 1)" using `1 \ x` by auto hoelzl@31468: hoelzl@47599: have [simp]: "(Float 3 -1) = 3 / 2" by simp hoelzl@31468: hoelzl@31468: show ?thesis hoelzl@31468: proof (cases "x \ Float 3 -1") hoelzl@31468: case True hoelzl@31468: show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def hoelzl@31468: using ln_float_bounds[OF `0 \ real (x - 1)` `real (x - 1) < 1`, of prec] `\ x \ 0` `\ x < 1` True hoelzl@31468: by auto hoelzl@31468: next hoelzl@47600: case False hence *: "3 / 2 < x" by auto hoelzl@40881: hoelzl@40881: with ln_add[of "3 / 2" "x - 3 / 2"] hoelzl@40881: have add: "ln x = ln (3 / 2) + ln (real x * 2 / 3)" hoelzl@31468: by (auto simp add: algebra_simps diff_divide_distrib) hoelzl@31468: hoelzl@31468: let "?ub_horner x" = "x * ub_ln_horner prec (get_odd prec) 1 x" hoelzl@31468: let "?lb_horner x" = "x * lb_ln_horner prec (get_even prec) 1 x" hoelzl@31468: hoelzl@31468: { have up: "real (rapprox_rat prec 2 3) \ 1" wenzelm@32960: by (rule rapprox_rat_le1) simp_all hoelzl@40881: have low: "2 / 3 \ rapprox_rat prec 2 3" wenzelm@32960: by (rule order_trans[OF _ rapprox_rat]) simp hoelzl@31468: from mult_less_le_imp_less[OF * low] * hoelzl@31468: have pos: "0 < real (x * rapprox_rat prec 2 3 - 1)" by auto hoelzl@31468: hoelzl@31468: have "ln (real x * 2/3) wenzelm@32960: \ ln (real (x * rapprox_rat prec 2 3 - 1) + 1)" hoelzl@31468: proof (rule ln_le_cancel_iff[symmetric, THEN iffD1]) wenzelm@32960: show "real x * 2 / 3 \ real (x * rapprox_rat prec 2 3 - 1) + 1" wenzelm@32960: using * low by auto wenzelm@32960: show "0 < real x * 2 / 3" using * by simp wenzelm@32960: show "0 < real (x * rapprox_rat prec 2 3 - 1) + 1" using pos by auto hoelzl@31468: qed hoelzl@40881: also have "\ \ ?ub_horner (x * rapprox_rat prec 2 3 - 1)" hoelzl@31468: proof (rule ln_float_bounds(2)) wenzelm@32960: from mult_less_le_imp_less[OF `real x < 2` up] low * wenzelm@32960: show "real (x * rapprox_rat prec 2 3 - 1) < 1" by auto wenzelm@32960: show "0 \ real (x * rapprox_rat prec 2 3 - 1)" using pos by auto hoelzl@31468: qed hoelzl@40881: finally have "ln x hoelzl@40881: \ ?ub_horner (Float 1 -1) hoelzl@40881: + ?ub_horner (x * rapprox_rat prec 2 3 - 1)" wenzelm@32960: using ln_float_bounds(2)[of "Float 1 -1" prec prec] add by auto } hoelzl@31468: moreover hoelzl@31468: { let ?max = "max (x * lapprox_rat prec 2 3 - 1) 0" hoelzl@31468: hoelzl@40881: have up: "lapprox_rat prec 2 3 \ 2/3" wenzelm@32960: by (rule order_trans[OF lapprox_rat], simp) hoelzl@31468: hoelzl@31468: have low: "0 \ real (lapprox_rat prec 2 3)" hoelzl@47599: using lapprox_rat_nonneg[of 2 3 prec] by simp hoelzl@31468: hoelzl@40881: have "?lb_horner ?max wenzelm@32960: \ ln (real ?max + 1)" hoelzl@31468: proof (rule ln_float_bounds(1)) wenzelm@32960: from mult_less_le_imp_less[OF `real x < 2` up] * low wenzelm@32960: show "real ?max < 1" by (cases "real (lapprox_rat prec 2 3) = 0", wenzelm@32960: auto simp add: real_of_float_max) wenzelm@32960: show "0 \ real ?max" by (auto simp add: real_of_float_max) hoelzl@31468: qed hoelzl@31468: also have "\ \ ln (real x * 2/3)" hoelzl@31468: proof (rule ln_le_cancel_iff[symmetric, THEN iffD1]) wenzelm@32960: show "0 < real ?max + 1" by (auto simp add: real_of_float_max) wenzelm@32960: show "0 < real x * 2/3" using * by auto wenzelm@32960: show "real ?max + 1 \ real x * 2/3" using * up wenzelm@32960: by (cases "0 < real x * real (lapprox_posrat prec 2 3) - 1", hoelzl@47599: auto simp add: max_def) hoelzl@31468: qed hoelzl@40881: finally have "?lb_horner (Float 1 -1) + ?lb_horner ?max hoelzl@40881: \ ln x" wenzelm@32960: using ln_float_bounds(1)[of "Float 1 -1" prec prec] add by auto } hoelzl@31468: ultimately hoelzl@31468: show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def hoelzl@31468: using `\ x \ 0` `\ x < 1` True False by auto hoelzl@31468: qed hoelzl@29805: next hoelzl@29805: case False hoelzl@31468: hence "\ x \ 0" and "\ x < 1" "0 < x" "\ x \ Float 3 -1" hoelzl@47600: using `1 \ x` by auto hoelzl@29805: show ?thesis hoelzl@47599: proof - hoelzl@47599: def m \ "mantissa x" hoelzl@47599: def e \ "exponent x" hoelzl@47599: from Float_mantissa_exponent[of x] have Float: "x = Float m e" by (simp add: m_def e_def) hoelzl@29805: let ?s = "Float (e + (bitlen m - 1)) 0" hoelzl@29805: let ?x = "Float m (- (bitlen m - 1))" hoelzl@29805: hoelzl@47599: have "0 < m" and "m \ 0" using `0 < x` Float powr_gt_zero[of 2 e] hoelzl@47600: by (auto simp: zero_less_mult_iff) hoelzl@47599: def bl \ "bitlen m - 1" hence "bl \ 0" using `m > 0` by (simp add: bitlen_def) hoelzl@47599: have "1 \ Float m e" using `1 \ x` Float unfolding less_eq_float_def by auto hoelzl@47599: from bitlen_div[OF `0 < m`] float_gt1_scale[OF `1 \ Float m e`] `bl \ 0` hoelzl@47599: have x_bnds: "0 \ real (?x - 1)" "real (?x - 1) < 1" hoelzl@47599: unfolding bl_def[symmetric] hoelzl@47599: by (auto simp: powr_realpow[symmetric] field_simps inverse_eq_divide) hoelzl@47599: (auto simp : powr_minus field_simps inverse_eq_divide) hoelzl@29805: hoelzl@29805: { hoelzl@40881: have "lb_ln2 prec * ?s \ ln 2 * (e + (bitlen m - 1))" (is "?lb2 \ _") hoelzl@47601: unfolding nat_0 power_0 mult_1_right times_float.rep_eq wenzelm@32960: using lb_ln2[of prec] hoelzl@47599: proof (rule mult_mono) hoelzl@47599: from float_gt1_scale[OF `1 \ Float m e`] hoelzl@47599: show "0 \ real (Float (e + (bitlen m - 1)) 0)" by simp hoelzl@47599: qed auto hoelzl@29805: moreover hoelzl@47599: from ln_float_bounds(1)[OF x_bnds] hoelzl@40881: have "(?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1) \ ln ?x" (is "?lb_horner \ _") by auto hoelzl@40881: ultimately have "?lb2 + ?lb_horner \ ln x" wenzelm@32960: unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto hoelzl@31468: } hoelzl@29805: moreover hoelzl@29805: { hoelzl@47599: from ln_float_bounds(2)[OF x_bnds] hoelzl@40881: have "ln ?x \ (?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1)" (is "_ \ ?ub_horner") by auto hoelzl@29805: moreover hoelzl@40881: have "ln 2 * (e + (bitlen m - 1)) \ ub_ln2 prec * ?s" (is "_ \ ?ub2") hoelzl@47601: unfolding nat_0 power_0 mult_1_right times_float.rep_eq wenzelm@32960: using ub_ln2[of prec] hoelzl@47599: proof (rule mult_mono) hoelzl@47599: from float_gt1_scale[OF `1 \ Float m e`] wenzelm@32960: show "0 \ real (e + (bitlen m - 1))" by auto hoelzl@47599: next hoelzl@47599: have "0 \ ln 2" by simp hoelzl@47599: thus "0 \ real (ub_ln2 prec)" using ub_ln2[of prec] by arith hoelzl@47599: qed auto hoelzl@40881: ultimately have "ln x \ ?ub2 + ?ub_horner" wenzelm@32960: unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto hoelzl@29805: } hoelzl@29805: ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps hoelzl@31468: unfolding if_not_P[OF `\ x \ 0`] if_not_P[OF `\ x < 1`] if_not_P[OF False] if_not_P[OF `\ x \ Float 3 -1`] Let_def hoelzl@47601: unfolding plus_float.rep_eq e_def[symmetric] m_def[symmetric] by simp hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: wenzelm@49351: lemma ub_ln_lb_ln_bounds: wenzelm@49351: assumes "0 < x" hoelzl@40881: shows "the (lb_ln prec x) \ ln x \ ln x \ the (ub_ln prec x)" hoelzl@29805: (is "?lb \ ?ln \ ?ln \ ?ub") hoelzl@29805: proof (cases "x < 1") hoelzl@47599: case False hence "1 \ x" unfolding less_float_def less_eq_float_def by auto hoelzl@29805: show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \ x`] . hoelzl@29805: next hoelzl@47600: case True have "\ x \ 0" using `0 < x` by auto hoelzl@47600: from True have "real x < 1" by simp hoelzl@47600: have "0 < real x" and "real x \ 0" using `0 < x` by auto hoelzl@31098: hence A: "0 < 1 / real x" by auto hoelzl@29805: hoelzl@29805: { hoelzl@29805: let ?divl = "float_divl (max prec 1) 1 x" hoelzl@47600: have A': "1 \ ?divl" using float_divl_pos_less1_bound[OF `0 < real x` `real x < 1`] by auto hoelzl@47600: hence B: "0 < real ?divl" by auto hoelzl@31468: hoelzl@40881: have "ln ?divl \ ln (1 / x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto hoelzl@40881: hence "ln x \ - ln ?divl" unfolding nonzero_inverse_eq_divide[OF `real x \ 0`, symmetric] ln_inverse[OF `0 < real x`] by auto hoelzl@31468: from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le] hoelzl@47601: have "?ln \ - the (lb_ln prec ?divl)" unfolding uminus_float.rep_eq by (rule order_trans) hoelzl@29805: } moreover hoelzl@29805: { hoelzl@29805: let ?divr = "float_divr prec 1 x" hoelzl@47599: have A': "1 \ ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding less_eq_float_def less_float_def by auto hoelzl@47600: hence B: "0 < real ?divr" by auto hoelzl@31468: hoelzl@40881: have "ln (1 / x) \ ln ?divr" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto hoelzl@40881: hence "- ln ?divr \ ln x" unfolding nonzero_inverse_eq_divide[OF `real x \ 0`, symmetric] ln_inverse[OF `0 < real x`] by auto hoelzl@29805: from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this hoelzl@47601: have "- the (ub_ln prec ?divr) \ ?ln" unfolding uminus_float.rep_eq by (rule order_trans) hoelzl@29805: } hoelzl@29805: ultimately show ?thesis unfolding lb_ln.simps[where x=x] ub_ln.simps[where x=x] hoelzl@29805: unfolding if_not_P[OF `\ x \ 0`] if_P[OF True] by auto hoelzl@29805: qed hoelzl@29805: wenzelm@49351: lemma lb_ln: wenzelm@49351: assumes "Some y = lb_ln prec x" hoelzl@40881: shows "y \ ln x" and "0 < real x" hoelzl@29805: proof - hoelzl@29805: have "0 < x" hoelzl@29805: proof (rule ccontr) hoelzl@47599: assume "\ 0 < x" hence "x \ 0" unfolding less_eq_float_def less_float_def by auto hoelzl@29805: thus False using assms by auto hoelzl@29805: qed hoelzl@47600: thus "0 < real x" by auto hoelzl@40881: have "the (lb_ln prec x) \ ln x" using ub_ln_lb_ln_bounds[OF `0 < x`] .. hoelzl@40881: thus "y \ ln x" unfolding assms[symmetric] by auto hoelzl@29805: qed hoelzl@29805: wenzelm@49351: lemma ub_ln: wenzelm@49351: assumes "Some y = ub_ln prec x" hoelzl@40881: shows "ln x \ y" and "0 < real x" hoelzl@29805: proof - hoelzl@29805: have "0 < x" hoelzl@29805: proof (rule ccontr) hoelzl@47600: assume "\ 0 < x" hence "x \ 0" by auto hoelzl@29805: thus False using assms by auto hoelzl@29805: qed hoelzl@47600: thus "0 < real x" by auto hoelzl@40881: have "ln x \ the (ub_ln prec x)" using ub_ln_lb_ln_bounds[OF `0 < x`] .. hoelzl@40881: thus "ln x \ y" unfolding assms[symmetric] by auto hoelzl@29805: qed hoelzl@29805: hoelzl@40881: lemma bnds_ln: "\ (x::real) lx ux. (Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \ x \ {lx .. ux} \ l \ ln x \ ln x \ u" hoelzl@29805: proof (rule allI, rule allI, rule allI, rule impI) hoelzl@40881: fix x::real and lx ux hoelzl@40881: assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \ x \ {lx .. ux}" hoelzl@40881: hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \ {lx .. ux}" by auto hoelzl@40881: hoelzl@40881: have "ln ux \ u" and "0 < real ux" using ub_ln u by auto hoelzl@40881: have "l \ ln lx" and "0 < real lx" and "0 < x" using lb_ln[OF l] x by auto hoelzl@40881: hoelzl@40881: from ln_le_cancel_iff[OF `0 < real lx` `0 < x`] `l \ ln lx` hoelzl@40881: have "l \ ln x" using x unfolding atLeastAtMost_iff by auto hoelzl@29805: moreover hoelzl@40881: from ln_le_cancel_iff[OF `0 < x` `0 < real ux`] `ln ux \ real u` hoelzl@40881: have "ln x \ u" using x unfolding atLeastAtMost_iff by auto hoelzl@40881: ultimately show "l \ ln x \ ln x \ u" .. hoelzl@29805: qed hoelzl@29805: hoelzl@29805: section "Implement floatarith" hoelzl@29805: hoelzl@29805: subsection "Define syntax and semantics" hoelzl@29805: hoelzl@29805: datatype floatarith hoelzl@29805: = Add floatarith floatarith hoelzl@29805: | Minus floatarith hoelzl@29805: | Mult floatarith floatarith hoelzl@29805: | Inverse floatarith hoelzl@29805: | Cos floatarith hoelzl@29805: | Arctan floatarith hoelzl@29805: | Abs floatarith hoelzl@29805: | Max floatarith floatarith hoelzl@29805: | Min floatarith floatarith hoelzl@29805: | Pi hoelzl@29805: | Sqrt floatarith hoelzl@29805: | Exp floatarith hoelzl@29805: | Ln floatarith hoelzl@29805: | Power floatarith nat hoelzl@32919: | Var nat hoelzl@29805: | Num float hoelzl@29805: hoelzl@31863: fun interpret_floatarith :: "floatarith \ real list \ real" where hoelzl@31098: "interpret_floatarith (Add a b) vs = (interpret_floatarith a vs) + (interpret_floatarith b vs)" | hoelzl@31098: "interpret_floatarith (Minus a) vs = - (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith (Mult a b) vs = (interpret_floatarith a vs) * (interpret_floatarith b vs)" | hoelzl@31098: "interpret_floatarith (Inverse a) vs = inverse (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith (Cos a) vs = cos (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith (Arctan a) vs = arctan (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith (Min a b) vs = min (interpret_floatarith a vs) (interpret_floatarith b vs)" | hoelzl@31098: "interpret_floatarith (Max a b) vs = max (interpret_floatarith a vs) (interpret_floatarith b vs)" | hoelzl@31098: "interpret_floatarith (Abs a) vs = abs (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith Pi vs = pi" | hoelzl@31098: "interpret_floatarith (Sqrt a) vs = sqrt (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith (Exp a) vs = exp (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith (Ln a) vs = ln (interpret_floatarith a vs)" | hoelzl@31098: "interpret_floatarith (Power a n) vs = (interpret_floatarith a vs)^n" | hoelzl@40881: "interpret_floatarith (Num f) vs = f" | hoelzl@32919: "interpret_floatarith (Var n) vs = vs ! n" hoelzl@29805: hoelzl@31811: lemma interpret_floatarith_divide: "interpret_floatarith (Mult a (Inverse b)) vs = (interpret_floatarith a vs) / (interpret_floatarith b vs)" huffman@36778: unfolding divide_inverse interpret_floatarith.simps .. hoelzl@31811: hoelzl@31811: lemma interpret_floatarith_diff: "interpret_floatarith (Add a (Minus b)) vs = (interpret_floatarith a vs) - (interpret_floatarith b vs)" haftmann@54230: unfolding interpret_floatarith.simps by simp hoelzl@31811: hoelzl@31811: lemma interpret_floatarith_sin: "interpret_floatarith (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) vs = hoelzl@31811: sin (interpret_floatarith a vs)" hoelzl@31811: unfolding sin_cos_eq interpret_floatarith.simps haftmann@54230: interpret_floatarith_divide interpret_floatarith_diff hoelzl@31811: by auto hoelzl@31811: hoelzl@31811: lemma interpret_floatarith_tan: hoelzl@31811: "interpret_floatarith (Mult (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) (Inverse (Cos a))) vs = hoelzl@31811: tan (interpret_floatarith a vs)" huffman@36778: unfolding interpret_floatarith.simps(3,4) interpret_floatarith_sin tan_def divide_inverse hoelzl@31811: by auto hoelzl@31811: hoelzl@31811: lemma interpret_floatarith_powr: "interpret_floatarith (Exp (Mult b (Ln a))) vs = (interpret_floatarith a vs) powr (interpret_floatarith b vs)" hoelzl@31811: unfolding powr_def interpret_floatarith.simps .. hoelzl@31811: hoelzl@31811: lemma interpret_floatarith_log: "interpret_floatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = log (interpret_floatarith b vs) (interpret_floatarith x vs)" huffman@36778: unfolding log_def interpret_floatarith.simps divide_inverse .. hoelzl@31811: hoelzl@31811: lemma interpret_floatarith_num: hoelzl@31811: shows "interpret_floatarith (Num (Float 0 0)) vs = 0" hoelzl@31811: and "interpret_floatarith (Num (Float 1 0)) vs = 1" haftmann@54489: and "interpret_floatarith (Num (Float (- 1) 0)) vs = - 1" huffman@47108: and "interpret_floatarith (Num (Float (numeral a) 0)) vs = numeral a" haftmann@54489: and "interpret_floatarith (Num (Float (- numeral a) 0)) vs = - numeral a" by auto hoelzl@31811: hoelzl@29805: subsection "Implement approximation function" hoelzl@29805: hoelzl@29805: fun lift_bin' :: "(float * float) option \ (float * float) option \ (float \ float \ float \ float \ (float * float)) \ (float * float) option" where hoelzl@29805: "lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" | hoelzl@29805: "lift_bin' a b f = None" hoelzl@29805: hoelzl@29805: fun lift_un :: "(float * float) option \ (float \ float \ ((float option) * (float option))) \ (float * float) option" where hoelzl@29805: "lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \ Some (l, u) hoelzl@29805: | t \ None)" | hoelzl@29805: "lift_un b f = None" hoelzl@29805: hoelzl@29805: fun lift_un' :: "(float * float) option \ (float \ float \ (float * float)) \ (float * float) option" where hoelzl@29805: "lift_un' (Some (l1, u1)) f = Some (f l1 u1)" | hoelzl@29805: "lift_un' b f = None" hoelzl@29805: hoelzl@31811: definition hoelzl@31811: "bounded_by xs vs \ hoelzl@31811: (\ i < length vs. case vs ! i of None \ True hoelzl@31811: | Some (l, u) \ xs ! i \ { real l .. real u })" hoelzl@31811: hoelzl@31811: lemma bounded_byE: hoelzl@31811: assumes "bounded_by xs vs" hoelzl@31811: shows "\ i. i < length vs \ case vs ! i of None \ True hoelzl@31811: | Some (l, u) \ xs ! i \ { real l .. real u }" hoelzl@31811: using assms bounded_by_def by blast hoelzl@31811: hoelzl@31811: lemma bounded_by_update: hoelzl@31811: assumes "bounded_by xs vs" hoelzl@31811: and bnd: "xs ! i \ { real l .. real u }" hoelzl@31811: shows "bounded_by xs (vs[i := Some (l,u)])" hoelzl@31811: proof - hoelzl@31811: { fix j hoelzl@31811: let ?vs = "vs[i := Some (l,u)]" hoelzl@31811: assume "j < length ?vs" hence [simp]: "j < length vs" by simp hoelzl@31811: have "case ?vs ! j of None \ True | Some (l, u) \ xs ! j \ { real l .. real u }" hoelzl@31811: proof (cases "?vs ! j") hoelzl@31811: case (Some b) hoelzl@31811: thus ?thesis hoelzl@31811: proof (cases "i = j") hoelzl@31811: case True hoelzl@31811: thus ?thesis using `?vs ! j = Some b` and bnd by auto hoelzl@31811: next hoelzl@31811: case False hoelzl@31811: thus ?thesis using `bounded_by xs vs` unfolding bounded_by_def by auto hoelzl@31811: qed hoelzl@31811: qed auto } hoelzl@31811: thus ?thesis unfolding bounded_by_def by auto hoelzl@31811: qed hoelzl@31811: hoelzl@31811: lemma bounded_by_None: hoelzl@31811: shows "bounded_by xs (replicate (length xs) None)" hoelzl@31811: unfolding bounded_by_def by auto hoelzl@31811: hoelzl@31811: fun approx approx' :: "nat \ floatarith \ (float * float) option list \ (float * float) option" where hoelzl@47599: "approx' prec a bs = (case (approx prec a bs) of Some (l, u) \ Some (float_round_down prec l, float_round_up prec u) | None \ None)" | hoelzl@31811: "approx prec (Add a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\ l1 u1 l2 u2. (l1 + l2, u1 + u2))" | hoelzl@29805: "approx prec (Minus a) bs = lift_un' (approx' prec a bs) (\ l u. (-u, -l))" | hoelzl@29805: "approx prec (Mult a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) hoelzl@47599: (\ a1 a2 b1 b2. (nprt a1 * pprt b2 + nprt a2 * nprt b2 + pprt a1 * pprt b1 + pprt a2 * nprt b1, hoelzl@47599: pprt a2 * pprt b2 + pprt a1 * nprt b2 + nprt a2 * pprt b1 + nprt a1 * nprt b1))" | hoelzl@29805: "approx prec (Inverse a) bs = lift_un (approx' prec a bs) (\ l u. if (0 < l \ u < 0) then (Some (float_divl prec 1 u), Some (float_divr prec 1 l)) else (None, None))" | hoelzl@29805: "approx prec (Cos a) bs = lift_un' (approx' prec a bs) (bnds_cos prec)" | hoelzl@29805: "approx prec Pi bs = Some (lb_pi prec, ub_pi prec)" | hoelzl@29805: "approx prec (Min a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\ l1 u1 l2 u2. (min l1 l2, min u1 u2))" | hoelzl@29805: "approx prec (Max a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) (\ l1 u1 l2 u2. (max l1 l2, max u1 u2))" | hoelzl@29805: "approx prec (Abs a) bs = lift_un' (approx' prec a bs) (\l u. (if l < 0 \ 0 < u then 0 else min \l\ \u\, max \l\ \u\))" | hoelzl@29805: "approx prec (Arctan a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_arctan prec l, ub_arctan prec u))" | hoelzl@31467: "approx prec (Sqrt a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_sqrt prec l, ub_sqrt prec u))" | hoelzl@29805: "approx prec (Exp a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_exp prec l, ub_exp prec u))" | hoelzl@29805: "approx prec (Ln a) bs = lift_un (approx' prec a bs) (\ l u. (lb_ln prec l, ub_ln prec u))" | hoelzl@29805: "approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds n)" | hoelzl@29805: "approx prec (Num f) bs = Some (f, f)" | hoelzl@32919: "approx prec (Var i) bs = (if i < length bs then bs ! i else None)" hoelzl@29805: hoelzl@29805: lemma lift_bin'_ex: hoelzl@29805: assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f" hoelzl@29805: shows "\ l1 u1 l2 u2. Some (l1, u1) = a \ Some (l2, u2) = b" hoelzl@29805: proof (cases a) hoelzl@29805: case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps .. hoelzl@29805: thus ?thesis using lift_bin'_Some by auto hoelzl@29805: next hoelzl@29805: case (Some a') hoelzl@29805: show ?thesis hoelzl@29805: proof (cases b) hoelzl@29805: case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps .. hoelzl@29805: thus ?thesis using lift_bin'_Some by auto hoelzl@29805: next hoelzl@29805: case (Some b') hoelzl@29805: obtain la ua where a': "a' = (la, ua)" by (cases a', auto) hoelzl@29805: obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto) hoelzl@29805: thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_bin'_f: hoelzl@29805: assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f" hoelzl@29805: and Pa: "\l u. Some (l, u) = g a \ P l u a" and Pb: "\l u. Some (l, u) = g b \ P l u b" hoelzl@29805: shows "\ l1 u1 l2 u2. P l1 u1 a \ P l2 u2 b \ l = fst (f l1 u1 l2 u2) \ u = snd (f l1 u1 l2 u2)" hoelzl@29805: proof - hoelzl@29805: obtain l1 u1 l2 u2 hoelzl@29805: where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto hoelzl@31809: have lu: "(l, u) = f l1 u1 l2 u2" using lift_bin'_Some[unfolded Sa[symmetric] Sb[symmetric] lift_bin'.simps] by auto hoelzl@29805: have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto hoelzl@31809: thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma approx_approx': hoelzl@40881: assumes Pa: "\l u. Some (l, u) = approx prec a vs \ l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" hoelzl@29805: and approx': "Some (l, u) = approx' prec a vs" hoelzl@40881: shows "l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" hoelzl@29805: proof - hoelzl@29805: obtain l' u' where S: "Some (l', u') = approx prec a vs" hoelzl@29805: using approx' unfolding approx'.simps by (cases "approx prec a vs", auto) hoelzl@47599: have l': "l = float_round_down prec l'" and u': "u = float_round_up prec u'" hoelzl@29805: using approx' unfolding approx'.simps S[symmetric] by auto hoelzl@31809: show ?thesis unfolding l' u' hoelzl@47599: using order_trans[OF Pa[OF S, THEN conjunct2] float_round_up[of u']] hoelzl@47599: using order_trans[OF float_round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_bin': hoelzl@29805: assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f" hoelzl@40881: and Pa: "\l u. Some (l, u) = approx prec a bs \ l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" (is "\l u. _ = ?g a \ ?P l u a") hoelzl@40881: and Pb: "\l u. Some (l, u) = approx prec b bs \ l \ interpret_floatarith b xs \ interpret_floatarith b xs \ u" hoelzl@40881: shows "\ l1 u1 l2 u2. (l1 \ interpret_floatarith a xs \ interpret_floatarith a xs \ u1) \ hoelzl@40881: (l2 \ interpret_floatarith b xs \ interpret_floatarith b xs \ u2) \ hoelzl@29805: l = fst (f l1 u1 l2 u2) \ u = snd (f l1 u1 l2 u2)" hoelzl@29805: proof - hoelzl@29805: { fix l u assume "Some (l, u) = approx' prec a bs" hoelzl@29805: with approx_approx'[of prec a bs, OF _ this] Pa hoelzl@40881: have "l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" by auto } note Pa = this hoelzl@29805: { fix l u assume "Some (l, u) = approx' prec b bs" hoelzl@29805: with approx_approx'[of prec b bs, OF _ this] Pb hoelzl@40881: have "l \ interpret_floatarith b xs \ interpret_floatarith b xs \ u" by auto } note Pb = this hoelzl@29805: hoelzl@29805: from lift_bin'_f[where g="\a. approx' prec a bs" and P = ?P, OF lift_bin'_Some, OF Pa Pb] hoelzl@29805: show ?thesis by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un'_ex: hoelzl@29805: assumes lift_un'_Some: "Some (l, u) = lift_un' a f" hoelzl@29805: shows "\ l u. Some (l, u) = a" hoelzl@29805: proof (cases a) hoelzl@29805: case None hence "None = lift_un' a f" unfolding None lift_un'.simps .. hoelzl@29805: thus ?thesis using lift_un'_Some by auto hoelzl@29805: next hoelzl@29805: case (Some a') hoelzl@29805: obtain la ua where a': "a' = (la, ua)" by (cases a', auto) hoelzl@29805: thus ?thesis unfolding `a = Some a'` a' by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un'_f: hoelzl@29805: assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f" hoelzl@29805: and Pa: "\l u. Some (l, u) = g a \ P l u a" hoelzl@29805: shows "\ l1 u1. P l1 u1 a \ l = fst (f l1 u1) \ u = snd (f l1 u1)" hoelzl@29805: proof - hoelzl@29805: obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto hoelzl@29805: have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto hoelzl@29805: have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto hoelzl@29805: thus ?thesis using Pa[OF Sa] by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un': hoelzl@29805: assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f" hoelzl@40881: and Pa: "\l u. Some (l, u) = approx prec a bs \ l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" (is "\l u. _ = ?g a \ ?P l u a") hoelzl@40881: shows "\ l1 u1. (l1 \ interpret_floatarith a xs \ interpret_floatarith a xs \ u1) \ hoelzl@29805: l = fst (f l1 u1) \ u = snd (f l1 u1)" hoelzl@29805: proof - hoelzl@29805: { fix l u assume "Some (l, u) = approx' prec a bs" hoelzl@29805: with approx_approx'[of prec a bs, OF _ this] Pa hoelzl@40881: have "l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" by auto } note Pa = this hoelzl@29805: from lift_un'_f[where g="\a. approx' prec a bs" and P = ?P, OF lift_un'_Some, OF Pa] hoelzl@29805: show ?thesis by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un'_bnds: hoelzl@40881: assumes bnds: "\ (x::real) lx ux. (l, u) = f lx ux \ x \ { lx .. ux } \ l \ f' x \ f' x \ u" hoelzl@29805: and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f" hoelzl@40881: and Pa: "\l u. Some (l, u) = approx prec a bs \ l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" hoelzl@31098: shows "real l \ f' (interpret_floatarith a xs) \ f' (interpret_floatarith a xs) \ real u" hoelzl@29805: proof - hoelzl@29805: from lift_un'[OF lift_un'_Some Pa] hoelzl@40881: obtain l1 u1 where "l1 \ interpret_floatarith a xs" and "interpret_floatarith a xs \ u1" and "l = fst (f l1 u1)" and "u = snd (f l1 u1)" by blast hoelzl@40881: hence "(l, u) = f l1 u1" and "interpret_floatarith a xs \ {l1 .. u1}" by auto hoelzl@29805: thus ?thesis using bnds by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un_ex: hoelzl@29805: assumes lift_un_Some: "Some (l, u) = lift_un a f" hoelzl@29805: shows "\ l u. Some (l, u) = a" hoelzl@29805: proof (cases a) hoelzl@29805: case None hence "None = lift_un a f" unfolding None lift_un.simps .. hoelzl@29805: thus ?thesis using lift_un_Some by auto hoelzl@29805: next hoelzl@29805: case (Some a') hoelzl@29805: obtain la ua where a': "a' = (la, ua)" by (cases a', auto) hoelzl@29805: thus ?thesis unfolding `a = Some a'` a' by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un_f: hoelzl@29805: assumes lift_un_Some: "Some (l, u) = lift_un (g a) f" hoelzl@29805: and Pa: "\l u. Some (l, u) = g a \ P l u a" hoelzl@29805: shows "\ l1 u1. P l1 u1 a \ Some l = fst (f l1 u1) \ Some u = snd (f l1 u1)" hoelzl@29805: proof - hoelzl@29805: obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto hoelzl@29805: have "fst (f l1 u1) \ None \ snd (f l1 u1) \ None" hoelzl@29805: proof (rule ccontr) hoelzl@29805: assume "\ (fst (f l1 u1) \ None \ snd (f l1 u1) \ None)" hoelzl@29805: hence or: "fst (f l1 u1) = None \ snd (f l1 u1) = None" by auto hoelzl@31809: hence "lift_un (g a) f = None" hoelzl@29805: proof (cases "fst (f l1 u1) = None") hoelzl@29805: case True hoelzl@29805: then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto) hoelzl@29805: thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto hoelzl@29805: next hoelzl@29805: case False hence "snd (f l1 u1) = None" using or by auto hoelzl@29805: with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto) hoelzl@29805: thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto hoelzl@29805: qed hoelzl@29805: thus False using lift_un_Some by auto hoelzl@29805: qed hoelzl@29805: then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto) hoelzl@29805: from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f] hoelzl@29805: have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto hoelzl@29805: thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un: hoelzl@29805: assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f" hoelzl@40881: and Pa: "\l u. Some (l, u) = approx prec a bs \ l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" (is "\l u. _ = ?g a \ ?P l u a") hoelzl@40881: shows "\ l1 u1. (l1 \ interpret_floatarith a xs \ interpret_floatarith a xs \ u1) \ hoelzl@29805: Some l = fst (f l1 u1) \ Some u = snd (f l1 u1)" hoelzl@29805: proof - hoelzl@29805: { fix l u assume "Some (l, u) = approx' prec a bs" hoelzl@29805: with approx_approx'[of prec a bs, OF _ this] Pa hoelzl@40881: have "l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" by auto } note Pa = this hoelzl@29805: from lift_un_f[where g="\a. approx' prec a bs" and P = ?P, OF lift_un_Some, OF Pa] hoelzl@29805: show ?thesis by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma lift_un_bnds: hoelzl@40881: assumes bnds: "\ (x::real) lx ux. (Some l, Some u) = f lx ux \ x \ { lx .. ux } \ l \ f' x \ f' x \ u" hoelzl@29805: and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f" hoelzl@40881: and Pa: "\l u. Some (l, u) = approx prec a bs \ l \ interpret_floatarith a xs \ interpret_floatarith a xs \ u" hoelzl@31098: shows "real l \ f' (interpret_floatarith a xs) \ f' (interpret_floatarith a xs) \ real u" hoelzl@29805: proof - hoelzl@29805: from lift_un[OF lift_un_Some Pa] hoelzl@40881: obtain l1 u1 where "l1 \ interpret_floatarith a xs" and "interpret_floatarith a xs \ u1" and "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" by blast hoelzl@40881: hence "(Some l, Some u) = f l1 u1" and "interpret_floatarith a xs \ {l1 .. u1}" by auto hoelzl@29805: thus ?thesis using bnds by auto hoelzl@29805: qed hoelzl@29805: hoelzl@29805: lemma approx: hoelzl@29805: assumes "bounded_by xs vs" hoelzl@29805: and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith") hoelzl@40881: shows "l \ interpret_floatarith arith xs \ interpret_floatarith arith xs \ u" (is "?P l u arith") hoelzl@31809: using `Some (l, u) = approx prec arith vs` wenzelm@45129: proof (induct arith arbitrary: l u) hoelzl@29805: case (Add a b) hoelzl@29805: from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps hoelzl@29805: obtain l1 u1 l2 u2 where "l = l1 + l2" and "u = u1 + u2" hoelzl@40881: "l1 \ interpret_floatarith a xs" and "interpret_floatarith a xs \ u1" hoelzl@40881: "l2 \ interpret_floatarith b xs" and "interpret_floatarith b xs \ u2" unfolding fst_conv snd_conv by blast hoelzl@31098: thus ?case unfolding interpret_floatarith.simps by auto hoelzl@29805: next hoelzl@29805: case (Minus a) hoelzl@29805: from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps hoelzl@29805: obtain l1 u1 where "l = -u1" and "u = -l1" hoelzl@40881: "l1 \ interpret_floatarith a xs" and "interpret_floatarith a xs \ u1" unfolding fst_conv snd_conv by blast hoelzl@47601: thus ?case unfolding interpret_floatarith.simps using minus_float.rep_eq by auto hoelzl@29805: next hoelzl@29805: case (Mult a b) hoelzl@29805: from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps hoelzl@31809: obtain l1 u1 l2 u2 hoelzl@47599: where l: "l = nprt l1 * pprt u2 + nprt u1 * nprt u2 + pprt l1 * pprt l2 + pprt u1 * nprt l2" hoelzl@47599: and u: "u = pprt u1 * pprt u2 + pprt l1 * nprt u2 + nprt u1 * pprt l2 + nprt l1 * nprt l2" hoelzl@40881: and "l1 \ interpret_floatarith a xs" and "interpret_floatarith a xs \ u1" hoelzl@40881: and "l2 \ interpret_floatarith b xs" and "interpret_floatarith b xs \ u2" unfolding fst_conv snd_conv by blast hoelzl@47599: thus ?case unfolding interpret_floatarith.simps l u hoelzl@29805: using mult_le_prts mult_ge_prts by auto hoelzl@29805: next hoelzl@29805: case (Inverse a) hoelzl@29805: from lift_un[OF Inverse.prems[unfolded approx.simps], unfolded if_distrib[of fst] if_distrib[of snd] fst_conv snd_conv] Inverse.hyps hoelzl@31809: obtain l1 u1 where l': "Some l = (if 0 < l1 \ u1 < 0 then Some (float_divl prec 1 u1) else None)" hoelzl@29805: and u': "Some u = (if 0 < l1 \ u1 < 0 then Some (float_divr prec 1 l1) else None)" hoelzl@40881: and l1: "l1 \ interpret_floatarith a xs" and u1: "interpret_floatarith a xs \ u1" by blast hoelzl@29805: have either: "0 < l1 \ u1 < 0" proof (rule ccontr) assume P: "\ (0 < l1 \ u1 < 0)" show False using l' unfolding if_not_P[OF P] by auto qed hoelzl@31098: moreover have l1_le_u1: "real l1 \ real u1" using l1 u1 by auto hoelzl@47600: ultimately have "real l1 \ 0" and "real u1 \ 0" by auto hoelzl@29805: hoelzl@40881: have inv: "inverse u1 \ inverse (interpret_floatarith a xs) hoelzl@40881: \ inverse (interpret_floatarith a xs) \ inverse l1" hoelzl@29805: proof (cases "0 < l1") hoelzl@31809: case True hence "0 < real u1" and "0 < real l1" "0 < interpret_floatarith a xs" hoelzl@47600: using l1_le_u1 l1 by auto hoelzl@29805: show ?thesis hoelzl@31098: unfolding inverse_le_iff_le[OF `0 < real u1` `0 < interpret_floatarith a xs`] wenzelm@32960: inverse_le_iff_le[OF `0 < interpret_floatarith a xs` `0 < real l1`] hoelzl@29805: using l1 u1 by auto hoelzl@29805: next hoelzl@29805: case False hence "u1 < 0" using either by blast hoelzl@31809: hence "real u1 < 0" and "real l1 < 0" "interpret_floatarith a xs < 0" hoelzl@47600: using l1_le_u1 u1 by auto hoelzl@29805: show ?thesis hoelzl@31098: unfolding inverse_le_iff_le_neg[OF `real u1 < 0` `interpret_floatarith a xs < 0`] wenzelm@32960: inverse_le_iff_le_neg[OF `interpret_floatarith a xs < 0` `real l1 < 0`] hoelzl@29805: using l1 u1 by auto hoelzl@29805: qed hoelzl@31468: hoelzl@29805: from l' have "l = float_divl prec 1 u1" by (cases "0 < l1 \ u1 < 0", auto) hoelzl@40881: hence "l \ inverse u1" unfolding nonzero_inverse_eq_divide[OF `real u1 \ 0`] using float_divl[of prec 1 u1] by auto hoelzl@31098: also have "\ \ inverse (interpret_floatarith a xs)" using inv by auto hoelzl@40881: finally have "l \ inverse (interpret_floatarith a xs)" . hoelzl@29805: moreover hoelzl@29805: from u' have "u = float_divr prec 1 l1" by (cases "0 < l1 \ u1 < 0", auto) hoelzl@40881: hence "inverse l1 \ u" unfolding nonzero_inverse_eq_divide[OF `real l1 \ 0`] using float_divr[of 1 l1 prec] by auto hoelzl@40881: hence "inverse (interpret_floatarith a xs) \ u" by (rule order_trans[OF inv[THEN conjunct2]]) hoelzl@31098: ultimately show ?case unfolding interpret_floatarith.simps using l1 u1 by auto hoelzl@29805: next hoelzl@29805: case (Abs x) hoelzl@29805: from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps hoelzl@29805: obtain l1 u1 where l': "l = (if l1 < 0 \ 0 < u1 then 0 else min \l1\ \u1\)" and u': "u = max \l1\ \u1\" hoelzl@40881: and l1: "l1 \ interpret_floatarith x xs" and u1: "interpret_floatarith x xs \ u1" by blast hoelzl@47600: thus ?case unfolding l' u' by (cases "l1 < 0 \ 0 < u1", auto simp add: real_of_float_min real_of_float_max) hoelzl@29805: next hoelzl@29805: case (Min a b) hoelzl@29805: from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps hoelzl@29805: obtain l1 u1 l2 u2 where l': "l = min l1 l2" and u': "u = min u1 u2" hoelzl@40881: and l1: "l1 \ interpret_floatarith a xs" and u1: "interpret_floatarith a xs \ u1" hoelzl@40881: and l1: "l2 \ interpret_floatarith b xs" and u1: "interpret_floatarith b xs \ u2" by blast hoelzl@31098: thus ?case unfolding l' u' by (auto simp add: real_of_float_min) hoelzl@29805: next hoelzl@29805: case (Max a b) hoelzl@29805: from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps hoelzl@29805: obtain l1 u1 l2 u2 where l': "l = max l1 l2" and u': "u = max u1 u2" hoelzl@40881: and l1: "l1 \ interpret_floatarith a xs" and u1: "interpret_floatarith a xs \ u1" hoelzl@40881: and l1: "l2 \ interpret_floatarith b xs" and u1: "interpret_floatarith b xs \ u2" by blast hoelzl@31098: thus ?case unfolding l' u' by (auto simp add: real_of_float_max) hoelzl@29805: next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto hoelzl@29805: next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto hoelzl@29805: next case Pi with pi_boundaries show ?case by auto hoelzl@31467: next case (Sqrt a) with lift_un'_bnds[OF bnds_sqrt] show ?case by auto hoelzl@29805: next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto hoelzl@29805: next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto hoelzl@29805: next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto hoelzl@29805: next case (Num f) thus ?case by auto hoelzl@29805: next hoelzl@32919: case (Var n) hoelzl@31811: from this[symmetric] `bounded_by xs vs`[THEN bounded_byE, of n] hoelzl@31811: show ?case by (cases "n < length vs", auto) hoelzl@31811: qed hoelzl@31811: hoelzl@31811: datatype form = Bound floatarith floatarith floatarith form hoelzl@31811: | Assign floatarith floatarith form hoelzl@31811: | Less floatarith floatarith hoelzl@31811: | LessEqual floatarith floatarith hoelzl@31811: | AtLeastAtMost floatarith floatarith floatarith hoelzl@31811: hoelzl@31811: fun interpret_form :: "form \ real list \ bool" where hoelzl@31811: "interpret_form (Bound x a b f) vs = (interpret_floatarith x vs \ { interpret_floatarith a vs .. interpret_floatarith b vs } \ interpret_form f vs)" | hoelzl@31811: "interpret_form (Assign x a f) vs = (interpret_floatarith x vs = interpret_floatarith a vs \ interpret_form f vs)" | hoelzl@31811: "interpret_form (Less a b) vs = (interpret_floatarith a vs < interpret_floatarith b vs)" | hoelzl@31811: "interpret_form (LessEqual a b) vs = (interpret_floatarith a vs \ interpret_floatarith b vs)" | hoelzl@31811: "interpret_form (AtLeastAtMost x a b) vs = (interpret_floatarith x vs \ { interpret_floatarith a vs .. interpret_floatarith b vs })" hoelzl@31811: hoelzl@31811: fun approx_form' and approx_form :: "nat \ form \ (float * float) option list \ nat list \ bool" where hoelzl@31811: "approx_form' prec f 0 n l u bs ss = approx_form prec f (bs[n := Some (l, u)]) ss" | hoelzl@31811: "approx_form' prec f (Suc s) n l u bs ss = hoelzl@31811: (let m = (l + u) * Float 1 -1 hoelzl@32919: in (if approx_form' prec f s n l m bs ss then approx_form' prec f s n m u bs ss else False))" | hoelzl@32919: "approx_form prec (Bound (Var n) a b f) bs ss = hoelzl@31811: (case (approx prec a bs, approx prec b bs) hoelzl@31811: of (Some (l, _), Some (_, u)) \ approx_form' prec f (ss ! n) n l u bs ss hoelzl@31811: | _ \ False)" | hoelzl@32919: "approx_form prec (Assign (Var n) a f) bs ss = hoelzl@31811: (case (approx prec a bs) hoelzl@31811: of (Some (l, u)) \ approx_form' prec f (ss ! n) n l u bs ss hoelzl@31811: | _ \ False)" | hoelzl@31811: "approx_form prec (Less a b) bs ss = hoelzl@31811: (case (approx prec a bs, approx prec b bs) hoelzl@31811: of (Some (l, u), Some (l', u')) \ u < l' hoelzl@31811: | _ \ False)" | hoelzl@31811: "approx_form prec (LessEqual a b) bs ss = hoelzl@31811: (case (approx prec a bs, approx prec b bs) hoelzl@31811: of (Some (l, u), Some (l', u')) \ u \ l' hoelzl@31811: | _ \ False)" | hoelzl@31811: "approx_form prec (AtLeastAtMost x a b) bs ss = hoelzl@31811: (case (approx prec x bs, approx prec a bs, approx prec b bs) hoelzl@31811: of (Some (lx, ux), Some (l, u), Some (l', u')) \ u \ lx \ ux \ l' hoelzl@31811: | _ \ False)" | hoelzl@31811: "approx_form _ _ _ _ = False" hoelzl@31811: hoelzl@32919: lemma lazy_conj: "(if A then B else False) = (A \ B)" by simp hoelzl@32919: hoelzl@31811: lemma approx_form_approx_form': hoelzl@40881: assumes "approx_form' prec f s n l u bs ss" and "(x::real) \ { l .. u }" hoelzl@40881: obtains l' u' where "x \ { l' .. u' }" wenzelm@49351: and "approx_form prec f (bs[n := Some (l', u')]) ss" hoelzl@31811: using assms proof (induct s arbitrary: l u) hoelzl@31811: case 0 hoelzl@31811: from this(1)[of l u] this(2,3) hoelzl@31811: show thesis by auto hoelzl@31811: next hoelzl@31811: case (Suc s) hoelzl@31811: hoelzl@31811: let ?m = "(l + u) * Float 1 -1" hoelzl@40881: have "real l \ ?m" and "?m \ real u" hoelzl@47599: unfolding less_eq_float_def using Suc.prems by auto hoelzl@31811: hoelzl@40881: with `x \ { l .. u }` hoelzl@40881: have "x \ { l .. ?m} \ x \ { ?m .. u }" by auto hoelzl@31811: thus thesis hoelzl@31811: proof (rule disjE) hoelzl@40881: assume *: "x \ { l .. ?m }" hoelzl@31811: with Suc.hyps[OF _ _ *] Suc.prems hoelzl@32919: show thesis by (simp add: Let_def lazy_conj) hoelzl@29805: next hoelzl@40881: assume *: "x \ { ?m .. u }" hoelzl@31811: with Suc.hyps[OF _ _ *] Suc.prems hoelzl@32919: show thesis by (simp add: Let_def lazy_conj) hoelzl@29805: qed hoelzl@29805: qed hoelzl@29805: hoelzl@31811: lemma approx_form_aux: hoelzl@31811: assumes "approx_form prec f vs ss" wenzelm@49351: and "bounded_by xs vs" hoelzl@31811: shows "interpret_form f xs" hoelzl@31811: using assms proof (induct f arbitrary: vs) hoelzl@31811: case (Bound x a b f) hoelzl@31811: then obtain n hoelzl@32919: where x_eq: "x = Var n" by (cases x) auto hoelzl@31811: hoelzl@31811: with Bound.prems obtain l u' l' u hoelzl@31811: where l_eq: "Some (l, u') = approx prec a vs" hoelzl@31811: and u_eq: "Some (l', u) = approx prec b vs" hoelzl@31811: and approx_form': "approx_form' prec f (ss ! n) n l u vs ss" haftmann@37411: by (cases "approx prec a vs", simp) (cases "approx prec b vs", auto) hoelzl@40881: hoelzl@31811: { assume "xs ! n \ { interpret_floatarith a xs .. interpret_floatarith b xs }" hoelzl@31811: with approx[OF Bound.prems(2) l_eq] and approx[OF Bound.prems(2) u_eq] hoelzl@40881: have "xs ! n \ { l .. u}" by auto hoelzl@31811: hoelzl@31811: from approx_form_approx_form'[OF approx_form' this] hoelzl@40881: obtain lx ux where bnds: "xs ! n \ { lx .. ux }" hoelzl@31811: and approx_form: "approx_form prec f (vs[n := Some (lx, ux)]) ss" . hoelzl@31811: hoelzl@31811: from `bounded_by xs vs` bnds hoelzl@31811: have "bounded_by xs (vs[n := Some (lx, ux)])" by (rule bounded_by_update) hoelzl@31811: with Bound.hyps[OF approx_form] hoelzl@31811: have "interpret_form f xs" by blast } hoelzl@31811: thus ?case using interpret_form.simps x_eq and interpret_floatarith.simps by simp hoelzl@31811: next hoelzl@31811: case (Assign x a f) hoelzl@31811: then obtain n hoelzl@32919: where x_eq: "x = Var n" by (cases x) auto hoelzl@31811: hoelzl@47599: with Assign.prems obtain l u hoelzl@31811: where bnd_eq: "Some (l, u) = approx prec a vs" hoelzl@32919: and x_eq: "x = Var n" hoelzl@31811: and approx_form': "approx_form' prec f (ss ! n) n l u vs ss" hoelzl@31811: by (cases "approx prec a vs") auto hoelzl@31811: hoelzl@31811: { assume bnds: "xs ! n = interpret_floatarith a xs" hoelzl@31811: with approx[OF Assign.prems(2) bnd_eq] hoelzl@40881: have "xs ! n \ { l .. u}" by auto hoelzl@31811: from approx_form_approx_form'[OF approx_form' this] hoelzl@40881: obtain lx ux where bnds: "xs ! n \ { lx .. ux }" hoelzl@31811: and approx_form: "approx_form prec f (vs[n := Some (lx, ux)]) ss" . hoelzl@31811: hoelzl@31811: from `bounded_by xs vs` bnds hoelzl@31811: have "bounded_by xs (vs[n := Some (lx, ux)])" by (rule bounded_by_update) hoelzl@31811: with Assign.hyps[OF approx_form] hoelzl@31811: have "interpret_form f xs" by blast } hoelzl@31811: thus ?case using interpret_form.simps x_eq and interpret_floatarith.simps by simp hoelzl@31811: next hoelzl@29805: case (Less a b) hoelzl@31811: then obtain l u l' u' hoelzl@31811: where l_eq: "Some (l, u) = approx prec a vs" wenzelm@49351: and u_eq: "Some (l', u') = approx prec b vs" wenzelm@49351: and inequality: "u < l'" hoelzl@31811: by (cases "approx prec a vs", auto, hoelzl@31811: cases "approx prec b vs", auto) hoelzl@47600: from inequality approx[OF Less.prems(2) l_eq] approx[OF Less.prems(2) u_eq] hoelzl@31811: show ?case by auto hoelzl@29805: next hoelzl@29805: case (LessEqual a b) hoelzl@31811: then obtain l u l' u' hoelzl@31811: where l_eq: "Some (l, u) = approx prec a vs" wenzelm@49351: and u_eq: "Some (l', u') = approx prec b vs" wenzelm@49351: and inequality: "u \ l'" hoelzl@31811: by (cases "approx prec a vs", auto, hoelzl@31811: cases "approx prec b vs", auto) hoelzl@47600: from inequality approx[OF LessEqual.prems(2) l_eq] approx[OF LessEqual.prems(2) u_eq] hoelzl@31811: show ?case by auto hoelzl@31811: next hoelzl@31811: case (AtLeastAtMost x a b) hoelzl@31811: then obtain lx ux l u l' u' hoelzl@31811: where x_eq: "Some (lx, ux) = approx prec x vs" hoelzl@31811: and l_eq: "Some (l, u) = approx prec a vs" hoelzl@31811: and u_eq: "Some (l', u') = approx prec b vs" hoelzl@31811: and inequality: "u \ lx \ ux \ l'" hoelzl@31811: by (cases "approx prec x vs", auto, hoelzl@31811: cases "approx prec a vs", auto, nipkow@56073: cases "approx prec b vs", auto) hoelzl@47600: from inequality approx[OF AtLeastAtMost.prems(2) l_eq] approx[OF AtLeastAtMost.prems(2) u_eq] approx[OF AtLeastAtMost.prems(2) x_eq] hoelzl@31811: show ?case by auto hoelzl@29805: qed hoelzl@29805: hoelzl@31811: lemma approx_form: hoelzl@31811: assumes "n = length xs" hoelzl@31811: assumes "approx_form prec f (replicate n None) ss" hoelzl@31811: shows "interpret_form f xs" hoelzl@31811: using approx_form_aux[OF _ bounded_by_None] assms by auto hoelzl@29805: hoelzl@31863: subsection {* Implementing Taylor series expansion *} hoelzl@31863: hoelzl@31863: fun isDERIV :: "nat \ floatarith \ real list \ bool" where hoelzl@31863: "isDERIV x (Add a b) vs = (isDERIV x a vs \ isDERIV x b vs)" | hoelzl@31863: "isDERIV x (Mult a b) vs = (isDERIV x a vs \ isDERIV x b vs)" | hoelzl@31863: "isDERIV x (Minus a) vs = isDERIV x a vs" | hoelzl@31863: "isDERIV x (Inverse a) vs = (isDERIV x a vs \ interpret_floatarith a vs \ 0)" | hoelzl@31863: "isDERIV x (Cos a) vs = isDERIV x a vs" | hoelzl@31863: "isDERIV x (Arctan a) vs = isDERIV x a vs" | hoelzl@31863: "isDERIV x (Min a b) vs = False" | hoelzl@31863: "isDERIV x (Max a b) vs = False" | hoelzl@31863: "isDERIV x (Abs a) vs = False" | hoelzl@31863: "isDERIV x Pi vs = True" | hoelzl@31863: "isDERIV x (Sqrt a) vs = (isDERIV x a vs \ interpret_floatarith a vs > 0)" | hoelzl@31863: "isDERIV x (Exp a) vs = isDERIV x a vs" | hoelzl@31863: "isDERIV x (Ln a) vs = (isDERIV x a vs \ interpret_floatarith a vs > 0)" | hoelzl@31863: "isDERIV x (Power a 0) vs = True" | hoelzl@31863: "isDERIV x (Power a (Suc n)) vs = isDERIV x a vs" | hoelzl@31863: "isDERIV x (Num f) vs = True" | hoelzl@32919: "isDERIV x (Var n) vs = True" hoelzl@31863: hoelzl@31863: fun DERIV_floatarith :: "nat \ floatarith \ floatarith" where hoelzl@31863: "DERIV_floatarith x (Add a b) = Add (DERIV_floatarith x a) (DERIV_floatarith x b)" | hoelzl@31863: "DERIV_floatarith x (Mult a b) = Add (Mult a (DERIV_floatarith x b)) (Mult (DERIV_floatarith x a) b)" | hoelzl@31863: "DERIV_floatarith x (Minus a) = Minus (DERIV_floatarith x a)" | hoelzl@31863: "DERIV_floatarith x (Inverse a) = Minus (Mult (DERIV_floatarith x a) (Inverse (Power a 2)))" | hoelzl@31863: "DERIV_floatarith x (Cos a) = Minus (Mult (Cos (Add (Mult Pi (Num (Float 1 -1))) (Minus a))) (DERIV_floatarith x a))" | hoelzl@31863: "DERIV_floatarith x (Arctan a) = Mult (Inverse (Add (Num 1) (Power a 2))) (DERIV_floatarith x a)" | hoelzl@31863: "DERIV_floatarith x (Min a b) = Num 0" | hoelzl@31863: "DERIV_floatarith x (Max a b) = Num 0" | hoelzl@31863: "DERIV_floatarith x (Abs a) = Num 0" | hoelzl@31863: "DERIV_floatarith x Pi = Num 0" | hoelzl@31863: "DERIV_floatarith x (Sqrt a) = (Mult (Inverse (Mult (Sqrt a) (Num 2))) (DERIV_floatarith x a))" | hoelzl@31863: "DERIV_floatarith x (Exp a) = Mult (Exp a) (DERIV_floatarith x a)" | hoelzl@31863: "DERIV_floatarith x (Ln a) = Mult (Inverse a) (DERIV_floatarith x a)" | hoelzl@31863: "DERIV_floatarith x (Power a 0) = Num 0" | hoelzl@31863: "DERIV_floatarith x (Power a (Suc n)) = Mult (Num (Float (int (Suc n)) 0)) (Mult (Power a n) (DERIV_floatarith x a))" | hoelzl@31863: "DERIV_floatarith x (Num f) = Num 0" | hoelzl@32919: "DERIV_floatarith x (Var n) = (if x = n then Num 1 else Num 0)" hoelzl@31863: hoelzl@31863: lemma DERIV_floatarith: hoelzl@31863: assumes "n < length vs" hoelzl@31863: assumes isDERIV: "isDERIV n f (vs[n := x])" hoelzl@31863: shows "DERIV (\ x'. interpret_floatarith f (vs[n := x'])) x :> hoelzl@31863: interpret_floatarith (DERIV_floatarith n f) (vs[n := x])" hoelzl@31863: (is "DERIV (?i f) x :> _") wenzelm@49351: using isDERIV wenzelm@49351: proof (induct f arbitrary: x) wenzelm@49351: case (Inverse a) wenzelm@49351: thus ?case hoelzl@56381: by (auto intro!: derivative_eq_intros simp add: algebra_simps power2_eq_square) wenzelm@49351: next wenzelm@49351: case (Cos a) wenzelm@49351: thus ?case hoelzl@56382: by (auto intro!: derivative_eq_intros hoelzl@31863: simp del: interpret_floatarith.simps(5) hoelzl@31863: simp add: interpret_floatarith_sin interpret_floatarith.simps(5)[of a]) wenzelm@49351: next wenzelm@49351: case (Power a n) wenzelm@49351: thus ?case hoelzl@56382: by (cases n) (auto intro!: derivative_eq_intros simp del: power_Suc simp add: real_of_nat_def) wenzelm@49351: next wenzelm@49351: case (Ln a) hoelzl@56382: thus ?case by (auto intro!: derivative_eq_intros simp add: divide_inverse) wenzelm@49351: next wenzelm@49351: case (Var i) wenzelm@49351: thus ?case using `n < length vs` by auto hoelzl@56381: qed (auto intro!: derivative_eq_intros) hoelzl@31863: hoelzl@31863: declare approx.simps[simp del] hoelzl@31863: hoelzl@31863: fun isDERIV_approx :: "nat \ nat \ floatarith \ (float * float) option list \ bool" where hoelzl@31863: "isDERIV_approx prec x (Add a b) vs = (isDERIV_approx prec x a vs \ isDERIV_approx prec x b vs)" | hoelzl@31863: "isDERIV_approx prec x (Mult a b) vs = (isDERIV_approx prec x a vs \ isDERIV_approx prec x b vs)" | hoelzl@31863: "isDERIV_approx prec x (Minus a) vs = isDERIV_approx prec x a vs" | hoelzl@31863: "isDERIV_approx prec x (Inverse a) vs = hoelzl@31863: (isDERIV_approx prec x a vs \ (case approx prec a vs of Some (l, u) \ 0 < l \ u < 0 | None \ False))" | hoelzl@31863: "isDERIV_approx prec x (Cos a) vs = isDERIV_approx prec x a vs" | hoelzl@31863: "isDERIV_approx prec x (Arctan a) vs = isDERIV_approx prec x a vs" | hoelzl@31863: "isDERIV_approx prec x (Min a b) vs = False" | hoelzl@31863: "isDERIV_approx prec x (Max a b) vs = False" | hoelzl@31863: "isDERIV_approx prec x (Abs a) vs = False" | hoelzl@31863: "isDERIV_approx prec x Pi vs = True" | hoelzl@31863: "isDERIV_approx prec x (Sqrt a) vs = hoelzl@31863: (isDERIV_approx prec x a vs \ (case approx prec a vs of Some (l, u) \ 0 < l | None \ False))" | hoelzl@31863: "isDERIV_approx prec x (Exp a) vs = isDERIV_approx prec x a vs" | hoelzl@31863: "isDERIV_approx prec x (Ln a) vs = hoelzl@31863: (isDERIV_approx prec x a vs \ (case approx prec a vs of Some (l, u) \ 0 < l | None \ False))" | hoelzl@31863: "isDERIV_approx prec x (Power a 0) vs = True" | hoelzl@31863: "isDERIV_approx prec x (Power a (Suc n)) vs = isDERIV_approx prec x a vs" | hoelzl@31863: "isDERIV_approx prec x (Num f) vs = True" | hoelzl@32919: "isDERIV_approx prec x (Var n) vs = True" hoelzl@31863: hoelzl@31863: lemma isDERIV_approx: hoelzl@31863: assumes "bounded_by xs vs" wenzelm@49351: and isDERIV_approx: "isDERIV_approx prec x f vs" hoelzl@31863: shows "isDERIV x f xs" wenzelm@49351: using isDERIV_approx wenzelm@49351: proof (induct f) hoelzl@31863: case (Inverse a) hoelzl@31863: then obtain l u where approx_Some: "Some (l, u) = approx prec a vs" hoelzl@31863: and *: "0 < l \ u < 0" wenzelm@49351: by (cases "approx prec a vs") auto hoelzl@31863: with approx[OF `bounded_by xs vs` approx_Some] hoelzl@47600: have "interpret_floatarith a xs \ 0" by auto hoelzl@31863: thus ?case using Inverse by auto hoelzl@31863: next hoelzl@31863: case (Ln a) hoelzl@31863: then obtain l u where approx_Some: "Some (l, u) = approx prec a vs" hoelzl@31863: and *: "0 < l" wenzelm@49351: by (cases "approx prec a vs") auto hoelzl@31863: with approx[OF `bounded_by xs vs` approx_Some] hoelzl@47600: have "0 < interpret_floatarith a xs" by auto hoelzl@31863: thus ?case using Ln by auto hoelzl@31863: next hoelzl@31863: case (Sqrt a) hoelzl@31863: then obtain l u where approx_Some: "Some (l, u) = approx prec a vs" hoelzl@31863: and *: "0 < l" wenzelm@49351: by (cases "approx prec a vs") auto hoelzl@31863: with approx[OF `bounded_by xs vs` approx_Some] hoelzl@47600: have "0 < interpret_floatarith a xs" by auto hoelzl@31863: thus ?case using Sqrt by auto hoelzl@31863: next wenzelm@49351: case (Power a n) thus ?case by (cases n) auto hoelzl@31863: qed auto hoelzl@31863: hoelzl@31863: lemma bounded_by_update_var: hoelzl@31863: assumes "bounded_by xs vs" and "vs ! i = Some (l, u)" wenzelm@49351: and bnd: "x \ { real l .. real u }" hoelzl@31863: shows "bounded_by (xs[i := x]) vs" hoelzl@31863: proof (cases "i < length xs") wenzelm@49351: case False wenzelm@49351: thus ?thesis using `bounded_by xs vs` by auto hoelzl@31863: next hoelzl@31863: let ?xs = "xs[i := x]" hoelzl@31863: case True hence "i < length ?xs" by auto wenzelm@49351: { wenzelm@49351: fix j wenzelm@49351: assume "j < length vs" wenzelm@49351: have "case vs ! j of None \ True | Some (l, u) \ ?xs ! j \ { real l .. real u }" wenzelm@49351: proof (cases "vs ! j") wenzelm@49351: case (Some b) wenzelm@49351: thus ?thesis wenzelm@49351: proof (cases "i = j") wenzelm@49351: case True wenzelm@49351: thus ?thesis using `vs ! i = Some (l, u)` Some and bnd `i < length ?xs` wenzelm@49351: by auto wenzelm@49351: next wenzelm@49351: case False wenzelm@49351: thus ?thesis wenzelm@49351: using `bounded_by xs vs`[THEN bounded_byE, OF `j < length vs`] Some by auto wenzelm@49351: qed wenzelm@49351: qed auto wenzelm@49351: } hoelzl@31863: thus ?thesis unfolding bounded_by_def by auto hoelzl@31863: qed hoelzl@31863: hoelzl@31863: lemma isDERIV_approx': hoelzl@31863: assumes "bounded_by xs vs" wenzelm@49351: and vs_x: "vs ! x = Some (l, u)" and X_in: "X \ { real l .. real u }" wenzelm@49351: and approx: "isDERIV_approx prec x f vs" hoelzl@31863: shows "isDERIV x f (xs[x := X])" hoelzl@31863: proof - hoelzl@31863: note bounded_by_update_var[OF `bounded_by xs vs` vs_x X_in] approx hoelzl@31863: thus ?thesis by (rule isDERIV_approx) hoelzl@31863: qed hoelzl@31863: hoelzl@31863: lemma DERIV_approx: hoelzl@31863: assumes "n < length xs" and bnd: "bounded_by xs vs" wenzelm@49351: and isD: "isDERIV_approx prec n f vs" wenzelm@49351: and app: "Some (l, u) = approx prec (DERIV_floatarith n f) vs" (is "_ = approx _ ?D _") hoelzl@40881: shows "\(x::real). l \ x \ x \ u \ hoelzl@31863: DERIV (\ x. interpret_floatarith f (xs[n := x])) (xs!n) :> x" hoelzl@31863: (is "\ x. _ \ _ \ DERIV (?i f) _ :> _") hoelzl@31863: proof (rule exI[of _ "?i ?D (xs!n)"], rule conjI[OF _ conjI]) hoelzl@31863: let "?i f x" = "interpret_floatarith f (xs[n := x])" hoelzl@31863: from approx[OF bnd app] hoelzl@40881: show "l \ ?i ?D (xs!n)" and "?i ?D (xs!n) \ u" hoelzl@31863: using `n < length xs` by auto hoelzl@31863: from DERIV_floatarith[OF `n < length xs`, of f "xs!n"] isDERIV_approx[OF bnd isD] hoelzl@31863: show "DERIV (?i f) (xs!n) :> (?i ?D (xs!n))" by simp hoelzl@31863: qed hoelzl@31863: wenzelm@49351: fun lift_bin :: "(float * float) option \ wenzelm@49351: (float * float) option \ (float \ float \ float \ float \ (float * float) option) \ wenzelm@49351: (float * float) option" where wenzelm@49351: "lift_bin (Some (l1, u1)) (Some (l2, u2)) f = f l1 u1 l2 u2" wenzelm@49351: | "lift_bin a b f = None" hoelzl@31863: hoelzl@31863: lemma lift_bin: hoelzl@31863: assumes lift_bin_Some: "Some (l, u) = lift_bin a b f" hoelzl@31863: obtains l1 u1 l2 u2 hoelzl@31863: where "a = Some (l1, u1)" wenzelm@49351: and "b = Some (l2, u2)" wenzelm@49351: and "f l1 u1 l2 u2 = Some (l, u)" wenzelm@49351: using assms by (cases a, simp, cases b, simp, auto) hoelzl@31863: hoelzl@31863: fun approx_tse where hoelzl@31863: "approx_tse prec n 0 c k f bs = approx prec f bs" | hoelzl@31863: "approx_tse prec n (Suc s) c k f bs = hoelzl@31863: (if isDERIV_approx prec n f bs then hoelzl@31863: lift_bin (approx prec f (bs[n := Some (c,c)])) hoelzl@31863: (approx_tse prec n s c (Suc k) (DERIV_floatarith n f) bs) hoelzl@31863: (\ l1 u1 l2 u2. approx prec hoelzl@32919: (Add (Var 0) hoelzl@31863: (Mult (Inverse (Num (Float (int k) 0))) hoelzl@32919: (Mult (Add (Var (Suc (Suc 0))) (Minus (Num c))) hoelzl@32919: (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), bs!n]) hoelzl@31863: else approx prec f bs)" hoelzl@31863: hoelzl@31863: lemma bounded_by_Cons: hoelzl@31863: assumes bnd: "bounded_by xs vs" wenzelm@49351: and x: "x \ { real l .. real u }" hoelzl@31863: shows "bounded_by (x#xs) ((Some (l, u))#vs)" hoelzl@31863: proof - wenzelm@49351: { wenzelm@49351: fix i assume *: "i < length ((Some (l, u))#vs)" hoelzl@31863: have "case ((Some (l,u))#vs) ! i of Some (l, u) \ (x#xs)!i \ { real l .. real u } | None \ True" hoelzl@31863: proof (cases i) hoelzl@31863: case 0 with x show ?thesis by auto hoelzl@31863: next hoelzl@31863: case (Suc i) with * have "i < length vs" by auto hoelzl@31863: from bnd[THEN bounded_byE, OF this] hoelzl@31863: show ?thesis unfolding Suc nth_Cons_Suc . wenzelm@49351: qed wenzelm@49351: } hoelzl@31863: thus ?thesis by (auto simp add: bounded_by_def) hoelzl@31863: qed hoelzl@31863: hoelzl@31863: lemma approx_tse_generic: hoelzl@31863: assumes "bounded_by xs vs" wenzelm@49351: and bnd_c: "bounded_by (xs[x := c]) vs" and "x < length vs" and "x < length xs" wenzelm@49351: and bnd_x: "vs ! x = Some (lx, ux)" wenzelm@49351: and ate: "Some (l, u) = approx_tse prec x s c k f vs" hoelzl@40881: shows "\ n. (\ m < n. \ (z::real) \ {lx .. ux}. hoelzl@31863: DERIV (\ y. interpret_floatarith ((DERIV_floatarith x ^^ m) f) (xs[x := y])) z :> hoelzl@31863: (interpret_floatarith ((DERIV_floatarith x ^^ (Suc m)) f) (xs[x := z]))) hoelzl@40881: \ (\ (t::real) \ {lx .. ux}. (\ i = 0.. j \ {k.. j \ {k.. {l .. u})" (is "\ n. ?taylor f k l u n") hoelzl@31863: using ate proof (induct s arbitrary: k f l u) hoelzl@31863: case 0 wenzelm@49351: { wenzelm@49351: fix t::real assume "t \ {lx .. ux}" hoelzl@31863: note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this] hoelzl@31863: from approx[OF this 0[unfolded approx_tse.simps]] hoelzl@40881: have "(interpret_floatarith f (xs[x := t])) \ {l .. u}" hoelzl@31863: by (auto simp add: algebra_simps) wenzelm@49351: } wenzelm@49351: thus ?case by (auto intro!: exI[of _ 0]) hoelzl@31863: next hoelzl@31863: case (Suc s) hoelzl@31863: show ?case hoelzl@31863: proof (cases "isDERIV_approx prec x f vs") hoelzl@31863: case False hoelzl@31863: note ap = Suc.prems[unfolded approx_tse.simps if_not_P[OF False]] wenzelm@49351: { wenzelm@49351: fix t::real assume "t \ {lx .. ux}" hoelzl@31863: note bounded_by_update_var[OF `bounded_by xs vs` bnd_x this] hoelzl@31863: from approx[OF this ap] hoelzl@40881: have "(interpret_floatarith f (xs[x := t])) \ {l .. u}" wenzelm@32960: by (auto simp add: algebra_simps) wenzelm@49351: } wenzelm@49351: thus ?thesis by (auto intro!: exI[of _ 0]) hoelzl@31863: next hoelzl@31863: case True hoelzl@31863: with Suc.prems hoelzl@31863: obtain l1 u1 l2 u2 hoelzl@31863: where a: "Some (l1, u1) = approx prec f (vs[x := Some (c,c)])" wenzelm@49351: and ate: "Some (l2, u2) = approx_tse prec x s c (Suc k) (DERIV_floatarith x f) vs" wenzelm@49351: and final: "Some (l, u) = approx prec wenzelm@49351: (Add (Var 0) wenzelm@49351: (Mult (Inverse (Num (Float (int k) 0))) wenzelm@49351: (Mult (Add (Var (Suc (Suc 0))) (Minus (Num c))) wenzelm@49351: (Var (Suc 0))))) [Some (l1, u1), Some (l2, u2), vs!x]" nipkow@56073: by (auto elim!: lift_bin) hoelzl@31863: hoelzl@31863: from bnd_c `x < length xs` hoelzl@40881: have bnd: "bounded_by (xs[x:=c]) (vs[x:= Some (c,c)])" hoelzl@31863: by (auto intro!: bounded_by_update) hoelzl@31863: hoelzl@31863: from approx[OF this a] hoelzl@40881: have f_c: "interpret_floatarith ((DERIV_floatarith x ^^ 0) f) (xs[x := c]) \ { l1 .. u1 }" hoelzl@31863: (is "?f 0 (real c) \ _") hoelzl@31863: by auto hoelzl@31863: wenzelm@49351: { wenzelm@49351: fix f :: "'a \ 'a" fix n :: nat fix x :: 'a hoelzl@31863: have "(f ^^ Suc n) x = (f ^^ n) (f x)" wenzelm@49351: by (induct n) auto wenzelm@49351: } hoelzl@31863: note funpow_Suc = this[symmetric] hoelzl@31863: from Suc.hyps[OF ate, unfolded this] hoelzl@31863: obtain n hoelzl@40881: where DERIV_hyp: "\ m z. \ m < n ; (z::real) \ { lx .. ux } \ \ DERIV (?f (Suc m)) z :> ?f (Suc (Suc m)) z" hoelzl@40881: and hyp: "\ t \ {real lx .. real ux}. (\ i = 0.. j \ {Suc k.. j \ {Suc k.. {l2 .. u2}" hoelzl@31863: (is "\ t \ _. ?X (Suc k) f n t \ _") hoelzl@31863: by blast hoelzl@31863: wenzelm@49351: { wenzelm@49351: fix m and z::real hoelzl@40881: assume "m < Suc n" and bnd_z: "z \ { lx .. ux }" hoelzl@31863: have "DERIV (?f m) z :> ?f (Suc m) z" hoelzl@31863: proof (cases m) wenzelm@32960: case 0 wenzelm@32960: with DERIV_floatarith[OF `x < length xs` isDERIV_approx'[OF `bounded_by xs vs` bnd_x bnd_z True]] wenzelm@32960: show ?thesis by simp hoelzl@31863: next wenzelm@32960: case (Suc m') wenzelm@32960: hence "m' < n" using `m < Suc n` by auto wenzelm@32960: from DERIV_hyp[OF this bnd_z] wenzelm@32960: show ?thesis using Suc by simp wenzelm@49351: qed wenzelm@49351: } note DERIV = this hoelzl@31863: hoelzl@31863: have "\ k i. k < i \ {k ..< i} = insert k {Suc k ..< i}" by auto hoelzl@31863: hence setprod_head_Suc: "\ k i. \ {k ..< k + Suc i} = k * \ {Suc k ..< Suc k + i}" by auto hoelzl@31863: have setsum_move0: "\ k F. setsum F {0.. k. F (Suc k)) {0.. "xs!x - c" hoelzl@40881: wenzelm@49351: { wenzelm@49351: fix t::real assume t: "t \ {lx .. ux}" hoelzl@31863: hence "bounded_by [xs!x] [vs!x]" wenzelm@32960: using `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] wenzelm@32960: by (cases "vs!x", auto simp add: bounded_by_def) hoelzl@31863: hoelzl@31863: with hyp[THEN bspec, OF t] f_c hoelzl@40881: have "bounded_by [?f 0 c, ?X (Suc k) f n t, xs!x] [Some (l1, u1), Some (l2, u2), vs!x]" wenzelm@32960: by (auto intro!: bounded_by_Cons) hoelzl@31863: from approx[OF this final, unfolded atLeastAtMost_iff[symmetric]] hoelzl@40881: have "?X (Suc k) f n t * (xs!x - real c) * inverse k + ?f 0 c \ {l .. u}" wenzelm@32960: by (auto simp add: algebra_simps) hoelzl@40881: also have "?X (Suc k) f n t * (xs!x - real c) * inverse (real k) + ?f 0 c = hoelzl@40881: (\ i = 0.. j \ {k.. j \ {k.. {l .. u}" . wenzelm@49351: } hoelzl@31863: thus ?thesis using DERIV by blast hoelzl@31863: qed hoelzl@31863: qed hoelzl@31863: hoelzl@31863: lemma setprod_fact: "\ {1..<1 + k} = fact (k :: nat)" hoelzl@31863: proof (induct k) wenzelm@49351: case 0 wenzelm@49351: show ?case by simp wenzelm@49351: next hoelzl@31863: case (Suc k) hoelzl@31863: have "{ 1 ..< Suc (Suc k) } = insert (Suc k) { 1 ..< Suc k }" by auto hoelzl@31863: hence "\ { 1 ..< Suc (Suc k) } = (Suc k) * \ { 1 ..< Suc k }" by auto hoelzl@31863: thus ?case using Suc by auto wenzelm@49351: qed hoelzl@31863: hoelzl@31863: lemma approx_tse: hoelzl@31863: assumes "bounded_by xs vs" wenzelm@49351: and bnd_x: "vs ! x = Some (lx, ux)" and bnd_c: "real c \ {lx .. ux}" wenzelm@49351: and "x < length vs" and "x < length xs" wenzelm@49351: and ate: "Some (l, u) = approx_tse prec x s c 1 f vs" hoelzl@40881: shows "interpret_floatarith f xs \ { l .. u }" hoelzl@31863: proof - hoelzl@31863: def F \ "\ n z. interpret_floatarith ((DERIV_floatarith x ^^ n) f) (xs[x := z])" hoelzl@31863: hence F0: "F 0 = (\ z. interpret_floatarith f (xs[x := z]))" by auto hoelzl@31863: hoelzl@40881: hence "bounded_by (xs[x := c]) vs" and "x < length vs" "x < length xs" hoelzl@31863: using `bounded_by xs vs` bnd_x bnd_c `x < length vs` `x < length xs` hoelzl@31863: by (auto intro!: bounded_by_update_var) hoelzl@31863: hoelzl@31863: from approx_tse_generic[OF `bounded_by xs vs` this bnd_x ate] hoelzl@31863: obtain n hoelzl@31863: where DERIV: "\ m z. m < n \ real lx \ z \ z \ real ux \ DERIV (F m) z :> F (Suc m) z" hoelzl@40881: and hyp: "\ (t::real). t \ {lx .. ux} \ hoelzl@40881: (\ j = 0.. {l .. u}" (is "\ t. _ \ ?taylor t \ _") hoelzl@31863: unfolding F_def atLeastAtMost_iff[symmetric] setprod_fact by blast hoelzl@31863: hoelzl@40881: have bnd_xs: "xs ! x \ { lx .. ux }" hoelzl@31863: using `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] bnd_x by auto hoelzl@31863: hoelzl@31863: show ?thesis hoelzl@31863: proof (cases n) hoelzl@31863: case 0 thus ?thesis using hyp[OF bnd_xs] unfolding F_def by auto hoelzl@31863: next hoelzl@31863: case (Suc n') hoelzl@31863: show ?thesis hoelzl@40881: proof (cases "xs ! x = c") hoelzl@31863: case True hoelzl@31863: from True[symmetric] hyp[OF bnd_xs] Suc show ?thesis wenzelm@32960: unfolding F_def Suc setsum_head_upt_Suc[OF zero_less_Suc] setsum_shift_bounds_Suc_ivl by auto hoelzl@31863: next hoelzl@31863: case False hoelzl@31863: hoelzl@40881: have "lx \ real c" "real c \ ux" "lx \ xs!x" "xs!x \ ux" wenzelm@32960: using Suc bnd_c `bounded_by xs vs`[THEN bounded_byE, OF `x < length vs`] bnd_x by auto hoelzl@31863: from Taylor.taylor[OF zero_less_Suc, of F, OF F0 DERIV[unfolded Suc] this False] hoelzl@40881: obtain t::real where t_bnd: "if xs ! x < c then xs ! x < t \ t < c else c < t \ t < xs ! x" wenzelm@32960: and fl_eq: "interpret_floatarith f (xs[x := xs ! x]) = hoelzl@40881: (\m = 0.. {lx .. ux}" hoelzl@40881: by (cases "xs ! x < c", auto) hoelzl@31863: hoelzl@31863: have "interpret_floatarith f (xs[x := xs ! x]) = ?taylor t" wenzelm@32960: unfolding fl_eq Suc by (auto simp add: algebra_simps divide_inverse) hoelzl@40881: also have "\ \ {l .. u}" using * by (rule hyp) hoelzl@31863: finally show ?thesis by simp hoelzl@31863: qed hoelzl@31863: qed hoelzl@31863: qed hoelzl@31863: hoelzl@31863: fun approx_tse_form' where hoelzl@31863: "approx_tse_form' prec t f 0 l u cmp = hoelzl@31863: (case approx_tse prec 0 t ((l + u) * Float 1 -1) 1 f [Some (l, u)] hoelzl@31863: of Some (l, u) \ cmp l u | None \ False)" | hoelzl@31863: "approx_tse_form' prec t f (Suc s) l u cmp = hoelzl@31863: (let m = (l + u) * Float 1 -1 hoelzl@32919: in (if approx_tse_form' prec t f s l m cmp then hoelzl@32919: approx_tse_form' prec t f s m u cmp else False))" hoelzl@31863: hoelzl@31863: lemma approx_tse_form': hoelzl@40881: fixes x :: real hoelzl@40881: assumes "approx_tse_form' prec t f s l u cmp" and "x \ {l .. u}" hoelzl@40881: shows "\ l' u' ly uy. x \ { l' .. u' } \ real l \ l' \ u' \ real u \ cmp ly uy \ hoelzl@31863: approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 f [Some (l', u')] = Some (ly, uy)" hoelzl@31863: using assms proof (induct s arbitrary: l u) hoelzl@31863: case 0 hoelzl@31863: then obtain ly uy hoelzl@31863: where *: "approx_tse prec 0 t ((l + u) * Float 1 -1) 1 f [Some (l, u)] = Some (ly, uy)" blanchet@55413: and **: "cmp ly uy" by (auto elim!: case_optionE) haftmann@46545: with 0 show ?case by auto hoelzl@31863: next hoelzl@31863: case (Suc s) hoelzl@31863: let ?m = "(l + u) * Float 1 -1" hoelzl@31863: from Suc.prems hoelzl@31863: have l: "approx_tse_form' prec t f s l ?m cmp" hoelzl@31863: and u: "approx_tse_form' prec t f s ?m u cmp" hoelzl@32919: by (auto simp add: Let_def lazy_conj) hoelzl@31863: hoelzl@40881: have m_l: "real l \ ?m" and m_u: "?m \ real u" hoelzl@47599: unfolding less_eq_float_def using Suc.prems by auto hoelzl@31863: hoelzl@40881: with `x \ { l .. u }` hoelzl@40881: have "x \ { l .. ?m} \ x \ { ?m .. u }" by auto hoelzl@31863: thus ?case hoelzl@31863: proof (rule disjE) hoelzl@40881: assume "x \ { l .. ?m}" hoelzl@31863: from Suc.hyps[OF l this] hoelzl@31863: obtain l' u' ly uy hoelzl@40881: where "x \ { l' .. u' } \ real l \ l' \ real u' \ ?m \ cmp ly uy \ hoelzl@31863: approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 f [Some (l', u')] = Some (ly, uy)" by blast hoelzl@31863: with m_u show ?thesis by (auto intro!: exI) hoelzl@31863: next hoelzl@40881: assume "x \ { ?m .. u }" hoelzl@31863: from Suc.hyps[OF u this] hoelzl@31863: obtain l' u' ly uy hoelzl@40881: where "x \ { l' .. u' } \ ?m \ real l' \ u' \ real u \ cmp ly uy \ hoelzl@31863: approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 f [Some (l', u')] = Some (ly, uy)" by blast hoelzl@31863: with m_u show ?thesis by (auto intro!: exI) hoelzl@31863: qed hoelzl@31863: qed hoelzl@31863: hoelzl@31863: lemma approx_tse_form'_less: hoelzl@40881: fixes x :: real hoelzl@31863: assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\ l u. 0 < l)" hoelzl@40881: and x: "x \ {l .. u}" hoelzl@31863: shows "interpret_floatarith b [x] < interpret_floatarith a [x]" hoelzl@31863: proof - hoelzl@31863: from approx_tse_form'[OF tse x] hoelzl@31863: obtain l' u' ly uy hoelzl@40881: where x': "x \ { l' .. u' }" and "l \ real l'" hoelzl@40881: and "real u' \ u" and "0 < ly" hoelzl@31863: and tse: "approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)" hoelzl@31863: by blast hoelzl@31863: hoelzl@31863: hence "bounded_by [x] [Some (l', u')]" by (auto simp add: bounded_by_def) hoelzl@31863: hoelzl@31863: from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x' hoelzl@40881: have "ly \ interpret_floatarith a [x] - interpret_floatarith b [x]" haftmann@54230: by auto hoelzl@47600: from order_less_le_trans[OF _ this, of 0] `0 < ly` hoelzl@31863: show ?thesis by auto hoelzl@31863: qed hoelzl@31863: hoelzl@31863: lemma approx_tse_form'_le: hoelzl@40881: fixes x :: real hoelzl@31863: assumes tse: "approx_tse_form' prec t (Add a (Minus b)) s l u (\ l u. 0 \ l)" hoelzl@40881: and x: "x \ {l .. u}" hoelzl@31863: shows "interpret_floatarith b [x] \ interpret_floatarith a [x]" hoelzl@31863: proof - hoelzl@31863: from approx_tse_form'[OF tse x] hoelzl@31863: obtain l' u' ly uy hoelzl@40881: where x': "x \ { l' .. u' }" and "l \ real l'" hoelzl@40881: and "real u' \ u" and "0 \ ly" hoelzl@31863: and tse: "approx_tse prec 0 t ((l' + u') * Float 1 -1) 1 (Add a (Minus b)) [Some (l', u')] = Some (ly, uy)" hoelzl@31863: by blast hoelzl@31863: hoelzl@31863: hence "bounded_by [x] [Some (l', u')]" by (auto simp add: bounded_by_def) hoelzl@31863: hoelzl@31863: from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x' hoelzl@40881: have "ly \ interpret_floatarith a [x] - interpret_floatarith b [x]" haftmann@54230: by auto hoelzl@47600: from order_trans[OF _ this, of 0] `0 \ ly` hoelzl@31863: show ?thesis by auto hoelzl@31863: qed hoelzl@31863: hoelzl@31863: definition hoelzl@31863: "approx_tse_form prec t s f = hoelzl@31863: (case f hoelzl@32919: of (Bound x a b f) \ x = Var 0 \ hoelzl@31863: (case (approx prec a [None], approx prec b [None]) hoelzl@31863: of (Some (l, u), Some (l', u')) \ hoelzl@31863: (case f hoelzl@31863: of Less lf rt \ approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\ l u. 0 < l) hoelzl@31863: | LessEqual lf rt \ approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\ l u. 0 \ l) hoelzl@31863: | AtLeastAtMost x lf rt \ hoelzl@32919: (if approx_tse_form' prec t (Add x (Minus lf)) s l u' (\ l u. 0 \ l) then hoelzl@32919: approx_tse_form' prec t (Add rt (Minus x)) s l u' (\ l u. 0 \ l) else False) hoelzl@31863: | _ \ False) hoelzl@31863: | _ \ False) hoelzl@31863: | _ \ False)" hoelzl@31863: hoelzl@31863: lemma approx_tse_form: hoelzl@31863: assumes "approx_tse_form prec t s f" hoelzl@31863: shows "interpret_form f [x]" hoelzl@31863: proof (cases f) hoelzl@31863: case (Bound i a b f') note f_def = this hoelzl@31863: with assms obtain l u l' u' hoelzl@31863: where a: "approx prec a [None] = Some (l, u)" hoelzl@31863: and b: "approx prec b [None] = Some (l', u')" blanchet@55413: unfolding approx_tse_form_def by (auto elim!: case_optionE) hoelzl@31863: hoelzl@32919: from Bound assms have "i = Var 0" unfolding approx_tse_form_def by auto hoelzl@31863: hence i: "interpret_floatarith i [x] = x" by auto hoelzl@31863: hoelzl@31863: { let "?f z" = "interpret_floatarith z [x]" hoelzl@31863: assume "?f i \ { ?f a .. ?f b }" hoelzl@31863: with approx[OF _ a[symmetric], of "[x]"] approx[OF _ b[symmetric], of "[x]"] hoelzl@40881: have bnd: "x \ { l .. u'}" unfolding bounded_by_def i by auto hoelzl@31863: hoelzl@31863: have "interpret_form f' [x]" hoelzl@31863: proof (cases f') hoelzl@31863: case (Less lf rt) hoelzl@31863: with Bound a b assms hoelzl@31863: have "approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\ l u. 0 < l)" wenzelm@32960: unfolding approx_tse_form_def by auto hoelzl@31863: from approx_tse_form'_less[OF this bnd] hoelzl@31863: show ?thesis using Less by auto hoelzl@31863: next hoelzl@31863: case (LessEqual lf rt) hoelzl@31863: with Bound a b assms hoelzl@31863: have "approx_tse_form' prec t (Add rt (Minus lf)) s l u' (\ l u. 0 \ l)" wenzelm@32960: unfolding approx_tse_form_def by auto hoelzl@31863: from approx_tse_form'_le[OF this bnd] hoelzl@31863: show ?thesis using LessEqual by auto hoelzl@31863: next hoelzl@31863: case (AtLeastAtMost x lf rt) hoelzl@31863: with Bound a b assms hoelzl@31863: have "approx_tse_form' prec t (Add rt (Minus x)) s l u' (\ l u. 0 \ l)" wenzelm@32960: and "approx_tse_form' prec t (Add x (Minus lf)) s l u' (\ l u. 0 \ l)" wenzelm@32960: unfolding approx_tse_form_def lazy_conj by auto hoelzl@31863: from approx_tse_form'_le[OF this(1) bnd] approx_tse_form'_le[OF this(2) bnd] hoelzl@31863: show ?thesis using AtLeastAtMost by auto hoelzl@31863: next hoelzl@31863: case (Bound x a b f') with assms blanchet@55413: show ?thesis by (auto elim!: case_optionE simp add: f_def approx_tse_form_def) hoelzl@31863: next hoelzl@31863: case (Assign x a f') with assms blanchet@55413: show ?thesis by (auto elim!: case_optionE simp add: f_def approx_tse_form_def) hoelzl@31863: qed } thus ?thesis unfolding f_def by auto wenzelm@49351: next wenzelm@49351: case Assign wenzelm@49351: with assms show ?thesis by (auto simp add: approx_tse_form_def) wenzelm@49351: next wenzelm@49351: case LessEqual wenzelm@49351: with assms show ?thesis by (auto simp add: approx_tse_form_def) wenzelm@49351: next wenzelm@49351: case Less wenzelm@49351: with assms show ?thesis by (auto simp add: approx_tse_form_def) wenzelm@49351: next wenzelm@49351: case AtLeastAtMost wenzelm@49351: with assms show ?thesis by (auto simp add: approx_tse_form_def) hoelzl@31863: qed hoelzl@31863: hoelzl@32919: text {* @{term approx_form_eval} is only used for the {\tt value}-command. *} hoelzl@32919: hoelzl@32919: fun approx_form_eval :: "nat \ form \ (float * float) option list \ (float * float) option list" where hoelzl@32919: "approx_form_eval prec (Bound (Var n) a b f) bs = hoelzl@32919: (case (approx prec a bs, approx prec b bs) hoelzl@32919: of (Some (l, _), Some (_, u)) \ approx_form_eval prec f (bs[n := Some (l, u)]) hoelzl@32919: | _ \ bs)" | hoelzl@32919: "approx_form_eval prec (Assign (Var n) a f) bs = hoelzl@32919: (case (approx prec a bs) hoelzl@32919: of (Some (l, u)) \ approx_form_eval prec f (bs[n := Some (l, u)]) hoelzl@32919: | _ \ bs)" | hoelzl@32919: "approx_form_eval prec (Less a b) bs = bs @ [approx prec a bs, approx prec b bs]" | hoelzl@32919: "approx_form_eval prec (LessEqual a b) bs = bs @ [approx prec a bs, approx prec b bs]" | hoelzl@32919: "approx_form_eval prec (AtLeastAtMost x a b) bs = hoelzl@32919: bs @ [approx prec x bs, approx prec a bs, approx prec b bs]" | hoelzl@32919: "approx_form_eval _ _ bs = bs" hoelzl@32919: hoelzl@29805: subsection {* Implement proof method \texttt{approximation} *} hoelzl@29805: hoelzl@31811: lemmas interpret_form_equations = interpret_form.simps interpret_floatarith.simps interpret_floatarith_num hoelzl@31098: interpret_floatarith_divide interpret_floatarith_diff interpret_floatarith_tan interpret_floatarith_powr interpret_floatarith_log hoelzl@31467: interpret_floatarith_sin hoelzl@29805: haftmann@36985: oracle approximation_oracle = {* fn (thy, t) => haftmann@36985: let haftmann@36985: fun bad t = error ("Bad term: " ^ Syntax.string_of_term_global thy t); haftmann@36985: wenzelm@38716: fun term_of_bool true = @{term True} wenzelm@38716: | term_of_bool false = @{term False}; haftmann@36985: haftmann@51143: val mk_int = HOLogic.mk_number @{typ int} o @{code integer_of_int}; haftmann@51143: val dest_int = @{code int_of_integer} o snd o HOLogic.dest_number; haftmann@51143: haftmann@36985: fun term_of_float (@{code Float} (k, l)) = haftmann@51143: @{term Float} $ mk_int k $ mk_int l; haftmann@36985: haftmann@36985: fun term_of_float_float_option NONE = @{term "None :: (float \ float) option"} haftmann@36985: | term_of_float_float_option (SOME ff) = @{term "Some :: float \ float \ _"} haftmann@36985: $ HOLogic.mk_prod (pairself term_of_float ff); haftmann@36985: haftmann@36985: val term_of_float_float_option_list = haftmann@36985: HOLogic.mk_list @{typ "(float \ float) option"} o map term_of_float_float_option; haftmann@36985: haftmann@51143: fun nat_of_term t = @{code nat_of_integer} haftmann@51143: (HOLogic.dest_nat t handle TERM _ => snd (HOLogic.dest_number t)); haftmann@36985: haftmann@36985: fun float_of_term (@{term Float} $ k $ l) = haftmann@51143: @{code Float} (dest_int k, dest_int l) haftmann@36985: | float_of_term t = bad t; haftmann@36985: haftmann@36985: fun floatarith_of_term (@{term Add} $ a $ b) = @{code Add} (floatarith_of_term a, floatarith_of_term b) haftmann@36985: | floatarith_of_term (@{term Minus} $ a) = @{code Minus} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Mult} $ a $ b) = @{code Mult} (floatarith_of_term a, floatarith_of_term b) haftmann@36985: | floatarith_of_term (@{term Inverse} $ a) = @{code Inverse} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Cos} $ a) = @{code Cos} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Arctan} $ a) = @{code Arctan} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Abs} $ a) = @{code Abs} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Max} $ a $ b) = @{code Max} (floatarith_of_term a, floatarith_of_term b) haftmann@36985: | floatarith_of_term (@{term Min} $ a $ b) = @{code Min} (floatarith_of_term a, floatarith_of_term b) haftmann@36985: | floatarith_of_term @{term Pi} = @{code Pi} haftmann@36985: | floatarith_of_term (@{term Sqrt} $ a) = @{code Sqrt} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Exp} $ a) = @{code Exp} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Ln} $ a) = @{code Ln} (floatarith_of_term a) haftmann@36985: | floatarith_of_term (@{term Power} $ a $ n) = haftmann@36985: @{code Power} (floatarith_of_term a, nat_of_term n) haftmann@36985: | floatarith_of_term (@{term Var} $ n) = @{code Var} (nat_of_term n) haftmann@36985: | floatarith_of_term (@{term Num} $ m) = @{code Num} (float_of_term m) haftmann@36985: | floatarith_of_term t = bad t; haftmann@36985: haftmann@36985: fun form_of_term (@{term Bound} $ a $ b $ c $ p) = @{code Bound} haftmann@36985: (floatarith_of_term a, floatarith_of_term b, floatarith_of_term c, form_of_term p) haftmann@36985: | form_of_term (@{term Assign} $ a $ b $ p) = @{code Assign} haftmann@36985: (floatarith_of_term a, floatarith_of_term b, form_of_term p) haftmann@36985: | form_of_term (@{term Less} $ a $ b) = @{code Less} haftmann@36985: (floatarith_of_term a, floatarith_of_term b) haftmann@36985: | form_of_term (@{term LessEqual} $ a $ b) = @{code LessEqual} haftmann@36985: (floatarith_of_term a, floatarith_of_term b) haftmann@36985: | form_of_term (@{term AtLeastAtMost} $ a $ b $ c) = @{code AtLeastAtMost} haftmann@36985: (floatarith_of_term a, floatarith_of_term b, floatarith_of_term c) haftmann@36985: | form_of_term t = bad t; haftmann@36985: haftmann@36985: fun float_float_option_of_term @{term "None :: (float \ float) option"} = NONE haftmann@36985: | float_float_option_of_term (@{term "Some :: float \ float \ _"} $ ff) = haftmann@36985: SOME (pairself float_of_term (HOLogic.dest_prod ff)) haftmann@36985: | float_float_option_of_term (@{term approx'} $ n $ a $ ffs) = @{code approx'} haftmann@36985: (nat_of_term n) (floatarith_of_term a) (float_float_option_list_of_term ffs) haftmann@36985: | float_float_option_of_term t = bad t haftmann@36985: and float_float_option_list_of_term haftmann@36985: (@{term "replicate :: _ \ (float \ float) option \ _"} $ n $ @{term "None :: (float \ float) option"}) = haftmann@36985: @{code replicate} (nat_of_term n) NONE haftmann@36985: | float_float_option_list_of_term (@{term approx_form_eval} $ n $ p $ ffs) = haftmann@36985: @{code approx_form_eval} (nat_of_term n) (form_of_term p) (float_float_option_list_of_term ffs) haftmann@36985: | float_float_option_list_of_term t = map float_float_option_of_term haftmann@36985: (HOLogic.dest_list t); haftmann@36985: haftmann@36985: val nat_list_of_term = map nat_of_term o HOLogic.dest_list ; haftmann@36985: haftmann@36985: fun bool_of_term (@{term approx_form} $ n $ p $ ffs $ ms) = @{code approx_form} haftmann@36985: (nat_of_term n) (form_of_term p) (float_float_option_list_of_term ffs) (nat_list_of_term ms) haftmann@36985: | bool_of_term (@{term approx_tse_form} $ m $ n $ q $ p) = haftmann@36985: @{code approx_tse_form} (nat_of_term m) (nat_of_term n) (nat_of_term q) (form_of_term p) haftmann@36985: | bool_of_term t = bad t; haftmann@36985: haftmann@36985: fun eval t = case fastype_of t haftmann@36985: of @{typ bool} => haftmann@36985: (term_of_bool o bool_of_term) t haftmann@36985: | @{typ "(float \ float) option"} => haftmann@36985: (term_of_float_float_option o float_float_option_of_term) t haftmann@36985: | @{typ "(float \ float) option list"} => haftmann@36985: (term_of_float_float_option_list o float_float_option_list_of_term) t haftmann@36985: | _ => bad t; haftmann@36985: wenzelm@52131: val normalize = eval o Envir.beta_norm o Envir.eta_long []; haftmann@36985: haftmann@36985: in Thm.cterm_of thy (Logic.mk_equals (t, normalize t)) end haftmann@36985: *} hoelzl@31099: hoelzl@31099: ML {* wenzelm@32212: fun reorder_bounds_tac prems i = hoelzl@29805: let haftmann@38558: fun variable_of_bound (Const (@{const_name Trueprop}, _) $ haftmann@37677: (Const (@{const_name Set.member}, _) $ hoelzl@31811: Free (name, _) $ _)) = name haftmann@38558: | variable_of_bound (Const (@{const_name Trueprop}, _) $ haftmann@38864: (Const (@{const_name HOL.eq}, _) $ hoelzl@31811: Free (name, _) $ _)) = name hoelzl@31811: | variable_of_bound t = raise TERM ("variable_of_bound", [t]) hoelzl@31811: hoelzl@31811: val variable_bounds hoelzl@31811: = map (` (variable_of_bound o prop_of)) prems hoelzl@31811: hoelzl@31811: fun add_deps (name, bnds) hoelzl@32650: = Graph.add_deps_acyclic (name, hoelzl@32650: remove (op =) name (Term.add_free_names (prop_of bnds) [])) hoelzl@32650: hoelzl@31811: val order = Graph.empty hoelzl@31811: |> fold Graph.new_node variable_bounds hoelzl@31811: |> fold add_deps variable_bounds hoelzl@32650: |> Graph.strong_conn |> map the_single |> rev hoelzl@31811: |> map_filter (AList.lookup (op =) variable_bounds) hoelzl@31811: hoelzl@31811: fun prepend_prem th tac hoelzl@31811: = tac THEN rtac (th RSN (2, @{thm mp})) i hoelzl@31811: in hoelzl@31811: fold prepend_prem order all_tac hoelzl@31811: end hoelzl@31811: haftmann@36985: fun approximation_conv ctxt ct = wenzelm@42361: approximation_oracle (Proof_Context.theory_of ctxt, Thm.term_of ct |> tap (tracing o Syntax.string_of_term ctxt)); haftmann@36985: haftmann@36985: fun approximate ctxt t = wenzelm@42361: approximation_oracle (Proof_Context.theory_of ctxt, t) haftmann@36985: |> Thm.prop_of |> Logic.dest_equals |> snd; haftmann@36985: hoelzl@31811: (* Should be in HOL.thy ? *) haftmann@36985: fun gen_eval_tac conv ctxt = CONVERSION haftmann@36985: (Object_Logic.judgment_conv (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)) haftmann@36985: THEN' rtac TrueI hoelzl@29805: wenzelm@39556: val form_equations = @{thms interpret_form_equations}; hoelzl@31811: hoelzl@31863: fun rewrite_interpret_form_tac ctxt prec splitting taylor i st = let haftmann@46545: fun lookup_splitting (Free (name, _)) hoelzl@31863: = case AList.lookup (op =) splitting name hoelzl@31863: of SOME s => HOLogic.mk_number @{typ nat} s hoelzl@31863: | NONE => @{term "0 :: nat"} hoelzl@31811: val vs = nth (prems_of st) (i - 1) hoelzl@31811: |> Logic.strip_imp_concl hoelzl@31811: |> HOLogic.dest_Trueprop hoelzl@31811: |> Term.strip_comb |> snd |> List.last hoelzl@31811: |> HOLogic.dest_list hoelzl@31811: val p = prec hoelzl@31811: |> HOLogic.mk_number @{typ nat} wenzelm@42361: |> Thm.cterm_of (Proof_Context.theory_of ctxt) hoelzl@31863: in case taylor hoelzl@31863: of NONE => let hoelzl@31863: val n = vs |> length hoelzl@31863: |> HOLogic.mk_number @{typ nat} wenzelm@42361: |> Thm.cterm_of (Proof_Context.theory_of ctxt) hoelzl@31863: val s = vs hoelzl@31863: |> map lookup_splitting hoelzl@31863: |> HOLogic.mk_list @{typ nat} wenzelm@42361: |> Thm.cterm_of (Proof_Context.theory_of ctxt) hoelzl@31863: in hoelzl@31863: (rtac (Thm.instantiate ([], [(@{cpat "?n::nat"}, n), hoelzl@31863: (@{cpat "?prec::nat"}, p), hoelzl@31863: (@{cpat "?ss::nat list"}, s)]) hoelzl@31863: @{thm "approx_form"}) i wenzelm@52090: THEN simp_tac (put_simpset (simpset_of @{context}) ctxt) i) st hoelzl@31863: end hoelzl@31863: hoelzl@31863: | SOME t => if length vs <> 1 then raise (TERM ("More than one variable used for taylor series expansion", [prop_of st])) hoelzl@31863: else let hoelzl@31863: val t = t hoelzl@31863: |> HOLogic.mk_number @{typ nat} wenzelm@42361: |> Thm.cterm_of (Proof_Context.theory_of ctxt) hoelzl@31863: val s = vs |> map lookup_splitting |> hd wenzelm@42361: |> Thm.cterm_of (Proof_Context.theory_of ctxt) hoelzl@31863: in hoelzl@31863: rtac (Thm.instantiate ([], [(@{cpat "?s::nat"}, s), hoelzl@31863: (@{cpat "?t::nat"}, t), hoelzl@31863: (@{cpat "?prec::nat"}, p)]) hoelzl@31863: @{thm "approx_tse_form"}) i st hoelzl@31863: end hoelzl@31811: end hoelzl@31811: haftmann@46545: val free = Args.context -- Args.term >> (fn (_, Free (n, _)) => n | (ctxt, t) => hoelzl@31811: error ("Bad free variable: " ^ Syntax.string_of_term ctxt t)); hoelzl@29805: *} hoelzl@29805: hoelzl@31811: lemma intervalE: "a \ x \ x \ b \ \ x \ { a .. b } \ P\ \ P" hoelzl@31811: by auto hoelzl@31811: hoelzl@31811: lemma meta_eqE: "x \ a \ \ x = a \ P\ \ P" hoelzl@31811: by auto hoelzl@31811: wenzelm@30549: method_setup approximation = {* wenzelm@36960: Scan.lift Parse.nat hoelzl@31863: -- hoelzl@31811: Scan.optional (Scan.lift (Args.$$$ "splitting" |-- Args.colon) wenzelm@36960: |-- Parse.and_list' (free --| Scan.lift (Args.$$$ "=") -- Scan.lift Parse.nat)) [] hoelzl@31863: -- hoelzl@31863: Scan.option (Scan.lift (Args.$$$ "taylor" |-- Args.colon) wenzelm@36960: |-- (free |-- Scan.lift (Args.$$$ "=") |-- Scan.lift Parse.nat)) hoelzl@31811: >> hoelzl@31863: (fn ((prec, splitting), taylor) => fn ctxt => wenzelm@30549: SIMPLE_METHOD' (fn i => hoelzl@31811: REPEAT (FIRST' [etac @{thm intervalE}, hoelzl@31811: etac @{thm meta_eqE}, hoelzl@31811: rtac @{thm impI}] i) wenzelm@52090: THEN Subgoal.FOCUS (fn {prems, ...} => reorder_bounds_tac prems i) ctxt i hoelzl@32650: THEN DETERM (TRY (filter_prems_tac (K false) i)) haftmann@52286: THEN DETERM (Reification.tac ctxt form_equations NONE i) hoelzl@31863: THEN rewrite_interpret_form_tac ctxt prec splitting taylor i haftmann@36985: THEN gen_eval_tac (approximation_conv ctxt) ctxt i)) wenzelm@55506: *} "real number approximation" hoelzl@31811: haftmann@56813: ML_file "approximation.ML" haftmann@56813: hoelzl@29805: end