src/HOL/Decision_Procs/Approximation.thy
 author hoelzl Wed Mar 11 13:53:51 2009 +0100 (2009-03-11) changeset 30443 873fa77be5f0 parent 30439 57c68b3af2ea child 30510 4120fc59dd85 permissions -rw-r--r--
Extended approximation boundaries by fractions and base-2 floating point numbers
 hoelzl@30443 ` 1` ```(* Author: Johannes Hoelzl 2008 / 2009 *) ``` wenzelm@30122 ` 2` hoelzl@29805 ` 3` ```header {* Prove unequations about real numbers by computation *} ``` wenzelm@30122 ` 4` hoelzl@29805 ` 5` ```theory Approximation ``` haftmann@29823 ` 6` ```imports Complex_Main Float Reflection Dense_Linear_Order Efficient_Nat ``` hoelzl@29805 ` 7` ```begin ``` hoelzl@29805 ` 8` hoelzl@29805 ` 9` ```section "Horner Scheme" ``` hoelzl@29805 ` 10` hoelzl@29805 ` 11` ```subsection {* Define auxiliary helper @{text horner} function *} ``` hoelzl@29805 ` 12` hoelzl@29805 ` 13` ```fun horner :: "(nat \ nat) \ (nat \ nat \ nat) \ nat \ nat \ nat \ real \ real" where ``` hoelzl@29805 ` 14` ```"horner F G 0 i k x = 0" | ``` hoelzl@29805 ` 15` ```"horner F G (Suc n) i k x = 1 / real k - x * horner F G n (F i) (G i k) x" ``` hoelzl@29805 ` 16` hoelzl@29805 ` 17` ```lemma horner_schema': fixes x :: real and a :: "nat \ real" ``` hoelzl@29805 ` 18` ``` 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)" by auto ``` hoelzl@29805 ` 21` ``` show ?thesis unfolding setsum_right_distrib shift_pow real_diff_def setsum_negf[symmetric] setsum_head_upt_Suc[OF zero_less_Suc] ``` hoelzl@29805 ` 22` ``` setsum_reindex[OF inj_Suc, unfolded comp_def, symmetric, of "\ n. (-1)^n *a n * x^n"] by auto ``` hoelzl@29805 ` 23` ```qed ``` hoelzl@29805 ` 24` hoelzl@29805 ` 25` ```lemma horner_schema: fixes f :: "nat \ nat" and G :: "nat \ nat \ nat" and F :: "nat \ nat" ``` hoelzl@29805 ` 26` ``` assumes f_Suc: "\n. f (Suc n) = G ((F^n) s) (f n)" ``` hoelzl@29805 ` 27` ``` shows "horner F G n ((F^j') s) (f j') x = (\ j = 0..< n. -1^j * (1 / real (f (j' + j))) * x^j)" ``` hoelzl@29805 ` 28` ```proof (induct n arbitrary: i k j') ``` hoelzl@29805 ` 29` ``` case (Suc n) ``` hoelzl@29805 ` 30` hoelzl@29805 ` 31` ``` show ?case unfolding horner.simps Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc] ``` hoelzl@29805 ` 32` ``` using horner_schema'[of "\ j. 1 / real (f (j' + j))"] by auto ``` hoelzl@29805 ` 33` ```qed auto ``` hoelzl@29805 ` 34` hoelzl@29805 ` 35` ```lemma horner_bounds': ``` hoelzl@29805 ` 36` ``` assumes "0 \ Ifloat x" and f_Suc: "\n. f (Suc n) = G ((F^n) s) (f n)" ``` hoelzl@29805 ` 37` ``` and lb_0: "\ i k x. lb 0 i k x = 0" ``` hoelzl@29805 ` 38` ``` and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)" ``` hoelzl@29805 ` 39` ``` and ub_0: "\ i k x. ub 0 i k x = 0" ``` hoelzl@29805 ` 40` ``` and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)" ``` hoelzl@29805 ` 41` ``` shows "Ifloat (lb n ((F^j') s) (f j') x) \ horner F G n ((F^j') s) (f j') (Ifloat x) \ ``` hoelzl@29805 ` 42` ``` horner F G n ((F^j') s) (f j') (Ifloat x) \ Ifloat (ub n ((F^j') s) (f j') x)" ``` hoelzl@29805 ` 43` ``` (is "?lb n j' \ ?horner n j' \ ?horner n j' \ ?ub n j'") ``` hoelzl@29805 ` 44` ```proof (induct n arbitrary: j') ``` hoelzl@29805 ` 45` ``` case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto ``` hoelzl@29805 ` 46` ```next ``` hoelzl@29805 ` 47` ``` case (Suc n) ``` hoelzl@29805 ` 48` ``` have "?lb (Suc n) j' \ ?horner (Suc n) j'" unfolding lb_Suc ub_Suc horner.simps Ifloat_sub diff_def ``` hoelzl@29805 ` 49` ``` proof (rule add_mono) ``` hoelzl@29805 ` 50` ``` show "Ifloat (lapprox_rat prec 1 (int (f j'))) \ 1 / real (f j')" using lapprox_rat[of prec 1 "int (f j')"] by auto ``` hoelzl@29805 ` 51` ``` from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct2] `0 \ Ifloat x` ``` hoelzl@29805 ` 52` ``` show "- Ifloat (x * ub n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) x) \ - (Ifloat x * horner F G n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) (Ifloat x))" ``` hoelzl@29805 ` 53` ``` unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono) ``` hoelzl@29805 ` 54` ``` qed ``` hoelzl@29805 ` 55` ``` moreover have "?horner (Suc n) j' \ ?ub (Suc n) j'" unfolding ub_Suc ub_Suc horner.simps Ifloat_sub diff_def ``` hoelzl@29805 ` 56` ``` proof (rule add_mono) ``` hoelzl@29805 ` 57` ``` show "1 / real (f j') \ Ifloat (rapprox_rat prec 1 (int (f j')))" using rapprox_rat[of 1 "int (f j')" prec] by auto ``` hoelzl@29805 ` 58` ``` from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct1] `0 \ Ifloat x` ``` hoelzl@29805 ` 59` ``` show "- (Ifloat x * horner F G n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) (Ifloat x)) \ ``` hoelzl@29805 ` 60` ``` - Ifloat (x * lb n (F ((F ^ j') s)) (G ((F ^ j') s) (f j')) x)" ``` hoelzl@29805 ` 61` ``` unfolding Ifloat_mult neg_le_iff_le by (rule mult_left_mono) ``` hoelzl@29805 ` 62` ``` qed ``` hoelzl@29805 ` 63` ``` ultimately show ?case by blast ``` hoelzl@29805 ` 64` ```qed ``` hoelzl@29805 ` 65` hoelzl@29805 ` 66` ```subsection "Theorems for floating point functions implementing the horner scheme" ``` hoelzl@29805 ` 67` hoelzl@29805 ` 68` ```text {* ``` hoelzl@29805 ` 69` hoelzl@29805 ` 70` ```Here @{term_type "f :: nat \ nat"} is the sequence defining the Taylor series, the coefficients are ``` hoelzl@29805 ` 71` ```all alternating and reciprocs. We use @{term G} and @{term F} to describe the computation of @{term f}. ``` hoelzl@29805 ` 72` hoelzl@29805 ` 73` ```*} ``` hoelzl@29805 ` 74` hoelzl@29805 ` 75` ```lemma horner_bounds: fixes F :: "nat \ nat" and G :: "nat \ nat \ nat" ``` hoelzl@29805 ` 76` ``` assumes "0 \ Ifloat x" and f_Suc: "\n. f (Suc n) = G ((F^n) s) (f n)" ``` hoelzl@29805 ` 77` ``` and lb_0: "\ i k x. lb 0 i k x = 0" ``` hoelzl@29805 ` 78` ``` and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) - x * (ub n (F i) (G i k) x)" ``` hoelzl@29805 ` 79` ``` and ub_0: "\ i k x. ub 0 i k x = 0" ``` hoelzl@29805 ` 80` ``` and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) - x * (lb n (F i) (G i k) x)" ``` hoelzl@29805 ` 81` ``` shows "Ifloat (lb n ((F^j') s) (f j') x) \ (\j=0..j=0.. Ifloat (ub n ((F^j') s) (f j') x)" (is "?ub") ``` hoelzl@29805 ` 83` ```proof - ``` hoelzl@29805 ` 84` ``` have "?lb \ ?ub" ``` hoelzl@29805 ` 85` ``` using horner_bounds'[where lb=lb, OF `0 \ Ifloat x` f_Suc lb_0 lb_Suc ub_0 ub_Suc] ``` hoelzl@29805 ` 86` ``` unfolding horner_schema[where f=f, OF f_Suc] . ``` hoelzl@29805 ` 87` ``` thus "?lb" and "?ub" by auto ``` hoelzl@29805 ` 88` ```qed ``` hoelzl@29805 ` 89` hoelzl@29805 ` 90` ```lemma horner_bounds_nonpos: fixes F :: "nat \ nat" and G :: "nat \ nat \ nat" ``` hoelzl@29805 ` 91` ``` assumes "Ifloat x \ 0" and f_Suc: "\n. f (Suc n) = G ((F^n) s) (f n)" ``` hoelzl@29805 ` 92` ``` and lb_0: "\ i k x. lb 0 i k x = 0" ``` hoelzl@29805 ` 93` ``` and lb_Suc: "\ n i k x. lb (Suc n) i k x = lapprox_rat prec 1 (int k) + x * (ub n (F i) (G i k) x)" ``` hoelzl@29805 ` 94` ``` and ub_0: "\ i k x. ub 0 i k x = 0" ``` hoelzl@29805 ` 95` ``` and ub_Suc: "\ n i k x. ub (Suc n) i k x = rapprox_rat prec 1 (int k) + x * (lb n (F i) (G i k) x)" ``` hoelzl@29805 ` 96` ``` shows "Ifloat (lb n ((F^j') s) (f j') x) \ (\j=0..j=0.. Ifloat (ub n ((F^j') s) (f j') x)" (is "?ub") ``` hoelzl@29805 ` 98` ```proof - ``` hoelzl@29805 ` 99` ``` { fix x y z :: float have "x - y * z = x + - y * z" ``` hoelzl@29805 ` 100` ``` by (cases x, cases y, cases z, simp add: plus_float.simps minus_float.simps uminus_float.simps times_float.simps algebra_simps) ``` hoelzl@29805 ` 101` ``` } note diff_mult_minus = this ``` hoelzl@29805 ` 102` hoelzl@29805 ` 103` ``` { fix x :: float have "- (- x) = x" by (cases x, auto simp add: uminus_float.simps) } note minus_minus = this ``` hoelzl@29805 ` 104` hoelzl@29805 ` 105` ``` have move_minus: "Ifloat (-x) = -1 * Ifloat x" by auto ``` hoelzl@29805 ` 106` hoelzl@29805 ` 107` ``` have sum_eq: "(\j=0..j = 0.. {0 ..< n}" ``` hoelzl@29805 ` 111` ``` show "1 / real (f (j' + j)) * Ifloat x ^ j = -1 ^ j * (1 / real (f (j' + j))) * Ifloat (- x) ^ j" ``` hoelzl@29805 ` 112` ``` unfolding move_minus power_mult_distrib real_mult_assoc[symmetric] ``` hoelzl@29805 ` 113` ``` unfolding real_mult_commute unfolding real_mult_assoc[of "-1^j", symmetric] power_mult_distrib[symmetric] ``` hoelzl@29805 ` 114` ``` by auto ``` hoelzl@29805 ` 115` ``` qed ``` hoelzl@29805 ` 116` hoelzl@29805 ` 117` ``` have "0 \ Ifloat (-x)" using assms by auto ``` hoelzl@29805 ` 118` ``` from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec ``` hoelzl@29805 ` 119` ``` 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 ` 120` ``` OF this f_Suc lb_0 refl ub_0 refl] ``` hoelzl@29805 ` 121` ``` show "?lb" and "?ub" unfolding minus_minus sum_eq ``` hoelzl@29805 ` 122` ``` by auto ``` hoelzl@29805 ` 123` ```qed ``` hoelzl@29805 ` 124` hoelzl@29805 ` 125` ```subsection {* Selectors for next even or odd number *} ``` hoelzl@29805 ` 126` hoelzl@29805 ` 127` ```text {* ``` hoelzl@29805 ` 128` hoelzl@29805 ` 129` ```The horner scheme computes alternating series. To get the upper and lower bounds we need to ``` hoelzl@29805 ` 130` ```guarantee to access a even or odd member. To do this we use @{term get_odd} and @{term get_even}. ``` hoelzl@29805 ` 131` hoelzl@29805 ` 132` ```*} ``` hoelzl@29805 ` 133` hoelzl@29805 ` 134` ```definition get_odd :: "nat \ nat" where ``` hoelzl@29805 ` 135` ``` "get_odd n = (if odd n then n else (Suc n))" ``` hoelzl@29805 ` 136` hoelzl@29805 ` 137` ```definition get_even :: "nat \ nat" where ``` hoelzl@29805 ` 138` ``` "get_even n = (if even n then n else (Suc n))" ``` hoelzl@29805 ` 139` hoelzl@29805 ` 140` ```lemma get_odd[simp]: "odd (get_odd n)" unfolding get_odd_def by (cases "odd n", auto) ``` hoelzl@29805 ` 141` ```lemma get_even[simp]: "even (get_even n)" unfolding get_even_def by (cases "even n", auto) ``` hoelzl@29805 ` 142` ```lemma get_odd_ex: "\ k. Suc k = get_odd n \ odd (Suc k)" ``` hoelzl@29805 ` 143` ```proof (cases "odd n") ``` hoelzl@29805 ` 144` ``` case True hence "0 < n" by (rule odd_pos) ``` hoelzl@29805 ` 145` ``` from gr0_implies_Suc[OF this] obtain k where "Suc k = n" by auto ``` hoelzl@29805 ` 146` ``` thus ?thesis unfolding get_odd_def if_P[OF True] using True[unfolded `Suc k = n`[symmetric]] by blast ``` hoelzl@29805 ` 147` ```next ``` hoelzl@29805 ` 148` ``` case False hence "odd (Suc n)" by auto ``` hoelzl@29805 ` 149` ``` thus ?thesis unfolding get_odd_def if_not_P[OF False] by blast ``` hoelzl@29805 ` 150` ```qed ``` hoelzl@29805 ` 151` hoelzl@29805 ` 152` ```lemma get_even_double: "\i. get_even n = 2 * i" using get_even[unfolded even_mult_two_ex] . ``` hoelzl@29805 ` 153` ```lemma get_odd_double: "\i. get_odd n = 2 * i + 1" using get_odd[unfolded odd_Suc_mult_two_ex] by auto ``` hoelzl@29805 ` 154` hoelzl@29805 ` 155` ```section "Power function" ``` hoelzl@29805 ` 156` hoelzl@29805 ` 157` ```definition float_power_bnds :: "nat \ float \ float \ float * float" where ``` hoelzl@29805 ` 158` ```"float_power_bnds n l u = (if odd n \ 0 < l then (l ^ n, u ^ n) ``` hoelzl@29805 ` 159` ``` else if u < 0 then (u ^ n, l ^ n) ``` hoelzl@29805 ` 160` ``` else (0, (max (-l) u) ^ n))" ``` hoelzl@29805 ` 161` hoelzl@29805 ` 162` ```lemma float_power_bnds: assumes "(l1, u1) = float_power_bnds n l u" and "x \ {Ifloat l .. Ifloat u}" ``` hoelzl@29805 ` 163` ``` shows "x^n \ {Ifloat l1..Ifloat u1}" ``` hoelzl@29805 ` 164` ```proof (cases "even n") ``` hoelzl@29805 ` 165` ``` case True ``` hoelzl@29805 ` 166` ``` show ?thesis ``` hoelzl@29805 ` 167` ``` proof (cases "0 < l") ``` hoelzl@29805 ` 168` ``` case True hence "odd n \ 0 < l" and "0 \ Ifloat l" unfolding less_float_def by auto ``` hoelzl@29805 ` 169` ``` have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \ 0 < l`] by auto ``` hoelzl@29805 ` 170` ``` have "Ifloat l^n \ x^n" and "x^n \ Ifloat u^n " using `0 \ Ifloat l` and assms unfolding atLeastAtMost_iff using power_mono[of "Ifloat l" x] power_mono[of x "Ifloat u"] by auto ``` hoelzl@29805 ` 171` ``` thus ?thesis using assms `0 < l` unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto ``` hoelzl@29805 ` 172` ``` next ``` hoelzl@29805 ` 173` ``` case False hence P: "\ (odd n \ 0 < l)" using `even n` by auto ``` hoelzl@29805 ` 174` ``` show ?thesis ``` hoelzl@29805 ` 175` ``` proof (cases "u < 0") ``` hoelzl@29805 ` 176` ``` case True hence "0 \ - Ifloat u" and "- Ifloat u \ - x" and "0 \ - x" and "-x \ - Ifloat l" using assms unfolding less_float_def by auto ``` hoelzl@29805 ` 177` ``` hence "Ifloat u^n \ x^n" and "x^n \ Ifloat l^n" using power_mono[of "-x" "-Ifloat l" n] power_mono[of "-Ifloat u" "-x" n] ``` hoelzl@29805 ` 178` ``` unfolding power_minus_even[OF `even n`] by auto ``` hoelzl@29805 ` 179` ``` moreover have u1: "u1 = l ^ n" and l1: "l1 = u ^ n" using assms unfolding float_power_bnds_def if_not_P[OF P] if_P[OF True] by auto ``` hoelzl@29805 ` 180` ``` ultimately show ?thesis using float_power by auto ``` hoelzl@29805 ` 181` ``` next ``` hoelzl@29805 ` 182` ``` case False ``` hoelzl@29805 ` 183` ``` have "\x\ \ Ifloat (max (-l) u)" ``` hoelzl@29805 ` 184` ``` proof (cases "-l \ u") ``` hoelzl@29805 ` 185` ``` case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding le_float_def by auto ``` hoelzl@29805 ` 186` ``` next ``` hoelzl@29805 ` 187` ``` case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding le_float_def by auto ``` hoelzl@29805 ` 188` ``` qed ``` hoelzl@29805 ` 189` ``` hence x_abs: "\x\ \ \Ifloat (max (-l) u)\" by auto ``` hoelzl@29805 ` 190` ``` have u1: "u1 = (max (-l) u) ^ n" and l1: "l1 = 0" using assms unfolding float_power_bnds_def if_not_P[OF P] if_not_P[OF False] by auto ``` hoelzl@29805 ` 191` ``` show ?thesis unfolding atLeastAtMost_iff l1 u1 float_power using zero_le_even_power[OF `even n`] power_mono_even[OF `even n` x_abs] by auto ``` hoelzl@29805 ` 192` ``` qed ``` hoelzl@29805 ` 193` ``` qed ``` hoelzl@29805 ` 194` ```next ``` hoelzl@29805 ` 195` ``` case False hence "odd n \ 0 < l" by auto ``` hoelzl@29805 ` 196` ``` have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \ 0 < l`] by auto ``` hoelzl@29805 ` 197` ``` have "Ifloat l^n \ x^n" and "x^n \ Ifloat u^n " using assms unfolding atLeastAtMost_iff using power_mono_odd[OF False] by auto ``` hoelzl@29805 ` 198` ``` thus ?thesis unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto ``` hoelzl@29805 ` 199` ```qed ``` hoelzl@29805 ` 200` hoelzl@29805 ` 201` ```lemma bnds_power: "\ x l u. (l1, u1) = float_power_bnds n l u \ x \ {Ifloat l .. Ifloat u} \ Ifloat l1 \ x^n \ x^n \ Ifloat u1" ``` hoelzl@29805 ` 202` ``` using float_power_bnds by auto ``` hoelzl@29805 ` 203` hoelzl@29805 ` 204` ```section "Square root" ``` hoelzl@29805 ` 205` hoelzl@29805 ` 206` ```text {* ``` hoelzl@29805 ` 207` hoelzl@29805 ` 208` ```The square root computation is implemented as newton iteration. As first first step we use the ``` hoelzl@29805 ` 209` ```nearest power of two greater than the square root. ``` hoelzl@29805 ` 210` hoelzl@29805 ` 211` ```*} ``` hoelzl@29805 ` 212` hoelzl@29805 ` 213` ```fun sqrt_iteration :: "nat \ nat \ float \ float" where ``` hoelzl@29805 ` 214` ```"sqrt_iteration prec 0 (Float m e) = Float 1 ((e + bitlen m) div 2 + 1)" | ``` hoelzl@29805 ` 215` ```"sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x ``` hoelzl@29805 ` 216` ``` in Float 1 -1 * (y + float_divr prec x y))" ``` hoelzl@29805 ` 217` hoelzl@29805 ` 218` ```definition ub_sqrt :: "nat \ float \ float option" where ``` hoelzl@29805 ` 219` ```"ub_sqrt prec x = (if 0 < x then Some (sqrt_iteration prec prec x) else if x < 0 then None else Some 0)" ``` hoelzl@29805 ` 220` hoelzl@29805 ` 221` ```definition lb_sqrt :: "nat \ float \ float option" where ``` hoelzl@29805 ` 222` ```"lb_sqrt prec x = (if 0 < x then Some (float_divl prec x (sqrt_iteration prec prec x)) else if x < 0 then None else Some 0)" ``` hoelzl@29805 ` 223` hoelzl@29805 ` 224` ```lemma sqrt_ub_pos_pos_1: ``` hoelzl@29805 ` 225` ``` assumes "sqrt x < b" and "0 < b" and "0 < x" ``` hoelzl@29805 ` 226` ``` shows "sqrt x < (b + x / b)/2" ``` hoelzl@29805 ` 227` ```proof - ``` hoelzl@29805 ` 228` ``` from assms have "0 < (b - sqrt x) ^ 2 " by simp ``` hoelzl@29805 ` 229` ``` also have "\ = b ^ 2 - 2 * b * sqrt x + (sqrt x) ^ 2" by algebra ``` hoelzl@29805 ` 230` ``` also have "\ = b ^ 2 - 2 * b * sqrt x + x" using assms by (simp add: real_sqrt_pow2) ``` hoelzl@29805 ` 231` ``` finally have "0 < b ^ 2 - 2 * b * sqrt x + x" by assumption ``` hoelzl@29805 ` 232` ``` hence "0 < b / 2 - sqrt x + x / (2 * b)" using assms ``` hoelzl@29805 ` 233` ``` by (simp add: field_simps power2_eq_square) ``` hoelzl@29805 ` 234` ``` thus ?thesis by (simp add: field_simps) ``` hoelzl@29805 ` 235` ```qed ``` hoelzl@29805 ` 236` hoelzl@29805 ` 237` ```lemma sqrt_iteration_bound: assumes "0 < Ifloat x" ``` hoelzl@29805 ` 238` ``` shows "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec n x)" ``` hoelzl@29805 ` 239` ```proof (induct n) ``` hoelzl@29805 ` 240` ``` case 0 ``` hoelzl@29805 ` 241` ``` show ?case ``` hoelzl@29805 ` 242` ``` proof (cases x) ``` hoelzl@29805 ` 243` ``` case (Float m e) ``` hoelzl@29805 ` 244` ``` hence "0 < m" using float_pos_m_pos[unfolded less_float_def] assms by auto ``` hoelzl@29805 ` 245` ``` hence "0 < sqrt (real m)" by auto ``` hoelzl@29805 ` 246` hoelzl@29805 ` 247` ``` have int_nat_bl: "int (nat (bitlen m)) = bitlen m" using bitlen_ge0 by auto ``` hoelzl@29805 ` 248` hoelzl@29805 ` 249` ``` have "Ifloat x = (real m / 2^nat (bitlen m)) * pow2 (e + int (nat (bitlen m)))" ``` hoelzl@29805 ` 250` ``` unfolding pow2_add pow2_int Float Ifloat.simps by auto ``` hoelzl@29805 ` 251` ``` also have "\ < 1 * pow2 (e + int (nat (bitlen m)))" ``` hoelzl@29805 ` 252` ``` proof (rule mult_strict_right_mono, auto) ``` hoelzl@29805 ` 253` ``` show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2] ``` hoelzl@29805 ` 254` ``` unfolding real_of_int_less_iff[of m, symmetric] by auto ``` hoelzl@29805 ` 255` ``` qed ``` hoelzl@29805 ` 256` ``` finally have "sqrt (Ifloat x) < sqrt (pow2 (e + bitlen m))" unfolding int_nat_bl by auto ``` hoelzl@29805 ` 257` ``` also have "\ \ pow2 ((e + bitlen m) div 2 + 1)" ``` hoelzl@29805 ` 258` ``` proof - ``` hoelzl@29805 ` 259` ``` let ?E = "e + bitlen m" ``` hoelzl@29805 ` 260` ``` have E_mod_pow: "pow2 (?E mod 2) < 4" ``` hoelzl@29805 ` 261` ``` proof (cases "?E mod 2 = 1") ``` hoelzl@29805 ` 262` ``` case True thus ?thesis by auto ``` hoelzl@29805 ` 263` ``` next ``` hoelzl@29805 ` 264` ``` case False ``` hoelzl@29805 ` 265` ``` have "0 \ ?E mod 2" by auto ``` hoelzl@29805 ` 266` ``` have "?E mod 2 < 2" by auto ``` hoelzl@29805 ` 267` ``` from this[THEN zless_imp_add1_zle] ``` hoelzl@29805 ` 268` ``` have "?E mod 2 \ 0" using False by auto ``` hoelzl@29805 ` 269` ``` from xt1(5)[OF `0 \ ?E mod 2` this] ``` hoelzl@29805 ` 270` ``` show ?thesis by auto ``` hoelzl@29805 ` 271` ``` qed ``` hoelzl@29805 ` 272` ``` hence "sqrt (pow2 (?E mod 2)) < sqrt (2 * 2)" by auto ``` hoelzl@29805 ` 273` ``` hence E_mod_pow: "sqrt (pow2 (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto ``` hoelzl@29805 ` 274` hoelzl@29805 ` 275` ``` have E_eq: "pow2 ?E = pow2 (?E div 2 + ?E div 2 + ?E mod 2)" by auto ``` hoelzl@29805 ` 276` ``` have "sqrt (pow2 ?E) = sqrt (pow2 (?E div 2) * pow2 (?E div 2) * pow2 (?E mod 2))" ``` hoelzl@29805 ` 277` ``` unfolding E_eq unfolding pow2_add .. ``` hoelzl@29805 ` 278` ``` also have "\ = pow2 (?E div 2) * sqrt (pow2 (?E mod 2))" ``` hoelzl@29805 ` 279` ``` unfolding real_sqrt_mult[of _ "pow2 (?E mod 2)"] real_sqrt_abs2 by auto ``` hoelzl@29805 ` 280` ``` also have "\ < pow2 (?E div 2) * 2" ``` hoelzl@29805 ` 281` ``` by (rule mult_strict_left_mono, auto intro: E_mod_pow) ``` hoelzl@29805 ` 282` ``` also have "\ = pow2 (?E div 2 + 1)" unfolding zadd_commute[of _ 1] pow2_add1 by auto ``` hoelzl@29805 ` 283` ``` finally show ?thesis by auto ``` hoelzl@29805 ` 284` ``` qed ``` hoelzl@29805 ` 285` ``` finally show ?thesis ``` hoelzl@29805 ` 286` ``` unfolding Float sqrt_iteration.simps Ifloat.simps by auto ``` hoelzl@29805 ` 287` ``` qed ``` hoelzl@29805 ` 288` ```next ``` hoelzl@29805 ` 289` ``` case (Suc n) ``` hoelzl@29805 ` 290` ``` let ?b = "sqrt_iteration prec n x" ``` hoelzl@29805 ` 291` ``` have "0 < sqrt (Ifloat x)" using `0 < Ifloat x` by auto ``` hoelzl@29805 ` 292` ``` also have "\ < Ifloat ?b" using Suc . ``` hoelzl@29805 ` 293` ``` finally have "sqrt (Ifloat x) < (Ifloat ?b + Ifloat x / Ifloat ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < Ifloat x`] by auto ``` hoelzl@29805 ` 294` ``` also have "\ \ (Ifloat ?b + Ifloat (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr) ``` hoelzl@29805 ` 295` ``` also have "\ = Ifloat (Float 1 -1) * (Ifloat ?b + Ifloat (float_divr prec x ?b))" by auto ``` hoelzl@29805 ` 296` ``` finally show ?case unfolding sqrt_iteration.simps Let_def Ifloat_mult Ifloat_add right_distrib . ``` hoelzl@29805 ` 297` ```qed ``` hoelzl@29805 ` 298` hoelzl@29805 ` 299` ```lemma sqrt_iteration_lower_bound: assumes "0 < Ifloat x" ``` hoelzl@29805 ` 300` ``` shows "0 < Ifloat (sqrt_iteration prec n x)" (is "0 < ?sqrt") ``` hoelzl@29805 ` 301` ```proof - ``` hoelzl@29805 ` 302` ``` have "0 < sqrt (Ifloat x)" using assms by auto ``` hoelzl@29805 ` 303` ``` also have "\ < ?sqrt" using sqrt_iteration_bound[OF assms] . ``` hoelzl@29805 ` 304` ``` finally show ?thesis . ``` hoelzl@29805 ` 305` ```qed ``` hoelzl@29805 ` 306` hoelzl@29805 ` 307` ```lemma lb_sqrt_lower_bound: assumes "0 \ Ifloat x" ``` hoelzl@29805 ` 308` ``` shows "0 \ Ifloat (the (lb_sqrt prec x))" ``` hoelzl@29805 ` 309` ```proof (cases "0 < x") ``` hoelzl@29805 ` 310` ``` case True hence "0 < Ifloat x" and "0 \ x" using `0 \ Ifloat x` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 311` ``` hence "0 < sqrt_iteration prec prec x" unfolding less_float_def using sqrt_iteration_lower_bound by auto ``` hoelzl@29805 ` 312` ``` hence "0 \ Ifloat (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \ x`] unfolding le_float_def by auto ``` hoelzl@29805 ` 313` ``` thus ?thesis unfolding lb_sqrt_def using True by auto ``` hoelzl@29805 ` 314` ```next ``` hoelzl@29805 ` 315` ``` case False with `0 \ Ifloat x` have "Ifloat x = 0" unfolding less_float_def by auto ``` hoelzl@29805 ` 316` ``` thus ?thesis unfolding lb_sqrt_def less_float_def by auto ``` hoelzl@29805 ` 317` ```qed ``` hoelzl@29805 ` 318` hoelzl@29805 ` 319` ```lemma lb_sqrt_upper_bound: assumes "0 \ Ifloat x" ``` hoelzl@29805 ` 320` ``` shows "Ifloat (the (lb_sqrt prec x)) \ sqrt (Ifloat x)" ``` hoelzl@29805 ` 321` ```proof (cases "0 < x") ``` hoelzl@29805 ` 322` ``` case True hence "0 < Ifloat x" and "0 \ Ifloat x" unfolding less_float_def by auto ``` hoelzl@29805 ` 323` ``` hence sqrt_gt0: "0 < sqrt (Ifloat x)" by auto ``` hoelzl@29805 ` 324` ``` hence sqrt_ub: "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto ``` hoelzl@29805 ` 325` ``` ``` hoelzl@29805 ` 326` ``` have "Ifloat (float_divl prec x (sqrt_iteration prec prec x)) \ Ifloat x / Ifloat (sqrt_iteration prec prec x)" by (rule float_divl) ``` hoelzl@29805 ` 327` ``` also have "\ < Ifloat x / sqrt (Ifloat x)" ``` hoelzl@29805 ` 328` ``` by (rule divide_strict_left_mono[OF sqrt_ub `0 < Ifloat x` mult_pos_pos[OF order_less_trans[OF sqrt_gt0 sqrt_ub] sqrt_gt0]]) ``` hoelzl@29805 ` 329` ``` also have "\ = sqrt (Ifloat x)" unfolding inverse_eq_iff_eq[of _ "sqrt (Ifloat x)", symmetric] sqrt_divide_self_eq[OF `0 \ Ifloat x`, symmetric] by auto ``` hoelzl@29805 ` 330` ``` finally show ?thesis unfolding lb_sqrt_def if_P[OF `0 < x`] by auto ``` hoelzl@29805 ` 331` ```next ``` hoelzl@29805 ` 332` ``` case False with `0 \ Ifloat x` ``` hoelzl@29805 ` 333` ``` have "\ x < 0" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 334` ``` show ?thesis unfolding lb_sqrt_def if_not_P[OF False] if_not_P[OF `\ x < 0`] using assms by auto ``` hoelzl@29805 ` 335` ```qed ``` hoelzl@29805 ` 336` hoelzl@29805 ` 337` ```lemma lb_sqrt: assumes "Some y = lb_sqrt prec x" ``` hoelzl@29805 ` 338` ``` shows "Ifloat y \ sqrt (Ifloat x)" and "0 \ Ifloat x" ``` hoelzl@29805 ` 339` ```proof - ``` hoelzl@29805 ` 340` ``` show "0 \ Ifloat x" ``` hoelzl@29805 ` 341` ``` proof (rule ccontr) ``` hoelzl@29805 ` 342` ``` assume "\ 0 \ Ifloat x" ``` hoelzl@29805 ` 343` ``` hence "lb_sqrt prec x = None" unfolding lb_sqrt_def less_float_def by auto ``` hoelzl@29805 ` 344` ``` thus False using assms by auto ``` hoelzl@29805 ` 345` ``` qed ``` hoelzl@29805 ` 346` ``` from lb_sqrt_upper_bound[OF this, of prec] ``` hoelzl@29805 ` 347` ``` show "Ifloat y \ sqrt (Ifloat x)" unfolding assms[symmetric] by auto ``` hoelzl@29805 ` 348` ```qed ``` hoelzl@29805 ` 349` hoelzl@29805 ` 350` ```lemma ub_sqrt_lower_bound: assumes "0 \ Ifloat x" ``` hoelzl@29805 ` 351` ``` shows "sqrt (Ifloat x) \ Ifloat (the (ub_sqrt prec x))" ``` hoelzl@29805 ` 352` ```proof (cases "0 < x") ``` hoelzl@29805 ` 353` ``` case True hence "0 < Ifloat x" unfolding less_float_def by auto ``` hoelzl@29805 ` 354` ``` hence "0 < sqrt (Ifloat x)" by auto ``` hoelzl@29805 ` 355` ``` hence "sqrt (Ifloat x) < Ifloat (sqrt_iteration prec prec x)" using sqrt_iteration_bound by auto ``` hoelzl@29805 ` 356` ``` thus ?thesis unfolding ub_sqrt_def if_P[OF `0 < x`] by auto ``` hoelzl@29805 ` 357` ```next ``` hoelzl@29805 ` 358` ``` case False with `0 \ Ifloat x` ``` hoelzl@29805 ` 359` ``` have "Ifloat x = 0" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 360` ``` thus ?thesis unfolding ub_sqrt_def less_float_def le_float_def by auto ``` hoelzl@29805 ` 361` ```qed ``` hoelzl@29805 ` 362` hoelzl@29805 ` 363` ```lemma ub_sqrt: assumes "Some y = ub_sqrt prec x" ``` hoelzl@29805 ` 364` ``` shows "sqrt (Ifloat x) \ Ifloat y" and "0 \ Ifloat x" ``` hoelzl@29805 ` 365` ```proof - ``` hoelzl@29805 ` 366` ``` show "0 \ Ifloat x" ``` hoelzl@29805 ` 367` ``` proof (rule ccontr) ``` hoelzl@29805 ` 368` ``` assume "\ 0 \ Ifloat x" ``` hoelzl@29805 ` 369` ``` hence "ub_sqrt prec x = None" unfolding ub_sqrt_def less_float_def by auto ``` hoelzl@29805 ` 370` ``` thus False using assms by auto ``` hoelzl@29805 ` 371` ``` qed ``` hoelzl@29805 ` 372` ``` from ub_sqrt_lower_bound[OF this, of prec] ``` hoelzl@29805 ` 373` ``` show "sqrt (Ifloat x) \ Ifloat y" unfolding assms[symmetric] by auto ``` hoelzl@29805 ` 374` ```qed ``` hoelzl@29805 ` 375` hoelzl@29805 ` 376` ```lemma bnds_sqrt: "\ x lx ux. (Some l, Some u) = (lb_sqrt prec lx, ub_sqrt prec ux) \ x \ {Ifloat lx .. Ifloat ux} \ Ifloat l \ sqrt x \ sqrt x \ Ifloat u" ``` hoelzl@29805 ` 377` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 378` ``` fix x lx ux ``` hoelzl@29805 ` 379` ``` assume "(Some l, Some u) = (lb_sqrt prec lx, ub_sqrt prec ux) \ x \ {Ifloat lx .. Ifloat ux}" ``` hoelzl@29805 ` 380` ``` hence l: "Some l = lb_sqrt prec lx " and u: "Some u = ub_sqrt prec ux" and x: "x \ {Ifloat lx .. Ifloat ux}" by auto ``` hoelzl@29805 ` 381` ``` ``` hoelzl@29805 ` 382` ``` have "Ifloat lx \ x" and "x \ Ifloat ux" using x by auto ``` hoelzl@29805 ` 383` hoelzl@29805 ` 384` ``` from lb_sqrt(1)[OF l] real_sqrt_le_mono[OF `Ifloat lx \ x`] ``` hoelzl@29805 ` 385` ``` have "Ifloat l \ sqrt x" by (rule order_trans) ``` hoelzl@29805 ` 386` ``` moreover ``` hoelzl@29805 ` 387` ``` from real_sqrt_le_mono[OF `x \ Ifloat ux`] ub_sqrt(1)[OF u] ``` hoelzl@29805 ` 388` ``` have "sqrt x \ Ifloat u" by (rule order_trans) ``` hoelzl@29805 ` 389` ``` ultimately show "Ifloat l \ sqrt x \ sqrt x \ Ifloat u" .. ``` hoelzl@29805 ` 390` ```qed ``` hoelzl@29805 ` 391` hoelzl@29805 ` 392` ```section "Arcus tangens and \" ``` hoelzl@29805 ` 393` hoelzl@29805 ` 394` ```subsection "Compute arcus tangens series" ``` hoelzl@29805 ` 395` hoelzl@29805 ` 396` ```text {* ``` hoelzl@29805 ` 397` hoelzl@29805 ` 398` ```As first step we implement the computation of the arcus tangens series. This is only valid in the range ``` hoelzl@29805 ` 399` ```@{term "{-1 :: real .. 1}"}. This is used to compute \ and then the entire arcus tangens. ``` hoelzl@29805 ` 400` hoelzl@29805 ` 401` ```*} ``` hoelzl@29805 ` 402` hoelzl@29805 ` 403` ```fun ub_arctan_horner :: "nat \ nat \ nat \ float \ float" ``` hoelzl@29805 ` 404` ```and lb_arctan_horner :: "nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 405` ``` "ub_arctan_horner prec 0 k x = 0" ``` hoelzl@29805 ` 406` ```| "ub_arctan_horner prec (Suc n) k x = ``` hoelzl@29805 ` 407` ``` (rapprox_rat prec 1 (int k)) - x * (lb_arctan_horner prec n (k + 2) x)" ``` hoelzl@29805 ` 408` ```| "lb_arctan_horner prec 0 k x = 0" ``` hoelzl@29805 ` 409` ```| "lb_arctan_horner prec (Suc n) k x = ``` hoelzl@29805 ` 410` ``` (lapprox_rat prec 1 (int k)) - x * (ub_arctan_horner prec n (k + 2) x)" ``` hoelzl@29805 ` 411` hoelzl@29805 ` 412` ```lemma arctan_0_1_bounds': assumes "0 \ Ifloat x" "Ifloat x \ 1" and "even n" ``` hoelzl@29805 ` 413` ``` shows "arctan (Ifloat x) \ {Ifloat (x * lb_arctan_horner prec n 1 (x * x)) .. Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x * x))}" ``` hoelzl@29805 ` 414` ```proof - ``` hoelzl@29805 ` 415` ``` let "?c i" = "-1^i * (1 / real (i * 2 + 1) * Ifloat x ^ (i * 2 + 1))" ``` hoelzl@29805 ` 416` ``` let "?S n" = "\ i=0.. Ifloat (x * x)" by auto ``` hoelzl@29805 ` 419` ``` from `even n` obtain m where "2 * m = n" unfolding even_mult_two_ex by auto ``` hoelzl@29805 ` 420` ``` ``` hoelzl@29805 ` 421` ``` have "arctan (Ifloat x) \ { ?S n .. ?S (Suc n) }" ``` hoelzl@29805 ` 422` ``` proof (cases "Ifloat x = 0") ``` hoelzl@29805 ` 423` ``` case False ``` hoelzl@29805 ` 424` ``` hence "0 < Ifloat x" using `0 \ Ifloat x` by auto ``` hoelzl@29805 ` 425` ``` hence prem: "0 < 1 / real (0 * 2 + (1::nat)) * Ifloat x ^ (0 * 2 + 1)" by auto ``` hoelzl@29805 ` 426` hoelzl@29805 ` 427` ``` have "\ Ifloat x \ \ 1" using `0 \ Ifloat x` `Ifloat x \ 1` by auto ``` hoelzl@29805 ` 428` ``` 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`] ``` hoelzl@29805 ` 429` ``` show ?thesis unfolding arctan_series[OF `\ Ifloat x \ \ 1`] Suc_plus1 . ``` hoelzl@29805 ` 430` ``` qed auto ``` hoelzl@29805 ` 431` ``` note arctan_bounds = this[unfolded atLeastAtMost_iff] ``` hoelzl@29805 ` 432` hoelzl@29805 ` 433` ``` have F: "\n. 2 * Suc n + 1 = 2 * n + 1 + 2" by auto ``` hoelzl@29805 ` 434` hoelzl@29805 ` 435` ``` note bounds = horner_bounds[where s=1 and f="\i. 2 * i + 1" and j'=0 ``` hoelzl@29805 ` 436` ``` and lb="\n i k x. lb_arctan_horner prec n k x" ``` hoelzl@29805 ` 437` ``` and ub="\n i k x. ub_arctan_horner prec n k x", ``` hoelzl@29805 ` 438` ``` OF `0 \ Ifloat (x*x)` F lb_arctan_horner.simps ub_arctan_horner.simps] ``` hoelzl@29805 ` 439` hoelzl@29805 ` 440` ``` { have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \ ?S n" ``` hoelzl@29805 ` 441` ``` using bounds(1) `0 \ Ifloat x` ``` hoelzl@29805 ` 442` ``` unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric] ``` hoelzl@29805 ` 443` ``` unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"] ``` hoelzl@29805 ` 444` ``` by (auto intro!: mult_left_mono) ``` hoelzl@29805 ` 445` ``` also have "\ \ arctan (Ifloat x)" using arctan_bounds .. ``` hoelzl@29805 ` 446` ``` finally have "Ifloat (x * lb_arctan_horner prec n 1 (x*x)) \ arctan (Ifloat x)" . } ``` hoelzl@29805 ` 447` ``` moreover ``` hoelzl@29805 ` 448` ``` { have "arctan (Ifloat x) \ ?S (Suc n)" using arctan_bounds .. ``` hoelzl@29805 ` 449` ``` also have "\ \ Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))" ``` hoelzl@29805 ` 450` ``` using bounds(2)[of "Suc n"] `0 \ Ifloat x` ``` hoelzl@29805 ` 451` ``` unfolding Ifloat_mult power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric] ``` hoelzl@29805 ` 452` ``` unfolding real_mult_commute mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "Ifloat x"] ``` hoelzl@29805 ` 453` ``` by (auto intro!: mult_left_mono) ``` hoelzl@29805 ` 454` ``` finally have "arctan (Ifloat x) \ Ifloat (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . } ``` hoelzl@29805 ` 455` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 456` ```qed ``` hoelzl@29805 ` 457` hoelzl@29805 ` 458` ```lemma arctan_0_1_bounds: assumes "0 \ Ifloat x" "Ifloat x \ 1" ``` hoelzl@29805 ` 459` ``` shows "arctan (Ifloat x) \ {Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) .. Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))}" ``` hoelzl@29805 ` 460` ```proof (cases "even n") ``` hoelzl@29805 ` 461` ``` case True ``` hoelzl@29805 ` 462` ``` obtain n' where "Suc n' = get_odd n" and "odd (Suc n')" using get_odd_ex by auto ``` hoelzl@29805 ` 463` ``` hence "even n'" unfolding even_nat_Suc by auto ``` hoelzl@29805 ` 464` ``` have "arctan (Ifloat x) \ Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))" ``` hoelzl@29805 ` 465` ``` unfolding `Suc n' = get_odd n`[symmetric] using arctan_0_1_bounds'[OF `0 \ Ifloat x` `Ifloat x \ 1` `even n'`] by auto ``` hoelzl@29805 ` 466` ``` moreover ``` hoelzl@29805 ` 467` ``` have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \ arctan (Ifloat x)" ``` hoelzl@29805 ` 468` ``` unfolding get_even_def if_P[OF True] using arctan_0_1_bounds'[OF `0 \ Ifloat x` `Ifloat x \ 1` `even n`] by auto ``` hoelzl@29805 ` 469` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 470` ```next ``` hoelzl@29805 ` 471` ``` case False hence "0 < n" by (rule odd_pos) ``` hoelzl@29805 ` 472` ``` from gr0_implies_Suc[OF this] obtain n' where "n = Suc n'" .. ``` hoelzl@29805 ` 473` ``` from False[unfolded this even_nat_Suc] ``` hoelzl@29805 ` 474` ``` have "even n'" and "even (Suc (Suc n'))" by auto ``` hoelzl@29805 ` 475` ``` have "get_odd n = Suc n'" unfolding get_odd_def if_P[OF False] using `n = Suc n'` . ``` hoelzl@29805 ` 476` hoelzl@29805 ` 477` ``` have "arctan (Ifloat x) \ Ifloat (x * ub_arctan_horner prec (get_odd n) 1 (x * x))" ``` hoelzl@29805 ` 478` ``` unfolding `get_odd n = Suc n'` using arctan_0_1_bounds'[OF `0 \ Ifloat x` `Ifloat x \ 1` `even n'`] by auto ``` hoelzl@29805 ` 479` ``` moreover ``` hoelzl@29805 ` 480` ``` have "Ifloat (x * lb_arctan_horner prec (get_even n) 1 (x * x)) \ arctan (Ifloat x)" ``` hoelzl@29805 ` 481` ``` unfolding get_even_def if_not_P[OF False] unfolding `n = Suc n'` using arctan_0_1_bounds'[OF `0 \ Ifloat x` `Ifloat x \ 1` `even (Suc (Suc n'))`] by auto ``` hoelzl@29805 ` 482` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 483` ```qed ``` hoelzl@29805 ` 484` hoelzl@29805 ` 485` ```subsection "Compute \" ``` hoelzl@29805 ` 486` hoelzl@29805 ` 487` ```definition ub_pi :: "nat \ float" where ``` hoelzl@29805 ` 488` ``` "ub_pi prec = (let A = rapprox_rat prec 1 5 ; ``` hoelzl@29805 ` 489` ``` B = lapprox_rat prec 1 239 ``` hoelzl@29805 ` 490` ``` in ((Float 1 2) * ((Float 1 2) * A * (ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (A * A)) - ``` hoelzl@29805 ` 491` ``` B * (lb_arctan_horner prec (get_even (prec div 14 + 1)) 1 (B * B)))))" ``` hoelzl@29805 ` 492` hoelzl@29805 ` 493` ```definition lb_pi :: "nat \ float" where ``` hoelzl@29805 ` 494` ``` "lb_pi prec = (let A = lapprox_rat prec 1 5 ; ``` hoelzl@29805 ` 495` ``` B = rapprox_rat prec 1 239 ``` hoelzl@29805 ` 496` ``` in ((Float 1 2) * ((Float 1 2) * A * (lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (A * A)) - ``` hoelzl@29805 ` 497` ``` B * (ub_arctan_horner prec (get_odd (prec div 14 + 1)) 1 (B * B)))))" ``` hoelzl@29805 ` 498` hoelzl@29805 ` 499` ```lemma pi_boundaries: "pi \ {Ifloat (lb_pi n) .. Ifloat (ub_pi n)}" ``` hoelzl@29805 ` 500` ```proof - ``` hoelzl@29805 ` 501` ``` have machin_pi: "pi = 4 * (4 * arctan (1 / 5) - arctan (1 / 239))" unfolding machin[symmetric] by auto ``` hoelzl@29805 ` 502` hoelzl@29805 ` 503` ``` { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \ k" and "0 < k" and "1 \ k" by auto ``` hoelzl@29805 ` 504` ``` let ?k = "rapprox_rat prec 1 k" ``` hoelzl@29805 ` 505` ``` have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto ``` hoelzl@29805 ` 506` ``` ``` hoelzl@29805 ` 507` ``` have "0 \ Ifloat ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \ k`) ``` hoelzl@29805 ` 508` ``` have "Ifloat ?k \ 1" unfolding rapprox_rat.simps(2)[OF zero_le_one `0 < k`] ``` hoelzl@29805 ` 509` ``` by (rule rapprox_posrat_le1, auto simp add: `0 < k` `1 \ k`) ``` hoelzl@29805 ` 510` hoelzl@29805 ` 511` ``` have "1 / real k \ Ifloat ?k" using rapprox_rat[where x=1 and y=k] by auto ``` hoelzl@29805 ` 512` ``` hence "arctan (1 / real k) \ arctan (Ifloat ?k)" by (rule arctan_monotone') ``` hoelzl@29805 ` 513` ``` also have "\ \ Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" ``` hoelzl@29805 ` 514` ``` using arctan_0_1_bounds[OF `0 \ Ifloat ?k` `Ifloat ?k \ 1`] by auto ``` hoelzl@29805 ` 515` ``` finally have "arctan (1 / (real k)) \ Ifloat (?k * ub_arctan_horner prec (get_odd n) 1 (?k * ?k))" . ``` hoelzl@29805 ` 516` ``` } note ub_arctan = this ``` hoelzl@29805 ` 517` hoelzl@29805 ` 518` ``` { fix prec n :: nat fix k :: int assume "1 < k" hence "0 \ k" and "0 < k" by auto ``` hoelzl@29805 ` 519` ``` let ?k = "lapprox_rat prec 1 k" ``` hoelzl@29805 ` 520` ``` have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto ``` hoelzl@29805 ` 521` ``` have "1 / real k \ 1" using `1 < k` by auto ``` hoelzl@29805 ` 522` hoelzl@29805 ` 523` ``` have "\n. 0 \ Ifloat ?k" using lapprox_rat_bottom[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`) ``` hoelzl@29805 ` 524` ``` have "\n. Ifloat ?k \ 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / real k \ 1`) ``` hoelzl@29805 ` 525` hoelzl@29805 ` 526` ``` have "Ifloat ?k \ 1 / real k" using lapprox_rat[where x=1 and y=k] by auto ``` hoelzl@29805 ` 527` hoelzl@29805 ` 528` ``` have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \ arctan (Ifloat ?k)" ``` hoelzl@29805 ` 529` ``` using arctan_0_1_bounds[OF `0 \ Ifloat ?k` `Ifloat ?k \ 1`] by auto ``` hoelzl@29805 ` 530` ``` also have "\ \ arctan (1 / real k)" using `Ifloat ?k \ 1 / real k` by (rule arctan_monotone') ``` hoelzl@29805 ` 531` ``` finally have "Ifloat (?k * lb_arctan_horner prec (get_even n) 1 (?k * ?k)) \ arctan (1 / (real k))" . ``` hoelzl@29805 ` 532` ``` } note lb_arctan = this ``` hoelzl@29805 ` 533` hoelzl@29805 ` 534` ``` have "pi \ Ifloat (ub_pi n)" ``` hoelzl@29805 ` 535` ``` unfolding ub_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub unfolding Float_num ``` hoelzl@29805 ` 536` ``` using lb_arctan[of 239] ub_arctan[of 5] ``` hoelzl@29805 ` 537` ``` by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps) ``` hoelzl@29805 ` 538` ``` moreover ``` hoelzl@29805 ` 539` ``` have "Ifloat (lb_pi n) \ pi" ``` hoelzl@29805 ` 540` ``` unfolding lb_pi_def machin_pi Let_def Ifloat_mult Ifloat_sub Float_num ``` hoelzl@29805 ` 541` ``` using lb_arctan[of 5] ub_arctan[of 239] ``` hoelzl@29805 ` 542` ``` by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps) ``` hoelzl@29805 ` 543` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 544` ```qed ``` hoelzl@29805 ` 545` hoelzl@29805 ` 546` ```subsection "Compute arcus tangens in the entire domain" ``` hoelzl@29805 ` 547` hoelzl@29805 ` 548` ```function lb_arctan :: "nat \ float \ float" and ub_arctan :: "nat \ float \ float" where ``` hoelzl@29805 ` 549` ``` "lb_arctan prec x = (let ub_horner = \ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ; ``` hoelzl@29805 ` 550` ``` lb_horner = \ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ``` hoelzl@29805 ` 551` ``` in (if x < 0 then - ub_arctan prec (-x) else ``` hoelzl@29805 ` 552` ``` if x \ Float 1 -1 then lb_horner x else ``` hoelzl@29805 ` 553` ``` if x \ Float 1 1 then Float 1 1 * lb_horner (float_divl prec x (1 + the (ub_sqrt prec (1 + x * x)))) ``` hoelzl@29805 ` 554` ``` else (let inv = float_divr prec 1 x ``` hoelzl@29805 ` 555` ``` in if inv > 1 then 0 ``` hoelzl@29805 ` 556` ``` else lb_pi prec * Float 1 -1 - ub_horner inv)))" ``` hoelzl@29805 ` 557` hoelzl@29805 ` 558` ```| "ub_arctan prec x = (let lb_horner = \ x. x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x) ; ``` hoelzl@29805 ` 559` ``` ub_horner = \ x. x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x) ``` hoelzl@29805 ` 560` ``` in (if x < 0 then - lb_arctan prec (-x) else ``` hoelzl@29805 ` 561` ``` if x \ Float 1 -1 then ub_horner x else ``` hoelzl@29805 ` 562` ``` if x \ Float 1 1 then let y = float_divr prec x (1 + the (lb_sqrt prec (1 + x * x))) ``` hoelzl@29805 ` 563` ``` in if y > 1 then ub_pi prec * Float 1 -1 ``` hoelzl@29805 ` 564` ``` else Float 1 1 * ub_horner y ``` hoelzl@29805 ` 565` ``` else ub_pi prec * Float 1 -1 - lb_horner (float_divl prec 1 x)))" ``` hoelzl@29805 ` 566` ```by pat_completeness auto ``` hoelzl@29805 ` 567` ```termination by (relation "measure (\ v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def) ``` hoelzl@29805 ` 568` hoelzl@29805 ` 569` ```declare ub_arctan_horner.simps[simp del] ``` hoelzl@29805 ` 570` ```declare lb_arctan_horner.simps[simp del] ``` hoelzl@29805 ` 571` hoelzl@29805 ` 572` ```lemma lb_arctan_bound': assumes "0 \ Ifloat x" ``` hoelzl@29805 ` 573` ``` shows "Ifloat (lb_arctan prec x) \ arctan (Ifloat x)" ``` hoelzl@29805 ` 574` ```proof - ``` hoelzl@29805 ` 575` ``` have "\ x < 0" and "0 \ x" unfolding less_float_def le_float_def using `0 \ Ifloat x` by auto ``` hoelzl@29805 ` 576` ``` let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 577` ``` and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 578` hoelzl@29805 ` 579` ``` show ?thesis ``` hoelzl@29805 ` 580` ``` proof (cases "x \ Float 1 -1") ``` hoelzl@29805 ` 581` ``` case True hence "Ifloat x \ 1" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 582` ``` show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_P[OF True] ``` hoelzl@29805 ` 583` ``` using arctan_0_1_bounds[OF `0 \ Ifloat x` `Ifloat x \ 1`] by auto ``` hoelzl@29805 ` 584` ``` next ``` hoelzl@29805 ` 585` ``` case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 586` ``` let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)" ``` hoelzl@29805 ` 587` ``` let ?fR = "1 + the (ub_sqrt prec (1 + x * x))" ``` hoelzl@29805 ` 588` ``` let ?DIV = "float_divl prec x ?fR" ``` hoelzl@29805 ` 589` ``` ``` hoelzl@29805 ` 590` ``` have sqr_ge0: "0 \ 1 + Ifloat x * Ifloat x" using sum_power2_ge_zero[of 1 "Ifloat x", unfolded numeral_2_eq_2] by auto ``` hoelzl@29805 ` 591` ``` hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) ``` hoelzl@29805 ` 592` hoelzl@29805 ` 593` ``` have "sqrt (Ifloat (1 + x * x)) \ Ifloat (the (ub_sqrt prec (1 + x * x)))" by (rule ub_sqrt_lower_bound, auto simp add: sqr_ge0) ``` hoelzl@29805 ` 594` ``` hence "?R \ Ifloat ?fR" by auto ``` hoelzl@29805 ` 595` ``` hence "0 < ?fR" and "0 < Ifloat ?fR" unfolding less_float_def using `0 < ?R` by auto ``` hoelzl@29805 ` 596` hoelzl@29805 ` 597` ``` have monotone: "Ifloat (float_divl prec x ?fR) \ Ifloat x / ?R" ``` hoelzl@29805 ` 598` ``` proof - ``` hoelzl@29805 ` 599` ``` have "Ifloat ?DIV \ Ifloat x / Ifloat ?fR" by (rule float_divl) ``` hoelzl@29805 ` 600` ``` also have "\ \ Ifloat x / ?R" by (rule divide_left_mono[OF `?R \ Ifloat ?fR` `0 \ Ifloat x` mult_pos_pos[OF order_less_le_trans[OF divisor_gt0 `?R \ Ifloat ?fR`] divisor_gt0]]) ``` hoelzl@29805 ` 601` ``` finally show ?thesis . ``` hoelzl@29805 ` 602` ``` qed ``` hoelzl@29805 ` 603` hoelzl@29805 ` 604` ``` show ?thesis ``` hoelzl@29805 ` 605` ``` proof (cases "x \ Float 1 1") ``` hoelzl@29805 ` 606` ``` case True ``` hoelzl@29805 ` 607` ``` ``` hoelzl@29805 ` 608` ``` have "Ifloat x \ sqrt (Ifloat (1 + x * x))" using real_sqrt_sum_squares_ge2[where x=1, unfolded numeral_2_eq_2] by auto ``` hoelzl@29805 ` 609` ``` also have "\ \ Ifloat (the (ub_sqrt prec (1 + x * x)))" by (rule ub_sqrt_lower_bound, auto simp add: sqr_ge0) ``` hoelzl@29805 ` 610` ``` finally have "Ifloat x \ Ifloat ?fR" by auto ``` hoelzl@29805 ` 611` ``` moreover have "Ifloat ?DIV \ Ifloat x / Ifloat ?fR" by (rule float_divl) ``` hoelzl@29805 ` 612` ``` ultimately have "Ifloat ?DIV \ 1" unfolding divide_le_eq_1_pos[OF `0 < Ifloat ?fR`, symmetric] by auto ``` hoelzl@29805 ` 613` hoelzl@29805 ` 614` ``` have "0 \ Ifloat ?DIV" using float_divl_lower_bound[OF `0 \ x` `0 < ?fR`] unfolding le_float_def by auto ``` hoelzl@29805 ` 615` hoelzl@29805 ` 616` ``` have "Ifloat (Float 1 1 * ?lb_horner ?DIV) \ 2 * arctan (Ifloat (float_divl prec x ?fR))" unfolding Ifloat_mult[of "Float 1 1"] Float_num ``` hoelzl@29805 ` 617` ``` using arctan_0_1_bounds[OF `0 \ Ifloat ?DIV` `Ifloat ?DIV \ 1`] by auto ``` hoelzl@29805 ` 618` ``` also have "\ \ 2 * arctan (Ifloat x / ?R)" ``` hoelzl@29805 ` 619` ``` using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) ``` huffman@30273 ` 620` ``` also have "2 * arctan (Ifloat x / ?R) = arctan (Ifloat x)" using arctan_half[symmetric] unfolding numeral_2_eq_2 power_Suc2 power_0 real_mult_1 . ``` hoelzl@29805 ` 621` ``` 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 ` 622` ``` next ``` hoelzl@29805 ` 623` ``` case False ``` hoelzl@29805 ` 624` ``` hence "2 < Ifloat x" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 625` ``` hence "1 \ Ifloat x" by auto ``` hoelzl@29805 ` 626` hoelzl@29805 ` 627` ``` let "?invx" = "float_divr prec 1 x" ``` hoelzl@29805 ` 628` ``` have "0 \ arctan (Ifloat x)" using arctan_monotone'[OF `0 \ Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto ``` hoelzl@29805 ` 629` hoelzl@29805 ` 630` ``` show ?thesis ``` hoelzl@29805 ` 631` ``` proof (cases "1 < ?invx") ``` hoelzl@29805 ` 632` ``` case True ``` hoelzl@29805 ` 633` ``` 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@29805 ` 634` ``` using `0 \ arctan (Ifloat x)` by auto ``` hoelzl@29805 ` 635` ``` next ``` hoelzl@29805 ` 636` ``` case False ``` hoelzl@29805 ` 637` ``` hence "Ifloat ?invx \ 1" unfolding less_float_def by auto ``` hoelzl@29805 ` 638` ``` have "0 \ Ifloat ?invx" by (rule order_trans[OF _ float_divr], auto simp add: `0 \ Ifloat x`) ``` hoelzl@29805 ` 639` hoelzl@29805 ` 640` ``` have "1 / Ifloat x \ 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto ``` hoelzl@29805 ` 641` ``` ``` hoelzl@29805 ` 642` ``` have "arctan (1 / Ifloat x) \ arctan (Ifloat ?invx)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divr) ``` hoelzl@29805 ` 643` ``` also have "\ \ Ifloat (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \ Ifloat ?invx` `Ifloat ?invx \ 1`] by auto ``` hoelzl@29805 ` 644` ``` finally have "pi / 2 - Ifloat (?ub_horner ?invx) \ arctan (Ifloat x)" ``` hoelzl@29805 ` 645` ``` using `0 \ arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \ 0`] ``` hoelzl@29805 ` 646` ``` unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto ``` hoelzl@29805 ` 647` ``` moreover ``` hoelzl@29805 ` 648` ``` have "Ifloat (lb_pi prec * Float 1 -1) \ pi / 2" unfolding Ifloat_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto ``` hoelzl@29805 ` 649` ``` ultimately ``` hoelzl@29805 ` 650` ``` 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] ``` hoelzl@29805 ` 651` ``` by auto ``` hoelzl@29805 ` 652` ``` qed ``` hoelzl@29805 ` 653` ``` qed ``` hoelzl@29805 ` 654` ``` qed ``` hoelzl@29805 ` 655` ```qed ``` hoelzl@29805 ` 656` hoelzl@29805 ` 657` ```lemma ub_arctan_bound': assumes "0 \ Ifloat x" ``` hoelzl@29805 ` 658` ``` shows "arctan (Ifloat x) \ Ifloat (ub_arctan prec x)" ``` hoelzl@29805 ` 659` ```proof - ``` hoelzl@29805 ` 660` ``` have "\ x < 0" and "0 \ x" unfolding less_float_def le_float_def using `0 \ Ifloat x` by auto ``` hoelzl@29805 ` 661` hoelzl@29805 ` 662` ``` let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 663` ``` and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)" ``` hoelzl@29805 ` 664` hoelzl@29805 ` 665` ``` show ?thesis ``` hoelzl@29805 ` 666` ``` proof (cases "x \ Float 1 -1") ``` hoelzl@29805 ` 667` ``` case True hence "Ifloat x \ 1" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 668` ``` show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\ x < 0`] if_P[OF True] ``` hoelzl@29805 ` 669` ``` using arctan_0_1_bounds[OF `0 \ Ifloat x` `Ifloat x \ 1`] by auto ``` hoelzl@29805 ` 670` ``` next ``` hoelzl@29805 ` 671` ``` case False hence "0 < Ifloat x" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 672` ``` let ?R = "1 + sqrt (1 + Ifloat x * Ifloat x)" ``` hoelzl@29805 ` 673` ``` let ?fR = "1 + the (lb_sqrt prec (1 + x * x))" ``` hoelzl@29805 ` 674` ``` let ?DIV = "float_divr prec x ?fR" ``` hoelzl@29805 ` 675` ``` ``` hoelzl@29805 ` 676` ``` have sqr_ge0: "0 \ 1 + Ifloat x * Ifloat x" using sum_power2_ge_zero[of 1 "Ifloat x", unfolded numeral_2_eq_2] by auto ``` hoelzl@29805 ` 677` ``` hence "0 \ Ifloat (1 + x*x)" by auto ``` hoelzl@29805 ` 678` ``` ``` hoelzl@29805 ` 679` ``` hence divisor_gt0: "0 < ?R" by (auto intro: add_pos_nonneg) ``` hoelzl@29805 ` 680` hoelzl@29805 ` 681` ``` have "Ifloat (the (lb_sqrt prec (1 + x * x))) \ sqrt (Ifloat (1 + x * x))" by (rule lb_sqrt_upper_bound, auto simp add: sqr_ge0) ``` hoelzl@29805 ` 682` ``` hence "Ifloat ?fR \ ?R" by auto ``` hoelzl@29805 ` 683` ``` have "0 < Ifloat ?fR" unfolding Ifloat_add Ifloat_1 by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \ Ifloat (1 + x*x)`]) ``` hoelzl@29805 ` 684` hoelzl@29805 ` 685` ``` have monotone: "Ifloat x / ?R \ Ifloat (float_divr prec x ?fR)" ``` hoelzl@29805 ` 686` ``` proof - ``` hoelzl@29805 ` 687` ``` from divide_left_mono[OF `Ifloat ?fR \ ?R` `0 \ Ifloat x` mult_pos_pos[OF divisor_gt0 `0 < Ifloat ?fR`]] ``` hoelzl@29805 ` 688` ``` have "Ifloat x / ?R \ Ifloat x / Ifloat ?fR" . ``` hoelzl@29805 ` 689` ``` also have "\ \ Ifloat ?DIV" by (rule float_divr) ``` hoelzl@29805 ` 690` ``` finally show ?thesis . ``` hoelzl@29805 ` 691` ``` qed ``` hoelzl@29805 ` 692` hoelzl@29805 ` 693` ``` show ?thesis ``` hoelzl@29805 ` 694` ``` proof (cases "x \ Float 1 1") ``` hoelzl@29805 ` 695` ``` case True ``` hoelzl@29805 ` 696` ``` show ?thesis ``` hoelzl@29805 ` 697` ``` proof (cases "?DIV > 1") ``` hoelzl@29805 ` 698` ``` case True ``` hoelzl@29805 ` 699` ``` have "pi / 2 \ Ifloat (ub_pi prec * Float 1 -1)" unfolding Ifloat_mult Float_num times_divide_eq_right real_mult_1 using pi_boundaries by auto ``` hoelzl@29805 ` 700` ``` from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le] ``` hoelzl@29805 ` 701` ``` 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 ` 702` ``` next ``` hoelzl@29805 ` 703` ``` case False ``` hoelzl@29805 ` 704` ``` hence "Ifloat ?DIV \ 1" unfolding less_float_def by auto ``` hoelzl@29805 ` 705` ``` ``` hoelzl@29805 ` 706` ``` have "0 \ Ifloat x / ?R" using `0 \ Ifloat x` `0 < ?R` unfolding real_0_le_divide_iff by auto ``` hoelzl@29805 ` 707` ``` hence "0 \ Ifloat ?DIV" using monotone by (rule order_trans) ``` hoelzl@29805 ` 708` huffman@30273 ` 709` ``` have "arctan (Ifloat x) = 2 * arctan (Ifloat x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 real_mult_1 . ``` hoelzl@29805 ` 710` ``` also have "\ \ 2 * arctan (Ifloat ?DIV)" ``` hoelzl@29805 ` 711` ``` using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono) ``` hoelzl@29805 ` 712` ``` also have "\ \ Ifloat (Float 1 1 * ?ub_horner ?DIV)" unfolding Ifloat_mult[of "Float 1 1"] Float_num ``` hoelzl@29805 ` 713` ``` using arctan_0_1_bounds[OF `0 \ Ifloat ?DIV` `Ifloat ?DIV \ 1`] by auto ``` hoelzl@29805 ` 714` ``` 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 ` 715` ``` qed ``` hoelzl@29805 ` 716` ``` next ``` hoelzl@29805 ` 717` ``` case False ``` hoelzl@29805 ` 718` ``` hence "2 < Ifloat x" unfolding le_float_def Float_num by auto ``` hoelzl@29805 ` 719` ``` hence "1 \ Ifloat x" by auto ``` hoelzl@29805 ` 720` ``` hence "0 < Ifloat x" by auto ``` hoelzl@29805 ` 721` ``` hence "0 < x" unfolding less_float_def by auto ``` hoelzl@29805 ` 722` hoelzl@29805 ` 723` ``` let "?invx" = "float_divl prec 1 x" ``` hoelzl@29805 ` 724` ``` have "0 \ arctan (Ifloat x)" using arctan_monotone'[OF `0 \ Ifloat x`] using arctan_tan[of 0, unfolded tan_zero] by auto ``` hoelzl@29805 ` 725` hoelzl@29805 ` 726` ``` have "Ifloat ?invx \ 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \ Ifloat x` divide_le_eq_1_pos[OF `0 < Ifloat x`]) ``` hoelzl@29805 ` 727` ``` have "0 \ Ifloat ?invx" unfolding Ifloat_0[symmetric] by (rule float_divl_lower_bound[unfolded le_float_def], auto simp add: `0 < x`) ``` hoelzl@29805 ` 728` ``` ``` hoelzl@29805 ` 729` ``` have "1 / Ifloat x \ 0" and "0 < 1 / Ifloat x" using `0 < Ifloat x` by auto ``` hoelzl@29805 ` 730` ``` ``` hoelzl@29805 ` 731` ``` have "Ifloat (?lb_horner ?invx) \ arctan (Ifloat ?invx)" using arctan_0_1_bounds[OF `0 \ Ifloat ?invx` `Ifloat ?invx \ 1`] by auto ``` hoelzl@29805 ` 732` ``` also have "\ \ arctan (1 / Ifloat x)" unfolding Ifloat_1[symmetric] by (rule arctan_monotone', rule float_divl) ``` hoelzl@29805 ` 733` ``` finally have "arctan (Ifloat x) \ pi / 2 - Ifloat (?lb_horner ?invx)" ``` hoelzl@29805 ` 734` ``` using `0 \ arctan (Ifloat x)` arctan_inverse[OF `1 / Ifloat x \ 0`] ``` hoelzl@29805 ` 735` ``` unfolding real_sgn_pos[OF `0 < 1 / Ifloat x`] le_diff_eq by auto ``` hoelzl@29805 ` 736` ``` moreover ``` hoelzl@29805 ` 737` ``` have "pi / 2 \ Ifloat (ub_pi prec * Float 1 -1)" unfolding Ifloat_mult Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto ``` hoelzl@29805 ` 738` ``` ultimately ``` hoelzl@29805 ` 739` ``` 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 `\ x \ Float 1 1`] if_not_P[OF False] ``` hoelzl@29805 ` 740` ``` by auto ``` hoelzl@29805 ` 741` ``` qed ``` hoelzl@29805 ` 742` ``` qed ``` hoelzl@29805 ` 743` ```qed ``` hoelzl@29805 ` 744` hoelzl@29805 ` 745` ```lemma arctan_boundaries: ``` hoelzl@29805 ` 746` ``` "arctan (Ifloat x) \ {Ifloat (lb_arctan prec x) .. Ifloat (ub_arctan prec x)}" ``` hoelzl@29805 ` 747` ```proof (cases "0 \ x") ``` hoelzl@29805 ` 748` ``` case True hence "0 \ Ifloat x" unfolding le_float_def by auto ``` hoelzl@29805 ` 749` ``` show ?thesis using ub_arctan_bound'[OF `0 \ Ifloat x`] lb_arctan_bound'[OF `0 \ Ifloat x`] unfolding atLeastAtMost_iff by auto ``` hoelzl@29805 ` 750` ```next ``` hoelzl@29805 ` 751` ``` let ?mx = "-x" ``` hoelzl@29805 ` 752` ``` case False hence "x < 0" and "0 \ Ifloat ?mx" unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 753` ``` hence bounds: "Ifloat (lb_arctan prec ?mx) \ arctan (Ifloat ?mx) \ arctan (Ifloat ?mx) \ Ifloat (ub_arctan prec ?mx)" ``` hoelzl@29805 ` 754` ``` using ub_arctan_bound'[OF `0 \ Ifloat ?mx`] lb_arctan_bound'[OF `0 \ Ifloat ?mx`] by auto ``` hoelzl@29805 ` 755` ``` show ?thesis unfolding Ifloat_minus arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`] ``` hoelzl@29805 ` 756` ``` unfolding atLeastAtMost_iff using bounds[unfolded Ifloat_minus arctan_minus] by auto ``` hoelzl@29805 ` 757` ```qed ``` hoelzl@29805 ` 758` hoelzl@29805 ` 759` ```lemma bnds_arctan: "\ x lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \ x \ {Ifloat lx .. Ifloat ux} \ Ifloat l \ arctan x \ arctan x \ Ifloat u" ``` hoelzl@29805 ` 760` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 761` ``` fix x lx ux ``` hoelzl@29805 ` 762` ``` assume "(l, u) = (lb_arctan prec lx, ub_arctan prec ux) \ x \ {Ifloat lx .. Ifloat ux}" ``` hoelzl@29805 ` 763` ``` hence l: "lb_arctan prec lx = l " and u: "ub_arctan prec ux = u" and x: "x \ {Ifloat lx .. Ifloat ux}" by auto ``` hoelzl@29805 ` 764` hoelzl@29805 ` 765` ``` { from arctan_boundaries[of lx prec, unfolded l] ``` hoelzl@29805 ` 766` ``` have "Ifloat l \ arctan (Ifloat lx)" by (auto simp del: lb_arctan.simps) ``` hoelzl@29805 ` 767` ``` also have "\ \ arctan x" using x by (auto intro: arctan_monotone') ``` hoelzl@29805 ` 768` ``` finally have "Ifloat l \ arctan x" . ``` hoelzl@29805 ` 769` ``` } moreover ``` hoelzl@29805 ` 770` ``` { have "arctan x \ arctan (Ifloat ux)" using x by (auto intro: arctan_monotone') ``` hoelzl@29805 ` 771` ``` also have "\ \ Ifloat u" using arctan_boundaries[of ux prec, unfolded u] by (auto simp del: ub_arctan.simps) ``` hoelzl@29805 ` 772` ``` finally have "arctan x \ Ifloat u" . ``` hoelzl@29805 ` 773` ``` } ultimately show "Ifloat l \ arctan x \ arctan x \ Ifloat u" .. ``` hoelzl@29805 ` 774` ```qed ``` hoelzl@29805 ` 775` hoelzl@29805 ` 776` ```section "Sinus and Cosinus" ``` hoelzl@29805 ` 777` hoelzl@29805 ` 778` ```subsection "Compute the cosinus and sinus series" ``` hoelzl@29805 ` 779` hoelzl@29805 ` 780` ```fun ub_sin_cos_aux :: "nat \ nat \ nat \ nat \ float \ float" ``` hoelzl@29805 ` 781` ```and lb_sin_cos_aux :: "nat \ nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 782` ``` "ub_sin_cos_aux prec 0 i k x = 0" ``` hoelzl@29805 ` 783` ```| "ub_sin_cos_aux prec (Suc n) i k x = ``` hoelzl@29805 ` 784` ``` (rapprox_rat prec 1 (int k)) - x * (lb_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" ``` hoelzl@29805 ` 785` ```| "lb_sin_cos_aux prec 0 i k x = 0" ``` hoelzl@29805 ` 786` ```| "lb_sin_cos_aux prec (Suc n) i k x = ``` hoelzl@29805 ` 787` ``` (lapprox_rat prec 1 (int k)) - x * (ub_sin_cos_aux prec n (i + 2) (k * i * (i + 1)) x)" ``` hoelzl@29805 ` 788` hoelzl@29805 ` 789` ```lemma cos_aux: ``` hoelzl@29805 ` 790` ``` shows "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \ (\ i=0.. i=0.. Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub") ``` hoelzl@29805 ` 792` ```proof - ``` hoelzl@29805 ` 793` ``` have "0 \ Ifloat (x * x)" unfolding Ifloat_mult by auto ``` hoelzl@29805 ` 794` ``` let "?f n" = "fact (2 * n)" ``` hoelzl@29805 ` 795` hoelzl@29805 ` 796` ``` { fix n ``` hoelzl@29805 ` 797` ``` have F: "\m. ((\i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto) ``` hoelzl@29805 ` 798` ``` have "?f (Suc n) = ?f n * ((\i. i + 2) ^ n) 1 * (((\i. i + 2) ^ n) 1 + 1)" ``` hoelzl@29805 ` 799` ``` unfolding F by auto } note f_eq = this ``` hoelzl@29805 ` 800` ``` ``` hoelzl@29805 ` 801` ``` from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, ``` hoelzl@29805 ` 802` ``` OF `0 \ Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] ``` hoelzl@29805 ` 803` ``` show "?lb" and "?ub" by (auto simp add: power_mult power2_eq_square[of "Ifloat x"]) ``` hoelzl@29805 ` 804` ```qed ``` hoelzl@29805 ` 805` hoelzl@29805 ` 806` ```lemma cos_boundaries: assumes "0 \ Ifloat x" and "Ifloat x \ pi / 2" ``` hoelzl@29805 ` 807` ``` shows "cos (Ifloat x) \ {Ifloat (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) .. Ifloat (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))}" ``` hoelzl@29805 ` 808` ```proof (cases "Ifloat x = 0") ``` hoelzl@29805 ` 809` ``` case False hence "Ifloat x \ 0" by auto ``` hoelzl@29805 ` 810` ``` hence "0 < x" and "0 < Ifloat x" using `0 \ Ifloat x` unfolding less_float_def by auto ``` hoelzl@29805 ` 811` ``` have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0 ``` hoelzl@29805 ` 812` ``` using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto ``` hoelzl@29805 ` 813` hoelzl@29805 ` 814` ``` { 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 ` 816` ``` proof - ``` hoelzl@29805 ` 817` ``` have "?sum = ?sum + (\ j = 0 ..< n. 0)" by auto ``` hoelzl@29805 ` 818` ``` also have "\ = ``` hoelzl@29805 ` 819` ``` (\ j = 0 ..< n. -1 ^ ((2 * j) div 2) / (real (fact (2 * j))) * x ^(2 * j)) + (\ j = 0 ..< n. 0)" by auto ``` hoelzl@29805 ` 820` ``` also have "\ = (\ i = 0 ..< 2 * n. if even i then -1 ^ (i div 2) / (real (fact i)) * x ^ i else 0)" ``` hoelzl@29805 ` 821` ``` unfolding sum_split_even_odd .. ``` hoelzl@29805 ` 822` ``` also have "\ = (\ i = 0 ..< 2 * n. (if even i then -1 ^ (i div 2) / (real (fact i)) else 0) * x ^ i)" ``` hoelzl@29805 ` 823` ``` by (rule setsum_cong2) auto ``` hoelzl@29805 ` 824` ``` finally show ?thesis by assumption ``` hoelzl@29805 ` 825` ``` qed } note morph_to_if_power = this ``` hoelzl@29805 ` 826` hoelzl@29805 ` 827` hoelzl@29805 ` 828` ``` { fix n :: nat assume "0 < n" ``` hoelzl@29805 ` 829` ``` hence "0 < 2 * n" by auto ``` hoelzl@29805 ` 830` ``` obtain t where "0 < t" and "t < Ifloat x" and ``` hoelzl@29805 ` 831` ``` cos_eq: "cos (Ifloat x) = (\ i = 0 ..< 2 * n. (if even(i) then (-1 ^ (i div 2))/(real (fact i)) else 0) * (Ifloat x) ^ i) ``` hoelzl@29805 ` 832` ``` + (cos (t + 1/2 * real (2 * n) * pi) / real (fact (2*n))) * (Ifloat x)^(2*n)" ``` hoelzl@29805 ` 833` ``` (is "_ = ?SUM + ?rest / ?fact * ?pow") ``` hoelzl@29805 ` 834` ``` using Maclaurin_cos_expansion2[OF `0 < Ifloat x` `0 < 2 * n`] by auto ``` hoelzl@29805 ` 835` hoelzl@29805 ` 836` ``` have "cos t * -1^n = cos t * cos (real n * pi) + sin t * sin (real n * pi)" by auto ``` hoelzl@29805 ` 837` ``` also have "\ = cos (t + real n * pi)" using cos_add by auto ``` hoelzl@29805 ` 838` ``` also have "\ = ?rest" by auto ``` hoelzl@29805 ` 839` ``` finally have "cos t * -1^n = ?rest" . ``` hoelzl@29805 ` 840` ``` moreover ``` hoelzl@29805 ` 841` ``` have "t \ pi / 2" using `t < Ifloat x` and `Ifloat x \ pi / 2` by auto ``` hoelzl@29805 ` 842` ``` hence "0 \ cos t" using `0 < t` and cos_ge_zero by auto ``` hoelzl@29805 ` 843` ``` ultimately have even: "even n \ 0 \ ?rest" and odd: "odd n \ 0 \ - ?rest " by auto ``` hoelzl@29805 ` 844` hoelzl@29805 ` 845` ``` have "0 < ?fact" by auto ``` hoelzl@29805 ` 846` ``` have "0 < ?pow" using `0 < Ifloat x` by auto ``` hoelzl@29805 ` 847` hoelzl@29805 ` 848` ``` { ``` hoelzl@29805 ` 849` ``` assume "even n" ``` hoelzl@29805 ` 850` ``` have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \ ?SUM" ``` hoelzl@29805 ` 851` ``` unfolding morph_to_if_power[symmetric] using cos_aux by auto ``` hoelzl@29805 ` 852` ``` also have "\ \ cos (Ifloat x)" ``` hoelzl@29805 ` 853` ``` proof - ``` hoelzl@29805 ` 854` ``` from even[OF `even n`] `0 < ?fact` `0 < ?pow` ``` hoelzl@29805 ` 855` ``` have "0 \ (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 856` ``` thus ?thesis unfolding cos_eq by auto ``` hoelzl@29805 ` 857` ``` qed ``` hoelzl@29805 ` 858` ``` finally have "Ifloat (lb_sin_cos_aux prec n 1 1 (x * x)) \ cos (Ifloat x)" . ``` hoelzl@29805 ` 859` ``` } note lb = this ``` hoelzl@29805 ` 860` hoelzl@29805 ` 861` ``` { ``` hoelzl@29805 ` 862` ``` assume "odd n" ``` hoelzl@29805 ` 863` ``` have "cos (Ifloat x) \ ?SUM" ``` hoelzl@29805 ` 864` ``` proof - ``` hoelzl@29805 ` 865` ``` from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] ``` hoelzl@29805 ` 866` ``` have "0 \ (- ?rest) / ?fact * ?pow" ``` hoelzl@29805 ` 867` ``` by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 868` ``` thus ?thesis unfolding cos_eq by auto ``` hoelzl@29805 ` 869` ``` qed ``` hoelzl@29805 ` 870` ``` also have "\ \ Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" ``` hoelzl@29805 ` 871` ``` unfolding morph_to_if_power[symmetric] using cos_aux by auto ``` hoelzl@29805 ` 872` ``` finally have "cos (Ifloat x) \ Ifloat (ub_sin_cos_aux prec n 1 1 (x * x))" . ``` hoelzl@29805 ` 873` ``` } note ub = this and lb ``` hoelzl@29805 ` 874` ``` } note ub = this(1) and lb = this(2) ``` hoelzl@29805 ` 875` hoelzl@29805 ` 876` ``` have "cos (Ifloat x) \ Ifloat (ub_sin_cos_aux prec (get_odd n) 1 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . ``` hoelzl@29805 ` 877` ``` moreover have "Ifloat (lb_sin_cos_aux prec (get_even n) 1 1 (x * x)) \ cos (Ifloat x)" ``` hoelzl@29805 ` 878` ``` proof (cases "0 < get_even n") ``` hoelzl@29805 ` 879` ``` case True show ?thesis using lb[OF True get_even] . ``` hoelzl@29805 ` 880` ``` next ``` hoelzl@29805 ` 881` ``` case False ``` hoelzl@29805 ` 882` ``` hence "get_even n = 0" by auto ``` hoelzl@29805 ` 883` ``` have "- (pi / 2) \ Ifloat x" by (rule order_trans[OF _ `0 < Ifloat x`[THEN less_imp_le]], auto) ``` hoelzl@29805 ` 884` ``` with `Ifloat x \ pi / 2` ``` hoelzl@29805 ` 885` ``` show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps Ifloat_minus Ifloat_0 using cos_ge_zero by auto ``` hoelzl@29805 ` 886` ``` qed ``` hoelzl@29805 ` 887` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 888` ```next ``` hoelzl@29805 ` 889` ``` case True ``` hoelzl@29805 ` 890` ``` show ?thesis ``` hoelzl@29805 ` 891` ``` proof (cases "n = 0") ``` hoelzl@29805 ` 892` ``` case True ``` hoelzl@29805 ` 893` ``` thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `Ifloat x = 0` lapprox_rat[where x="-1" and y=1] by auto ``` hoelzl@29805 ` 894` ``` next ``` hoelzl@29805 ` 895` ``` case False with not0_implies_Suc obtain m where "n = Suc m" by blast ``` hoelzl@29805 ` 896` ``` thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `Ifloat 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 ` 897` ``` qed ``` hoelzl@29805 ` 898` ```qed ``` hoelzl@29805 ` 899` hoelzl@29805 ` 900` ```lemma sin_aux: assumes "0 \ Ifloat x" ``` hoelzl@29805 ` 901` ``` shows "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \ (\ i=0.. i=0.. Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub") ``` hoelzl@29805 ` 903` ```proof - ``` hoelzl@29805 ` 904` ``` have "0 \ Ifloat (x * x)" unfolding Ifloat_mult by auto ``` hoelzl@29805 ` 905` ``` let "?f n" = "fact (2 * n + 1)" ``` hoelzl@29805 ` 906` hoelzl@29805 ` 907` ``` { fix n ``` hoelzl@29805 ` 908` ``` have F: "\m. ((\i. i + 2) ^ n) m = m + 2 * n" by (induct n arbitrary: m, auto) ``` hoelzl@29805 ` 909` ``` have "?f (Suc n) = ?f n * ((\i. i + 2) ^ n) 2 * (((\i. i + 2) ^ n) 2 + 1)" ``` hoelzl@29805 ` 910` ``` unfolding F by auto } note f_eq = this ``` hoelzl@29805 ` 911` ``` ``` hoelzl@29805 ` 912` ``` from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0, ``` hoelzl@29805 ` 913` ``` OF `0 \ Ifloat (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps] ``` hoelzl@29805 ` 914` ``` show "?lb" and "?ub" using `0 \ Ifloat x` unfolding Ifloat_mult ``` hoelzl@29805 ` 915` ``` unfolding power_add power_one_right real_mult_assoc[symmetric] setsum_left_distrib[symmetric] ``` hoelzl@29805 ` 916` ``` unfolding real_mult_commute ``` hoelzl@29805 ` 917` ``` by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "Ifloat x"]) ``` hoelzl@29805 ` 918` ```qed ``` hoelzl@29805 ` 919` hoelzl@29805 ` 920` ```lemma sin_boundaries: assumes "0 \ Ifloat x" and "Ifloat x \ pi / 2" ``` hoelzl@29805 ` 921` ``` shows "sin (Ifloat x) \ {Ifloat (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) .. Ifloat (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))}" ``` hoelzl@29805 ` 922` ```proof (cases "Ifloat x = 0") ``` hoelzl@29805 ` 923` ``` case False hence "Ifloat x \ 0" by auto ``` hoelzl@29805 ` 924` ``` hence "0 < x" and "0 < Ifloat x" using `0 \ Ifloat x` unfolding less_float_def by auto ``` hoelzl@29805 ` 925` ``` have "0 < x * x" using `0 < x` unfolding less_float_def Ifloat_mult Ifloat_0 ``` hoelzl@29805 ` 926` ``` using mult_pos_pos[where a="Ifloat x" and b="Ifloat x"] by auto ``` hoelzl@29805 ` 927` hoelzl@29805 ` 928` ``` { 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 ` 929` ``` = (\ 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 ` 930` ``` proof - ``` hoelzl@29805 ` 931` ``` have pow: "!!i. x ^ (2 * i + 1) = x * x ^ (2 * i)" by auto ``` hoelzl@29805 ` 932` ``` have "?SUM = (\ j = 0 ..< n. 0) + ?SUM" by auto ``` hoelzl@29805 ` 933` ``` also have "\ = (\ i = 0 ..< 2 * n. if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i)) * x ^ i)" ``` hoelzl@29805 ` 934` ``` unfolding sum_split_even_odd .. ``` hoelzl@29805 ` 935` ``` also have "\ = (\ i = 0 ..< 2 * n. (if even i then 0 else -1 ^ ((i - Suc 0) div 2) / (real (fact i))) * x ^ i)" ``` hoelzl@29805 ` 936` ``` by (rule setsum_cong2) auto ``` hoelzl@29805 ` 937` ``` finally show ?thesis by assumption ``` hoelzl@29805 ` 938` ``` qed } note setsum_morph = this ``` hoelzl@29805 ` 939` hoelzl@29805 ` 940` ``` { fix n :: nat assume "0 < n" ``` hoelzl@29805 ` 941` ``` hence "0 < 2 * n + 1" by auto ``` hoelzl@29805 ` 942` ``` obtain t where "0 < t" and "t < Ifloat x" and ``` hoelzl@29805 ` 943` ``` sin_eq: "sin (Ifloat x) = (\ i = 0 ..< 2 * n + 1. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i) ``` hoelzl@29805 ` 944` ``` + (sin (t + 1/2 * real (2 * n + 1) * pi) / real (fact (2*n + 1))) * (Ifloat x)^(2*n + 1)" ``` hoelzl@29805 ` 945` ``` (is "_ = ?SUM + ?rest / ?fact * ?pow") ``` hoelzl@29805 ` 946` ``` using Maclaurin_sin_expansion3[OF `0 < 2 * n + 1` `0 < Ifloat x`] by auto ``` hoelzl@29805 ` 947` hoelzl@29805 ` 948` ``` have "?rest = cos t * -1^n" unfolding sin_add cos_add real_of_nat_add left_distrib right_distrib by auto ``` hoelzl@29805 ` 949` ``` moreover ``` hoelzl@29805 ` 950` ``` have "t \ pi / 2" using `t < Ifloat x` and `Ifloat x \ pi / 2` by auto ``` hoelzl@29805 ` 951` ``` hence "0 \ cos t" using `0 < t` and cos_ge_zero by auto ``` hoelzl@29805 ` 952` ``` ultimately have even: "even n \ 0 \ ?rest" and odd: "odd n \ 0 \ - ?rest " by auto ``` hoelzl@29805 ` 953` hoelzl@29805 ` 954` ``` have "0 < ?fact" by (rule real_of_nat_fact_gt_zero) ``` hoelzl@29805 ` 955` ``` have "0 < ?pow" using `0 < Ifloat x` by (rule zero_less_power) ``` hoelzl@29805 ` 956` hoelzl@29805 ` 957` ``` { ``` hoelzl@29805 ` 958` ``` assume "even n" ``` hoelzl@29805 ` 959` ``` have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \ ``` hoelzl@29805 ` 960` ``` (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)" ``` hoelzl@29805 ` 961` ``` using sin_aux[OF `0 \ Ifloat x`] unfolding setsum_morph[symmetric] by auto ``` hoelzl@29805 ` 962` ``` also have "\ \ ?SUM" by auto ``` hoelzl@29805 ` 963` ``` also have "\ \ sin (Ifloat x)" ``` hoelzl@29805 ` 964` ``` proof - ``` hoelzl@29805 ` 965` ``` from even[OF `even n`] `0 < ?fact` `0 < ?pow` ``` hoelzl@29805 ` 966` ``` have "0 \ (?rest / ?fact) * ?pow" by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 967` ``` thus ?thesis unfolding sin_eq by auto ``` hoelzl@29805 ` 968` ``` qed ``` hoelzl@29805 ` 969` ``` finally have "Ifloat (x * lb_sin_cos_aux prec n 2 1 (x * x)) \ sin (Ifloat x)" . ``` hoelzl@29805 ` 970` ``` } note lb = this ``` hoelzl@29805 ` 971` hoelzl@29805 ` 972` ``` { ``` hoelzl@29805 ` 973` ``` assume "odd n" ``` hoelzl@29805 ` 974` ``` have "sin (Ifloat x) \ ?SUM" ``` hoelzl@29805 ` 975` ``` proof - ``` hoelzl@29805 ` 976` ``` from `0 < ?fact` and `0 < ?pow` and odd[OF `odd n`] ``` hoelzl@29805 ` 977` ``` have "0 \ (- ?rest) / ?fact * ?pow" ``` hoelzl@29805 ` 978` ``` by (metis mult_nonneg_nonneg divide_nonneg_pos less_imp_le) ``` hoelzl@29805 ` 979` ``` thus ?thesis unfolding sin_eq by auto ``` hoelzl@29805 ` 980` ``` qed ``` hoelzl@29805 ` 981` ``` also have "\ \ (\ i = 0 ..< 2 * n. (if even(i) then 0 else (-1 ^ ((i - Suc 0) div 2))/(real (fact i))) * (Ifloat x) ^ i)" ``` hoelzl@29805 ` 982` ``` by auto ``` hoelzl@29805 ` 983` ``` also have "\ \ Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" ``` hoelzl@29805 ` 984` ``` using sin_aux[OF `0 \ Ifloat x`] unfolding setsum_morph[symmetric] by auto ``` hoelzl@29805 ` 985` ``` finally have "sin (Ifloat x) \ Ifloat (x * ub_sin_cos_aux prec n 2 1 (x * x))" . ``` hoelzl@29805 ` 986` ``` } note ub = this and lb ``` hoelzl@29805 ` 987` ``` } note ub = this(1) and lb = this(2) ``` hoelzl@29805 ` 988` hoelzl@29805 ` 989` ``` have "sin (Ifloat x) \ Ifloat (x * ub_sin_cos_aux prec (get_odd n) 2 1 (x * x))" using ub[OF odd_pos[OF get_odd] get_odd] . ``` hoelzl@29805 ` 990` ``` moreover have "Ifloat (x * lb_sin_cos_aux prec (get_even n) 2 1 (x * x)) \ sin (Ifloat x)" ``` hoelzl@29805 ` 991` ``` proof (cases "0 < get_even n") ``` hoelzl@29805 ` 992` ``` case True show ?thesis using lb[OF True get_even] . ``` hoelzl@29805 ` 993` ``` next ``` hoelzl@29805 ` 994` ``` case False ``` hoelzl@29805 ` 995` ``` hence "get_even n = 0" by auto ``` hoelzl@29805 ` 996` ``` with `Ifloat x \ pi / 2` `0 \ Ifloat x` ``` hoelzl@29805 ` 997` ``` show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps Ifloat_minus Ifloat_0 using sin_ge_zero by auto ``` hoelzl@29805 ` 998` ``` qed ``` hoelzl@29805 ` 999` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1000` ```next ``` hoelzl@29805 ` 1001` ``` case True ``` hoelzl@29805 ` 1002` ``` show ?thesis ``` hoelzl@29805 ` 1003` ``` proof (cases "n = 0") ``` hoelzl@29805 ` 1004` ``` case True ``` hoelzl@29805 ` 1005` ``` thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `Ifloat x = 0` lapprox_rat[where x="-1" and y=1] by auto ``` hoelzl@29805 ` 1006` ``` next ``` hoelzl@29805 ` 1007` ``` case False with not0_implies_Suc obtain m where "n = Suc m" by blast ``` hoelzl@29805 ` 1008` ``` thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `Ifloat 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 ` 1009` ``` qed ``` hoelzl@29805 ` 1010` ```qed ``` hoelzl@29805 ` 1011` hoelzl@29805 ` 1012` ```subsection "Compute the cosinus in the entire domain" ``` hoelzl@29805 ` 1013` hoelzl@29805 ` 1014` ```definition lb_cos :: "nat \ float \ float" where ``` hoelzl@29805 ` 1015` ```"lb_cos prec x = (let ``` hoelzl@29805 ` 1016` ``` horner = \ x. lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x) ; ``` hoelzl@29805 ` 1017` ``` half = \ x. if x < 0 then - 1 else Float 1 1 * x * x - 1 ``` hoelzl@29805 ` 1018` ``` in if x < Float 1 -1 then horner x ``` hoelzl@29805 ` 1019` ```else if x < 1 then half (horner (x * Float 1 -1)) ``` hoelzl@29805 ` 1020` ``` else half (half (horner (x * Float 1 -2))))" ``` hoelzl@29805 ` 1021` hoelzl@29805 ` 1022` ```definition ub_cos :: "nat \ float \ float" where ``` hoelzl@29805 ` 1023` ```"ub_cos prec x = (let ``` hoelzl@29805 ` 1024` ``` horner = \ x. ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x) ; ``` hoelzl@29805 ` 1025` ``` half = \ x. Float 1 1 * x * x - 1 ``` hoelzl@29805 ` 1026` ``` in if x < Float 1 -1 then horner x ``` hoelzl@29805 ` 1027` ```else if x < 1 then half (horner (x * Float 1 -1)) ``` hoelzl@29805 ` 1028` ``` else half (half (horner (x * Float 1 -2))))" ``` hoelzl@29805 ` 1029` hoelzl@29805 ` 1030` ```definition bnds_cos :: "nat \ float \ float \ float * float" where ``` hoelzl@29805 ` 1031` ```"bnds_cos prec lx ux = (let lpi = lb_pi prec ``` hoelzl@29805 ` 1032` ``` in if lx < -lpi \ ux > lpi then (Float -1 0, Float 1 0) ``` hoelzl@29805 ` 1033` ``` else if ux \ 0 then (lb_cos prec (-lx), ub_cos prec (-ux)) ``` hoelzl@29805 ` 1034` ``` else if 0 \ lx then (lb_cos prec ux, ub_cos prec lx) ``` hoelzl@29805 ` 1035` ``` else (min (lb_cos prec (-lx)) (lb_cos prec ux), Float 1 0))" ``` hoelzl@29805 ` 1036` hoelzl@29805 ` 1037` ```lemma lb_cos: assumes "0 \ Ifloat x" and "Ifloat x \ pi" ``` hoelzl@29805 ` 1038` ``` shows "cos (Ifloat x) \ {Ifloat (lb_cos prec x) .. Ifloat (ub_cos prec x)}" (is "?cos x \ { Ifloat (?lb x) .. Ifloat (?ub x) }") ``` hoelzl@29805 ` 1039` ```proof - ``` hoelzl@29805 ` 1040` ``` { fix x :: real ``` hoelzl@29805 ` 1041` ``` have "cos x = cos (x / 2 + x / 2)" by auto ``` hoelzl@29805 ` 1042` ``` 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 ` 1043` ``` unfolding cos_add by auto ``` hoelzl@29805 ` 1044` ``` also have "\ = 2 * cos (x / 2) * cos (x / 2) - 1" by algebra ``` hoelzl@29805 ` 1045` ``` finally have "cos x = 2 * cos (x / 2) * cos (x / 2) - 1" . ``` hoelzl@29805 ` 1046` ``` } note x_half = this[symmetric] ``` hoelzl@29805 ` 1047` hoelzl@29805 ` 1048` ``` have "\ x < 0" using `0 \ Ifloat x` unfolding less_float_def by auto ``` hoelzl@29805 ` 1049` ``` let "?ub_horner x" = "ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 1 1 (x * x)" ``` hoelzl@29805 ` 1050` ``` let "?lb_horner x" = "lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 1 1 (x * x)" ``` hoelzl@29805 ` 1051` ``` let "?ub_half x" = "Float 1 1 * x * x - 1" ``` hoelzl@29805 ` 1052` ``` let "?lb_half x" = "if x < 0 then - 1 else Float 1 1 * x * x - 1" ``` hoelzl@29805 ` 1053` hoelzl@29805 ` 1054` ``` show ?thesis ``` hoelzl@29805 ` 1055` ``` proof (cases "x < Float 1 -1") ``` hoelzl@29805 ` 1056` ``` case True hence "Ifloat x \ pi / 2" unfolding less_float_def using pi_ge_two by auto ``` hoelzl@29805 ` 1057` ``` 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@29805 ` 1058` ``` using cos_boundaries[OF `0 \ Ifloat x` `Ifloat x \ pi / 2`] . ``` hoelzl@29805 ` 1059` ``` next ``` hoelzl@29805 ` 1060` ``` case False ``` hoelzl@29805 ` 1061` ``` ``` hoelzl@29805 ` 1062` ``` { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)" ``` hoelzl@29805 ` 1063` ``` assume "Ifloat y \ cos ?x2" and "-pi \ Ifloat x" and "Ifloat x \ pi" ``` hoelzl@29805 ` 1064` ``` hence "- (pi / 2) \ ?x2" and "?x2 \ pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto ``` hoelzl@29805 ` 1065` ``` hence "0 \ cos ?x2" by (rule cos_ge_zero) ``` hoelzl@29805 ` 1066` ``` ``` hoelzl@29805 ` 1067` ``` have "Ifloat (?lb_half y) \ cos (Ifloat x)" ``` hoelzl@29805 ` 1068` ``` proof (cases "y < 0") ``` hoelzl@29805 ` 1069` ``` case True show ?thesis using cos_ge_minus_one unfolding if_P[OF True] by auto ``` hoelzl@29805 ` 1070` ``` next ``` hoelzl@29805 ` 1071` ``` case False ``` hoelzl@29805 ` 1072` ``` hence "0 \ Ifloat y" unfolding less_float_def by auto ``` hoelzl@29805 ` 1073` ``` from mult_mono[OF `Ifloat y \ cos ?x2` `Ifloat y \ cos ?x2` `0 \ cos ?x2` this] ``` hoelzl@29805 ` 1074` ``` have "Ifloat y * Ifloat y \ cos ?x2 * cos ?x2" . ``` hoelzl@29805 ` 1075` ``` hence "2 * Ifloat y * Ifloat y \ 2 * cos ?x2 * cos ?x2" by auto ``` hoelzl@29805 ` 1076` ``` hence "2 * Ifloat y * Ifloat y - 1 \ 2 * cos (Ifloat x / 2) * cos (Ifloat x / 2) - 1" unfolding Float_num Ifloat_mult by auto ``` hoelzl@29805 ` 1077` ``` thus ?thesis unfolding if_not_P[OF False] x_half Float_num Ifloat_mult Ifloat_sub by auto ``` hoelzl@29805 ` 1078` ``` qed ``` hoelzl@29805 ` 1079` ``` } note lb_half = this ``` hoelzl@29805 ` 1080` ``` ``` hoelzl@29805 ` 1081` ``` { fix y x :: float let ?x2 = "Ifloat (x * Float 1 -1)" ``` hoelzl@29805 ` 1082` ``` assume ub: "cos ?x2 \ Ifloat y" and "- pi \ Ifloat x" and "Ifloat x \ pi" ``` hoelzl@29805 ` 1083` ``` hence "- (pi / 2) \ ?x2" and "?x2 \ pi / 2" using pi_ge_two unfolding Ifloat_mult Float_num by auto ``` hoelzl@29805 ` 1084` ``` hence "0 \ cos ?x2" by (rule cos_ge_zero) ``` hoelzl@29805 ` 1085` ``` ``` hoelzl@29805 ` 1086` ``` have "cos (Ifloat x) \ Ifloat (?ub_half y)" ``` hoelzl@29805 ` 1087` ``` proof - ``` hoelzl@29805 ` 1088` ``` have "0 \ Ifloat y" using `0 \ cos ?x2` ub by (rule order_trans) ``` hoelzl@29805 ` 1089` ``` from mult_mono[OF ub ub this `0 \ cos ?x2`] ``` hoelzl@29805 ` 1090` ``` have "cos ?x2 * cos ?x2 \ Ifloat y * Ifloat y" . ``` hoelzl@29805 ` 1091` ``` hence "2 * cos ?x2 * cos ?x2 \ 2 * Ifloat y * Ifloat y" by auto ``` hoelzl@29805 ` 1092` ``` hence "2 * cos (Ifloat x / 2) * cos (Ifloat x / 2) - 1 \ 2 * Ifloat y * Ifloat y - 1" unfolding Float_num Ifloat_mult by auto ``` hoelzl@29805 ` 1093` ``` thus ?thesis unfolding x_half Ifloat_mult Float_num Ifloat_sub by auto ``` hoelzl@29805 ` 1094` ``` qed ``` hoelzl@29805 ` 1095` ``` } note ub_half = this ``` hoelzl@29805 ` 1096` ``` ``` hoelzl@29805 ` 1097` ``` let ?x2 = "x * Float 1 -1" ``` hoelzl@29805 ` 1098` ``` let ?x4 = "x * Float 1 -1 * Float 1 -1" ``` hoelzl@29805 ` 1099` ``` ``` hoelzl@29805 ` 1100` ``` have "-pi \ Ifloat x" using pi_ge_zero[THEN le_imp_neg_le, unfolded minus_zero] `0 \ Ifloat x` by (rule order_trans) ``` hoelzl@29805 ` 1101` ``` ``` hoelzl@29805 ` 1102` ``` show ?thesis ``` hoelzl@29805 ` 1103` ``` proof (cases "x < 1") ``` hoelzl@29805 ` 1104` ``` case True hence "Ifloat x \ 1" unfolding less_float_def by auto ``` hoelzl@29805 ` 1105` ``` have "0 \ Ifloat ?x2" and "Ifloat ?x2 \ pi / 2" using pi_ge_two `0 \ Ifloat x` unfolding Ifloat_mult Float_num using assms by auto ``` hoelzl@29805 ` 1106` ``` from cos_boundaries[OF this] ``` hoelzl@29805 ` 1107` ``` have lb: "Ifloat (?lb_horner ?x2) \ ?cos ?x2" and ub: "?cos ?x2 \ Ifloat (?ub_horner ?x2)" by auto ``` hoelzl@29805 ` 1108` ``` ``` hoelzl@29805 ` 1109` ``` have "Ifloat (?lb x) \ ?cos x" ``` hoelzl@29805 ` 1110` ``` proof - ``` hoelzl@29805 ` 1111` ``` from lb_half[OF lb `-pi \ Ifloat x` `Ifloat x \ pi`] ``` hoelzl@29805 ` 1112` ``` show ?thesis unfolding lb_cos_def[where x=x] Let_def using `\ x < 0` `\ x < Float 1 -1` `x < 1` by auto ``` hoelzl@29805 ` 1113` ``` qed ``` hoelzl@29805 ` 1114` ``` moreover have "?cos x \ Ifloat (?ub x)" ``` hoelzl@29805 ` 1115` ``` proof - ``` hoelzl@29805 ` 1116` ``` from ub_half[OF ub `-pi \ Ifloat x` `Ifloat x \ pi`] ``` hoelzl@29805 ` 1117` ``` show ?thesis unfolding ub_cos_def[where x=x] Let_def using `\ x < 0` `\ x < Float 1 -1` `x < 1` by auto ``` hoelzl@29805 ` 1118` ``` qed ``` hoelzl@29805 ` 1119` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1120` ``` next ``` hoelzl@29805 ` 1121` ``` case False ``` hoelzl@29805 ` 1122` ``` have "0 \ Ifloat ?x4" and "Ifloat ?x4 \ pi / 2" using pi_ge_two `0 \ Ifloat x` `Ifloat x \ pi` unfolding Ifloat_mult Float_num by auto ``` hoelzl@29805 ` 1123` ``` from cos_boundaries[OF this] ``` hoelzl@29805 ` 1124` ``` have lb: "Ifloat (?lb_horner ?x4) \ ?cos ?x4" and ub: "?cos ?x4 \ Ifloat (?ub_horner ?x4)" by auto ``` hoelzl@29805 ` 1125` ``` ``` hoelzl@29805 ` 1126` ``` have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by (cases x, auto simp add: times_float.simps) ``` hoelzl@29805 ` 1127` ``` ``` hoelzl@29805 ` 1128` ``` have "Ifloat (?lb x) \ ?cos x" ``` hoelzl@29805 ` 1129` ``` proof - ``` hoelzl@29805 ` 1130` ``` have "-pi \ Ifloat ?x2" and "Ifloat ?x2 \ pi" unfolding Ifloat_mult Float_num using pi_ge_two `0 \ Ifloat x` `Ifloat x \ pi` by auto ``` hoelzl@29805 ` 1131` ``` from lb_half[OF lb_half[OF lb this] `-pi \ Ifloat x` `Ifloat x \ pi`, unfolded eq_4] ``` hoelzl@29805 ` 1132` ``` 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 ` 1133` ``` qed ``` hoelzl@29805 ` 1134` ``` moreover have "?cos x \ Ifloat (?ub x)" ``` hoelzl@29805 ` 1135` ``` proof - ``` hoelzl@29805 ` 1136` ``` have "-pi \ Ifloat ?x2" and "Ifloat ?x2 \ pi" unfolding Ifloat_mult Float_num using pi_ge_two `0 \ Ifloat x` `Ifloat x \ pi` by auto ``` hoelzl@29805 ` 1137` ``` from ub_half[OF ub_half[OF ub this] `-pi \ Ifloat x` `Ifloat x \ pi`, unfolded eq_4] ``` hoelzl@29805 ` 1138` ``` 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 ` 1139` ``` qed ``` hoelzl@29805 ` 1140` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1141` ``` qed ``` hoelzl@29805 ` 1142` ``` qed ``` hoelzl@29805 ` 1143` ```qed ``` hoelzl@29805 ` 1144` hoelzl@29805 ` 1145` ```lemma lb_cos_minus: assumes "-pi \ Ifloat x" and "Ifloat x \ 0" ``` hoelzl@29805 ` 1146` ``` shows "cos (Ifloat (-x)) \ {Ifloat (lb_cos prec (-x)) .. Ifloat (ub_cos prec (-x))}" ``` hoelzl@29805 ` 1147` ```proof - ``` hoelzl@29805 ` 1148` ``` have "0 \ Ifloat (-x)" and "Ifloat (-x) \ pi" using `-pi \ Ifloat x` `Ifloat x \ 0` by auto ``` hoelzl@29805 ` 1149` ``` from lb_cos[OF this] show ?thesis . ``` hoelzl@29805 ` 1150` ```qed ``` hoelzl@29805 ` 1151` hoelzl@29805 ` 1152` ```lemma bnds_cos: "\ x lx ux. (l, u) = bnds_cos prec lx ux \ x \ {Ifloat lx .. Ifloat ux} \ Ifloat l \ cos x \ cos x \ Ifloat u" ``` hoelzl@29805 ` 1153` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 1154` ``` fix x lx ux ``` hoelzl@29805 ` 1155` ``` assume "(l, u) = bnds_cos prec lx ux \ x \ {Ifloat lx .. Ifloat ux}" ``` hoelzl@29805 ` 1156` ``` hence bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \ {Ifloat lx .. Ifloat ux}" by auto ``` hoelzl@29805 ` 1157` hoelzl@29805 ` 1158` ``` let ?lpi = "lb_pi prec" ``` hoelzl@29805 ` 1159` ``` have [intro!]: "Ifloat lx \ Ifloat ux" using x by auto ``` hoelzl@29805 ` 1160` ``` hence "lx \ ux" unfolding le_float_def . ``` hoelzl@29805 ` 1161` hoelzl@29805 ` 1162` ``` show "Ifloat l \ cos x \ cos x \ Ifloat u" ``` hoelzl@29805 ` 1163` ``` proof (cases "lx < -?lpi \ ux > ?lpi") ``` hoelzl@29805 ` 1164` ``` case True ``` hoelzl@29805 ` 1165` ``` show ?thesis using bnds unfolding bnds_cos_def if_P[OF True] Let_def using cos_le_one cos_ge_minus_one by auto ``` hoelzl@29805 ` 1166` ``` next ``` hoelzl@29805 ` 1167` ``` case False note not_out = this ``` hoelzl@29805 ` 1168` ``` hence lpi_lx: "- Ifloat ?lpi \ Ifloat lx" and lpi_ux: "Ifloat ux \ Ifloat ?lpi" unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 1169` hoelzl@29805 ` 1170` ``` from pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1, THEN le_imp_neg_le] lpi_lx ``` hoelzl@29805 ` 1171` ``` have "- pi \ Ifloat lx" by (rule order_trans) ``` hoelzl@29805 ` 1172` ``` hence "- pi \ x" and "- pi \ Ifloat ux" and "x \ Ifloat ux" using x by auto ``` hoelzl@29805 ` 1173` ``` ``` hoelzl@29805 ` 1174` ``` from lpi_ux pi_boundaries[unfolded atLeastAtMost_iff, THEN conjunct1] ``` hoelzl@29805 ` 1175` ``` have "Ifloat ux \ pi" by (rule order_trans) ``` hoelzl@29805 ` 1176` ``` hence "x \ pi" and "Ifloat lx \ pi" and "Ifloat lx \ x" using x by auto ``` hoelzl@29805 ` 1177` hoelzl@29805 ` 1178` ``` note lb_cos_minus_bottom = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct1] ``` hoelzl@29805 ` 1179` ``` note lb_cos_minus_top = lb_cos_minus[unfolded atLeastAtMost_iff, THEN conjunct2] ``` hoelzl@29805 ` 1180` ``` note lb_cos_bottom = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct1] ``` hoelzl@29805 ` 1181` ``` note lb_cos_top = lb_cos[unfolded atLeastAtMost_iff, THEN conjunct2] ``` hoelzl@29805 ` 1182` hoelzl@29805 ` 1183` ``` show ?thesis ``` hoelzl@29805 ` 1184` ``` proof (cases "ux \ 0") ``` hoelzl@29805 ` 1185` ``` case True hence "Ifloat ux \ 0" unfolding le_float_def by auto ``` hoelzl@29805 ` 1186` ``` hence "x \ 0" and "Ifloat lx \ 0" using x by auto ``` hoelzl@29805 ` 1187` ``` ``` hoelzl@29805 ` 1188` ``` { have "Ifloat (lb_cos prec (-lx)) \ cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \ Ifloat lx` `Ifloat lx \ 0`] . ``` hoelzl@29805 ` 1189` ``` also have "\ \ cos x" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \ Ifloat lx` `Ifloat lx \ x` `x \ 0`] . ``` hoelzl@29805 ` 1190` ``` finally have "Ifloat (lb_cos prec (-lx)) \ cos x" . } ``` hoelzl@29805 ` 1191` ``` moreover ``` hoelzl@29805 ` 1192` ``` { have "cos x \ cos (Ifloat (-ux))" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \ x` `x \ Ifloat ux` `Ifloat ux \ 0`] . ``` hoelzl@29805 ` 1193` ``` also have "\ \ Ifloat (ub_cos prec (-ux))" using lb_cos_minus_top[OF `-pi \ Ifloat ux` `Ifloat ux \ 0`] . ``` hoelzl@29805 ` 1194` ``` finally have "cos x \ Ifloat (ub_cos prec (-ux))" . } ``` hoelzl@29805 ` 1195` ``` ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_P[OF True] by auto ``` hoelzl@29805 ` 1196` ``` next ``` hoelzl@29805 ` 1197` ``` case False note not_ux = this ``` hoelzl@29805 ` 1198` ``` ``` hoelzl@29805 ` 1199` ``` show ?thesis ``` hoelzl@29805 ` 1200` ``` proof (cases "0 \ lx") ``` hoelzl@29805 ` 1201` ``` case True hence "0 \ Ifloat lx" unfolding le_float_def by auto ``` hoelzl@29805 ` 1202` ``` hence "0 \ x" and "0 \ Ifloat ux" using x by auto ``` hoelzl@29805 ` 1203` ``` ``` hoelzl@29805 ` 1204` ``` { have "Ifloat (lb_cos prec ux) \ cos (Ifloat ux)" using lb_cos_bottom[OF `0 \ Ifloat ux` `Ifloat ux \ pi`] . ``` hoelzl@29805 ` 1205` ``` also have "\ \ cos x" using cos_monotone_0_pi'[OF `0 \ x` `x \ Ifloat ux` `Ifloat ux \ pi`] . ``` hoelzl@29805 ` 1206` ``` finally have "Ifloat (lb_cos prec ux) \ cos x" . } ``` hoelzl@29805 ` 1207` ``` moreover ``` hoelzl@29805 ` 1208` ``` { have "cos x \ cos (Ifloat lx)" using cos_monotone_0_pi'[OF `0 \ Ifloat lx` `Ifloat lx \ x` `x \ pi`] . ``` hoelzl@29805 ` 1209` ``` also have "\ \ Ifloat (ub_cos prec lx)" using lb_cos_top[OF `0 \ Ifloat lx` `Ifloat lx \ pi`] . ``` hoelzl@29805 ` 1210` ``` finally have "cos x \ Ifloat (ub_cos prec lx)" . } ``` hoelzl@29805 ` 1211` ``` ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_not_P[OF not_ux] if_P[OF True] by auto ``` hoelzl@29805 ` 1212` ``` next ``` hoelzl@29805 ` 1213` ``` case False with not_ux ``` hoelzl@29805 ` 1214` ``` have "Ifloat lx \ 0" and "0 \ Ifloat ux" unfolding le_float_def by auto ``` hoelzl@29805 ` 1215` hoelzl@29805 ` 1216` ``` have "Ifloat (min (lb_cos prec (-lx)) (lb_cos prec ux)) \ cos x" ``` hoelzl@29805 ` 1217` ``` proof (cases "x \ 0") ``` hoelzl@29805 ` 1218` ``` case True ``` hoelzl@29805 ` 1219` ``` have "Ifloat (lb_cos prec (-lx)) \ cos (Ifloat (-lx))" using lb_cos_minus_bottom[OF `-pi \ Ifloat lx` `Ifloat lx \ 0`] . ``` hoelzl@29805 ` 1220` ``` also have "\ \ cos x" unfolding Ifloat_minus cos_minus using cos_monotone_minus_pi_0'[OF `- pi \ Ifloat lx` `Ifloat lx \ x` `x \ 0`] . ``` hoelzl@29805 ` 1221` ``` finally show ?thesis unfolding Ifloat_min by auto ``` hoelzl@29805 ` 1222` ``` next ``` hoelzl@29805 ` 1223` ``` case False hence "0 \ x" by auto ``` hoelzl@29805 ` 1224` ``` have "Ifloat (lb_cos prec ux) \ cos (Ifloat ux)" using lb_cos_bottom[OF `0 \ Ifloat ux` `Ifloat ux \ pi`] . ``` hoelzl@29805 ` 1225` ``` also have "\ \ cos x" using cos_monotone_0_pi'[OF `0 \ x` `x \ Ifloat ux` `Ifloat ux \ pi`] . ``` hoelzl@29805 ` 1226` ``` finally show ?thesis unfolding Ifloat_min by auto ``` hoelzl@29805 ` 1227` ``` qed ``` hoelzl@29805 ` 1228` ``` moreover have "cos x \ Ifloat (Float 1 0)" by auto ``` hoelzl@29805 ` 1229` ``` ultimately show ?thesis using bnds unfolding bnds_cos_def Let_def if_not_P[OF not_out] if_not_P[OF not_ux] if_not_P[OF False] by auto ``` hoelzl@29805 ` 1230` ``` qed ``` hoelzl@29805 ` 1231` ``` qed ``` hoelzl@29805 ` 1232` ``` qed ``` hoelzl@29805 ` 1233` ```qed ``` hoelzl@29805 ` 1234` hoelzl@29805 ` 1235` ```subsection "Compute the sinus in the entire domain" ``` hoelzl@29805 ` 1236` hoelzl@29805 ` 1237` ```function lb_sin :: "nat \ float \ float" and ub_sin :: "nat \ float \ float" where ``` hoelzl@29805 ` 1238` ```"lb_sin prec x = (let sqr_diff = \ x. if x > 1 then 0 else 1 - x * x ``` hoelzl@29805 ` 1239` ``` in if x < 0 then - ub_sin prec (- x) ``` hoelzl@29805 ` 1240` ```else if x \ Float 1 -1 then x * lb_sin_cos_aux prec (get_even (prec div 4 + 1)) 2 1 (x * x) ``` hoelzl@29805 ` 1241` ``` else the (lb_sqrt prec (sqr_diff (ub_cos prec x))))" | ``` hoelzl@29805 ` 1242` hoelzl@29805 ` 1243` ```"ub_sin prec x = (let sqr_diff = \ x. if x < 0 then 1 else 1 - x * x ``` hoelzl@29805 ` 1244` ``` in if x < 0 then - lb_sin prec (- x) ``` hoelzl@29805 ` 1245` ```else if x \ Float 1 -1 then x * ub_sin_cos_aux prec (get_odd (prec div 4 + 1)) 2 1 (x * x) ``` hoelzl@29805 ` 1246` ``` else the (ub_sqrt prec (sqr_diff (lb_cos prec x))))" ``` hoelzl@29805 ` 1247` ```by pat_completeness auto ``` hoelzl@29805 ` 1248` ```termination by (relation "measure (\ v. let (prec, x) = sum_case id id v in (if x < 0 then 1 else 0))", auto simp add: less_float_def) ``` hoelzl@29805 ` 1249` hoelzl@29805 ` 1250` ```definition bnds_sin :: "nat \ float \ float \ float * float" where ``` hoelzl@29805 ` 1251` ```"bnds_sin prec lx ux = (let ``` hoelzl@29805 ` 1252` ``` lpi = lb_pi prec ; ``` hoelzl@29805 ` 1253` ``` half_pi = lpi * Float 1 -1 ``` hoelzl@29805 ` 1254` ``` in if lx \ - half_pi \ half_pi \ ux then (Float -1 0, Float 1 0) ``` hoelzl@29805 ` 1255` ``` else (lb_sin prec lx, ub_sin prec ux))" ``` hoelzl@29805 ` 1256` hoelzl@29805 ` 1257` ```lemma lb_sin: assumes "- (pi / 2) \ Ifloat x" and "Ifloat x \ pi / 2" ``` hoelzl@29805 ` 1258` ``` shows "sin (Ifloat x) \ { Ifloat (lb_sin prec x) .. Ifloat (ub_sin prec x) }" (is "?sin x \ { ?lb x .. ?ub x}") ``` hoelzl@29805 ` 1259` ```proof - ``` hoelzl@29805 ` 1260` ``` { fix x :: float assume "0 \ Ifloat x" and "Ifloat x \ pi / 2" ``` hoelzl@29805 ` 1261` ``` hence "\ (x < 0)" and "- (pi / 2) \ Ifloat x" unfolding less_float_def using pi_ge_two by auto ``` hoelzl@29805 ` 1262` hoelzl@29805 ` 1263` ``` have "Ifloat x \ pi" using `Ifloat x \ pi / 2` using pi_ge_two by auto ``` hoelzl@29805 ` 1264` hoelzl@29805 ` 1265` ``` have "?sin x \ { ?lb x .. ?ub x}" ``` hoelzl@29805 ` 1266` ``` proof (cases "x \ Float 1 -1") ``` hoelzl@29805 ` 1267` ``` case True from sin_boundaries[OF `0 \ Ifloat x` `Ifloat x \ pi / 2`] ``` hoelzl@29805 ` 1268` ``` show ?thesis unfolding lb_sin.simps[of prec x] ub_sin.simps[of prec x] if_not_P[OF `\ (x < 0)`] if_P[OF True] Let_def . ``` hoelzl@29805 ` 1269` ``` next ``` hoelzl@29805 ` 1270` ``` case False ``` hoelzl@29805 ` 1271` ``` have "0 \ cos (Ifloat x)" using cos_ge_zero[OF _ `Ifloat x \ pi /2`] `0 \ Ifloat x` pi_ge_two by auto ``` hoelzl@29805 ` 1272` ``` have "0 \ sin (Ifloat x)" using `0 \ Ifloat x` and `Ifloat x \ pi / 2` using sin_ge_zero by auto ``` hoelzl@29805 ` 1273` ``` ``` hoelzl@29805 ` 1274` ``` have "?sin x \ ?ub x" ``` hoelzl@29805 ` 1275` ``` proof (cases "lb_cos prec x < 0") ``` hoelzl@29805 ` 1276` ``` case True ``` hoelzl@29805 ` 1277` ``` have "?sin x \ 1" using sin_le_one . ``` hoelzl@29805 ` 1278` ``` also have "\ \ Ifloat (the (ub_sqrt prec 1))" using ub_sqrt_lower_bound[where prec=prec and x=1] unfolding Ifloat_1 by auto ``` hoelzl@29805 ` 1279` ``` finally show ?thesis unfolding ub_sin.simps if_not_P[OF `\ (x < 0)`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF True] Let_def . ``` hoelzl@29805 ` 1280` ``` next ``` hoelzl@29805 ` 1281` ``` case False hence "0 \ Ifloat (lb_cos prec x)" unfolding less_float_def by auto ``` hoelzl@29805 ` 1282` ``` ``` hoelzl@29805 ` 1283` ``` have "sin (Ifloat x) = sqrt (1 - cos (Ifloat x) ^ 2)" unfolding sin_squared_eq[symmetric] real_sqrt_abs using `0 \ sin (Ifloat x)` by auto ``` hoelzl@29805 ` 1284` ``` also have "\ \ sqrt (Ifloat (1 - lb_cos prec x * lb_cos prec x))" ``` hoelzl@29805 ` 1285` ``` proof (rule real_sqrt_le_mono) ``` huffman@30273 ` 1286` ``` have "Ifloat (lb_cos prec x * lb_cos prec x) \ cos (Ifloat x) ^ 2" unfolding numeral_2_eq_2 power_Suc2 power_0 Ifloat_mult ``` hoelzl@29805 ` 1287` ``` using `0 \ Ifloat (lb_cos prec x)` lb_cos[OF `0 \ Ifloat x` `Ifloat x \ pi`] `0 \ cos (Ifloat x)` by(auto intro!: mult_mono) ``` hoelzl@29805 ` 1288` ``` thus "1 - cos (Ifloat x) ^ 2 \ Ifloat (1 - lb_cos prec x * lb_cos prec x)" unfolding Ifloat_sub Ifloat_1 by auto ``` hoelzl@29805 ` 1289` ``` qed ``` hoelzl@29805 ` 1290` ``` also have "\ \ Ifloat (the (ub_sqrt prec (1 - lb_cos prec x * lb_cos prec x)))" ``` hoelzl@29805 ` 1291` ``` proof (rule ub_sqrt_lower_bound) ``` hoelzl@29805 ` 1292` ``` have "Ifloat (lb_cos prec x) \ cos (Ifloat x)" using lb_cos[OF `0 \ Ifloat x` `Ifloat x \ pi`] by auto ``` hoelzl@29805 ` 1293` ``` from mult_mono[OF order_trans[OF this cos_le_one] order_trans[OF this cos_le_one]] ``` hoelzl@29805 ` 1294` ``` have "Ifloat (lb_cos prec x) * Ifloat (lb_cos prec x) \ 1" using `0 \ Ifloat (lb_cos prec x)` by auto ``` hoelzl@29805 ` 1295` ``` thus "0 \ Ifloat (1 - lb_cos prec x * lb_cos prec x)" by auto ``` hoelzl@29805 ` 1296` ``` qed ``` hoelzl@29805 ` 1297` ``` finally show ?thesis unfolding ub_sin.simps if_not_P[OF `\ (x < 0)`] if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF False] Let_def . ``` hoelzl@29805 ` 1298` ``` qed ``` hoelzl@29805 ` 1299` ``` moreover ``` hoelzl@29805 ` 1300` ``` have "?lb x \ ?sin x" ``` hoelzl@29805 ` 1301` ``` proof (cases "1 < ub_cos prec x") ``` hoelzl@29805 ` 1302` ``` case True ``` hoelzl@29805 ` 1303` ``` show ?thesis unfolding lb_sin.simps if_not_P[OF `\ (x < 0)`] if_not_P[OF `\ x \ Float 1 -1`] if_P[OF True] Let_def ``` hoelzl@29805 ` 1304` ``` by (rule order_trans[OF _ sin_ge_zero[OF `0 \ Ifloat x` `Ifloat x \ pi`]]) ``` hoelzl@29805 ` 1305` ``` (auto simp add: lb_sqrt_upper_bound[where prec=prec and x=0, unfolded Ifloat_0 real_sqrt_zero]) ``` hoelzl@29805 ` 1306` ``` next ``` hoelzl@29805 ` 1307` ``` case False hence "Ifloat (ub_cos prec x) \ 1" unfolding less_float_def by auto ``` hoelzl@29805 ` 1308` ``` have "0 \ Ifloat (ub_cos prec x)" using order_trans[OF `0 \ cos (Ifloat x)`] lb_cos `0 \ Ifloat x` `Ifloat x \ pi` by auto ``` hoelzl@29805 ` 1309` ``` ``` hoelzl@29805 ` 1310` ``` have "Ifloat (the (lb_sqrt prec (1 - ub_cos prec x * ub_cos prec x))) \ sqrt (Ifloat (1 - ub_cos prec x * ub_cos prec x))" ``` hoelzl@29805 ` 1311` ``` proof (rule lb_sqrt_upper_bound) ``` hoelzl@29805 ` 1312` ``` from mult_mono[OF `Ifloat (ub_cos prec x) \ 1` `Ifloat (ub_cos prec x) \ 1`] `0 \ Ifloat (ub_cos prec x)` ``` hoelzl@29805 ` 1313` ``` have "Ifloat (ub_cos prec x) * Ifloat (ub_cos prec x) \ 1" by auto ``` hoelzl@29805 ` 1314` ``` thus "0 \ Ifloat (1 - ub_cos prec x * ub_cos prec x)" by auto ``` hoelzl@29805 ` 1315` ``` qed ``` hoelzl@29805 ` 1316` ``` also have "\ \ sqrt (1 - cos (Ifloat x) ^ 2)" ``` hoelzl@29805 ` 1317` ``` proof (rule real_sqrt_le_mono) ``` huffman@30273 ` 1318` ``` have "cos (Ifloat x) ^ 2 \ Ifloat (ub_cos prec x * ub_cos prec x)" unfolding numeral_2_eq_2 power_Suc2 power_0 Ifloat_mult ``` hoelzl@29805 ` 1319` ``` using `0 \ Ifloat (ub_cos prec x)` lb_cos[OF `0 \ Ifloat x` `Ifloat x \ pi`] `0 \ cos (Ifloat x)` by(auto intro!: mult_mono) ``` hoelzl@29805 ` 1320` ``` thus "Ifloat (1 - ub_cos prec x * ub_cos prec x) \ 1 - cos (Ifloat x) ^ 2" unfolding Ifloat_sub Ifloat_1 by auto ``` hoelzl@29805 ` 1321` ``` qed ``` hoelzl@29805 ` 1322` ``` also have "\ = sin (Ifloat x)" unfolding sin_squared_eq[symmetric] real_sqrt_abs using `0 \ sin (Ifloat x)` by auto ``` hoelzl@29805 ` 1323` ``` finally show ?thesis unfolding lb_sin.simps if_not_P[OF `\ (x < 0)`] if_not_P[OF `\ x \ Float 1 -1`] if_not_P[OF False] Let_def . ``` hoelzl@29805 ` 1324` ``` qed ``` hoelzl@29805 ` 1325` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1326` ``` qed ``` hoelzl@29805 ` 1327` ``` } note for_pos = this ``` hoelzl@29805 ` 1328` hoelzl@29805 ` 1329` ``` show ?thesis ``` hoelzl@29805 ` 1330` ``` proof (cases "x < 0") ``` hoelzl@29805 ` 1331` ``` case True ``` hoelzl@29805 ` 1332` ``` hence "0 \ Ifloat (-x)" and "Ifloat (- x) \ pi / 2" using `-(pi/2) \ Ifloat x` unfolding less_float_def by auto ``` hoelzl@29805 ` 1333` ``` from for_pos[OF this] ``` hoelzl@29805 ` 1334` ``` show ?thesis unfolding Ifloat_minus sin_minus lb_sin.simps[of prec x] ub_sin.simps[of prec x] if_P[OF True] Let_def atLeastAtMost_iff by auto ``` hoelzl@29805 ` 1335` ``` next ``` hoelzl@29805 ` 1336` ``` case False hence "0 \ Ifloat x" unfolding less_float_def by auto ``` hoelzl@29805 ` 1337` ``` from for_pos[OF this `Ifloat x \ pi /2`] ``` hoelzl@29805 ` 1338` ``` show ?thesis . ``` hoelzl@29805 ` 1339` ``` qed ``` hoelzl@29805 ` 1340` ```qed ``` hoelzl@29805 ` 1341` hoelzl@29805 ` 1342` ```lemma bnds_sin: "\ x lx ux. (l, u) = bnds_sin prec lx ux \ x \ {Ifloat lx .. Ifloat ux} \ Ifloat l \ sin x \ sin x \ Ifloat u" ``` hoelzl@29805 ` 1343` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 1344` ``` fix x lx ux ``` hoelzl@29805 ` 1345` ``` assume "(l, u) = bnds_sin prec lx ux \ x \ {Ifloat lx .. Ifloat ux}" ``` hoelzl@29805 ` 1346` ``` hence bnds: "(l, u) = bnds_sin prec lx ux" and x: "x \ {Ifloat lx .. Ifloat ux}" by auto ``` hoelzl@29805 ` 1347` ``` show "Ifloat l \ sin x \ sin x \ Ifloat u" ``` hoelzl@29805 ` 1348` ``` proof (cases "lx \ - (lb_pi prec * Float 1 -1) \ lb_pi prec * Float 1 -1 \ ux") ``` hoelzl@29805 ` 1349` ``` case True show ?thesis using bnds unfolding bnds_sin_def if_P[OF True] Let_def by auto ``` hoelzl@29805 ` 1350` ``` next ``` hoelzl@29805 ` 1351` ``` case False ``` hoelzl@29805 ` 1352` ``` hence "- lb_pi prec * Float 1 -1 \ lx" and "ux \ lb_pi prec * Float 1 -1" unfolding le_float_def by auto ``` hoelzl@29805 ` 1353` ``` moreover have "Ifloat (lb_pi prec * Float 1 -1) \ pi / 2" unfolding Ifloat_mult using pi_boundaries by auto ``` hoelzl@29805 ` 1354` ``` ultimately have "- (pi / 2) \ Ifloat lx" and "Ifloat ux \ pi / 2" and "Ifloat lx \ Ifloat ux" unfolding le_float_def using x by auto ``` hoelzl@29805 ` 1355` ``` hence "- (pi / 2) \ Ifloat ux" and "Ifloat lx \ pi / 2" by auto ``` hoelzl@29805 ` 1356` ``` ``` hoelzl@29805 ` 1357` ``` have "- (pi / 2) \ x""x \ pi / 2" using `Ifloat ux \ pi / 2` `- (pi /2) \ Ifloat lx` x by auto ``` hoelzl@29805 ` 1358` ``` ``` hoelzl@29805 ` 1359` ``` { have "Ifloat (lb_sin prec lx) \ sin (Ifloat lx)" using lb_sin[OF `- (pi / 2) \ Ifloat lx` `Ifloat lx \ pi / 2`] unfolding atLeastAtMost_iff by auto ``` hoelzl@29805 ` 1360` ``` also have "\ \ sin x" using sin_monotone_2pi' `- (pi / 2) \ Ifloat lx` x `x \ pi / 2` by auto ``` hoelzl@29805 ` 1361` ``` finally have "Ifloat (lb_sin prec lx) \ sin x" . } ``` hoelzl@29805 ` 1362` ``` moreover ``` hoelzl@29805 ` 1363` ``` { have "sin x \ sin (Ifloat ux)" using sin_monotone_2pi' `- (pi / 2) \ x` x `Ifloat ux \ pi / 2` by auto ``` hoelzl@29805 ` 1364` ``` also have "\ \ Ifloat (ub_sin prec ux)" using lb_sin[OF `- (pi / 2) \ Ifloat ux` `Ifloat ux \ pi / 2`] unfolding atLeastAtMost_iff by auto ``` hoelzl@29805 ` 1365` ``` finally have "sin x \ Ifloat (ub_sin prec ux)" . } ``` hoelzl@29805 ` 1366` ``` ultimately ``` hoelzl@29805 ` 1367` ``` show ?thesis using bnds unfolding bnds_sin_def if_not_P[OF False] Let_def by auto ``` hoelzl@29805 ` 1368` ``` qed ``` hoelzl@29805 ` 1369` ```qed ``` hoelzl@29805 ` 1370` hoelzl@29805 ` 1371` ```section "Exponential function" ``` hoelzl@29805 ` 1372` hoelzl@29805 ` 1373` ```subsection "Compute the series of the exponential function" ``` hoelzl@29805 ` 1374` hoelzl@29805 ` 1375` ```fun ub_exp_horner :: "nat \ nat \ nat \ nat \ float \ float" and lb_exp_horner :: "nat \ nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 1376` ```"ub_exp_horner prec 0 i k x = 0" | ``` hoelzl@29805 ` 1377` ```"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 ` 1378` ```"lb_exp_horner prec 0 i k x = 0" | ``` hoelzl@29805 ` 1379` ```"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 ` 1380` hoelzl@29805 ` 1381` ```lemma bnds_exp_horner: assumes "Ifloat x \ 0" ``` hoelzl@29805 ` 1382` ``` shows "exp (Ifloat x) \ { Ifloat (lb_exp_horner prec (get_even n) 1 1 x) .. Ifloat (ub_exp_horner prec (get_odd n) 1 1 x) }" ``` hoelzl@29805 ` 1383` ```proof - ``` hoelzl@29805 ` 1384` ``` { fix n ``` hoelzl@29805 ` 1385` ``` have F: "\ m. ((\i. i + 1) ^ n) m = n + m" by (induct n, auto) ``` hoelzl@29805 ` 1386` ``` have "fact (Suc n) = fact n * ((\i. i + 1) ^ n) 1" unfolding F by auto } note f_eq = this ``` hoelzl@29805 ` 1387` ``` ``` hoelzl@29805 ` 1388` ``` 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 ` 1389` ``` OF assms f_eq lb_exp_horner.simps ub_exp_horner.simps] ``` hoelzl@29805 ` 1390` hoelzl@29805 ` 1391` ``` { have "Ifloat (lb_exp_horner prec (get_even n) 1 1 x) \ (\j = 0.. \ exp (Ifloat x)" ``` hoelzl@29805 ` 1394` ``` proof - ``` hoelzl@29805 ` 1395` ``` obtain t where "\t\ \ \Ifloat x\" and "exp (Ifloat x) = (\m = 0.. exp t / real (fact (get_even n)) * (Ifloat x) ^ (get_even n)" ``` hoelzl@29805 ` 1398` ``` by (auto intro!: mult_nonneg_nonneg divide_nonneg_pos simp add: get_even zero_le_even_power exp_gt_zero) ``` hoelzl@29805 ` 1399` ``` ultimately show ?thesis ``` hoelzl@29805 ` 1400` ``` using get_odd exp_gt_zero by (auto intro!: pordered_cancel_semiring_class.mult_nonneg_nonneg) ``` hoelzl@29805 ` 1401` ``` qed ``` hoelzl@29805 ` 1402` ``` finally have "Ifloat (lb_exp_horner prec (get_even n) 1 1 x) \ exp (Ifloat x)" . ``` hoelzl@29805 ` 1403` ``` } moreover ``` hoelzl@29805 ` 1404` ``` { ``` hoelzl@29805 ` 1405` ``` have x_less_zero: "Ifloat x ^ get_odd n \ 0" ``` hoelzl@29805 ` 1406` ``` proof (cases "Ifloat x = 0") ``` hoelzl@29805 ` 1407` ``` case True ``` hoelzl@29805 ` 1408` ``` have "(get_odd n) \ 0" using get_odd[THEN odd_pos] by auto ``` hoelzl@29805 ` 1409` ``` thus ?thesis unfolding True power_0_left by auto ``` hoelzl@29805 ` 1410` ``` next ``` hoelzl@29805 ` 1411` ``` case False hence "Ifloat x < 0" using `Ifloat x \ 0` by auto ``` hoelzl@29805 ` 1412` ``` show ?thesis by (rule less_imp_le, auto simp add: power_less_zero_eq get_odd `Ifloat x < 0`) ``` hoelzl@29805 ` 1413` ``` qed ``` hoelzl@29805 ` 1414` hoelzl@29805 ` 1415` ``` obtain t where "\t\ \ \Ifloat x\" and "exp (Ifloat x) = (\m = 0.. 0" ``` hoelzl@29805 ` 1418` ``` by (auto intro!: mult_nonneg_nonpos divide_nonpos_pos simp add: x_less_zero exp_gt_zero) ``` hoelzl@29805 ` 1419` ``` ultimately have "exp (Ifloat x) \ (\j = 0.. \ Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)" ``` hoelzl@29805 ` 1422` ``` using bounds(2) by auto ``` hoelzl@29805 ` 1423` ``` finally have "exp (Ifloat x) \ Ifloat (ub_exp_horner prec (get_odd n) 1 1 x)" . ``` hoelzl@29805 ` 1424` ``` } ultimately show ?thesis by auto ``` hoelzl@29805 ` 1425` ```qed ``` hoelzl@29805 ` 1426` hoelzl@29805 ` 1427` ```subsection "Compute the exponential function on the entire domain" ``` hoelzl@29805 ` 1428` hoelzl@29805 ` 1429` ```function ub_exp :: "nat \ float \ float" and lb_exp :: "nat \ float \ float" where ``` hoelzl@29805 ` 1430` ```"lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x)) ``` hoelzl@29805 ` 1431` ``` else let ``` hoelzl@29805 ` 1432` ``` 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@29805 ` 1433` ``` in if x < - 1 then (case floor_fl x of (Float m e) \ (horner (float_divl prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e)) ``` hoelzl@29805 ` 1434` ``` else horner x)" | ``` hoelzl@29805 ` 1435` ```"ub_exp prec x = (if 0 < x then float_divr prec 1 (lb_exp prec (-x)) ``` hoelzl@29805 ` 1436` ``` else if x < - 1 then (case floor_fl x of (Float m e) \ ``` hoelzl@29805 ` 1437` ``` (ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e)) ``` hoelzl@29805 ` 1438` ``` else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)" ``` hoelzl@29805 ` 1439` ```by pat_completeness auto ``` hoelzl@29805 ` 1440` ```termination by (relation "measure (\ v. let (prec, x) = sum_case id id v in (if 0 < x then 1 else 0))", auto simp add: less_float_def) ``` hoelzl@29805 ` 1441` hoelzl@29805 ` 1442` ```lemma exp_m1_ge_quarter: "(1 / 4 :: real) \ exp (- 1)" ``` hoelzl@29805 ` 1443` ```proof - ``` hoelzl@29805 ` 1444` ``` have eq4: "4 = Suc (Suc (Suc (Suc 0)))" by auto ``` hoelzl@29805 ` 1445` hoelzl@29805 ` 1446` ``` have "1 / 4 = Ifloat (Float 1 -2)" unfolding Float_num by auto ``` hoelzl@29805 ` 1447` ``` also have "\ \ Ifloat (lb_exp_horner 1 (get_even 4) 1 1 (- 1))" ``` hoelzl@29805 ` 1448` ``` unfolding get_even_def eq4 ``` hoelzl@29805 ` 1449` ``` by (auto simp add: lapprox_posrat_def rapprox_posrat_def normfloat.simps) ``` hoelzl@29805 ` 1450` ``` also have "\ \ exp (Ifloat (- 1))" using bnds_exp_horner[where x="- 1"] by auto ``` hoelzl@29805 ` 1451` ``` finally show ?thesis unfolding Ifloat_minus Ifloat_1 . ``` hoelzl@29805 ` 1452` ```qed ``` hoelzl@29805 ` 1453` hoelzl@29805 ` 1454` ```lemma lb_exp_pos: assumes "\ 0 < x" shows "0 < lb_exp prec x" ``` hoelzl@29805 ` 1455` ```proof - ``` hoelzl@29805 ` 1456` ``` let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" ``` hoelzl@29805 ` 1457` ``` let "?horner x" = "let y = ?lb_horner x in if y \ 0 then Float 1 -2 else y" ``` hoelzl@29805 ` 1458` ``` have pos_horner: "\ x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \ 0", auto simp add: le_float_def less_float_def) ``` hoelzl@29805 ` 1459` ``` moreover { fix x :: float fix num :: nat ``` hoelzl@29805 ` 1460` ``` have "0 < Ifloat (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def Ifloat_0] by (rule zero_less_power) ``` hoelzl@29805 ` 1461` ``` also have "\ = Ifloat ((?horner x) ^ num)" using float_power by auto ``` hoelzl@29805 ` 1462` ``` finally have "0 < Ifloat ((?horner x) ^ num)" . ``` hoelzl@29805 ` 1463` ``` } ``` hoelzl@29805 ` 1464` ``` ultimately show ?thesis ``` hoelzl@29805 ` 1465` ``` unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] Let_def by (cases "floor_fl x", cases "x < - 1", auto simp add: le_float_def less_float_def normfloat) ``` hoelzl@29805 ` 1466` ```qed ``` hoelzl@29805 ` 1467` hoelzl@29805 ` 1468` ```lemma exp_boundaries': assumes "x \ 0" ``` hoelzl@29805 ` 1469` ``` shows "exp (Ifloat x) \ { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}" ``` hoelzl@29805 ` 1470` ```proof - ``` hoelzl@29805 ` 1471` ``` let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x" ``` hoelzl@29805 ` 1472` ``` let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x" ``` hoelzl@29805 ` 1473` hoelzl@29805 ` 1474` ``` have "Ifloat x \ 0" and "\ x > 0" using `x \ 0` unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 1475` ``` show ?thesis ``` hoelzl@29805 ` 1476` ``` proof (cases "x < - 1") ``` hoelzl@29805 ` 1477` ``` case False hence "- 1 \ Ifloat x" unfolding less_float_def by auto ``` hoelzl@29805 ` 1478` ``` show ?thesis ``` hoelzl@29805 ` 1479` ``` proof (cases "?lb_exp_horner x \ 0") ``` hoelzl@29805 ` 1480` ``` from `\ x < - 1` have "- 1 \ Ifloat x" unfolding less_float_def by auto ``` hoelzl@29805 ` 1481` ``` hence "exp (- 1) \ exp (Ifloat x)" unfolding exp_le_cancel_iff . ``` hoelzl@29805 ` 1482` ``` from order_trans[OF exp_m1_ge_quarter this] ``` hoelzl@29805 ` 1483` ``` have "Ifloat (Float 1 -2) \ exp (Ifloat x)" unfolding Float_num . ``` hoelzl@29805 ` 1484` ``` moreover case True ``` hoelzl@29805 ` 1485` ``` ultimately show ?thesis using bnds_exp_horner `Ifloat x \ 0` `\ x > 0` `\ x < - 1` by auto ``` hoelzl@29805 ` 1486` ``` next ``` hoelzl@29805 ` 1487` ``` case False thus ?thesis using bnds_exp_horner `Ifloat x \ 0` `\ x > 0` `\ x < - 1` by (auto simp add: Let_def) ``` hoelzl@29805 ` 1488` ``` qed ``` hoelzl@29805 ` 1489` ``` next ``` hoelzl@29805 ` 1490` ``` case True ``` hoelzl@29805 ` 1491` ``` ``` hoelzl@29805 ` 1492` ``` obtain m e where Float_floor: "floor_fl x = Float m e" by (cases "floor_fl x", auto) ``` hoelzl@29805 ` 1493` ``` let ?num = "nat (- m) * 2 ^ nat e" ``` hoelzl@29805 ` 1494` ``` ``` hoelzl@29805 ` 1495` ``` have "Ifloat (floor_fl x) < - 1" using floor_fl `x < - 1` unfolding le_float_def less_float_def Ifloat_minus Ifloat_1 by (rule order_le_less_trans) ``` hoelzl@29805 ` 1496` ``` hence "Ifloat (floor_fl x) < 0" unfolding Float_floor Ifloat.simps using zero_less_pow2[of xe] by auto ``` hoelzl@29805 ` 1497` ``` hence "m < 0" ``` hoelzl@29805 ` 1498` ``` unfolding less_float_def Ifloat_0 Float_floor Ifloat.simps ``` hoelzl@29805 ` 1499` ``` unfolding pos_prod_lt[OF zero_less_pow2[of e], unfolded real_mult_commute] by auto ``` hoelzl@29805 ` 1500` ``` hence "1 \ - m" by auto ``` hoelzl@29805 ` 1501` ``` hence "0 < nat (- m)" by auto ``` hoelzl@29805 ` 1502` ``` moreover ``` hoelzl@29805 ` 1503` ``` have "0 \ e" using floor_pos_exp Float_floor[symmetric] by auto ``` hoelzl@29805 ` 1504` ``` hence "(0::nat) < 2 ^ nat e" by auto ``` hoelzl@29805 ` 1505` ``` ultimately have "0 < ?num" by auto ``` hoelzl@29805 ` 1506` ``` hence "real ?num \ 0" by auto ``` hoelzl@29805 ` 1507` ``` have e_nat: "int (nat e) = e" using `0 \ e` by auto ``` hoelzl@29805 ` 1508` ``` have num_eq: "real ?num = Ifloat (- floor_fl x)" using `0 < nat (- m)` ``` hoelzl@29805 ` 1509` ``` unfolding Float_floor Ifloat_minus Ifloat.simps real_of_nat_mult pow2_int[of "nat e", unfolded e_nat] realpow_real_of_nat[symmetric] by auto ``` hoelzl@29805 ` 1510` ``` have "0 < - floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] unfolding less_float_def num_eq[symmetric] Ifloat_0 real_of_nat_zero . ``` hoelzl@29805 ` 1511` ``` hence "Ifloat (floor_fl x) < 0" unfolding less_float_def by auto ``` hoelzl@29805 ` 1512` ``` ``` hoelzl@29805 ` 1513` ``` have "exp (Ifloat x) \ Ifloat (ub_exp prec x)" ``` hoelzl@29805 ` 1514` ``` proof - ``` hoelzl@29805 ` 1515` ``` have div_less_zero: "Ifloat (float_divr prec x (- floor_fl x)) \ 0" ``` hoelzl@29805 ` 1516` ``` using float_divr_nonpos_pos_upper_bound[OF `x \ 0` `0 < - floor_fl x`] unfolding le_float_def Ifloat_0 . ``` hoelzl@29805 ` 1517` ``` ``` hoelzl@29805 ` 1518` ``` have "exp (Ifloat x) = exp (real ?num * (Ifloat x / real ?num))" using `real ?num \ 0` by auto ``` hoelzl@29805 ` 1519` ``` also have "\ = exp (Ifloat x / real ?num) ^ ?num" unfolding exp_real_of_nat_mult .. ``` hoelzl@29805 ` 1520` ``` also have "\ \ exp (Ifloat (float_divr prec x (- floor_fl x))) ^ ?num" unfolding num_eq ``` hoelzl@29805 ` 1521` ``` by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto ``` hoelzl@29805 ` 1522` ``` also have "\ \ Ifloat ((?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num)" unfolding float_power ``` hoelzl@29805 ` 1523` ``` by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto) ``` hoelzl@29805 ` 1524` ``` finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def . ``` hoelzl@29805 ` 1525` ``` qed ``` hoelzl@29805 ` 1526` ``` moreover ``` hoelzl@29805 ` 1527` ``` have "Ifloat (lb_exp prec x) \ exp (Ifloat x)" ``` hoelzl@29805 ` 1528` ``` proof - ``` hoelzl@29805 ` 1529` ``` let ?divl = "float_divl prec x (- Float m e)" ``` hoelzl@29805 ` 1530` ``` let ?horner = "?lb_exp_horner ?divl" ``` hoelzl@29805 ` 1531` ``` ``` hoelzl@29805 ` 1532` ``` show ?thesis ``` hoelzl@29805 ` 1533` ``` proof (cases "?horner \ 0") ``` hoelzl@29805 ` 1534` ``` case False hence "0 \ Ifloat ?horner" unfolding le_float_def by auto ``` hoelzl@29805 ` 1535` ``` ``` hoelzl@29805 ` 1536` ``` have div_less_zero: "Ifloat (float_divl prec x (- floor_fl x)) \ 0" ``` hoelzl@29805 ` 1537` ``` using `Ifloat (floor_fl x) < 0` `Ifloat x \ 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg) ``` hoelzl@29805 ` 1538` ``` ``` hoelzl@29805 ` 1539` ``` have "Ifloat ((?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num) \ ``` hoelzl@29805 ` 1540` ``` exp (Ifloat (float_divl prec x (- floor_fl x))) ^ ?num" unfolding float_power ``` hoelzl@29805 ` 1541` ``` using `0 \ Ifloat ?horner`[unfolded Float_floor[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono) ``` hoelzl@29805 ` 1542` ``` also have "\ \ exp (Ifloat x / real ?num) ^ ?num" unfolding num_eq ``` hoelzl@29805 ` 1543` ``` using float_divl by (auto intro!: power_mono simp del: Ifloat_minus) ``` hoelzl@29805 ` 1544` ``` also have "\ = exp (real ?num * (Ifloat x / real ?num))" unfolding exp_real_of_nat_mult .. ``` hoelzl@29805 ` 1545` ``` also have "\ = exp (Ifloat x)" using `real ?num \ 0` by auto ``` hoelzl@29805 ` 1546` ``` finally show ?thesis ``` hoelzl@29805 ` 1547` ``` unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_not_P[OF False] by auto ``` hoelzl@29805 ` 1548` ``` next ``` hoelzl@29805 ` 1549` ``` case True ``` hoelzl@29805 ` 1550` ``` have "Ifloat (floor_fl x) \ 0" and "Ifloat (floor_fl x) \ 0" using `Ifloat (floor_fl x) < 0` by auto ``` hoelzl@29805 ` 1551` ``` from divide_right_mono_neg[OF floor_fl[of x] `Ifloat (floor_fl x) \ 0`, unfolded divide_self[OF `Ifloat (floor_fl x) \ 0`]] ``` hoelzl@29805 ` 1552` ``` have "- 1 \ Ifloat x / Ifloat (- floor_fl x)" unfolding Ifloat_minus by auto ``` hoelzl@29805 ` 1553` ``` from order_trans[OF exp_m1_ge_quarter this[unfolded exp_le_cancel_iff[where x="- 1", symmetric]]] ``` hoelzl@29805 ` 1554` ``` have "Ifloat (Float 1 -2) \ exp (Ifloat x / Ifloat (- floor_fl x))" unfolding Float_num . ``` hoelzl@29805 ` 1555` ``` hence "Ifloat (Float 1 -2) ^ ?num \ exp (Ifloat x / Ifloat (- floor_fl x)) ^ ?num" ``` hoelzl@29805 ` 1556` ``` by (auto intro!: power_mono simp add: Float_num) ``` hoelzl@29805 ` 1557` ``` also have "\ = exp (Ifloat x)" unfolding num_eq exp_real_of_nat_mult[symmetric] using `Ifloat (floor_fl x) \ 0` by auto ``` hoelzl@29805 ` 1558` ``` finally show ?thesis ``` hoelzl@29805 ` 1559` ``` unfolding lb_exp.simps if_not_P[OF `\ 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_P[OF True] float_power . ``` hoelzl@29805 ` 1560` ``` qed ``` hoelzl@29805 ` 1561` ``` qed ``` hoelzl@29805 ` 1562` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1563` ``` qed ``` hoelzl@29805 ` 1564` ```qed ``` hoelzl@29805 ` 1565` hoelzl@29805 ` 1566` ```lemma exp_boundaries: "exp (Ifloat x) \ { Ifloat (lb_exp prec x) .. Ifloat (ub_exp prec x)}" ``` hoelzl@29805 ` 1567` ```proof - ``` hoelzl@29805 ` 1568` ``` show ?thesis ``` hoelzl@29805 ` 1569` ``` proof (cases "0 < x") ``` hoelzl@29805 ` 1570` ``` case False hence "x \ 0" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1571` ``` from exp_boundaries'[OF this] show ?thesis . ``` hoelzl@29805 ` 1572` ``` next ``` hoelzl@29805 ` 1573` ``` case True hence "-x \ 0" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1574` ``` ``` hoelzl@29805 ` 1575` ``` have "Ifloat (lb_exp prec x) \ exp (Ifloat x)" ``` hoelzl@29805 ` 1576` ``` proof - ``` hoelzl@29805 ` 1577` ``` from exp_boundaries'[OF `-x \ 0`] ``` hoelzl@29805 ` 1578` ``` have ub_exp: "exp (- Ifloat x) \ Ifloat (ub_exp prec (-x))" unfolding atLeastAtMost_iff Ifloat_minus by auto ``` hoelzl@29805 ` 1579` ``` ``` hoelzl@29805 ` 1580` ``` have "Ifloat (float_divl prec 1 (ub_exp prec (-x))) \ Ifloat 1 / Ifloat (ub_exp prec (-x))" using float_divl . ``` hoelzl@29805 ` 1581` ``` also have "Ifloat 1 / Ifloat (ub_exp prec (-x)) \ exp (Ifloat x)" ``` hoelzl@29805 ` 1582` ``` using ub_exp[unfolded inverse_le_iff_le[OF order_less_le_trans[OF exp_gt_zero ub_exp] exp_gt_zero, symmetric]] ``` hoelzl@29805 ` 1583` ``` unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide by auto ``` hoelzl@29805 ` 1584` ``` finally show ?thesis unfolding lb_exp.simps if_P[OF True] . ``` hoelzl@29805 ` 1585` ``` qed ``` hoelzl@29805 ` 1586` ``` moreover ``` hoelzl@29805 ` 1587` ``` have "exp (Ifloat x) \ Ifloat (ub_exp prec x)" ``` hoelzl@29805 ` 1588` ``` proof - ``` hoelzl@29805 ` 1589` ``` have "\ 0 < -x" using `0 < x` unfolding less_float_def by auto ``` hoelzl@29805 ` 1590` ``` ``` hoelzl@29805 ` 1591` ``` from exp_boundaries'[OF `-x \ 0`] ``` hoelzl@29805 ` 1592` ``` have lb_exp: "Ifloat (lb_exp prec (-x)) \ exp (- Ifloat x)" unfolding atLeastAtMost_iff Ifloat_minus by auto ``` hoelzl@29805 ` 1593` ``` ``` hoelzl@29805 ` 1594` ``` have "exp (Ifloat x) \ Ifloat 1 / Ifloat (lb_exp prec (-x))" ``` hoelzl@29805 ` 1595` ``` using lb_exp[unfolded inverse_le_iff_le[OF exp_gt_zero lb_exp_pos[OF `\ 0 < -x`, unfolded less_float_def Ifloat_0], symmetric]] ``` hoelzl@29805 ` 1596` ``` unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide Ifloat_1 by auto ``` hoelzl@29805 ` 1597` ``` also have "\ \ Ifloat (float_divr prec 1 (lb_exp prec (-x)))" using float_divr . ``` hoelzl@29805 ` 1598` ``` finally show ?thesis unfolding ub_exp.simps if_P[OF True] . ``` hoelzl@29805 ` 1599` ``` qed ``` hoelzl@29805 ` 1600` ``` ultimately show ?thesis by auto ``` hoelzl@29805 ` 1601` ``` qed ``` hoelzl@29805 ` 1602` ```qed ``` hoelzl@29805 ` 1603` hoelzl@29805 ` 1604` ```lemma bnds_exp: "\ x lx ux. (l, u) = (lb_exp prec lx, ub_exp prec ux) \ x \ {Ifloat lx .. Ifloat ux} \ Ifloat l \ exp x \ exp x \ Ifloat u" ``` hoelzl@29805 ` 1605` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 1606` ``` fix x lx ux ``` hoelzl@29805 ` 1607` ``` assume "(l, u) = (lb_exp prec lx, ub_exp prec ux) \ x \ {Ifloat lx .. Ifloat ux}" ``` hoelzl@29805 ` 1608` ``` hence l: "lb_exp prec lx = l " and u: "ub_exp prec ux = u" and x: "x \ {Ifloat lx .. Ifloat ux}" by auto ``` hoelzl@29805 ` 1609` hoelzl@29805 ` 1610` ``` { from exp_boundaries[of lx prec, unfolded l] ``` hoelzl@29805 ` 1611` ``` have "Ifloat l \ exp (Ifloat lx)" by (auto simp del: lb_exp.simps) ``` hoelzl@29805 ` 1612` ``` also have "\ \ exp x" using x by auto ``` hoelzl@29805 ` 1613` ``` finally have "Ifloat l \ exp x" . ``` hoelzl@29805 ` 1614` ``` } moreover ``` hoelzl@29805 ` 1615` ``` { have "exp x \ exp (Ifloat ux)" using x by auto ``` hoelzl@29805 ` 1616` ``` also have "\ \ Ifloat u" using exp_boundaries[of ux prec, unfolded u] by (auto simp del: ub_exp.simps) ``` hoelzl@29805 ` 1617` ``` finally have "exp x \ Ifloat u" . ``` hoelzl@29805 ` 1618` ``` } ultimately show "Ifloat l \ exp x \ exp x \ Ifloat u" .. ``` hoelzl@29805 ` 1619` ```qed ``` hoelzl@29805 ` 1620` hoelzl@29805 ` 1621` ```section "Logarithm" ``` hoelzl@29805 ` 1622` hoelzl@29805 ` 1623` ```subsection "Compute the logarithm series" ``` hoelzl@29805 ` 1624` hoelzl@29805 ` 1625` ```fun ub_ln_horner :: "nat \ nat \ nat \ float \ float" ``` hoelzl@29805 ` 1626` ```and lb_ln_horner :: "nat \ nat \ nat \ float \ float" where ``` hoelzl@29805 ` 1627` ```"ub_ln_horner prec 0 i x = 0" | ``` hoelzl@29805 ` 1628` ```"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 ` 1629` ```"lb_ln_horner prec 0 i x = 0" | ``` hoelzl@29805 ` 1630` ```"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 ` 1631` hoelzl@29805 ` 1632` ```lemma ln_bounds: ``` hoelzl@29805 ` 1633` ``` assumes "0 \ x" and "x < 1" ``` hoelzl@29805 ` 1634` ``` shows "(\i=0..<2*n. -1^i * (1 / real (i + 1)) * x^(Suc i)) \ ln (x + 1)" (is "?lb") ``` hoelzl@29805 ` 1635` ``` and "ln (x + 1) \ (\i=0..<2*n + 1. -1^i * (1 / real (i + 1)) * x^(Suc i))" (is "?ub") ``` hoelzl@29805 ` 1636` ```proof - ``` hoelzl@29805 ` 1637` ``` let "?a n" = "(1/real (n +1)) * x^(Suc n)" ``` hoelzl@29805 ` 1638` hoelzl@29805 ` 1639` ``` have ln_eq: "(\ i. -1^i * ?a i) = ln (x + 1)" ``` hoelzl@29805 ` 1640` ``` using ln_series[of "x + 1"] `0 \ x` `x < 1` by auto ``` hoelzl@29805 ` 1641` hoelzl@29805 ` 1642` ``` have "norm x < 1" using assms by auto ``` hoelzl@29805 ` 1643` ``` have "?a ----> 0" unfolding Suc_plus1[symmetric] inverse_eq_divide[symmetric] ``` hoelzl@29805 ` 1644` ``` using LIMSEQ_mult[OF LIMSEQ_inverse_real_of_nat LIMSEQ_Suc[OF LIMSEQ_power_zero[OF `norm x < 1`]]] by auto ``` hoelzl@29805 ` 1645` ``` { fix n have "0 \ ?a n" by (rule mult_nonneg_nonneg, auto intro!: mult_nonneg_nonneg simp add: `0 \ x`) } ``` hoelzl@29805 ` 1646` ``` { fix n have "?a (Suc n) \ ?a n" unfolding inverse_eq_divide[symmetric] ``` hoelzl@29805 ` 1647` ``` proof (rule mult_mono) ``` hoelzl@29805 ` 1648` ``` show "0 \ x ^ Suc (Suc n)" by (auto intro!: mult_nonneg_nonneg simp add: `0 \ x`) ``` hoelzl@29805 ` 1649` ``` have "x ^ Suc (Suc n) \ x ^ Suc n * 1" unfolding power_Suc2 real_mult_assoc[symmetric] ``` hoelzl@29805 ` 1650` ``` by (rule mult_left_mono, fact less_imp_le[OF `x < 1`], auto intro!: mult_nonneg_nonneg simp add: `0 \ x`) ``` hoelzl@29805 ` 1651` ``` thus "x ^ Suc (Suc n) \ x ^ Suc n" by auto ``` hoelzl@29805 ` 1652` ``` qed auto } ``` hoelzl@29805 ` 1653` ``` from summable_Leibniz'(2,4)[OF `?a ----> 0` `\n. 0 \ ?a n`, OF `\n. ?a (Suc n) \ ?a n`, unfolded ln_eq] ``` hoelzl@29805 ` 1654` ``` show "?lb" and "?ub" by auto ``` hoelzl@29805 ` 1655` ```qed ``` hoelzl@29805 ` 1656` hoelzl@29805 ` 1657` ```lemma ln_float_bounds: ``` hoelzl@29805 ` 1658` ``` assumes "0 \ Ifloat x" and "Ifloat x < 1" ``` hoelzl@29805 ` 1659` ``` shows "Ifloat (x * lb_ln_horner prec (get_even n) 1 x) \ ln (Ifloat x + 1)" (is "?lb \ ?ln") ``` hoelzl@29805 ` 1660` ``` and "ln (Ifloat x + 1) \ Ifloat (x * ub_ln_horner prec (get_odd n) 1 x)" (is "?ln \ ?ub") ``` hoelzl@29805 ` 1661` ```proof - ``` hoelzl@29805 ` 1662` ``` obtain ev where ev: "get_even n = 2 * ev" using get_even_double .. ``` hoelzl@29805 ` 1663` ``` obtain od where od: "get_odd n = 2 * od + 1" using get_odd_double .. ``` hoelzl@29805 ` 1664` hoelzl@29805 ` 1665` ``` let "?s n" = "-1^n * (1 / real (1 + n)) * (Ifloat x)^(Suc n)" ``` hoelzl@29805 ` 1666` hoelzl@29805 ` 1667` ``` have "?lb \ setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 real_mult_assoc[symmetric] Ifloat_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "Ifloat x"] ev ``` hoelzl@29805 ` 1668` ``` 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@29805 ` 1669` ``` OF `0 \ Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \ Ifloat x` ``` hoelzl@29805 ` 1670` ``` by (rule mult_right_mono) ``` hoelzl@29805 ` 1671` ``` also have "\ \ ?ln" using ln_bounds(1)[OF `0 \ Ifloat x` `Ifloat x < 1`] by auto ``` hoelzl@29805 ` 1672` ``` finally show "?lb \ ?ln" . ``` hoelzl@29805 ` 1673` hoelzl@29805 ` 1674` ``` have "?ln \ setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \ Ifloat x` `Ifloat x < 1`] by auto ``` hoelzl@29805 ` 1675` ``` also have "\ \ ?ub" unfolding power_Suc2 real_mult_assoc[symmetric] Ifloat_mult setsum_left_distrib[symmetric] unfolding real_mult_commute[of "Ifloat x"] od ``` hoelzl@29805 ` 1676` ``` 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@29805 ` 1677` ``` OF `0 \ Ifloat x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \ Ifloat x` ``` hoelzl@29805 ` 1678` ``` by (rule mult_right_mono) ``` hoelzl@29805 ` 1679` ``` finally show "?ln \ ?ub" . ``` hoelzl@29805 ` 1680` ```qed ``` hoelzl@29805 ` 1681` hoelzl@29805 ` 1682` ```lemma ln_add: assumes "0 < x" and "0 < y" shows "ln (x + y) = ln x + ln (1 + y / x)" ``` hoelzl@29805 ` 1683` ```proof - ``` hoelzl@29805 ` 1684` ``` have "x \ 0" using assms by auto ``` hoelzl@29805 ` 1685` ``` have "x + y = x * (1 + y / x)" unfolding right_distrib times_divide_eq_right nonzero_mult_divide_cancel_left[OF `x \ 0`] by auto ``` hoelzl@29805 ` 1686` ``` moreover ``` hoelzl@29805 ` 1687` ``` have "0 < y / x" using assms divide_pos_pos by auto ``` hoelzl@29805 ` 1688` ``` hence "0 < 1 + y / x" by auto ``` hoelzl@29805 ` 1689` ``` ultimately show ?thesis using ln_mult assms by auto ``` hoelzl@29805 ` 1690` ```qed ``` hoelzl@29805 ` 1691` hoelzl@29805 ` 1692` ```subsection "Compute the logarithm of 2" ``` hoelzl@29805 ` 1693` hoelzl@29805 ` 1694` ```definition ub_ln2 where "ub_ln2 prec = (let third = rapprox_rat (max prec 1) 1 3 ``` hoelzl@29805 ` 1695` ``` in (Float 1 -1 * ub_ln_horner prec (get_odd prec) 1 (Float 1 -1)) + ``` hoelzl@29805 ` 1696` ``` (third * ub_ln_horner prec (get_odd prec) 1 third))" ``` hoelzl@29805 ` 1697` ```definition lb_ln2 where "lb_ln2 prec = (let third = lapprox_rat prec 1 3 ``` hoelzl@29805 ` 1698` ``` in (Float 1 -1 * lb_ln_horner prec (get_even prec) 1 (Float 1 -1)) + ``` hoelzl@29805 ` 1699` ``` (third * lb_ln_horner prec (get_even prec) 1 third))" ``` hoelzl@29805 ` 1700` hoelzl@29805 ` 1701` ```lemma ub_ln2: "ln 2 \ Ifloat (ub_ln2 prec)" (is "?ub_ln2") ``` hoelzl@29805 ` 1702` ``` and lb_ln2: "Ifloat (lb_ln2 prec) \ ln 2" (is "?lb_ln2") ``` hoelzl@29805 ` 1703` ```proof - ``` hoelzl@29805 ` 1704` ``` let ?uthird = "rapprox_rat (max prec 1) 1 3" ``` hoelzl@29805 ` 1705` ``` let ?lthird = "lapprox_rat prec 1 3" ``` hoelzl@29805 ` 1706` hoelzl@29805 ` 1707` ``` have ln2_sum: "ln 2 = ln (1/2 + 1) + ln (1 / 3 + 1)" ``` hoelzl@29805 ` 1708` ``` using ln_add[of "3 / 2" "1 / 2"] by auto ``` hoelzl@29805 ` 1709` ``` have lb3: "Ifloat ?lthird \ 1 / 3" using lapprox_rat[of prec 1 3] by auto ``` hoelzl@29805 ` 1710` ``` hence lb3_ub: "Ifloat ?lthird < 1" by auto ``` hoelzl@29805 ` 1711` ``` have lb3_lb: "0 \ Ifloat ?lthird" using lapprox_rat_bottom[of 1 3] by auto ``` hoelzl@29805 ` 1712` ``` have ub3: "1 / 3 \ Ifloat ?uthird" using rapprox_rat[of 1 3] by auto ``` hoelzl@29805 ` 1713` ``` hence ub3_lb: "0 \ Ifloat ?uthird" by auto ``` hoelzl@29805 ` 1714` hoelzl@29805 ` 1715` ``` have lb2: "0 \ Ifloat (Float 1 -1)" and ub2: "Ifloat (Float 1 -1) < 1" unfolding Float_num by auto ``` hoelzl@29805 ` 1716` hoelzl@29805 ` 1717` ``` have "0 \ (1::int)" and "0 < (3::int)" by auto ``` hoelzl@29805 ` 1718` ``` have ub3_ub: "Ifloat ?uthird < 1" unfolding rapprox_rat.simps(2)[OF `0 \ 1` `0 < 3`] ``` hoelzl@29805 ` 1719` ``` by (rule rapprox_posrat_less1, auto) ``` hoelzl@29805 ` 1720` hoelzl@29805 ` 1721` ``` have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto ``` hoelzl@29805 ` 1722` ``` have uthird_gt0: "0 < Ifloat ?uthird + 1" using ub3_lb by auto ``` hoelzl@29805 ` 1723` ``` have lthird_gt0: "0 < Ifloat ?lthird + 1" using lb3_lb by auto ``` hoelzl@29805 ` 1724` hoelzl@29805 ` 1725` ``` show ?ub_ln2 unfolding ub_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric] ``` hoelzl@29805 ` 1726` ``` proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2]) ``` hoelzl@29805 ` 1727` ``` have "ln (1 / 3 + 1) \ ln (Ifloat ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto ``` hoelzl@29805 ` 1728` ``` also have "\ \ Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" ``` hoelzl@29805 ` 1729` ``` using ln_float_bounds(2)[OF ub3_lb ub3_ub] . ``` hoelzl@29805 ` 1730` ``` finally show "ln (1 / 3 + 1) \ Ifloat (?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird)" . ``` hoelzl@29805 ` 1731` ``` qed ``` hoelzl@29805 ` 1732` ``` show ?lb_ln2 unfolding lb_ln2_def Let_def Ifloat_add ln2_sum Float_num(4)[symmetric] ``` hoelzl@29805 ` 1733` ``` proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2]) ``` hoelzl@29805 ` 1734` ``` have "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \ ln (Ifloat ?lthird + 1)" ``` hoelzl@29805 ` 1735` ``` using ln_float_bounds(1)[OF lb3_lb lb3_ub] . ``` hoelzl@29805 ` 1736` ``` also have "\ \ ln (1 / 3 + 1)" unfolding ln_le_cancel_iff[OF lthird_gt0 third_gt0] using lb3 by auto ``` hoelzl@29805 ` 1737` ``` finally show "Ifloat (?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird) \ ln (1 / 3 + 1)" . ``` hoelzl@29805 ` 1738` ``` qed ``` hoelzl@29805 ` 1739` ```qed ``` hoelzl@29805 ` 1740` hoelzl@29805 ` 1741` ```subsection "Compute the logarithm in the entire domain" ``` hoelzl@29805 ` 1742` hoelzl@29805 ` 1743` ```function ub_ln :: "nat \ float \ float option" and lb_ln :: "nat \ float \ float option" where ``` hoelzl@29805 ` 1744` ```"ub_ln prec x = (if x \ 0 then None ``` hoelzl@29805 ` 1745` ``` else if x < 1 then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x))) ``` hoelzl@29805 ` 1746` ``` else let horner = \x. (x - 1) * ub_ln_horner prec (get_odd prec) 1 (x - 1) in ``` hoelzl@29805 ` 1747` ``` if x < Float 1 1 then Some (horner x) ``` hoelzl@29805 ` 1748` ``` else let l = bitlen (mantissa x) - 1 in ``` hoelzl@29805 ` 1749` ``` Some (ub_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))" | ``` hoelzl@29805 ` 1750` ```"lb_ln prec x = (if x \ 0 then None ``` hoelzl@29805 ` 1751` ``` else if x < 1 then Some (- the (ub_ln prec (float_divr prec 1 x))) ``` hoelzl@29805 ` 1752` ``` else let horner = \x. (x - 1) * lb_ln_horner prec (get_even prec) 1 (x - 1) in ``` hoelzl@29805 ` 1753` ``` if x < Float 1 1 then Some (horner x) ``` hoelzl@29805 ` 1754` ``` else let l = bitlen (mantissa x) - 1 in ``` hoelzl@29805 ` 1755` ``` Some (lb_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l))))" ``` hoelzl@29805 ` 1756` ```by pat_completeness auto ``` hoelzl@29805 ` 1757` hoelzl@29805 ` 1758` ```termination proof (relation "measure (\ v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto) ``` hoelzl@29805 ` 1759` ``` fix prec x assume "\ x \ 0" and "x < 1" and "float_divl (max prec (Suc 0)) 1 x < 1" ``` hoelzl@29805 ` 1760` ``` hence "0 < x" and "0 < max prec (Suc 0)" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1761` ``` from float_divl_pos_less1_bound[OF `0 < x` `x < 1` `0 < max prec (Suc 0)`] ``` hoelzl@29805 ` 1762` ``` show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1763` ```next ``` hoelzl@29805 ` 1764` ``` fix prec x assume "\ x \ 0" and "x < 1" and "float_divr prec 1 x < 1" ``` hoelzl@29805 ` 1765` ``` hence "0 < x" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1766` ``` from float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`, of prec] ``` hoelzl@29805 ` 1767` ``` show False using `float_divr prec 1 x < 1` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1768` ```qed ``` hoelzl@29805 ` 1769` hoelzl@29805 ` 1770` ```lemma ln_shifted_float: assumes "0 < m" shows "ln (Ifloat (Float m e)) = ln 2 * real (e + (bitlen m - 1)) + ln (Ifloat (Float m (- (bitlen m - 1))))" ``` hoelzl@29805 ` 1771` ```proof - ``` hoelzl@29805 ` 1772` ``` let ?B = "2^nat (bitlen m - 1)" ``` hoelzl@29805 ` 1773` ``` have "0 < real m" and "\X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \ 0" using assms by auto ``` hoelzl@29805 ` 1774` ``` hence "0 \ bitlen m - 1" using bitlen_ge1[OF `m \ 0`] by auto ``` hoelzl@29805 ` 1775` ``` show ?thesis ``` hoelzl@29805 ` 1776` ``` proof (cases "0 \ e") ``` hoelzl@29805 ` 1777` ``` case True ``` hoelzl@29805 ` 1778` ``` show ?thesis unfolding normalized_float[OF `m \ 0`] ``` hoelzl@29805 ` 1779` ``` unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] ``` hoelzl@29805 ` 1780` ``` unfolding Ifloat_ge0_exp[OF True] ln_mult[OF `0 < real m` `0 < 2^nat e`] ``` hoelzl@29805 ` 1781` ``` ln_realpow[OF `0 < 2`] algebra_simps using `0 \ bitlen m - 1` True by auto ``` hoelzl@29805 ` 1782` ``` next ``` hoelzl@29805 ` 1783` ``` case False hence "0 < -e" by auto ``` hoelzl@29805 ` 1784` ``` hence pow_gt0: "(0::real) < 2^nat (-e)" by auto ``` hoelzl@29805 ` 1785` ``` hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto ``` hoelzl@29805 ` 1786` ``` show ?thesis unfolding normalized_float[OF `m \ 0`] ``` hoelzl@29805 ` 1787` ``` unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`] ``` hoelzl@29805 ` 1788` ``` unfolding Ifloat_nge0_exp[OF False] ln_mult[OF `0 < real m` inv_gt0] ln_inverse[OF pow_gt0] ``` hoelzl@29805 ` 1789` ``` ln_realpow[OF `0 < 2`] algebra_simps using `0 \ bitlen m - 1` False by auto ``` hoelzl@29805 ` 1790` ``` qed ``` hoelzl@29805 ` 1791` ```qed ``` hoelzl@29805 ` 1792` hoelzl@29805 ` 1793` ```lemma ub_ln_lb_ln_bounds': assumes "1 \ x" ``` hoelzl@29805 ` 1794` ``` shows "Ifloat (the (lb_ln prec x)) \ ln (Ifloat x) \ ln (Ifloat x) \ Ifloat (the (ub_ln prec x))" ``` hoelzl@29805 ` 1795` ``` (is "?lb \ ?ln \ ?ln \ ?ub") ``` hoelzl@29805 ` 1796` ```proof (cases "x < Float 1 1") ``` hoelzl@29805 ` 1797` ``` case True hence "Ifloat (x - 1) < 1" unfolding less_float_def Float_num by auto ``` hoelzl@29805 ` 1798` ``` have "\ x \ 0" and "\ x < 1" using `1 \ x` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1799` ``` hence "0 \ Ifloat (x - 1)" using `1 \ x` unfolding less_float_def Float_num by auto ``` hoelzl@29805 ` 1800` ``` show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps Let_def ``` hoelzl@29805 ` 1801` ``` using ln_float_bounds[OF `0 \ Ifloat (x - 1)` `Ifloat (x - 1) < 1`] `\ x \ 0` `\ x < 1` True by auto ``` hoelzl@29805 ` 1802` ```next ``` hoelzl@29805 ` 1803` ``` case False ``` hoelzl@29805 ` 1804` ``` have "\ x \ 0" and "\ x < 1" "0 < x" using `1 \ x` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1805` ``` show ?thesis ``` hoelzl@29805 ` 1806` ``` proof (cases x) ``` hoelzl@29805 ` 1807` ``` case (Float m e) ``` hoelzl@29805 ` 1808` ``` let ?s = "Float (e + (bitlen m - 1)) 0" ``` hoelzl@29805 ` 1809` ``` let ?x = "Float m (- (bitlen m - 1))" ``` hoelzl@29805 ` 1810` hoelzl@29805 ` 1811` ``` have "0 < m" and "m \ 0" using float_pos_m_pos `0 < x` Float by auto ``` hoelzl@29805 ` 1812` hoelzl@29805 ` 1813` ``` { ``` hoelzl@29805 ` 1814` ``` have "Ifloat (lb_ln2 prec * ?s) \ ln 2 * real (e + (bitlen m - 1))" (is "?lb2 \ _") ``` huffman@30273 ` 1815` ``` unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right ``` hoelzl@29805 ` 1816` ``` using lb_ln2[of prec] ``` hoelzl@29805 ` 1817` ``` proof (rule mult_right_mono) ``` hoelzl@29805 ` 1818` ``` have "1 \ Float m e" using `1 \ x` Float unfolding le_float_def by auto ``` hoelzl@29805 ` 1819` ``` from float_gt1_scale[OF this] ``` hoelzl@29805 ` 1820` ``` show "0 \ real (e + (bitlen m - 1))" by auto ``` hoelzl@29805 ` 1821` ``` qed ``` hoelzl@29805 ` 1822` ``` moreover ``` hoelzl@29805 ` 1823` ``` from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \ 0`, symmetric]] ``` hoelzl@29805 ` 1824` ``` have "0 \ Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto ``` hoelzl@29805 ` 1825` ``` from ln_float_bounds(1)[OF this] ``` hoelzl@29805 ` 1826` ``` have "Ifloat ((?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1)) \ ln (Ifloat ?x)" (is "?lb_horner \ _") by auto ``` hoelzl@29805 ` 1827` ``` ultimately have "?lb2 + ?lb_horner \ ln (Ifloat x)" ``` hoelzl@29805 ` 1828` ``` unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto ``` hoelzl@29805 ` 1829` ``` } ``` hoelzl@29805 ` 1830` ``` moreover ``` hoelzl@29805 ` 1831` ``` { ``` hoelzl@29805 ` 1832` ``` from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \ 0`, symmetric]] ``` hoelzl@29805 ` 1833` ``` have "0 \ Ifloat (?x - 1)" and "Ifloat (?x - 1) < 1" by auto ``` hoelzl@29805 ` 1834` ``` from ln_float_bounds(2)[OF this] ``` hoelzl@29805 ` 1835` ``` have "ln (Ifloat ?x) \ Ifloat ((?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1))" (is "_ \ ?ub_horner") by auto ``` hoelzl@29805 ` 1836` ``` moreover ``` hoelzl@29805 ` 1837` ``` have "ln 2 * real (e + (bitlen m - 1)) \ Ifloat (ub_ln2 prec * ?s)" (is "_ \ ?ub2") ``` huffman@30273 ` 1838` ``` unfolding Ifloat_mult Ifloat_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right ``` hoelzl@29805 ` 1839` ``` using ub_ln2[of prec] ``` hoelzl@29805 ` 1840` ``` proof (rule mult_right_mono) ``` hoelzl@29805 ` 1841` ``` have "1 \ Float m e" using `1 \ x` Float unfolding le_float_def by auto ``` hoelzl@29805 ` 1842` ``` from float_gt1_scale[OF this] ``` hoelzl@29805 ` 1843` ``` show "0 \ real (e + (bitlen m - 1))" by auto ``` hoelzl@29805 ` 1844` ``` qed ``` hoelzl@29805 ` 1845` ``` ultimately have "ln (Ifloat x) \ ?ub2 + ?ub_horner" ``` hoelzl@29805 ` 1846` ``` unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto ``` hoelzl@29805 ` 1847` ``` } ``` hoelzl@29805 ` 1848` ``` ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps ``` hoelzl@29805 ` 1849` ``` unfolding if_not_P[OF `\ x \ 0`] if_not_P[OF `\ x < 1`] if_not_P[OF False] Let_def ``` hoelzl@29805 ` 1850` ``` unfolding scale.simps[of m e, unfolded Float[symmetric]] mantissa.simps[of m e, unfolded Float[symmetric]] Ifloat_add by auto ``` hoelzl@29805 ` 1851` ``` qed ``` hoelzl@29805 ` 1852` ```qed ``` hoelzl@29805 ` 1853` hoelzl@29805 ` 1854` ```lemma ub_ln_lb_ln_bounds: assumes "0 < x" ``` hoelzl@29805 ` 1855` ``` shows "Ifloat (the (lb_ln prec x)) \ ln (Ifloat x) \ ln (Ifloat x) \ Ifloat (the (ub_ln prec x))" ``` hoelzl@29805 ` 1856` ``` (is "?lb \ ?ln \ ?ln \ ?ub") ``` hoelzl@29805 ` 1857` ```proof (cases "x < 1") ``` hoelzl@29805 ` 1858` ``` case False hence "1 \ x" unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1859` ``` show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \ x`] . ``` hoelzl@29805 ` 1860` ```next ``` hoelzl@29805 ` 1861` ``` case True have "\ x \ 0" using `0 < x` unfolding less_float_def le_float_def by auto ``` hoelzl@29805 ` 1862` hoelzl@29805 ` 1863` ``` have "0 < Ifloat x" and "Ifloat x \ 0" using `0 < x` unfolding less_float_def by auto ``` hoelzl@29805 ` 1864` ``` hence A: "0 < 1 / Ifloat x" by auto ``` hoelzl@29805 ` 1865` hoelzl@29805 ` 1866` ``` { ``` hoelzl@29805 ` 1867` ``` let ?divl = "float_divl (max prec 1) 1 x" ``` hoelzl@29805 ` 1868` ``` have A': "1 \ ?divl" using float_divl_pos_less1_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 1869` ``` hence B: "0 < Ifloat ?divl" unfolding le_float_def by auto ``` hoelzl@29805 ` 1870` ``` ``` hoelzl@29805 ` 1871` ``` have "ln (Ifloat ?divl) \ ln (1 / Ifloat x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto ``` hoelzl@29805 ` 1872` ``` hence "ln (Ifloat x) \ - ln (Ifloat ?divl)" unfolding nonzero_inverse_eq_divide[OF `Ifloat x \ 0`, symmetric] ln_inverse[OF `0 < Ifloat x`] by auto ``` hoelzl@29805 ` 1873` ``` from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le] ``` hoelzl@29805 ` 1874` ``` have "?ln \ Ifloat (- the (lb_ln prec ?divl))" unfolding Ifloat_minus by (rule order_trans) ``` hoelzl@29805 ` 1875` ``` } moreover ``` hoelzl@29805 ` 1876` ``` { ``` hoelzl@29805 ` 1877` ``` let ?divr = "float_divr prec 1 x" ``` hoelzl@29805 ` 1878` ``` have A': "1 \ ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 1879` ``` hence B: "0 < Ifloat ?divr" unfolding le_float_def by auto ``` hoelzl@29805 ` 1880` ``` ``` hoelzl@29805 ` 1881` ``` have "ln (1 / Ifloat x) \ ln (Ifloat ?divr)" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto ``` hoelzl@29805 ` 1882` ``` hence "- ln (Ifloat ?divr) \ ln (Ifloat x)" unfolding nonzero_inverse_eq_divide[OF `Ifloat x \ 0`, symmetric] ln_inverse[OF `0 < Ifloat x`] by auto ``` hoelzl@29805 ` 1883` ``` from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this ``` hoelzl@29805 ` 1884` ``` have "Ifloat (- the (ub_ln prec ?divr)) \ ?ln" unfolding Ifloat_minus by (rule order_trans) ``` hoelzl@29805 ` 1885` ``` } ``` hoelzl@29805 ` 1886` ``` ultimately show ?thesis unfolding lb_ln.simps[where x=x] ub_ln.simps[where x=x] ``` hoelzl@29805 ` 1887` ``` unfolding if_not_P[OF `\ x \ 0`] if_P[OF True] by auto ``` hoelzl@29805 ` 1888` ```qed ``` hoelzl@29805 ` 1889` hoelzl@29805 ` 1890` ```lemma lb_ln: assumes "Some y = lb_ln prec x" ``` hoelzl@29805 ` 1891` ``` shows "Ifloat y \ ln (Ifloat x)" and "0 < Ifloat x" ``` hoelzl@29805 ` 1892` ```proof - ``` hoelzl@29805 ` 1893` ``` have "0 < x" ``` hoelzl@29805 ` 1894` ``` proof (rule ccontr) ``` hoelzl@29805 ` 1895` ``` assume "\ 0 < x" hence "x \ 0" unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 1896` ``` thus False using assms by auto ``` hoelzl@29805 ` 1897` ``` qed ``` hoelzl@29805 ` 1898` ``` thus "0 < Ifloat x" unfolding less_float_def by auto ``` hoelzl@29805 ` 1899` ``` have "Ifloat (the (lb_ln prec x)) \ ln (Ifloat x)" using ub_ln_lb_ln_bounds[OF `0 < x`] .. ``` hoelzl@29805 ` 1900` ``` thus "Ifloat y \ ln (Ifloat x)" unfolding assms[symmetric] by auto ``` hoelzl@29805 ` 1901` ```qed ``` hoelzl@29805 ` 1902` hoelzl@29805 ` 1903` ```lemma ub_ln: assumes "Some y = ub_ln prec x" ``` hoelzl@29805 ` 1904` ``` shows "ln (Ifloat x) \ Ifloat y" and "0 < Ifloat x" ``` hoelzl@29805 ` 1905` ```proof - ``` hoelzl@29805 ` 1906` ``` have "0 < x" ``` hoelzl@29805 ` 1907` ``` proof (rule ccontr) ``` hoelzl@29805 ` 1908` ``` assume "\ 0 < x" hence "x \ 0" unfolding le_float_def less_float_def by auto ``` hoelzl@29805 ` 1909` ``` thus False using assms by auto ``` hoelzl@29805 ` 1910` ``` qed ``` hoelzl@29805 ` 1911` ``` thus "0 < Ifloat x" unfolding less_float_def by auto ``` hoelzl@29805 ` 1912` ``` have "ln (Ifloat x) \ Ifloat (the (ub_ln prec x))" using ub_ln_lb_ln_bounds[OF `0 < x`] .. ``` hoelzl@29805 ` 1913` ``` thus "ln (Ifloat x) \ Ifloat y" unfolding assms[symmetric] by auto ``` hoelzl@29805 ` 1914` ```qed ``` hoelzl@29805 ` 1915` hoelzl@29805 ` 1916` ```lemma bnds_ln: "\ x lx ux. (Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \ x \ {Ifloat lx .. Ifloat ux} \ Ifloat l \ ln x \ ln x \ Ifloat u" ``` hoelzl@29805 ` 1917` ```proof (rule allI, rule allI, rule allI, rule impI) ``` hoelzl@29805 ` 1918` ``` fix x lx ux ``` hoelzl@29805 ` 1919` ``` assume "(Some l, Some u) = (lb_ln prec lx, ub_ln prec ux) \ x \ {Ifloat lx .. Ifloat ux}" ``` hoelzl@29805 ` 1920` ``` hence l: "Some l = lb_ln prec lx " and u: "Some u = ub_ln prec ux" and x: "x \ {Ifloat lx .. Ifloat ux}" by auto ``` hoelzl@29805 ` 1921` hoelzl@29805 ` 1922` ``` have "ln (Ifloat ux) \ Ifloat u" and "0 < Ifloat ux" using ub_ln u by auto ``` hoelzl@29805 ` 1923` ``` have "Ifloat l \ ln (Ifloat lx)" and "0 < Ifloat lx" and "0 < x" using lb_ln[OF l] x by auto ``` hoelzl@29805 ` 1924` hoelzl@29805 ` 1925` ``` from ln_le_cancel_iff[OF `0 < Ifloat lx` `0 < x`] `Ifloat l \ ln (Ifloat lx)` ``` hoelzl@29805 ` 1926` ``` have "Ifloat l \ ln x" using x unfolding atLeastAtMost_iff by auto ``` hoelzl@29805 ` 1927` ``` moreover ``` hoelzl@29805 ` 1928` ``` from ln_le_cancel_iff[OF `0 < x` `0 < Ifloat ux`] `ln (Ifloat ux) \ Ifloat u` ``` hoelzl@29805 ` 1929` ``` have "ln x \ Ifloat u" using x unfolding atLeastAtMost_iff by auto ``` hoelzl@29805 ` 1930` ``` ultimately show "Ifloat l \ ln x \ ln x \ Ifloat u" .. ``` hoelzl@29805 ` 1931` ```qed ``` hoelzl@29805 ` 1932` hoelzl@29805 ` 1933` hoelzl@29805 ` 1934` ```section "Implement floatarith" ``` hoelzl@29805 ` 1935` hoelzl@29805 ` 1936` ```subsection "Define syntax and semantics" ``` hoelzl@29805 ` 1937` hoelzl@29805 ` 1938` ```datatype floatarith ``` hoelzl@29805 ` 1939` ``` = Add floatarith floatarith ``` hoelzl@29805 ` 1940` ``` | Minus floatarith ``` hoelzl@29805 ` 1941` ``` | Mult floatarith floatarith ``` hoelzl@29805 ` 1942` ``` | Inverse floatarith ``` hoelzl@29805 ` 1943` ``` | Sin floatarith ``` hoelzl@29805 ` 1944` ``` | Cos floatarith ``` hoelzl@29805 ` 1945` ``` | Arctan floatarith ``` hoelzl@29805 ` 1946` ``` | Abs floatarith ``` hoelzl@29805 ` 1947` ``` | Max floatarith floatarith ``` hoelzl@29805 ` 1948` ``` | Min floatarith floatarith ``` hoelzl@29805 ` 1949` ``` | Pi ``` hoelzl@29805 ` 1950` ``` | Sqrt floatarith ``` hoelzl@29805 ` 1951` ``` | Exp floatarith ``` hoelzl@29805 ` 1952` ``` | Ln floatarith ``` hoelzl@29805 ` 1953` ``` | Power floatarith nat ``` hoelzl@29805 ` 1954` ``` | Atom nat ``` hoelzl@29805 ` 1955` ``` | Num float ``` hoelzl@29805 ` 1956` hoelzl@29805 ` 1957` ```fun Ifloatarith :: "floatarith \ real list \ real" ``` hoelzl@29805 ` 1958` ```where ``` hoelzl@29805 ` 1959` ```"Ifloatarith (Add a b) vs = (Ifloatarith a vs) + (Ifloatarith b vs)" | ``` hoelzl@29805 ` 1960` ```"Ifloatarith (Minus a) vs = - (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1961` ```"Ifloatarith (Mult a b) vs = (Ifloatarith a vs) * (Ifloatarith b vs)" | ``` hoelzl@29805 ` 1962` ```"Ifloatarith (Inverse a) vs = inverse (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1963` ```"Ifloatarith (Sin a) vs = sin (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1964` ```"Ifloatarith (Cos a) vs = cos (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1965` ```"Ifloatarith (Arctan a) vs = arctan (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1966` ```"Ifloatarith (Min a b) vs = min (Ifloatarith a vs) (Ifloatarith b vs)" | ``` hoelzl@29805 ` 1967` ```"Ifloatarith (Max a b) vs = max (Ifloatarith a vs) (Ifloatarith b vs)" | ``` hoelzl@29805 ` 1968` ```"Ifloatarith (Abs a) vs = abs (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1969` ```"Ifloatarith Pi vs = pi" | ``` hoelzl@29805 ` 1970` ```"Ifloatarith (Sqrt a) vs = sqrt (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1971` ```"Ifloatarith (Exp a) vs = exp (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1972` ```"Ifloatarith (Ln a) vs = ln (Ifloatarith a vs)" | ``` hoelzl@29805 ` 1973` ```"Ifloatarith (Power a n) vs = (Ifloatarith a vs)^n" | ``` hoelzl@29805 ` 1974` ```"Ifloatarith (Num f) vs = Ifloat f" | ``` hoelzl@29805 ` 1975` ```"Ifloatarith (Atom n) vs = vs ! n" ``` hoelzl@29805 ` 1976` hoelzl@29805 ` 1977` ```subsection "Implement approximation function" ``` hoelzl@29805 ` 1978` hoelzl@29805 ` 1979` ```fun lift_bin :: "(float * float) option \ (float * float) option \ (float \ float \ float \ float \ (float option * float option)) \ (float * float) option" where ``` hoelzl@29805 ` 1980` ```"lift_bin (Some (l1, u1)) (Some (l2, u2)) f = (case (f l1 u1 l2 u2) of (Some l, Some u) \ Some (l, u) ``` hoelzl@29805 ` 1981` ``` | t \ None)" | ``` hoelzl@29805 ` 1982` ```"lift_bin a b f = None" ``` hoelzl@29805 ` 1983` hoelzl@29805 ` 1984` ```fun lift_bin' :: "(float * float) option \ (float * float) option \ (float \ float \ float \ float \ (float * float)) \ (float * float) option" where ``` hoelzl@29805 ` 1985` ```"lift_bin' (Some (l1, u1)) (Some (l2, u2)) f = Some (f l1 u1 l2 u2)" | ``` hoelzl@29805 ` 1986` ```"lift_bin' a b f = None" ``` hoelzl@29805 ` 1987` hoelzl@29805 ` 1988` ```fun lift_un :: "(float * float) option \ (float \ float \ ((float option) * (float option))) \ (float * float) option" where ``` hoelzl@29805 ` 1989` ```"lift_un (Some (l1, u1)) f = (case (f l1 u1) of (Some l, Some u) \ Some (l, u) ``` hoelzl@29805 ` 1990` ``` | t \ None)" | ``` hoelzl@29805 ` 1991` ```"lift_un b f = None" ``` hoelzl@29805 ` 1992` hoelzl@29805 ` 1993` ```fun lift_un' :: "(float * float) option \ (float \ float \ (float * float)) \ (float * float) option" where ``` hoelzl@29805 ` 1994` ```"lift_un' (Some (l1, u1)) f = Some (f l1 u1)" | ``` hoelzl@29805 ` 1995` ```"lift_un' b f = None" ``` hoelzl@29805 ` 1996` hoelzl@29805 ` 1997` ```fun bounded_by :: "real list \ (float * float) list \ bool " where ``` hoelzl@29805 ` 1998` ```bounded_by_Cons: "bounded_by (v#vs) ((l, u)#bs) = ((Ifloat l \ v \ v \ Ifloat u) \ bounded_by vs bs)" | ``` hoelzl@29805 ` 1999` ```bounded_by_Nil: "bounded_by [] [] = True" | ``` hoelzl@29805 ` 2000` ```"bounded_by _ _ = False" ``` hoelzl@29805 ` 2001` hoelzl@29805 ` 2002` ```lemma bounded_by: assumes "bounded_by vs bs" and "i < length bs" ``` hoelzl@29805 ` 2003` ``` shows "Ifloat (fst (bs ! i)) \ vs ! i \ vs ! i \ Ifloat (snd (bs ! i))" ``` hoelzl@29805 ` 2004` ``` using `bounded_by vs bs` and `i < length bs` ``` hoelzl@29805 ` 2005` ```proof (induct arbitrary: i rule: bounded_by.induct) ``` hoelzl@29805 ` 2006` ``` fix v :: real and vs :: "real list" and l u :: float and bs :: "(float * float) list" and i :: nat ``` hoelzl@29805 ` 2007` ``` assume hyp: "\i. \bounded_by vs bs; i < length bs\ \ Ifloat (fst (bs ! i)) \ vs ! i \ vs ! i \ Ifloat (snd (bs ! i))" ``` hoelzl@29805 ` 2008` ``` assume bounded: "bounded_by (v # vs) ((l, u) # bs)" and length: "i < length ((l, u) # bs)" ``` hoelzl@29805 ` 2009` ``` show "Ifloat (fst (((l, u) # bs) ! i)) \ (v # vs) ! i \ (v # vs) ! i \ Ifloat (snd (((l, u) # bs) ! i))" ``` hoelzl@29805 ` 2010` ``` proof (cases i) ``` hoelzl@29805 ` 2011` ``` case 0 ``` hoelzl@29805 ` 2012` ``` show ?thesis using bounded unfolding 0 nth_Cons_0 fst_conv snd_conv bounded_by.simps .. ``` hoelzl@29805 ` 2013` ``` next ``` hoelzl@29805 ` 2014` ``` case (Suc i) with length have "i < length bs" by auto ``` hoelzl@29805 ` 2015` ``` show ?thesis unfolding Suc nth_Cons_Suc bounded_by.simps ``` hoelzl@29805 ` 2016` ``` using hyp[OF bounded[unfolded bounded_by.simps, THEN conjunct2] `i < length bs`] . ``` hoelzl@29805 ` 2017` ``` qed ``` hoelzl@29805 ` 2018` ```qed auto ``` hoelzl@29805 ` 2019` hoelzl@29805 ` 2020` ```fun approx approx' :: "nat \ floatarith \ (float * float) list \ (float * float) option" where ``` hoelzl@29805 ` 2021` ```"approx' prec a bs = (case (approx prec a bs) of Some (l, u) \ Some (round_down prec l, round_up prec u) | None \ None)" | ``` hoelzl@29805 ` 2022` ```"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 ` 2023` ```"approx prec (Minus a) bs = lift_un' (approx' prec a bs) (\ l u. (-u, -l))" | ``` hoelzl@29805 ` 2024` ```"approx prec (Mult a b) bs = lift_bin' (approx' prec a bs) (approx' prec b bs) ``` hoelzl@29805 ` 2025` ``` (\ a1 a2 b1 b2. (float_nprt a1 * float_pprt b2 + float_nprt a2 * float_nprt b2 + float_pprt a1 * float_pprt b1 + float_pprt a2 * float_nprt b1, ``` hoelzl@29805 ` 2026` ``` float_pprt a2 * float_pprt b2 + float_pprt a1 * float_nprt b2 + float_nprt a2 * float_pprt b1 + float_nprt a1 * float_nprt b1))" | ``` hoelzl@29805 ` 2027` ```"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 ` 2028` ```"approx prec (Sin a) bs = lift_un' (approx' prec a bs) (bnds_sin prec)" | ``` hoelzl@29805 ` 2029` ```"approx prec (Cos a) bs = lift_un' (approx' prec a bs) (bnds_cos prec)" | ``` hoelzl@29805 ` 2030` ```"approx prec Pi bs = Some (lb_pi prec, ub_pi prec)" | ``` hoelzl@29805 ` 2031` ```"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 ` 2032` ```"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 ` 2033` ```"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 ` 2034` ```"approx prec (Arctan a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_arctan prec l, ub_arctan prec u))" | ``` hoelzl@29805 ` 2035` ```"approx prec (Sqrt a) bs = lift_un (approx' prec a bs) (\ l u. (lb_sqrt prec l, ub_sqrt prec u))" | ``` hoelzl@29805 ` 2036` ```"approx prec (Exp a) bs = lift_un' (approx' prec a bs) (\ l u. (lb_exp prec l, ub_exp prec u))" | ``` hoelzl@29805 ` 2037` ```"approx prec (Ln a) bs = lift_un (approx' prec a bs) (\ l u. (lb_ln prec l, ub_ln prec u))" | ``` hoelzl@29805 ` 2038` ```"approx prec (Power a n) bs = lift_un' (approx' prec a bs) (float_power_bnds n)" | ``` hoelzl@29805 ` 2039` ```"approx prec (Num f) bs = Some (f, f)" | ``` hoelzl@29805 ` 2040` ```"approx prec (Atom i) bs = (if i < length bs then Some (bs ! i) else None)" ``` hoelzl@29805 ` 2041` hoelzl@29805 ` 2042` ```lemma lift_bin'_ex: ``` hoelzl@29805 ` 2043` ``` assumes lift_bin'_Some: "Some (l, u) = lift_bin' a b f" ``` hoelzl@29805 ` 2044` ``` shows "\ l1 u1 l2 u2. Some (l1, u1) = a \ Some (l2, u2) = b" ``` hoelzl@29805 ` 2045` ```proof (cases a) ``` hoelzl@29805 ` 2046` ``` case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps .. ``` hoelzl@29805 ` 2047` ``` thus ?thesis using lift_bin'_Some by auto ``` hoelzl@29805 ` 2048` ```next ``` hoelzl@29805 ` 2049` ``` case (Some a') ``` hoelzl@29805 ` 2050` ``` show ?thesis ``` hoelzl@29805 ` 2051` ``` proof (cases b) ``` hoelzl@29805 ` 2052` ``` case None hence "None = lift_bin' a b f" unfolding None lift_bin'.simps .. ``` hoelzl@29805 ` 2053` ``` thus ?thesis using lift_bin'_Some by auto ``` hoelzl@29805 ` 2054` ``` next ``` hoelzl@29805 ` 2055` ``` case (Some b') ``` hoelzl@29805 ` 2056` ``` obtain la ua where a': "a' = (la, ua)" by (cases a', auto) ``` hoelzl@29805 ` 2057` ``` obtain lb ub where b': "b' = (lb, ub)" by (cases b', auto) ``` hoelzl@29805 ` 2058` ``` thus ?thesis unfolding `a = Some a'` `b = Some b'` a' b' by auto ``` hoelzl@29805 ` 2059` ``` qed ``` hoelzl@29805 ` 2060` ```qed ``` hoelzl@29805 ` 2061` hoelzl@29805 ` 2062` ```lemma lift_bin'_f: ``` hoelzl@29805 ` 2063` ``` assumes lift_bin'_Some: "Some (l, u) = lift_bin' (g a) (g b) f" ``` hoelzl@29805 ` 2064` ``` 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 ` 2065` ``` 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 ` 2066` ```proof - ``` hoelzl@29805 ` 2067` ``` obtain l1 u1 l2 u2 ``` hoelzl@29805 ` 2068` ``` where Sa: "Some (l1, u1) = g a" and Sb: "Some (l2, u2) = g b" using lift_bin'_ex[OF assms(1)] by auto ``` hoelzl@29805 ` 2069` ``` 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 ` 2070` ``` have "l = fst (f l1 u1 l2 u2)" and "u = snd (f l1 u1 l2 u2)" unfolding lu[symmetric] by auto ``` hoelzl@29805 ` 2071` ``` thus ?thesis using Pa[OF Sa] Pb[OF Sb] by auto ``` hoelzl@29805 ` 2072` ```qed ``` hoelzl@29805 ` 2073` hoelzl@29805 ` 2074` ```lemma approx_approx': ``` hoelzl@29805 ` 2075` ``` assumes Pa: "\l u. Some (l, u) = approx prec a vs \ Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" ``` hoelzl@29805 ` 2076` ``` and approx': "Some (l, u) = approx' prec a vs" ``` hoelzl@29805 ` 2077` ``` shows "Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" ``` hoelzl@29805 ` 2078` ```proof - ``` hoelzl@29805 ` 2079` ``` obtain l' u' where S: "Some (l', u') = approx prec a vs" ``` hoelzl@29805 ` 2080` ``` using approx' unfolding approx'.simps by (cases "approx prec a vs", auto) ``` hoelzl@29805 ` 2081` ``` have l': "l = round_down prec l'" and u': "u = round_up prec u'" ``` hoelzl@29805 ` 2082` ``` using approx' unfolding approx'.simps S[symmetric] by auto ``` hoelzl@29805 ` 2083` ``` show ?thesis unfolding l' u' ``` hoelzl@29805 ` 2084` ``` using order_trans[OF Pa[OF S, THEN conjunct2] round_up[of u']] ``` hoelzl@29805 ` 2085` ``` using order_trans[OF round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto ``` hoelzl@29805 ` 2086` ```qed ``` hoelzl@29805 ` 2087` hoelzl@29805 ` 2088` ```lemma lift_bin': ``` hoelzl@29805 ` 2089` ``` assumes lift_bin'_Some: "Some (l, u) = lift_bin' (approx' prec a bs) (approx' prec b bs) f" ``` hoelzl@29805 ` 2090` ``` and Pa: "\l u. Some (l, u) = approx prec a bs \ Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" (is "\l u. _ = ?g a \ ?P l u a") ``` hoelzl@29805 ` 2091` ``` and Pb: "\l u. Some (l, u) = approx prec b bs \ Ifloat l \ Ifloatarith b xs \ Ifloatarith b xs \ Ifloat u" ``` hoelzl@29805 ` 2092` ``` shows "\ l1 u1 l2 u2. (Ifloat l1 \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u1) \ ``` hoelzl@29805 ` 2093` ``` (Ifloat l2 \ Ifloatarith b xs \ Ifloatarith b xs \ Ifloat u2) \ ``` hoelzl@29805 ` 2094` ``` l = fst (f l1 u1 l2 u2) \ u = snd (f l1 u1 l2 u2)" ``` hoelzl@29805 ` 2095` ```proof - ``` hoelzl@29805 ` 2096` ``` { fix l u assume "Some (l, u) = approx' prec a bs" ``` hoelzl@29805 ` 2097` ``` with approx_approx'[of prec a bs, OF _ this] Pa ``` hoelzl@29805 ` 2098` ``` have "Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" by auto } note Pa = this ``` hoelzl@29805 ` 2099` ``` { fix l u assume "Some (l, u) = approx' prec b bs" ``` hoelzl@29805 ` 2100` ``` with approx_approx'[of prec b bs, OF _ this] Pb ``` hoelzl@29805 ` 2101` ``` have "Ifloat l \ Ifloatarith b xs \ Ifloatarith b xs \ Ifloat u" by auto } note Pb = this ``` hoelzl@29805 ` 2102` hoelzl@29805 ` 2103` ``` from lift_bin'_f[where g="\a. approx' prec a bs" and P = ?P, OF lift_bin'_Some, OF Pa Pb] ``` hoelzl@29805 ` 2104` ``` show ?thesis by auto ``` hoelzl@29805 ` 2105` ```qed ``` hoelzl@29805 ` 2106` hoelzl@29805 ` 2107` ```lemma lift_un'_ex: ``` hoelzl@29805 ` 2108` ``` assumes lift_un'_Some: "Some (l, u) = lift_un' a f" ``` hoelzl@29805 ` 2109` ``` shows "\ l u. Some (l, u) = a" ``` hoelzl@29805 ` 2110` ```proof (cases a) ``` hoelzl@29805 ` 2111` ``` case None hence "None = lift_un' a f" unfolding None lift_un'.simps .. ``` hoelzl@29805 ` 2112` ``` thus ?thesis using lift_un'_Some by auto ``` hoelzl@29805 ` 2113` ```next ``` hoelzl@29805 ` 2114` ``` case (Some a') ``` hoelzl@29805 ` 2115` ``` obtain la ua where a': "a' = (la, ua)" by (cases a', auto) ``` hoelzl@29805 ` 2116` ``` thus ?thesis unfolding `a = Some a'` a' by auto ``` hoelzl@29805 ` 2117` ```qed ``` hoelzl@29805 ` 2118` hoelzl@29805 ` 2119` ```lemma lift_un'_f: ``` hoelzl@29805 ` 2120` ``` assumes lift_un'_Some: "Some (l, u) = lift_un' (g a) f" ``` hoelzl@29805 ` 2121` ``` and Pa: "\l u. Some (l, u) = g a \ P l u a" ``` hoelzl@29805 ` 2122` ``` shows "\ l1 u1. P l1 u1 a \ l = fst (f l1 u1) \ u = snd (f l1 u1)" ``` hoelzl@29805 ` 2123` ```proof - ``` hoelzl@29805 ` 2124` ``` obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un'_ex[OF assms(1)] by auto ``` hoelzl@29805 ` 2125` ``` have lu: "(l, u) = f l1 u1" using lift_un'_Some[unfolded Sa[symmetric] lift_un'.simps] by auto ``` hoelzl@29805 ` 2126` ``` have "l = fst (f l1 u1)" and "u = snd (f l1 u1)" unfolding lu[symmetric] by auto ``` hoelzl@29805 ` 2127` ``` thus ?thesis using Pa[OF Sa] by auto ``` hoelzl@29805 ` 2128` ```qed ``` hoelzl@29805 ` 2129` hoelzl@29805 ` 2130` ```lemma lift_un': ``` hoelzl@29805 ` 2131` ``` assumes lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f" ``` hoelzl@29805 ` 2132` ``` and Pa: "\l u. Some (l, u) = approx prec a bs \ Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" (is "\l u. _ = ?g a \ ?P l u a") ``` hoelzl@29805 ` 2133` ``` shows "\ l1 u1. (Ifloat l1 \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u1) \ ``` hoelzl@29805 ` 2134` ``` l = fst (f l1 u1) \ u = snd (f l1 u1)" ``` hoelzl@29805 ` 2135` ```proof - ``` hoelzl@29805 ` 2136` ``` { fix l u assume "Some (l, u) = approx' prec a bs" ``` hoelzl@29805 ` 2137` ``` with approx_approx'[of prec a bs, OF _ this] Pa ``` hoelzl@29805 ` 2138` ``` have "Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" by auto } note Pa = this ``` hoelzl@29805 ` 2139` ``` from lift_un'_f[where g="\a. approx' prec a bs" and P = ?P, OF lift_un'_Some, OF Pa] ``` hoelzl@29805 ` 2140` ``` show ?thesis by auto ``` hoelzl@29805 ` 2141` ```qed ``` hoelzl@29805 ` 2142` hoelzl@29805 ` 2143` ```lemma lift_un'_bnds: ``` hoelzl@29805 ` 2144` ``` assumes bnds: "\ x lx ux. (l, u) = f lx ux \ x \ { Ifloat lx .. Ifloat ux } \ Ifloat l \ f' x \ f' x \ Ifloat u" ``` hoelzl@29805 ` 2145` ``` and lift_un'_Some: "Some (l, u) = lift_un' (approx' prec a bs) f" ``` hoelzl@29805 ` 2146` ``` and Pa: "\l u. Some (l, u) = approx prec a bs \ Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" ``` hoelzl@29805 ` 2147` ``` shows "Ifloat l \ f' (Ifloatarith a xs) \ f' (Ifloatarith a xs) \ Ifloat u" ``` hoelzl@29805 ` 2148` ```proof - ``` hoelzl@29805 ` 2149` ``` from lift_un'[OF lift_un'_Some Pa] ``` hoelzl@29805 ` 2150` ``` obtain l1 u1 where "Ifloat l1 \ Ifloatarith a xs" and "Ifloatarith a xs \ Ifloat u1" and "l = fst (f l1 u1)" and "u = snd (f l1 u1)" by blast ``` hoelzl@29805 ` 2151` ``` hence "(l, u) = f l1 u1" and "Ifloatarith a xs \ {Ifloat l1 .. Ifloat u1}" by auto ``` hoelzl@29805 ` 2152` ``` thus ?thesis using bnds by auto ``` hoelzl@29805 ` 2153` ```qed ``` hoelzl@29805 ` 2154` hoelzl@29805 ` 2155` ```lemma lift_un_ex: ``` hoelzl@29805 ` 2156` ``` assumes lift_un_Some: "Some (l, u) = lift_un a f" ``` hoelzl@29805 ` 2157` ``` shows "\ l u. Some (l, u) = a" ``` hoelzl@29805 ` 2158` ```proof (cases a) ``` hoelzl@29805 ` 2159` ``` case None hence "None = lift_un a f" unfolding None lift_un.simps .. ``` hoelzl@29805 ` 2160` ``` thus ?thesis using lift_un_Some by auto ``` hoelzl@29805 ` 2161` ```next ``` hoelzl@29805 ` 2162` ``` case (Some a') ``` hoelzl@29805 ` 2163` ``` obtain la ua where a': "a' = (la, ua)" by (cases a', auto) ``` hoelzl@29805 ` 2164` ``` thus ?thesis unfolding `a = Some a'` a' by auto ``` hoelzl@29805 ` 2165` ```qed ``` hoelzl@29805 ` 2166` hoelzl@29805 ` 2167` ```lemma lift_un_f: ``` hoelzl@29805 ` 2168` ``` assumes lift_un_Some: "Some (l, u) = lift_un (g a) f" ``` hoelzl@29805 ` 2169` ``` and Pa: "\l u. Some (l, u) = g a \ P l u a" ``` hoelzl@29805 ` 2170` ``` shows "\ l1 u1. P l1 u1 a \ Some l = fst (f l1 u1) \ Some u = snd (f l1 u1)" ``` hoelzl@29805 ` 2171` ```proof - ``` hoelzl@29805 ` 2172` ``` obtain l1 u1 where Sa: "Some (l1, u1) = g a" using lift_un_ex[OF assms(1)] by auto ``` hoelzl@29805 ` 2173` ``` have "fst (f l1 u1) \ None \ snd (f l1 u1) \ None" ``` hoelzl@29805 ` 2174` ``` proof (rule ccontr) ``` hoelzl@29805 ` 2175` ``` assume "\ (fst (f l1 u1) \ None \ snd (f l1 u1) \ None)" ``` hoelzl@29805 ` 2176` ``` hence or: "fst (f l1 u1) = None \ snd (f l1 u1) = None" by auto ``` hoelzl@29805 ` 2177` ``` hence "lift_un (g a) f = None" ``` hoelzl@29805 ` 2178` ``` proof (cases "fst (f l1 u1) = None") ``` hoelzl@29805 ` 2179` ``` case True ``` hoelzl@29805 ` 2180` ``` then obtain b where b: "f l1 u1 = (None, b)" by (cases "f l1 u1", auto) ``` hoelzl@29805 ` 2181` ``` thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto ``` hoelzl@29805 ` 2182` ``` next ``` hoelzl@29805 ` 2183` ``` case False hence "snd (f l1 u1) = None" using or by auto ``` hoelzl@29805 ` 2184` ``` with False obtain b where b: "f l1 u1 = (Some b, None)" by (cases "f l1 u1", auto) ``` hoelzl@29805 ` 2185` ``` thus ?thesis unfolding Sa[symmetric] lift_un.simps b by auto ``` hoelzl@29805 ` 2186` ``` qed ``` hoelzl@29805 ` 2187` ``` thus False using lift_un_Some by auto ``` hoelzl@29805 ` 2188` ``` qed ``` hoelzl@29805 ` 2189` ``` then obtain a' b' where f: "f l1 u1 = (Some a', Some b')" by (cases "f l1 u1", auto) ``` hoelzl@29805 ` 2190` ``` from lift_un_Some[unfolded Sa[symmetric] lift_un.simps f] ``` hoelzl@29805 ` 2191` ``` have "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" unfolding f by auto ``` hoelzl@29805 ` 2192` ``` thus ?thesis unfolding Sa[symmetric] lift_un.simps using Pa[OF Sa] by auto ``` hoelzl@29805 ` 2193` ```qed ``` hoelzl@29805 ` 2194` hoelzl@29805 ` 2195` ```lemma lift_un: ``` hoelzl@29805 ` 2196` ``` assumes lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f" ``` hoelzl@29805 ` 2197` ``` and Pa: "\l u. Some (l, u) = approx prec a bs \ Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" (is "\l u. _ = ?g a \ ?P l u a") ``` hoelzl@29805 ` 2198` ``` shows "\ l1 u1. (Ifloat l1 \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u1) \ ``` hoelzl@29805 ` 2199` ``` Some l = fst (f l1 u1) \ Some u = snd (f l1 u1)" ``` hoelzl@29805 ` 2200` ```proof - ``` hoelzl@29805 ` 2201` ``` { fix l u assume "Some (l, u) = approx' prec a bs" ``` hoelzl@29805 ` 2202` ``` with approx_approx'[of prec a bs, OF _ this] Pa ``` hoelzl@29805 ` 2203` ``` have "Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" by auto } note Pa = this ``` hoelzl@29805 ` 2204` ``` from lift_un_f[where g="\a. approx' prec a bs" and P = ?P, OF lift_un_Some, OF Pa] ``` hoelzl@29805 ` 2205` ``` show ?thesis by auto ``` hoelzl@29805 ` 2206` ```qed ``` hoelzl@29805 ` 2207` hoelzl@29805 ` 2208` ```lemma lift_un_bnds: ``` hoelzl@29805 ` 2209` ``` assumes bnds: "\ x lx ux. (Some l, Some u) = f lx ux \ x \ { Ifloat lx .. Ifloat ux } \ Ifloat l \ f' x \ f' x \ Ifloat u" ``` hoelzl@29805 ` 2210` ``` and lift_un_Some: "Some (l, u) = lift_un (approx' prec a bs) f" ``` hoelzl@29805 ` 2211` ``` and Pa: "\l u. Some (l, u) = approx prec a bs \ Ifloat l \ Ifloatarith a xs \ Ifloatarith a xs \ Ifloat u" ``` hoelzl@29805 ` 2212` ``` shows "Ifloat l \ f' (Ifloatarith a xs) \ f' (Ifloatarith a xs) \ Ifloat u" ``` hoelzl@29805 ` 2213` ```proof - ``` hoelzl@29805 ` 2214` ``` from lift_un[OF lift_un_Some Pa] ``` hoelzl@29805 ` 2215` ``` obtain l1 u1 where "Ifloat l1 \ Ifloatarith a xs" and "Ifloatarith a xs \ Ifloat u1" and "Some l = fst (f l1 u1)" and "Some u = snd (f l1 u1)" by blast ``` hoelzl@29805 ` 2216` ``` hence "(Some l, Some u) = f l1 u1" and "Ifloatarith a xs \ {Ifloat l1 .. Ifloat u1}" by auto ``` hoelzl@29805 ` 2217` ``` thus ?thesis using bnds by auto ``` hoelzl@29805 ` 2218` ```qed ``` hoelzl@29805 ` 2219` hoelzl@29805 ` 2220` ```lemma approx: ``` hoelzl@29805 ` 2221` ``` assumes "bounded_by xs vs" ``` hoelzl@29805 ` 2222` ``` and "Some (l, u) = approx prec arith vs" (is "_ = ?g arith") ``` hoelzl@29805 ` 2223` ``` shows "Ifloat l \ Ifloatarith arith xs \ Ifloatarith arith xs \ Ifloat u" (is "?P l u arith") ``` hoelzl@29805 ` 2224` ``` using `Some (l, u) = approx prec arith vs` ``` hoelzl@29805 ` 2225` ```proof (induct arith arbitrary: l u x) ``` hoelzl@29805 ` 2226` ``` case (Add a b) ``` hoelzl@29805 ` 2227` ``` from lift_bin'[OF Add.prems[unfolded approx.simps]] Add.hyps ``` hoelzl@29805 ` 2228` ``` obtain l1 u1 l2 u2 where "l = l1 + l2" and "u = u1 + u2" ``` hoelzl@29805 ` 2229` ``` "Ifloat l1 \ Ifloatarith a xs" and "Ifloatarith a xs \ Ifloat u1" ``` hoelzl@29805 ` 2230` ``` "Ifloat l2 \ Ifloatarith b xs" and "Ifloatarith b xs \ Ifloat u2" unfolding fst_conv snd_conv by blast ``` hoelzl@29805 ` 2231` ``` thus ?case unfolding Ifloatarith.simps by auto ``` hoelzl@29805 ` 2232` ```next ``` hoelzl@29805 ` 2233` ``` case (Minus a) ``` hoelzl@29805 ` 2234` ``` from lift_un'[OF Minus.prems[unfolded approx.simps]] Minus.hyps ``` hoelzl@29805 ` 2235` ``` obtain l1 u1 where "l = -u1" and "u = -l1" ``` hoelzl@29805 ` 2236` ``` "Ifloat l1 \ Ifloatarith a xs" and "Ifloatarith a xs \ Ifloat u1" unfolding fst_conv snd_conv by blast ``` hoelzl@29805 ` 2237` ``` thus ?case unfolding Ifloatarith.simps using Ifloat_minus by auto ``` hoelzl@29805 ` 2238` ```next ``` hoelzl@29805 ` 2239` ``` case (Mult a b) ``` hoelzl@29805 ` 2240` ``` from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps ``` hoelzl@29805 ` 2241` ``` obtain l1 u1 l2 u2 ``` hoelzl@29805 ` 2242` ``` where l: "l = float_nprt l1 * float_pprt u2 + float_nprt u1 * float_nprt u2 + float_pprt l1 * float_pprt l2 + float_pprt u1 * float_nprt l2" ``` hoelzl@29805 ` 2243` ``` and u: "u = float_pprt u1 * float_pprt u2 + float_pprt l1 * float_nprt u2 + float_nprt u1 * float_pprt l2 + float_nprt l1 * float_nprt l2" ``` hoelzl@29805 ` 2244` ``` and "Ifloat l1 \ Ifloatarith a xs" and "Ifloatarith a xs \ Ifloat u1" ``` hoelzl@29805 ` 2245` ``` and "Ifloat l2 \ Ifloatarith b xs" and "Ifloatarith b xs \ Ifloat u2" unfolding fst_conv snd_conv by blast ``` hoelzl@29805 ` 2246` ``` thus ?case unfolding Ifloatarith.simps l u Ifloat_add Ifloat_mult Ifloat_nprt Ifloat_pprt ``` hoelzl@29805 ` 2247` ``` using mult_le_prts mult_ge_prts by auto ``` hoelzl@29805 ` 2248` ```next ``` hoelzl@29805 ` 2249` ``` case (Inverse a) ``` hoelzl@29805 ` 2250` ``` 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@29805 ` 2251` ``` obtain l1 u1 where l': "Some l = (if 0 < l1 \ u1 < 0 then Some (float_divl prec 1 u1) else None)" ``` hoelzl@29805 ` 2252` ``` and u': "Some u = (if 0 < l1 \ u1 < 0 then Some (float_divr prec 1 l1) else None)" ``` hoelzl@29805 ` 2253` ``` and l1: "Ifloat l1 \ Ifloatarith a xs" and u1: "Ifloatarith a xs \ Ifloat u1" by blast ``` hoelzl@29805 ` 2254` ``` 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@29805 ` 2255` ``` moreover have l1_le_u1: "Ifloat l1 \ Ifloat u1" using l1 u1 by auto ``` hoelzl@29805 ` 2256` ``` ultimately have "Ifloat l1 \ 0" and "Ifloat u1 \ 0" unfolding less_float_def by auto ``` hoelzl@29805 ` 2257` hoelzl@29805 ` 2258` ``` have inv: "inverse (Ifloat u1) \ inverse (Ifloatarith a xs) ``` hoelzl@29805 ` 2259` ``` \ inverse (Ifloatarith a xs) \ inverse (Ifloat l1)" ``` hoelzl@29805 ` 2260` ``` proof (cases "0 < l1") ``` hoelzl@29805 ` 2261` ``` case True hence "0 < Ifloat u1" and "0 < Ifloat l1" "0 < Ifloatarith a xs" ``` hoelzl@29805 ` 2262` ``` unfolding less_float_def using l1_le_u1 l1 by auto ``` hoelzl@29805 ` 2263` ``` show ?thesis ``` hoelzl@29805 ` 2264` ``` unfolding inverse_le_iff_le[OF `0 < Ifloat u1` `0 < Ifloatarith a xs`] ``` hoelzl@29805 ` 2265` ``` inverse_le_iff_le[OF `0 < Ifloatarith a xs` `0 < Ifloat l1`] ``` hoelzl@29805 ` 2266` ``` using l1 u1 by auto ``` hoelzl@29805 ` 2267` ``` next ``` hoelzl@29805 ` 2268` ``` case False hence "u1 < 0" using either by blast ``` hoelzl@29805 ` 2269` ``` hence "Ifloat u1 < 0" and "Ifloat l1 < 0" "Ifloatarith a xs < 0" ``` hoelzl@29805 ` 2270` ``` unfolding less_float_def using l1_le_u1 u1 by auto ``` hoelzl@29805 ` 2271` ``` show ?thesis ``` hoelzl@29805 ` 2272` ``` unfolding inverse_le_iff_le_neg[OF `Ifloat u1 < 0` `Ifloatarith a xs < 0`] ``` hoelzl@29805 ` 2273` ``` inverse_le_iff_le_neg[OF `Ifloatarith a xs < 0` `Ifloat l1 < 0`] ``` hoelzl@29805 ` 2274` ``` using l1 u1 by auto ``` hoelzl@29805 ` 2275` ``` qed ``` hoelzl@29805 ` 2276` ``` ``` hoelzl@29805 ` 2277` ``` from l' have "l = float_divl prec 1 u1" by (cases "0 < l1 \ u1 < 0", auto) ``` hoelzl@29805 ` 2278` ``` hence "Ifloat l \ inverse (Ifloat u1)" unfolding nonzero_inverse_eq_divide[OF `Ifloat u1 \ 0`] using float_divl[of prec 1 u1] by auto ``` hoelzl@29805 ` 2279` ``` also have "\ \ inverse (Ifloatarith a xs)" using inv by auto ``` hoelzl@29805 ` 2280` ``` finally have "Ifloat l \ inverse (Ifloatarith a xs)" . ``` hoelzl@29805 ` 2281` ``` moreover ``` hoelzl@29805 ` 2282` ``` from u' have "u = float_divr prec 1 l1" by (cases "0 < l1 \ u1 < 0", auto) ``` hoelzl@29805 ` 2283` ``` hence "inverse (Ifloat l1) \ Ifloat u" unfolding nonzero_inverse_eq_divide[OF `Ifloat l1 \ 0`] using float_divr[of 1 l1 prec] by auto ``` hoelzl@29805 ` 2284` ``` hence "inverse (Ifloatarith a xs) \ Ifloat u" by (rule order_trans[OF inv[THEN conjunct2]]) ``` hoelzl@29805 ` 2285` ``` ultimately show ?case unfolding Ifloatarith.simps using l1 u1 by auto ``` hoelzl@29805 ` 2286` ```next ``` hoelzl@29805 ` 2287` ``` case (Abs x) ``` hoelzl@29805 ` 2288` ``` from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps ``` hoelzl@29805 ` 2289` ``` obtain l1 u1 where l': "l = (if l1 < 0 \ 0 < u1 then 0 else min \l1\ \u1\)" and u': "u = max \l1\ \u1\" ``` hoelzl@29805 ` 2290` ``` and l1: "Ifloat l1 \ Ifloatarith x xs" and u1: "Ifloatarith x xs \ Ifloat u1" by blast ``` hoelzl@29805 ` 2291` ``` thus ?case unfolding l' u' by (cases "l1 < 0 \ 0 < u1", auto simp add: Ifloat_min Ifloat_max Ifloat_abs less_float_def) ``` hoelzl@29805 ` 2292` ```next ``` hoelzl@29805 ` 2293` ``` case (Min a b) ``` hoelzl@29805 ` 2294` ``` from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps ``` hoelzl@29805 ` 2295` ``` obtain l1 u1 l2 u2 where l': "l = min l1 l2" and u': "u = min u1 u2" ``` hoelzl@29805 ` 2296` ``` and l1: "Ifloat l1 \ Ifloatarith a xs" and u1: "Ifloatarith a xs \ Ifloat u1" ``` hoelzl@29805 ` 2297` ``` and l1: "Ifloat l2 \ Ifloatarith b xs" and u1: "Ifloatarith b xs \ Ifloat u2" by blast ``` hoelzl@29805 ` 2298` ``` thus ?case unfolding l' u' by (auto simp add: Ifloat_min) ``` hoelzl@29805 ` 2299` ```next ``` hoelzl@29805 ` 2300` ``` case (Max a b) ``` hoelzl@29805 ` 2301` ``` from lift_bin'[OF Max.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Max.hyps ``` hoelzl@29805 ` 2302` ``` obtain l1 u1 l2 u2 where l': "l = max l1 l2" and u': "u = max u1 u2" ``` hoelzl@29805 ` 2303` ``` and l1: "Ifloat l1 \ Ifloatarith a xs" and u1: "Ifloatarith a xs \ Ifloat u1" ``` hoelzl@29805 ` 2304` ``` and l1: "Ifloat l2 \ Ifloatarith b xs" and u1: "Ifloatarith b xs \ Ifloat u2" by blast ``` hoelzl@29805 ` 2305` ``` thus ?case unfolding l' u' by (auto simp add: Ifloat_max) ``` hoelzl@29805 ` 2306` ```next case (Sin a) with lift_un'_bnds[OF bnds_sin] show ?case by auto ``` hoelzl@29805 ` 2307` ```next case (Cos a) with lift_un'_bnds[OF bnds_cos] show ?case by auto ``` hoelzl@29805 ` 2308` ```next case (Arctan a) with lift_un'_bnds[OF bnds_arctan] show ?case by auto ``` hoelzl@29805 ` 2309` ```next case Pi with pi_boundaries show ?case by auto ``` hoelzl@29805 ` 2310` ```next case (Sqrt a) with lift_un_bnds[OF bnds_sqrt] show ?case by auto ``` hoelzl@29805 ` 2311` ```next case (Exp a) with lift_un'_bnds[OF bnds_exp] show ?case by auto ``` hoelzl@29805 ` 2312` ```next case (Ln a) with lift_un_bnds[OF bnds_ln] show ?case by auto ``` hoelzl@29805 ` 2313` ```next case (Power a n) with lift_un'_bnds[OF bnds_power] show ?case by auto ``` hoelzl@29805 ` 2314` ```next case (Num f) thus ?case by auto ``` hoelzl@29805 ` 2315` ```next ``` hoelzl@29805 ` 2316` ``` case (Atom n) ``` hoelzl@29805 ` 2317` ``` show ?case ``` hoelzl@29805 ` 2318` ``` proof (cases "n < length vs") ``` hoelzl@29805 ` 2319` ``` case True ``` hoelzl@29805 ` 2320` ``` with Atom have "vs ! n = (l, u)" by auto ``` hoelzl@29805 ` 2321` ``` thus ?thesis using bounded_by[OF assms(1) True] by auto ``` hoelzl@29805 ` 2322` ``` next ``` hoelzl@29805 ` 2323` ``` case False thus ?thesis using Atom by auto ``` hoelzl@29805 ` 2324` ``` qed ``` hoelzl@29805 ` 2325` ```qed ``` hoelzl@29805 ` 2326` hoelzl@29805 ` 2327` ```datatype ApproxEq = Less floatarith floatarith ``` hoelzl@29805 ` 2328` ``` | LessEqual floatarith floatarith ``` hoelzl@29805 ` 2329` hoelzl@29805 ` 2330` ```fun uneq :: "ApproxEq \ real list \ bool" where ``` hoelzl@29805 ` 2331` ```"uneq (Less a b) vs = (Ifloatarith a vs < Ifloatarith b vs)" | ``` hoelzl@29805 ` 2332` ```"uneq (LessEqual a b) vs = (Ifloatarith a vs \ Ifloatarith b vs)" ``` hoelzl@29805 ` 2333` hoelzl@29805 ` 2334` ```fun uneq' :: "nat \ ApproxEq \ (float * float) list \ bool" where ``` hoelzl@29805 ` 2335` ```"uneq' prec (Less a b) bs = (case (approx prec a bs, approx prec b bs) of (Some (l, u), Some (l', u')) \ u < l' | _ \ False)" | ``` hoelzl@29805 ` 2336` ```"uneq' prec (LessEqual a b) bs = (case (approx prec a bs, approx prec b bs) of (Some (l, u), Some (l', u')) \ u \ l' | _ \ False)" ``` hoelzl@29805 ` 2337` hoelzl@29805 ` 2338` ```lemma uneq_approx: fixes m :: nat assumes "bounded_by vs bs" and "uneq' prec eq bs" ``` hoelzl@29805 ` 2339` ``` shows "uneq eq vs" ``` hoelzl@29805 ` 2340` ```proof (cases eq) ``` hoelzl@29805 ` 2341` ``` case (Less a b) ``` hoelzl@29805 ` 2342` ``` show ?thesis ``` hoelzl@29805 ` 2343` ``` proof (cases "\ u l u' l'. approx prec a bs = Some (l, u) \ ``` hoelzl@29805 ` 2344` ``` approx prec b bs = Some (l', u')") ``` hoelzl@29805 ` 2345` ``` case True ``` hoelzl@29805 ` 2346` ``` then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)" ``` hoelzl@29805 ` 2347` ``` and b_approx: "approx prec b bs = Some (l', u') " by auto ``` hoelzl@29805 ` 2348` ``` with `uneq' prec eq bs` have "Ifloat u < Ifloat l'" ``` hoelzl@29805 ` 2349` ``` unfolding Less uneq'.simps less_float_def by auto ``` hoelzl@29805 ` 2350` ``` moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs` ``` hoelzl@29805 ` 2351` ``` have "Ifloatarith a vs \ Ifloat u" and "Ifloat l' \ Ifloatarith b vs" ``` hoelzl@29805 ` 2352` ``` using approx by auto ``` hoelzl@29805 ` 2353` ``` ultimately show ?thesis unfolding uneq.simps Less by auto ``` hoelzl@29805 ` 2354` ``` next ``` hoelzl@29805 ` 2355` ``` case False ``` hoelzl@29805 ` 2356` ``` hence "approx prec a bs = None \ approx prec b bs = None" ``` hoelzl@29805 ` 2357` ``` unfolding not_Some_eq[symmetric] by auto ``` hoelzl@29805 ` 2358` ``` hence "\ uneq' prec eq bs" unfolding Less uneq'.simps ``` hoelzl@29805 ` 2359` ``` by (cases "approx prec a bs = None", auto) ``` hoelzl@29805 ` 2360` ``` thus ?thesis using assms by auto ``` hoelzl@29805 ` 2361` ``` qed ``` hoelzl@29805 ` 2362` ```next ``` hoelzl@29805 ` 2363` ``` case (LessEqual a b) ``` hoelzl@29805 ` 2364` ``` show ?thesis ``` hoelzl@29805 ` 2365` ``` proof (cases "\ u l u' l'. approx prec a bs = Some (l, u) \ ``` hoelzl@29805 ` 2366` ``` approx prec b bs = Some (l', u')") ``` hoelzl@29805 ` 2367` ``` case True ``` hoelzl@29805 ` 2368` ``` then obtain l u l' u' where a_approx: "approx prec a bs = Some (l, u)" ``` hoelzl@29805 ` 2369` ``` and b_approx: "approx prec b bs = Some (l', u') " by auto ``` hoelzl@29805 ` 2370` ``` with `uneq' prec eq bs` have "Ifloat u \ Ifloat l'" ``` hoelzl@29805 ` 2371` ``` unfolding LessEqual uneq'.simps le_float_def by auto ``` hoelzl@29805 ` 2372` ``` moreover from a_approx[symmetric] and b_approx[symmetric] and `bounded_by vs bs` ``` hoelzl@29805 ` 2373` ``` have "Ifloatarith a vs \ Ifloat u" and "Ifloat l' \ Ifloatarith b vs" ``` hoelzl@29805 ` 2374` ``` using approx by auto ``` hoelzl@29805 ` 2375` ``` ultimately show ?thesis unfolding uneq.simps LessEqual by auto ``` hoelzl@29805 ` 2376` ``` next ``` hoelzl@29805 ` 2377` ``` case False ``` hoelzl@29805 ` 2378` ``` hence "approx prec a bs = None \ approx prec b bs = None" ``` hoelzl@29805 ` 2379` ``` unfolding not_Some_eq[symmetric] by auto ``` hoelzl@29805 ` 2380` ``` hence "\ uneq' prec eq bs" unfolding LessEqual uneq'.simps ``` hoelzl@29805 ` 2381` ``` by (cases "approx prec a bs = None", auto) ``` hoelzl@29805 ` 2382` ``` thus ?thesis using assms by auto ``` hoelzl@29805 ` 2383` ``` qed ``` hoelzl@29805 ` 2384` ```qed ``` hoelzl@29805 ` 2385` hoelzl@29805 ` 2386` ```lemma Ifloatarith_divide: "Ifloatarith (Mult a (Inverse b)) vs = (Ifloatarith a vs) / (Ifloatarith b vs)" ``` hoelzl@29805 ` 2387` ``` unfolding real_divide_def Ifloatarith.simps .. ``` hoelzl@29805 ` 2388` hoelzl@29805 ` 2389` ```lemma Ifloatarith_diff: "Ifloatarith (Add a (Minus b)) vs = (Ifloatarith a vs) - (Ifloatarith b vs)" ``` hoelzl@29805 ` 2390` ``` unfolding real_diff_def Ifloatarith.simps .. ``` hoelzl@29805 ` 2391` hoelzl@29805 ` 2392` ```lemma Ifloatarith_tan: "Ifloatarith (Mult (Sin a) (Inverse (Cos a))) vs = tan (Ifloatarith a vs)" ``` hoelzl@29805 ` 2393` ``` unfolding tan_def Ifloatarith.simps real_divide_def .. ``` hoelzl@29805 ` 2394` hoelzl@29805 ` 2395` ```lemma Ifloatarith_powr: "Ifloatarith (Exp (Mult b (Ln a))) vs = (Ifloatarith a vs) powr (Ifloatarith b vs)" ``` hoelzl@29805 ` 2396` ``` unfolding powr_def Ifloatarith.simps .. ``` hoelzl@29805 ` 2397` hoelzl@29805 ` 2398` ```lemma Ifloatarith_log: "Ifloatarith ((Mult (Ln x) (Inverse (Ln b)))) vs = log (Ifloatarith b vs) (Ifloatarith x vs)" ``` hoelzl@29805 ` 2399` ``` unfolding log_def Ifloatarith.simps real_divide_def .. ``` hoelzl@29805 ` 2400` hoelzl@29805 ` 2401` ```lemma Ifloatarith_num: shows "Ifloatarith (Num (Float 0 0)) vs = 0" and "Ifloatarith (Num (Float 1 0)) vs = 1" and "Ifloatarith (Num (Float (number_of a) 0)) vs = number_of a" by auto ``` hoelzl@29805 ` 2402` hoelzl@29805 ` 2403` ```subsection {* Implement proof method \texttt{approximation} *} ``` hoelzl@29805 ` 2404` hoelzl@29805 ` 2405` ```lemma bounded_divl: assumes "Ifloat a / Ifloat b \ x" shows "Ifloat (float_divl p a b) \ x" by (rule order_trans[OF _ assms], rule float_divl) ``` hoelzl@29805 ` 2406` ```lemma bounded_divr: assumes "x \ Ifloat a / Ifloat b" shows "x \ Ifloat (float_divr p a b)" by (rule order_trans[OF assms _], rule float_divr) ``` hoelzl@29805 ` 2407` ```lemma bounded_num: shows "Ifloat (Float 5 1) = 10" and "Ifloat (Float 0 0) = 0" and "Ifloat (Float 1 0) = 1" and "Ifloat (Float (number_of n) 0) = (number_of n)" ``` hoelzl@29805 ` 2408` ``` and "0 * pow2 e = Ifloat (Float 0 e)" and "1 * pow2 e = Ifloat (Float 1 e)" and "number_of m * pow2 e = Ifloat (Float (number_of m) e)" ``` hoelzl@30443 ` 2409` ``` and "Ifloat (Float (number_of A) (int B)) = (number_of A) * 2^B" ``` hoelzl@30443 ` 2410` ``` and "Ifloat (Float 1 (int B)) = 2^B" ``` hoelzl@30443 ` 2411` ``` and "Ifloat (Float (number_of A) (- int B)) = (number_of A) / 2^B" ``` hoelzl@30443 ` 2412` ``` and "Ifloat (Float 1 (- int B)) = 1 / 2^B" ``` hoelzl@30443 ` 2413` ``` by (auto simp add: Ifloat.simps pow2_def real_divide_def) ``` hoelzl@29805 ` 2414` hoelzl@29805 ` 2415` ```lemmas bounded_by_equations = bounded_by_Cons bounded_by_Nil float_power bounded_divl bounded_divr bounded_num HOL.simp_thms ``` hoelzl@29805 ` 2416` ```lemmas uneq_equations = uneq.simps Ifloatarith.simps Ifloatarith_num Ifloatarith_divide Ifloatarith_diff Ifloatarith_tan Ifloatarith_powr Ifloatarith_log ``` hoelzl@29805 ` 2417` hoelzl@29805 ` 2418` ```ML {* ``` hoelzl@29805 ` 2419` ``` val uneq_equations = PureThy.get_thms @{theory} "uneq_equations"; ``` hoelzl@29805 ` 2420` ``` val bounded_by_equations = PureThy.get_thms @{theory} "bounded_by_equations"; ``` hoelzl@29805 ` 2421` ``` val bounded_by_simpset = (HOL_basic_ss addsimps bounded_by_equations) ``` hoelzl@29805 ` 2422` hoelzl@29805 ` 2423` ``` fun reify_uneq ctxt i = (fn st => ``` hoelzl@29805 ` 2424` ``` let ``` hoelzl@29805 ` 2425` ``` val to = HOLogic.dest_Trueprop (Logic.strip_imp_concl (List.nth (prems_of st, i - 1))) ``` hoelzl@29805 ` 2426` ``` in (Reflection.genreify_tac ctxt uneq_equations (SOME to) i) st ``` hoelzl@29805 ` 2427` ``` end) ``` hoelzl@29805 ` 2428` hoelzl@29805 ` 2429` ``` fun rule_uneq ctxt prec i thm = let ``` hoelzl@29805 ` 2430` ``` fun conv_num typ = HOLogic.dest_number #> snd #> HOLogic.mk_number typ ``` hoelzl@29805 ` 2431` ``` val to_natc = conv_num @{typ "nat"} #> Thm.cterm_of (ProofContext.theory_of ctxt) ``` hoelzl@29805 ` 2432` ``` val to_nat = conv_num @{typ "nat"} ``` hoelzl@29805 ` 2433` ``` val to_int = conv_num @{typ "int"} ``` hoelzl@30443 ` 2434` ``` fun int_to_float A = @{term "Float"} \$ to_int A \$ @{term "0 :: int"} ``` hoelzl@29805 ` 2435` hoelzl@29805 ` 2436` ``` val prec' = to_nat prec ``` hoelzl@29805 ` 2437` hoelzl@29805 ` 2438` ``` fun bot_float (Const (@{const_name "times"}, _) \$ mantisse \$ (Const (@{const_name "pow2"}, _) \$ exp)) ``` hoelzl@29805 ` 2439` ``` = @{term "Float"} \$ to_int mantisse \$ to_int exp ``` hoelzl@30443 ` 2440` ``` | bot_float (Const (@{const_name "divide"}, _) \$ mantisse \$ (@{term "power 2 :: nat \ real"} \$ exp)) ``` hoelzl@30443 ` 2441` ``` = @{term "Float"} \$ to_int mantisse \$ (@{term "uminus :: int \ int"} \$ (@{term "int :: nat \ int"} \$ to_nat exp)) ``` hoelzl@30443 ` 2442` ``` | bot_float (Const (@{const_name "times"}, _) \$ mantisse \$ (@{term "power 2 :: nat \ real"} \$ exp)) ``` hoelzl@30443 ` 2443` ``` = @{term "Float"} \$ to_int mantisse \$ (@{term "int :: nat \ int"} \$ to_nat exp) ``` hoelzl@30443 ` 2444` ``` | bot_float (Const (@{const_name "divide"}, _) \$ A \$ (@{term "power 10 :: nat \ real"} \$ exp)) ``` hoelzl@30443 ` 2445` ``` = @{term "float_divl"} \$ prec' \$ (int_to_float A) \$ (@{term "power (Float 5 1)"} \$ to_nat exp) ``` hoelzl@30443 ` 2446` ``` | bot_float (Const (@{const_name "divide"}, _) \$ A \$ B) ``` hoelzl@30443 ` 2447` ``` = @{term "float_divl"} \$ prec' \$ int_to_float A \$ int_to_float B ``` hoelzl@30443 ` 2448` ``` | bot_float (@{term "power 2 :: nat \ real"} \$ exp) ``` hoelzl@30443 ` 2449` ``` = @{term "Float 1"} \$ (@{term "int :: nat \ int"} \$ to_nat exp) ``` hoelzl@30443 ` 2450` ``` | bot_float A = int_to_float A ``` hoelzl@29805 ` 2451` hoelzl@29805 ` 2452` ``` fun top_float (Const (@{const_name "times"}, _) \$ mantisse \$ (Const (@{const_name "pow2"}, _) \$ exp)) ``` hoelzl@29805 ` 2453` ``` = @{term "Float"} \$ to_int mantisse \$ to_int exp ``` hoelzl@30443 ` 2454` ``` | top_float (Const (@{const_name "divide"}, _) \$ mantisse \$ (@{term "power 2 :: nat \ real"} \$ exp)) ``` hoelzl@30443 ` 2455` ` = @{`