replace the float datatype by a type with unique representation
authorhoelzl
Wed Apr 18 14:29:21 2012 +0200 (2012-04-18)
changeset 47599400b158f1589
parent 47598 d20bdee675dc
child 47600 e12289b5796b
replace the float datatype by a type with unique representation
src/HOL/Decision_Procs/Approximation.thy
src/HOL/Library/Float.thy
     1.1 --- a/src/HOL/Decision_Procs/Approximation.thy	Wed Apr 18 14:29:20 2012 +0200
     1.2 +++ b/src/HOL/Decision_Procs/Approximation.thy	Wed Apr 18 14:29:21 2012 +0200
     1.3 @@ -54,23 +54,9 @@
     1.4    case 0 thus ?case unfolding lb_0 ub_0 horner.simps by auto
     1.5  next
     1.6    case (Suc n)
     1.7 -  have "?lb (Suc n) j' \<le> ?horner (Suc n) j'" unfolding lb_Suc ub_Suc horner.simps real_of_float_sub diff_minus
     1.8 -  proof (rule add_mono)
     1.9 -    show "(lapprox_rat prec 1 (f j')) \<le> 1 / (f j')" using lapprox_rat[of prec 1  "f j'"] by auto
    1.10 -    from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct2] `0 \<le> real x`
    1.11 -    show "- real (x * ub n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x) \<le>
    1.12 -          - (x * horner F G n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x)"
    1.13 -      unfolding real_of_float_mult neg_le_iff_le by (rule mult_left_mono)
    1.14 -  qed
    1.15 -  moreover have "?horner (Suc n) j' \<le> ?ub (Suc n) j'" unfolding ub_Suc horner.simps real_of_float_sub diff_minus
    1.16 -  proof (rule add_mono)
    1.17 -    show "1 / (f j') \<le> (rapprox_rat prec 1 (f j'))" using rapprox_rat[of 1 "f j'" prec] by auto
    1.18 -    from Suc[where j'="Suc j'", unfolded funpow.simps comp_def f_Suc, THEN conjunct1] `0 \<le> real x`
    1.19 -    show "- (x * horner F G n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x) \<le>
    1.20 -          - real (x * lb n (F ((F ^^ j') s)) (G ((F ^^ j') s) (f j')) x)"
    1.21 -      unfolding real_of_float_mult neg_le_iff_le by (rule mult_left_mono)
    1.22 -  qed
    1.23 -  ultimately show ?case by blast
    1.24 +  thus ?case using lapprox_rat[of prec 1 "f j'"] using rapprox_rat[of 1 "f j'" prec]
    1.25 +    Suc[where j'="Suc j'"] `0 \<le> real x`
    1.26 +    by (auto intro!: add_mono mult_left_mono simp add: lb_Suc ub_Suc field_simps f_Suc)
    1.27  qed
    1.28  
    1.29  subsection "Theorems for floating point functions implementing the horner scheme"
    1.30 @@ -106,24 +92,10 @@
    1.31    shows "(lb n ((F ^^ j') s) (f j') x) \<le> (\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j)" (is "?lb") and
    1.32      "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) \<le> (ub n ((F ^^ j') s) (f j') x)" (is "?ub")
    1.33  proof -
    1.34 -  { fix x y z :: float have "x - y * z = x + - y * z"
    1.35 -      by (cases x, cases y, cases z, simp add: plus_float.simps minus_float_def times_float.simps algebra_simps)
    1.36 -  } note diff_mult_minus = this
    1.37 -
    1.38 -  { fix x :: float have "- (- x) = x" by (cases x) auto } note minus_minus = this
    1.39 -
    1.40 -  have move_minus: "(-x) = -1 * real x" by auto (* coercion "inside" is necessary *)
    1.41 -
    1.42 +  { fix x y z :: float have "x - y * z = x + - y * z" by simp } note diff_mult_minus = this
    1.43    have sum_eq: "(\<Sum>j=0..<n. (1 / (f (j' + j))) * real x ^ j) =
    1.44      (\<Sum>j = 0..<n. -1 ^ j * (1 / (f (j' + j))) * real (- x) ^ j)"
    1.45 -  proof (rule setsum_cong, simp)
    1.46 -    fix j assume "j \<in> {0 ..< n}"
    1.47 -    show "1 / (f (j' + j)) * real x ^ j = -1 ^ j * (1 / (f (j' + j))) * real (- x) ^ j"
    1.48 -      unfolding move_minus power_mult_distrib mult_assoc[symmetric]
    1.49 -      unfolding mult_commute unfolding mult_assoc[of "-1 ^ j", symmetric] power_mult_distrib[symmetric]
    1.50 -      by auto
    1.51 -  qed
    1.52 -
    1.53 +    by (auto simp add: field_simps power_mult_distrib[symmetric])
    1.54    have "0 \<le> real (-x)" using assms by auto
    1.55    from horner_bounds[where G=G and F=F and f=f and s=s and prec=prec
    1.56      and lb="\<lambda> n i k x. lb n i k (-x)" and ub="\<lambda> n i k x. ub n i k (-x)", unfolded lb_Suc ub_Suc diff_mult_minus,
    1.57 @@ -177,9 +149,11 @@
    1.58    show ?thesis
    1.59    proof (cases "0 < l")
    1.60      case True hence "odd n \<or> 0 < l" and "0 \<le> real l" unfolding less_float_def by auto
    1.61 -    have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto
    1.62 -    have "real l ^ n \<le> x ^ n" and "x ^ n \<le> real u ^ n " using `0 \<le> real l` and assms unfolding atLeastAtMost_iff using power_mono[of l x] power_mono[of x u] by auto
    1.63 -    thus ?thesis using assms `0 < l` unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
    1.64 +    have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms
    1.65 +      unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto
    1.66 +    have "real l ^ n \<le> x ^ n" and "x ^ n \<le> real u ^ n " using `0 \<le> real l` assms
    1.67 +      by (auto simp: power_mono)
    1.68 +    thus ?thesis using assms `0 < l` unfolding l1 u1 by auto
    1.69    next
    1.70      case False hence P: "\<not> (odd n \<or> 0 < l)" using `even n` by auto
    1.71      show ?thesis
    1.72 @@ -188,25 +162,25 @@
    1.73        hence "real u ^ n \<le> x ^ n" and "x ^ n \<le> real l ^ n" using power_mono[of  "-x" "-real l" n] power_mono[of "-real u" "-x" n]
    1.74          unfolding power_minus_even[OF `even n`] by auto
    1.75        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
    1.76 -      ultimately show ?thesis using float_power by auto
    1.77 +      ultimately show ?thesis by auto
    1.78      next
    1.79        case False
    1.80        have "\<bar>x\<bar> \<le> real (max (-l) u)"
    1.81        proof (cases "-l \<le> u")
    1.82 -        case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding le_float_def by auto
    1.83 +        case True thus ?thesis unfolding max_def if_P[OF True] using assms unfolding less_eq_float_def by auto
    1.84        next
    1.85 -        case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding le_float_def by auto
    1.86 +        case False thus ?thesis unfolding max_def if_not_P[OF False] using assms unfolding less_eq_float_def by auto
    1.87        qed
    1.88        hence x_abs: "\<bar>x\<bar> \<le> \<bar>real (max (-l) u)\<bar>" by auto
    1.89        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
    1.90 -      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
    1.91 +      show ?thesis unfolding atLeastAtMost_iff l1 u1 using zero_le_even_power[OF `even n`] power_mono_even[OF `even n` x_abs] by auto
    1.92      qed
    1.93    qed
    1.94  next
    1.95    case False hence "odd n \<or> 0 < l" by auto
    1.96    have u1: "u1 = u ^ n" and l1: "l1 = l ^ n" using assms unfolding float_power_bnds_def if_P[OF `odd n \<or> 0 < l`] by auto
    1.97    have "real l ^ n \<le> x ^ n" and "x ^ n \<le> real u ^ n " using assms unfolding atLeastAtMost_iff using power_mono_odd[OF False] by auto
    1.98 -  thus ?thesis unfolding atLeastAtMost_iff l1 u1 float_power less_float_def by auto
    1.99 +  thus ?thesis unfolding atLeastAtMost_iff l1 u1 less_float_def by auto
   1.100  qed
   1.101  
   1.102  lemma bnds_power: "\<forall> (x::real) l u. (l1, u1) = float_power_bnds n l u \<and> x \<in> {l .. u} \<longrightarrow> l1 \<le> x ^ n \<and> x ^ n \<le> u1"
   1.103 @@ -222,10 +196,17 @@
   1.104  *}
   1.105  
   1.106  fun sqrt_iteration :: "nat \<Rightarrow> nat \<Rightarrow> float \<Rightarrow> float" where
   1.107 -"sqrt_iteration prec 0 (Float m e) = Float 1 ((e + bitlen m) div 2 + 1)" |
   1.108 +"sqrt_iteration prec 0 x = Float 1 ((bitlen \<bar>mantissa x\<bar> + exponent x) div 2 + 1)" |
   1.109  "sqrt_iteration prec (Suc m) x = (let y = sqrt_iteration prec m x
   1.110                                    in Float 1 -1 * (y + float_divr prec x y))"
   1.111  
   1.112 +lemma compute_sqrt_iteration_base[code]:
   1.113 +  shows "sqrt_iteration prec n (Float m e) =
   1.114 +    (if n = 0 then Float 1 ((if m = 0 then 0 else bitlen \<bar>m\<bar> + e) div 2 + 1)
   1.115 +    else (let y = sqrt_iteration prec (n - 1) (Float m e) in
   1.116 +      Float 1 -1 * (y + float_divr prec (Float m e) y)))"
   1.117 +  using bitlen_Float by (cases n) simp_all
   1.118 +
   1.119  function ub_sqrt lb_sqrt :: "nat \<Rightarrow> float \<Rightarrow> float" where
   1.120  "ub_sqrt prec x = (if 0 < x then (sqrt_iteration prec prec x)
   1.121                else if x < 0 then - lb_sqrt prec (- x)
   1.122 @@ -259,23 +240,23 @@
   1.123    show ?case
   1.124    proof (cases x)
   1.125      case (Float m e)
   1.126 -    hence "0 < m" using float_pos_m_pos[unfolded less_float_def] assms by auto
   1.127 +    hence "0 < m" using assms powr_gt_zero[of 2 e] by (auto simp: sign_simps)
   1.128      hence "0 < sqrt m" by auto
   1.129  
   1.130 -    have int_nat_bl: "(nat (bitlen m)) = bitlen m" using bitlen_ge0 by auto
   1.131 -
   1.132 -    have "x = (m / 2^nat (bitlen m)) * pow2 (e + (nat (bitlen m)))"
   1.133 -      unfolding pow2_add pow2_int Float real_of_float_simp by auto
   1.134 -    also have "\<dots> < 1 * pow2 (e + nat (bitlen m))"
   1.135 +    have int_nat_bl: "(nat (bitlen m)) = bitlen m" using bitlen_nonneg by auto
   1.136 +
   1.137 +    have "x = (m / 2^nat (bitlen m)) * 2 powr (e + (nat (bitlen m)))"
   1.138 +      unfolding Float by (auto simp: powr_realpow[symmetric] field_simps powr_add)
   1.139 +    also have "\<dots> < 1 * 2 powr (e + nat (bitlen m))"
   1.140      proof (rule mult_strict_right_mono, auto)
   1.141        show "real m < 2^nat (bitlen m)" using bitlen_bounds[OF `0 < m`, THEN conjunct2]
   1.142          unfolding real_of_int_less_iff[of m, symmetric] by auto
   1.143      qed
   1.144 -    finally have "sqrt x < sqrt (pow2 (e + bitlen m))" unfolding int_nat_bl by auto
   1.145 -    also have "\<dots> \<le> pow2 ((e + bitlen m) div 2 + 1)"
   1.146 +    finally have "sqrt x < sqrt (2 powr (e + bitlen m))" unfolding int_nat_bl by auto
   1.147 +    also have "\<dots> \<le> 2 powr ((e + bitlen m) div 2 + 1)"
   1.148      proof -
   1.149        let ?E = "e + bitlen m"
   1.150 -      have E_mod_pow: "pow2 (?E mod 2) < 4"
   1.151 +      have E_mod_pow: "2 powr (?E mod 2) < 4"
   1.152        proof (cases "?E mod 2 = 1")
   1.153          case True thus ?thesis by auto
   1.154        next
   1.155 @@ -287,21 +268,23 @@
   1.156          from xt1(5)[OF `0 \<le> ?E mod 2` this]
   1.157          show ?thesis by auto
   1.158        qed
   1.159 -      hence "sqrt (pow2 (?E mod 2)) < sqrt (2 * 2)" by auto
   1.160 -      hence E_mod_pow: "sqrt (pow2 (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto
   1.161 -
   1.162 -      have E_eq: "pow2 ?E = pow2 (?E div 2 + ?E div 2 + ?E mod 2)" by auto
   1.163 -      have "sqrt (pow2 ?E) = sqrt (pow2 (?E div 2) * pow2 (?E div 2) * pow2 (?E mod 2))"
   1.164 -        unfolding E_eq unfolding pow2_add ..
   1.165 -      also have "\<dots> = pow2 (?E div 2) * sqrt (pow2 (?E mod 2))"
   1.166 -        unfolding real_sqrt_mult[of _ "pow2 (?E mod 2)"] real_sqrt_abs2 by auto
   1.167 -      also have "\<dots> < pow2 (?E div 2) * 2"
   1.168 +      hence "sqrt (2 powr (?E mod 2)) < sqrt (2 * 2)" by auto
   1.169 +      hence E_mod_pow: "sqrt (2 powr (?E mod 2)) < 2" unfolding real_sqrt_abs2 by auto
   1.170 +
   1.171 +      have E_eq: "2 powr ?E = 2 powr (?E div 2 + ?E div 2 + ?E mod 2)" by auto
   1.172 +      have "sqrt (2 powr ?E) = sqrt (2 powr (?E div 2) * 2 powr (?E div 2) * 2 powr (?E mod 2))"
   1.173 +        unfolding E_eq unfolding powr_add[symmetric] by (simp add: int_of_reals del: real_of_ints)
   1.174 +      also have "\<dots> = 2 powr (?E div 2) * sqrt (2 powr (?E mod 2))"
   1.175 +        unfolding real_sqrt_mult[of _ "2 powr (?E mod 2)"] real_sqrt_abs2 by auto
   1.176 +      also have "\<dots> < 2 powr (?E div 2) * 2 powr 1"
   1.177          by (rule mult_strict_left_mono, auto intro: E_mod_pow)
   1.178 -      also have "\<dots> = pow2 (?E div 2 + 1)" unfolding add_commute[of _ 1] pow2_add1 by auto
   1.179 +      also have "\<dots> = 2 powr (?E div 2 + 1)" unfolding add_commute[of _ 1] powr_add[symmetric]
   1.180 +        by simp
   1.181        finally show ?thesis by auto
   1.182      qed
   1.183 -    finally show ?thesis
   1.184 -      unfolding Float sqrt_iteration.simps real_of_float_simp by auto
   1.185 +    finally show ?thesis using `0 < m`
   1.186 +      unfolding Float
   1.187 +      by (subst compute_sqrt_iteration_base) (simp add: ac_simps real_Float del: Float_def)
   1.188    qed
   1.189  next
   1.190    case (Suc n)
   1.191 @@ -310,8 +293,8 @@
   1.192    also have "\<dots> < real ?b" using Suc .
   1.193    finally have "sqrt x < (?b + x / ?b)/2" using sqrt_ub_pos_pos_1[OF Suc _ `0 < real x`] by auto
   1.194    also have "\<dots> \<le> (?b + (float_divr prec x ?b))/2" by (rule divide_right_mono, auto simp add: float_divr)
   1.195 -  also have "\<dots> = (Float 1 -1) * (?b + (float_divr prec x ?b))" by auto
   1.196 -  finally show ?case unfolding sqrt_iteration.simps Let_def real_of_float_mult real_of_float_add right_distrib .
   1.197 +  also have "\<dots> = (Float 1 -1) * (?b + (float_divr prec x ?b))" by simp
   1.198 +  finally show ?case unfolding sqrt_iteration.simps Let_def right_distrib .
   1.199  qed
   1.200  
   1.201  lemma sqrt_iteration_lower_bound: assumes "0 < real x"
   1.202 @@ -325,9 +308,9 @@
   1.203  lemma lb_sqrt_lower_bound: assumes "0 \<le> real x"
   1.204    shows "0 \<le> real (lb_sqrt prec x)"
   1.205  proof (cases "0 < x")
   1.206 -  case True hence "0 < real x" and "0 \<le> x" using `0 \<le> real x` unfolding less_float_def le_float_def by auto
   1.207 +  case True hence "0 < real x" and "0 \<le> x" using `0 \<le> real x` unfolding less_float_def less_eq_float_def by auto
   1.208    hence "0 < sqrt_iteration prec prec x" unfolding less_float_def using sqrt_iteration_lower_bound by auto
   1.209 -  hence "0 \<le> real (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \<le> x`] unfolding le_float_def by auto
   1.210 +  hence "0 \<le> real (float_divl prec x (sqrt_iteration prec prec x))" using float_divl_lower_bound[OF `0 \<le> x`] unfolding less_eq_float_def by auto
   1.211    thus ?thesis unfolding lb_sqrt.simps using True by auto
   1.212  next
   1.213    case False with `0 \<le> real x` have "real x = 0" unfolding less_float_def by auto
   1.214 @@ -446,7 +429,7 @@
   1.215  
   1.216    { have "(x * lb_arctan_horner prec n 1 (x*x)) \<le> ?S n"
   1.217        using bounds(1) `0 \<le> real x`
   1.218 -      unfolding real_of_float_mult power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
   1.219 +      unfolding power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
   1.220        unfolding mult_commute[where 'a=real] mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"]
   1.221        by (auto intro!: mult_left_mono)
   1.222      also have "\<dots> \<le> arctan x" using arctan_bounds ..
   1.223 @@ -455,7 +438,7 @@
   1.224    { have "arctan x \<le> ?S (Suc n)" using arctan_bounds ..
   1.225      also have "\<dots> \<le> (x * ub_arctan_horner prec (Suc n) 1 (x*x))"
   1.226        using bounds(2)[of "Suc n"] `0 \<le> real x`
   1.227 -      unfolding real_of_float_mult power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
   1.228 +      unfolding power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
   1.229        unfolding mult_commute[where 'a=real] mult_commute[of _ "2::nat"] power_mult power2_eq_square[of "real x"]
   1.230        by (auto intro!: mult_left_mono)
   1.231      finally have "arctan x \<le> (x * ub_arctan_horner prec (Suc n) 1 (x*x))" . }
   1.232 @@ -512,8 +495,8 @@
   1.233      have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
   1.234  
   1.235      have "0 \<le> real ?k" by (rule order_trans[OF _ rapprox_rat], auto simp add: `0 \<le> k`)
   1.236 -    have "real ?k \<le> 1" unfolding rapprox_rat.simps(2)[OF zero_le_one `0 < k`]
   1.237 -      by (rule rapprox_posrat_le1, auto simp add: `0 < k` `1 \<le> k`)
   1.238 +    have "real ?k \<le> 1" 
   1.239 +      by (rule rapprox_rat_le1, auto simp add: `0 < k` `1 \<le> k`)
   1.240  
   1.241      have "1 / k \<le> ?k" using rapprox_rat[where x=1 and y=k] by auto
   1.242      hence "arctan (1 / k) \<le> arctan ?k" by (rule arctan_monotone')
   1.243 @@ -526,8 +509,7 @@
   1.244      let ?k = "lapprox_rat prec 1 k"
   1.245      have "1 div k = 0" using div_pos_pos_trivial[OF _ `1 < k`] by auto
   1.246      have "1 / k \<le> 1" using `1 < k` by auto
   1.247 -
   1.248 -    have "\<And>n. 0 \<le> real ?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`)
   1.249 +    have "\<And>n. 0 \<le> real ?k" using lapprox_rat_nonneg[where x=1 and y=k, OF zero_le_one `0 < k`] by (auto simp add: `1 div k = 0`)
   1.250      have "\<And>n. real ?k \<le> 1" using lapprox_rat by (rule order_trans, auto simp add: `1 / k \<le> 1`)
   1.251  
   1.252      have "?k \<le> 1 / k" using lapprox_rat[where x=1 and y=k] by auto
   1.253 @@ -539,14 +521,14 @@
   1.254    } note lb_arctan = this
   1.255  
   1.256    have "pi \<le> ub_pi n"
   1.257 -    unfolding ub_pi_def machin_pi Let_def real_of_float_mult real_of_float_sub unfolding Float_num
   1.258 -    using lb_arctan[of 239] ub_arctan[of 5]
   1.259 -    by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
   1.260 +    unfolding ub_pi_def machin_pi Let_def unfolding Float_num
   1.261 +    using lb_arctan[of 239] ub_arctan[of 5] powr_realpow[of 2 2]
   1.262 +    by (auto intro!: mult_left_mono add_mono simp add: diff_minus)
   1.263    moreover
   1.264    have "lb_pi n \<le> pi"
   1.265 -    unfolding lb_pi_def machin_pi Let_def real_of_float_mult real_of_float_sub Float_num
   1.266 -    using lb_arctan[of 5] ub_arctan[of 239]
   1.267 -    by (auto intro!: mult_left_mono add_mono simp add: diff_minus simp del: lapprox_rat.simps rapprox_rat.simps)
   1.268 +    unfolding lb_pi_def machin_pi Let_def Float_num
   1.269 +    using lb_arctan[of 5] ub_arctan[of 239] powr_realpow[of 2 2]
   1.270 +    by (auto intro!: mult_left_mono add_mono simp add: diff_minus)
   1.271    ultimately show ?thesis by auto
   1.272  qed
   1.273  
   1.274 @@ -579,17 +561,17 @@
   1.275  lemma lb_arctan_bound': assumes "0 \<le> real x"
   1.276    shows "lb_arctan prec x \<le> arctan x"
   1.277  proof -
   1.278 -  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> real x` by auto
   1.279 +  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def less_eq_float_def using `0 \<le> real x` by auto
   1.280    let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
   1.281      and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
   1.282  
   1.283    show ?thesis
   1.284    proof (cases "x \<le> Float 1 -1")
   1.285 -    case True hence "real x \<le> 1" unfolding le_float_def Float_num by auto
   1.286 +    case True hence "real x \<le> 1" unfolding less_eq_float_def Float_num by auto
   1.287      show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
   1.288        using arctan_0_1_bounds[OF `0 \<le> real x` `real x \<le> 1`] by auto
   1.289    next
   1.290 -    case False hence "0 < real x" unfolding le_float_def Float_num by auto
   1.291 +    case False hence "0 < real x" unfolding less_eq_float_def Float_num by auto
   1.292      let ?R = "1 + sqrt (1 + real x * real x)"
   1.293      let ?fR = "1 + ub_sqrt prec (1 + x * x)"
   1.294      let ?DIV = "float_divl prec x ?fR"
   1.295 @@ -621,9 +603,9 @@
   1.296        moreover have "?DIV \<le> real x / ?fR" by (rule float_divl)
   1.297        ultimately have "real ?DIV \<le> 1" unfolding divide_le_eq_1_pos[OF `0 < real ?fR`, symmetric] by auto
   1.298  
   1.299 -      have "0 \<le> real ?DIV" using float_divl_lower_bound[OF `0 \<le> x` `0 < ?fR`] unfolding le_float_def by auto
   1.300 -
   1.301 -      have "(Float 1 1 * ?lb_horner ?DIV) \<le> 2 * arctan (float_divl prec x ?fR)" unfolding real_of_float_mult[of "Float 1 1"] Float_num
   1.302 +      have "0 \<le> real ?DIV" using float_divl_lower_bound[OF `0 \<le> x` `0 < ?fR`] unfolding less_eq_float_def by auto
   1.303 +
   1.304 +      have "(Float 1 1 * ?lb_horner ?DIV) \<le> 2 * arctan (float_divl prec x ?fR)"
   1.305          using arctan_0_1_bounds[OF `0 \<le> real ?DIV` `real ?DIV \<le> 1`] by auto
   1.306        also have "\<dots> \<le> 2 * arctan (x / ?R)"
   1.307          using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
   1.308 @@ -631,7 +613,7 @@
   1.309        finally show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF True] .
   1.310      next
   1.311        case False
   1.312 -      hence "2 < real x" unfolding le_float_def Float_num by auto
   1.313 +      hence "2 < real x" unfolding less_eq_float_def Float_num by auto
   1.314        hence "1 \<le> real x" by auto
   1.315  
   1.316        let "?invx" = "float_divr prec 1 x"
   1.317 @@ -649,13 +631,14 @@
   1.318  
   1.319          have "1 / x \<noteq> 0" and "0 < 1 / x" using `0 < real x` by auto
   1.320  
   1.321 -        have "arctan (1 / x) \<le> arctan ?invx" unfolding real_of_float_1[symmetric] by (rule arctan_monotone', rule float_divr)
   1.322 +        have "arctan (1 / x) \<le> arctan ?invx" unfolding real_of_float_one[symmetric] by (rule arctan_monotone', rule float_divr)
   1.323          also have "\<dots> \<le> (?ub_horner ?invx)" using arctan_0_1_bounds[OF `0 \<le> real ?invx` `real ?invx \<le> 1`] by auto
   1.324          finally have "pi / 2 - (?ub_horner ?invx) \<le> arctan x"
   1.325            using `0 \<le> arctan x` arctan_inverse[OF `1 / x \<noteq> 0`]
   1.326            unfolding real_sgn_pos[OF `0 < 1 / real x`] le_diff_eq by auto
   1.327          moreover
   1.328 -        have "lb_pi prec * Float 1 -1 \<le> pi / 2" unfolding real_of_float_mult Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto
   1.329 +        have "lb_pi prec * Float 1 -1 \<le> pi / 2"
   1.330 +          unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by simp
   1.331          ultimately
   1.332          show ?thesis unfolding lb_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF `\<not> x \<le> Float 1 1`] if_not_P[OF False]
   1.333            by auto
   1.334 @@ -667,18 +650,18 @@
   1.335  lemma ub_arctan_bound': assumes "0 \<le> real x"
   1.336    shows "arctan x \<le> ub_arctan prec x"
   1.337  proof -
   1.338 -  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def le_float_def using `0 \<le> real x` by auto
   1.339 +  have "\<not> x < 0" and "0 \<le> x" unfolding less_float_def less_eq_float_def using `0 \<le> real x` by auto
   1.340  
   1.341    let "?ub_horner x" = "x * ub_arctan_horner prec (get_odd (prec div 4 + 1)) 1 (x * x)"
   1.342      and "?lb_horner x" = "x * lb_arctan_horner prec (get_even (prec div 4 + 1)) 1 (x * x)"
   1.343  
   1.344    show ?thesis
   1.345    proof (cases "x \<le> Float 1 -1")
   1.346 -    case True hence "real x \<le> 1" unfolding le_float_def Float_num by auto
   1.347 +    case True hence "real x \<le> 1" unfolding less_eq_float_def Float_num by auto
   1.348      show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_P[OF True]
   1.349        using arctan_0_1_bounds[OF `0 \<le> real x` `real x \<le> 1`] by auto
   1.350    next
   1.351 -    case False hence "0 < real x" unfolding le_float_def Float_num by auto
   1.352 +    case False hence "0 < real x" unfolding less_eq_float_def Float_num by auto
   1.353      let ?R = "1 + sqrt (1 + real x * real x)"
   1.354      let ?fR = "1 + lb_sqrt prec (1 + x * x)"
   1.355      let ?DIV = "float_divr prec x ?fR"
   1.356 @@ -691,7 +674,7 @@
   1.357      have "lb_sqrt prec (1 + x * x) \<le> sqrt (1 + x * x)"
   1.358        using bnds_sqrt'[of "1 + x * x"] by auto
   1.359      hence "?fR \<le> ?R" by auto
   1.360 -    have "0 < real ?fR" unfolding real_of_float_add real_of_float_1 by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \<le> real (1 + x*x)`])
   1.361 +    have "0 < real ?fR" by (rule order_less_le_trans[OF zero_less_one], auto simp add: lb_sqrt_lower_bound[OF `0 \<le> real (1 + x*x)`])
   1.362  
   1.363      have monotone: "x / ?R \<le> (float_divr prec x ?fR)"
   1.364      proof -
   1.365 @@ -707,7 +690,7 @@
   1.366        show ?thesis
   1.367        proof (cases "?DIV > 1")
   1.368          case True
   1.369 -        have "pi / 2 \<le> ub_pi prec * Float 1 -1" unfolding real_of_float_mult Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto
   1.370 +        have "pi / 2 \<le> ub_pi prec * Float 1 -1" unfolding Float_num times_divide_eq_right mult_1_left using pi_boundaries by auto
   1.371          from order_less_le_trans[OF arctan_ubound this, THEN less_imp_le]
   1.372          show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_P[OF True] .
   1.373        next
   1.374 @@ -720,13 +703,13 @@
   1.375          have "arctan x = 2 * arctan (x / ?R)" using arctan_half unfolding numeral_2_eq_2 power_Suc2 power_0 mult_1_left .
   1.376          also have "\<dots> \<le> 2 * arctan (?DIV)"
   1.377            using arctan_monotone'[OF monotone] by (auto intro!: mult_left_mono)
   1.378 -        also have "\<dots> \<le> (Float 1 1 * ?ub_horner ?DIV)" unfolding real_of_float_mult[of "Float 1 1"] Float_num
   1.379 +        also have "\<dots> \<le> (Float 1 1 * ?ub_horner ?DIV)" unfolding Float_num
   1.380            using arctan_0_1_bounds[OF `0 \<le> real ?DIV` `real ?DIV \<le> 1`] by auto
   1.381          finally show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x \<le> Float 1 -1`] if_P[OF `x \<le> Float 1 1`] if_not_P[OF False] .
   1.382        qed
   1.383      next
   1.384        case False
   1.385 -      hence "2 < real x" unfolding le_float_def Float_num by auto
   1.386 +      hence "2 < real x" unfolding less_eq_float_def Float_num by auto
   1.387        hence "1 \<le> real x" by auto
   1.388        hence "0 < real x" by auto
   1.389        hence "0 < x" unfolding less_float_def by auto
   1.390 @@ -735,17 +718,17 @@
   1.391        have "0 \<le> arctan x" using arctan_monotone'[OF `0 \<le> real x`] using arctan_tan[of 0, unfolded tan_zero] by auto
   1.392  
   1.393        have "real ?invx \<le> 1" unfolding less_float_def by (rule order_trans[OF float_divl], auto simp add: `1 \<le> real x` divide_le_eq_1_pos[OF `0 < real x`])
   1.394 -      have "0 \<le> real ?invx" unfolding real_of_float_0[symmetric] by (rule float_divl_lower_bound[unfolded le_float_def], auto simp add: `0 < x`)
   1.395 +      have "0 \<le> real ?invx" by (rule float_divl_lower_bound[unfolded less_eq_float_def], auto simp add: `0 < x`)
   1.396  
   1.397        have "1 / x \<noteq> 0" and "0 < 1 / x" using `0 < real x` by auto
   1.398  
   1.399        have "(?lb_horner ?invx) \<le> arctan (?invx)" using arctan_0_1_bounds[OF `0 \<le> real ?invx` `real ?invx \<le> 1`] by auto
   1.400 -      also have "\<dots> \<le> arctan (1 / x)" unfolding real_of_float_1[symmetric] by (rule arctan_monotone', rule float_divl)
   1.401 +      also have "\<dots> \<le> arctan (1 / x)" unfolding real_of_float_one[symmetric] by (rule arctan_monotone', rule float_divl)
   1.402        finally have "arctan x \<le> pi / 2 - (?lb_horner ?invx)"
   1.403          using `0 \<le> arctan x` arctan_inverse[OF `1 / x \<noteq> 0`]
   1.404          unfolding real_sgn_pos[OF `0 < 1 / x`] le_diff_eq by auto
   1.405        moreover
   1.406 -      have "pi / 2 \<le> ub_pi prec * Float 1 -1" unfolding real_of_float_mult Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto
   1.407 +      have "pi / 2 \<le> ub_pi prec * Float 1 -1" unfolding Float_num times_divide_eq_right mult_1_right using pi_boundaries by auto
   1.408        ultimately
   1.409        show ?thesis unfolding ub_arctan.simps Let_def if_not_P[OF `\<not> x < 0`]if_not_P[OF `\<not> x \<le> Float 1 -1`] if_not_P[OF False]
   1.410          by auto
   1.411 @@ -756,15 +739,16 @@
   1.412  lemma arctan_boundaries:
   1.413    "arctan x \<in> {(lb_arctan prec x) .. (ub_arctan prec x)}"
   1.414  proof (cases "0 \<le> x")
   1.415 -  case True hence "0 \<le> real x" unfolding le_float_def by auto
   1.416 +  case True hence "0 \<le> real x" unfolding less_eq_float_def by auto
   1.417    show ?thesis using ub_arctan_bound'[OF `0 \<le> real x`] lb_arctan_bound'[OF `0 \<le> real x`] unfolding atLeastAtMost_iff by auto
   1.418  next
   1.419    let ?mx = "-x"
   1.420 -  case False hence "x < 0" and "0 \<le> real ?mx" unfolding le_float_def less_float_def by auto
   1.421 +  case False hence "x < 0" and "0 \<le> real ?mx" unfolding less_eq_float_def less_float_def by auto
   1.422    hence bounds: "lb_arctan prec ?mx \<le> arctan ?mx \<and> arctan ?mx \<le> ub_arctan prec ?mx"
   1.423      using ub_arctan_bound'[OF `0 \<le> real ?mx`] lb_arctan_bound'[OF `0 \<le> real ?mx`] by auto
   1.424    show ?thesis unfolding real_of_float_minus arctan_minus lb_arctan.simps[where x=x] ub_arctan.simps[where x=x] Let_def if_P[OF `x < 0`]
   1.425 -    unfolding atLeastAtMost_iff using bounds[unfolded real_of_float_minus arctan_minus] by auto
   1.426 +    unfolding atLeastAtMost_iff using bounds[unfolded real_of_float_minus arctan_minus]
   1.427 +    by (simp add: arctan_minus)
   1.428  qed
   1.429  
   1.430  lemma bnds_arctan: "\<forall> (x::real) lx ux. (l, u) = (lb_arctan prec lx, ub_arctan prec ux) \<and> x \<in> {lx .. ux} \<longrightarrow> l \<le> arctan x \<and> arctan x \<le> u"
   1.431 @@ -800,7 +784,7 @@
   1.432    shows "(lb_sin_cos_aux prec n 1 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x ^(2 * i))" (is "?lb")
   1.433    and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x^(2 * i)) \<le> (ub_sin_cos_aux prec n 1 1 (x * x))" (is "?ub")
   1.434  proof -
   1.435 -  have "0 \<le> real (x * x)" unfolding real_of_float_mult by auto
   1.436 +  have "0 \<le> real (x * x)" by auto
   1.437    let "?f n" = "fact (2 * n)"
   1.438  
   1.439    { fix n
   1.440 @@ -818,7 +802,7 @@
   1.441  proof (cases "real x = 0")
   1.442    case False hence "real x \<noteq> 0" by auto
   1.443    hence "0 < x" and "0 < real x" using `0 \<le> real x` unfolding less_float_def by auto
   1.444 -  have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_mult real_of_float_0
   1.445 +  have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_zero
   1.446      using mult_pos_pos[where a="real x" and b="real x"] by auto
   1.447  
   1.448    { fix x n have "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i))) * x ^ (2 * i))
   1.449 @@ -893,7 +877,7 @@
   1.450      hence "get_even n = 0" by auto
   1.451      have "- (pi / 2) \<le> x" by (rule order_trans[OF _ `0 < real x`[THEN less_imp_le]], auto)
   1.452      with `x \<le> pi / 2`
   1.453 -    show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps real_of_float_minus real_of_float_0 using cos_ge_zero by auto
   1.454 +    show ?thesis unfolding `get_even n = 0` lb_sin_cos_aux.simps real_of_float_minus real_of_float_zero using cos_ge_zero by auto
   1.455    qed
   1.456    ultimately show ?thesis by auto
   1.457  next
   1.458 @@ -901,7 +885,9 @@
   1.459    show ?thesis
   1.460    proof (cases "n = 0")
   1.461      case True
   1.462 -    thus ?thesis unfolding `n = 0` get_even_def get_odd_def using `real x = 0` lapprox_rat[where x="-1" and y=1] by auto
   1.463 +    thus ?thesis unfolding `n = 0` get_even_def get_odd_def
   1.464 +      using `real x = 0` lapprox_rat[where x="-1" and y=1]
   1.465 +      by (auto simp: compute_lapprox_rat compute_rapprox_rat)
   1.466    next
   1.467      case False with not0_implies_Suc obtain m where "n = Suc m" by blast
   1.468      thus ?thesis unfolding `n = Suc m` get_even_def get_odd_def using `real x = 0` rapprox_rat[where x=1 and y=1] lapprox_rat[where x=1 and y=1] by (cases "even (Suc m)", auto)
   1.469 @@ -912,7 +898,7 @@
   1.470    shows "(x * lb_sin_cos_aux prec n 2 1 (x * x)) \<le> (\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * x^(2 * i + 1))" (is "?lb")
   1.471    and "(\<Sum> i=0..<n. -1^i * (1/real (fact (2 * i + 1))) * x^(2 * i + 1)) \<le> (x * ub_sin_cos_aux prec n 2 1 (x * x))" (is "?ub")
   1.472  proof -
   1.473 -  have "0 \<le> real (x * x)" unfolding real_of_float_mult by auto
   1.474 +  have "0 \<le> real (x * x)" by auto
   1.475    let "?f n" = "fact (2 * n + 1)"
   1.476  
   1.477    { fix n
   1.478 @@ -922,7 +908,7 @@
   1.479  
   1.480    from horner_bounds[where lb="lb_sin_cos_aux prec" and ub="ub_sin_cos_aux prec" and j'=0,
   1.481      OF `0 \<le> real (x * x)` f_eq lb_sin_cos_aux.simps ub_sin_cos_aux.simps]
   1.482 -  show "?lb" and "?ub" using `0 \<le> real x` unfolding real_of_float_mult
   1.483 +  show "?lb" and "?ub" using `0 \<le> real x`
   1.484      unfolding power_add power_one_right mult_assoc[symmetric] setsum_left_distrib[symmetric]
   1.485      unfolding mult_commute[where 'a=real]
   1.486      by (auto intro!: mult_left_mono simp add: power_mult power2_eq_square[of "real x"])
   1.487 @@ -933,7 +919,7 @@
   1.488  proof (cases "real x = 0")
   1.489    case False hence "real x \<noteq> 0" by auto
   1.490    hence "0 < x" and "0 < real x" using `0 \<le> real x` unfolding less_float_def by auto
   1.491 -  have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_mult real_of_float_0
   1.492 +  have "0 < x * x" using `0 < x` unfolding less_float_def real_of_float_zero
   1.493      using mult_pos_pos[where a="real x" and b="real x"] by auto
   1.494  
   1.495    { fix x n have "(\<Sum> j = 0 ..< n. -1 ^ (((2 * j + 1) - Suc 0) div 2) / (real (fact (2 * j + 1))) * x ^(2 * j + 1))
   1.496 @@ -1006,7 +992,7 @@
   1.497      case False
   1.498      hence "get_even n = 0" by auto
   1.499      with `x \<le> pi / 2` `0 \<le> real x`
   1.500 -    show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps real_of_float_minus real_of_float_0 using sin_ge_zero by auto
   1.501 +    show ?thesis unfolding `get_even n = 0` ub_sin_cos_aux.simps real_of_float_minus using sin_ge_zero by auto
   1.502    qed
   1.503    ultimately show ?thesis by auto
   1.504  next
   1.505 @@ -1065,7 +1051,7 @@
   1.506      case False
   1.507      { fix y x :: float let ?x2 = "(x * Float 1 -1)"
   1.508        assume "y \<le> cos ?x2" and "-pi \<le> x" and "x \<le> pi"
   1.509 -      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding real_of_float_mult Float_num by auto
   1.510 +      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto
   1.511        hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
   1.512  
   1.513        have "(?lb_half y) \<le> cos x"
   1.514 @@ -1077,14 +1063,14 @@
   1.515          from mult_mono[OF `y \<le> cos ?x2` `y \<le> cos ?x2` `0 \<le> cos ?x2` this]
   1.516          have "real y * real y \<le> cos ?x2 * cos ?x2" .
   1.517          hence "2 * real y * real y \<le> 2 * cos ?x2 * cos ?x2" by auto
   1.518 -        hence "2 * real y * real y - 1 \<le> 2 * cos (x / 2) * cos (x / 2) - 1" unfolding Float_num real_of_float_mult by auto
   1.519 -        thus ?thesis unfolding if_not_P[OF False] x_half Float_num real_of_float_mult real_of_float_sub by auto
   1.520 +        hence "2 * real y * real y - 1 \<le> 2 * cos (x / 2) * cos (x / 2) - 1" unfolding Float_num by auto
   1.521 +        thus ?thesis unfolding if_not_P[OF False] x_half Float_num by auto
   1.522        qed
   1.523      } note lb_half = this
   1.524  
   1.525      { fix y x :: float let ?x2 = "(x * Float 1 -1)"
   1.526        assume ub: "cos ?x2 \<le> y" and "- pi \<le> x" and "x \<le> pi"
   1.527 -      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding real_of_float_mult Float_num by auto
   1.528 +      hence "- (pi / 2) \<le> ?x2" and "?x2 \<le> pi / 2" using pi_ge_two unfolding Float_num by auto
   1.529        hence "0 \<le> cos ?x2" by (rule cos_ge_zero)
   1.530  
   1.531        have "cos x \<le> (?ub_half y)"
   1.532 @@ -1093,8 +1079,8 @@
   1.533          from mult_mono[OF ub ub this `0 \<le> cos ?x2`]
   1.534          have "cos ?x2 * cos ?x2 \<le> real y * real y" .
   1.535          hence "2 * cos ?x2 * cos ?x2 \<le> 2 * real y * real y" by auto
   1.536 -        hence "2 * cos (x / 2) * cos (x / 2) - 1 \<le> 2 * real y * real y - 1" unfolding Float_num real_of_float_mult by auto
   1.537 -        thus ?thesis unfolding x_half real_of_float_mult Float_num real_of_float_sub by auto
   1.538 +        hence "2 * cos (x / 2) * cos (x / 2) - 1 \<le> 2 * real y * real y - 1" unfolding Float_num by auto
   1.539 +        thus ?thesis unfolding x_half Float_num by auto
   1.540        qed
   1.541      } note ub_half = this
   1.542  
   1.543 @@ -1106,7 +1092,7 @@
   1.544      show ?thesis
   1.545      proof (cases "x < 1")
   1.546        case True hence "real x \<le> 1" unfolding less_float_def by auto
   1.547 -      have "0 \<le> real ?x2" and "?x2 \<le> pi / 2" using pi_ge_two `0 \<le> real x` unfolding real_of_float_mult Float_num using assms by auto
   1.548 +      have "0 \<le> real ?x2" and "?x2 \<le> pi / 2" using pi_ge_two `0 \<le> real x` using assms by auto
   1.549        from cos_boundaries[OF this]
   1.550        have lb: "(?lb_horner ?x2) \<le> ?cos ?x2" and ub: "?cos ?x2 \<le> (?ub_horner ?x2)" by auto
   1.551  
   1.552 @@ -1123,21 +1109,21 @@
   1.553        ultimately show ?thesis by auto
   1.554      next
   1.555        case False
   1.556 -      have "0 \<le> real ?x4" and "?x4 \<le> pi / 2" using pi_ge_two `0 \<le> real x` `x \<le> pi` unfolding real_of_float_mult Float_num by auto
   1.557 +      have "0 \<le> real ?x4" and "?x4 \<le> pi / 2" using pi_ge_two `0 \<le> real x` `x \<le> pi` unfolding Float_num by auto
   1.558        from cos_boundaries[OF this]
   1.559        have lb: "(?lb_horner ?x4) \<le> ?cos ?x4" and ub: "?cos ?x4 \<le> (?ub_horner ?x4)" by auto
   1.560  
   1.561 -      have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by (cases x, auto simp add: times_float.simps)
   1.562 +      have eq_4: "?x2 * Float 1 -1 = x * Float 1 -2" by simp
   1.563  
   1.564        have "(?lb x) \<le> ?cos x"
   1.565        proof -
   1.566 -        have "-pi \<le> ?x2" and "?x2 \<le> pi" unfolding real_of_float_mult Float_num using pi_ge_two `0 \<le> real x` `x \<le> pi` by auto
   1.567 +        have "-pi \<le> ?x2" and "?x2 \<le> pi" using pi_ge_two `0 \<le> real x` `x \<le> pi` by auto
   1.568          from lb_half[OF lb_half[OF lb this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4]
   1.569          show ?thesis unfolding lb_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def .
   1.570        qed
   1.571        moreover have "?cos x \<le> (?ub x)"
   1.572        proof -
   1.573 -        have "-pi \<le> ?x2" and "?x2 \<le> pi" unfolding real_of_float_mult Float_num using pi_ge_two `0 \<le> real x` ` x \<le> pi` by auto
   1.574 +        have "-pi \<le> ?x2" and "?x2 \<le> pi" using pi_ge_two `0 \<le> real x` ` x \<le> pi` by auto
   1.575          from ub_half[OF ub_half[OF ub this] `-pi \<le> x` `x \<le> pi`, unfolded eq_4]
   1.576          show ?thesis unfolding ub_cos_def[where x=x] if_not_P[OF `\<not> x < 0`] if_not_P[OF `\<not> x < Float 1 -1`] if_not_P[OF `\<not> x < 1`] Let_def .
   1.577        qed
   1.578 @@ -1155,8 +1141,8 @@
   1.579  
   1.580  definition bnds_cos :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float * float" where
   1.581  "bnds_cos prec lx ux = (let
   1.582 -    lpi = round_down prec (lb_pi prec) ;
   1.583 -    upi = round_up prec (ub_pi prec) ;
   1.584 +    lpi = float_round_down prec (lb_pi prec) ;
   1.585 +    upi = float_round_up prec (ub_pi prec) ;
   1.586      k = floor_fl (float_divr prec (lx + lpi) (2 * lpi)) ;
   1.587      lx = lx - k * 2 * (if k < 0 then lpi else upi) ;
   1.588      ux = ux - k * 2 * (if k < 0 then upi else lpi)
   1.589 @@ -1169,14 +1155,7 @@
   1.590  
   1.591  lemma floor_int:
   1.592    obtains k :: int where "real k = (floor_fl f)"
   1.593 -proof -
   1.594 -  assume *: "\<And> k :: int. real k = (floor_fl f) \<Longrightarrow> thesis"
   1.595 -  obtain m e where fl: "Float m e = floor_fl f" by (cases "floor_fl f", auto)
   1.596 -  from floor_pos_exp[OF this]
   1.597 -  have "real (m* 2^(nat e)) = (floor_fl f)"
   1.598 -    by (auto simp add: fl[symmetric] real_of_float_def pow2_def)
   1.599 -  from *[OF this] show thesis by blast
   1.600 -qed
   1.601 +  by (simp add: floor_fl_def)
   1.602  
   1.603  lemma cos_periodic_nat[simp]: fixes n :: nat shows "cos (x + n * (2 * pi)) = cos x"
   1.604  proof (induct n arbitrary: x)
   1.605 @@ -1203,8 +1182,8 @@
   1.606    fix x :: real fix lx ux
   1.607    assume bnds: "(l, u) = bnds_cos prec lx ux" and x: "x \<in> {lx .. ux}"
   1.608  
   1.609 -  let ?lpi = "round_down prec (lb_pi prec)"
   1.610 -  let ?upi = "round_up prec (ub_pi prec)"
   1.611 +  let ?lpi = "float_round_down prec (lb_pi prec)"
   1.612 +  let ?upi = "float_round_up prec (ub_pi prec)"
   1.613    let ?k = "floor_fl (float_divr prec (lx + ?lpi) (2 * ?lpi))"
   1.614    let ?lx = "lx - ?k * 2 * (if ?k < 0 then ?lpi else ?upi)"
   1.615    let ?ux = "ux - ?k * 2 * (if ?k < 0 then ?upi else ?lpi)"
   1.616 @@ -1212,24 +1191,27 @@
   1.617    obtain k :: int where k: "k = real ?k" using floor_int .
   1.618  
   1.619    have upi: "pi \<le> ?upi" and lpi: "?lpi \<le> pi"
   1.620 -    using round_up[of "ub_pi prec" prec] pi_boundaries[of prec]
   1.621 -          round_down[of prec "lb_pi prec"] by auto
   1.622 +    using float_round_up[of "ub_pi prec" prec] pi_boundaries[of prec]
   1.623 +          float_round_down[of prec "lb_pi prec"] by auto
   1.624    hence "?lx \<le> x - k * (2 * pi) \<and> x - k * (2 * pi) \<le> ?ux"
   1.625 -    using x by (cases "k = 0") (auto intro!: add_mono
   1.626 -                simp add: diff_minus k[symmetric] less_float_def)
   1.627 +    using x unfolding k[symmetric]
   1.628 +    by (cases "k = 0")
   1.629 +       (auto intro!: add_mono
   1.630 +                simp add: diff_minus k[symmetric] less_float_def
   1.631 +                simp del: float_of_numeral)
   1.632    note lx = this[THEN conjunct1] and ux = this[THEN conjunct2]
   1.633    hence lx_less_ux: "?lx \<le> real ?ux" by (rule order_trans)
   1.634  
   1.635    { assume "- ?lpi \<le> ?lx" and x_le_0: "x - k * (2 * pi) \<le> 0"
   1.636      with lpi[THEN le_imp_neg_le] lx
   1.637      have pi_lx: "- pi \<le> ?lx" and lx_0: "real ?lx \<le> 0"
   1.638 -      by (simp_all add: le_float_def)
   1.639 +      by (simp_all add: less_eq_float_def)
   1.640  
   1.641      have "(lb_cos prec (- ?lx)) \<le> cos (real (- ?lx))"
   1.642        using lb_cos_minus[OF pi_lx lx_0] by simp
   1.643      also have "\<dots> \<le> cos (x + (-k) * (2 * pi))"
   1.644        using cos_monotone_minus_pi_0'[OF pi_lx lx x_le_0]
   1.645 -      by (simp only: real_of_float_minus real_of_int_minus
   1.646 +      by (simp only: real_of_float_uminus real_of_int_minus
   1.647          cos_minus diff_minus mult_minus_left)
   1.648      finally have "(lb_cos prec (- ?lx)) \<le> cos x"
   1.649        unfolding cos_periodic_int . }
   1.650 @@ -1238,11 +1220,11 @@
   1.651    { assume "0 \<le> ?lx" and pi_x: "x - k * (2 * pi) \<le> pi"
   1.652      with lx
   1.653      have pi_lx: "?lx \<le> pi" and lx_0: "0 \<le> real ?lx"
   1.654 -      by (auto simp add: le_float_def)
   1.655 +      by (auto simp add: less_eq_float_def)
   1.656  
   1.657      have "cos (x + (-k) * (2 * pi)) \<le> cos ?lx"
   1.658        using cos_monotone_0_pi'[OF lx_0 lx pi_x]
   1.659 -      by (simp only: real_of_float_minus real_of_int_minus
   1.660 +      by (simp only: real_of_int_minus
   1.661          cos_minus diff_minus mult_minus_left)
   1.662      also have "\<dots> \<le> (ub_cos prec ?lx)"
   1.663        using lb_cos[OF lx_0 pi_lx] by simp
   1.664 @@ -1253,11 +1235,11 @@
   1.665    { assume pi_x: "- pi \<le> x - k * (2 * pi)" and "?ux \<le> 0"
   1.666      with ux
   1.667      have pi_ux: "- pi \<le> ?ux" and ux_0: "real ?ux \<le> 0"
   1.668 -      by (simp_all add: le_float_def)
   1.669 +      by (simp_all add: less_eq_float_def)
   1.670  
   1.671      have "cos (x + (-k) * (2 * pi)) \<le> cos (real (- ?ux))"
   1.672        using cos_monotone_minus_pi_0'[OF pi_x ux ux_0]
   1.673 -      by (simp only: real_of_float_minus real_of_int_minus
   1.674 +      by (simp only: real_of_float_uminus real_of_int_minus
   1.675            cos_minus diff_minus mult_minus_left)
   1.676      also have "\<dots> \<le> (ub_cos prec (- ?ux))"
   1.677        using lb_cos_minus[OF pi_ux ux_0, of prec] by simp
   1.678 @@ -1268,13 +1250,13 @@
   1.679    { assume "?ux \<le> ?lpi" and x_ge_0: "0 \<le> x - k * (2 * pi)"
   1.680      with lpi ux
   1.681      have pi_ux: "?ux \<le> pi" and ux_0: "0 \<le> real ?ux"
   1.682 -      by (simp_all add: le_float_def)
   1.683 +      by (simp_all add: less_eq_float_def)
   1.684  
   1.685      have "(lb_cos prec ?ux) \<le> cos ?ux"
   1.686        using lb_cos[OF ux_0 pi_ux] by simp
   1.687      also have "\<dots> \<le> cos (x + (-k) * (2 * pi))"
   1.688        using cos_monotone_0_pi'[OF x_ge_0 ux pi_ux]
   1.689 -      by (simp only: real_of_float_minus real_of_int_minus
   1.690 +      by (simp only: real_of_int_minus
   1.691          cos_minus diff_minus mult_minus_left)
   1.692      finally have "(lb_cos prec ?ux) \<le> cos x"
   1.693        unfolding cos_periodic_int . }
   1.694 @@ -1290,7 +1272,7 @@
   1.695      from True lpi[THEN le_imp_neg_le] lx ux
   1.696      have "- pi \<le> x - k * (2 * pi)"
   1.697        and "x - k * (2 * pi) \<le> 0"
   1.698 -      by (auto simp add: le_float_def)
   1.699 +      by (auto simp add: less_eq_float_def)
   1.700      with True negative_ux negative_lx
   1.701      show ?thesis unfolding l u by simp
   1.702    next case False note 1 = this show ?thesis
   1.703 @@ -1303,7 +1285,7 @@
   1.704      from True lpi lx ux
   1.705      have "0 \<le> x - k * (2 * pi)"
   1.706        and "x - k * (2 * pi) \<le> pi"
   1.707 -      by (auto simp add: le_float_def)
   1.708 +      by (auto simp add: less_eq_float_def)
   1.709      with True positive_ux positive_lx
   1.710      show ?thesis unfolding l u by simp
   1.711    next case False note 2 = this show ?thesis
   1.712 @@ -1314,7 +1296,8 @@
   1.713        by (auto simp add: bnds_cos_def Let_def)
   1.714  
   1.715      show ?thesis unfolding u l using negative_lx positive_ux Cond
   1.716 -      by (cases "x - k * (2 * pi) < 0", simp_all add: real_of_float_min)
   1.717 +      by (cases "x - k * (2 * pi) < 0") (auto simp add: real_of_float_min)
   1.718 +
   1.719    next case False note 3 = this show ?thesis
   1.720    proof (cases "0 \<le> ?lx \<and> ?ux \<le> 2 * ?lpi")
   1.721      case True note Cond = this with bnds 1 2 3
   1.722 @@ -1331,28 +1314,27 @@
   1.723        case False hence "pi \<le> x - k * (2 * pi)" by simp
   1.724        hence pi_x: "- pi \<le> x - k * (2 * pi) - 2 * pi" by simp
   1.725  
   1.726 -      have "?ux \<le> 2 * pi" using Cond lpi by (auto simp add: le_float_def)
   1.727 +      have "?ux \<le> 2 * pi" using Cond lpi by (auto simp add: less_eq_float_def)
   1.728        hence "x - k * (2 * pi) - 2 * pi \<le> 0" using ux by simp
   1.729  
   1.730        have ux_0: "real (?ux - 2 * ?lpi) \<le> 0"
   1.731 -        using Cond by (auto simp add: le_float_def)
   1.732 +        using Cond by (auto simp add: less_eq_float_def)
   1.733  
   1.734        from 2 and Cond have "\<not> ?ux \<le> ?lpi" by auto
   1.735 -      hence "- ?lpi \<le> ?ux - 2 * ?lpi" by (auto simp add: le_float_def)
   1.736 +      hence "- ?lpi \<le> ?ux - 2 * ?lpi" by (auto simp add: less_eq_float_def)
   1.737        hence pi_ux: "- pi \<le> (?ux - 2 * ?lpi)"
   1.738 -        using lpi[THEN le_imp_neg_le] by (auto simp add: le_float_def)
   1.739 +        using lpi[THEN le_imp_neg_le] by (auto simp add: less_eq_float_def)
   1.740  
   1.741        have x_le_ux: "x - k * (2 * pi) - 2 * pi \<le> (?ux - 2 * ?lpi)"
   1.742          using ux lpi by auto
   1.743 -
   1.744        have "cos x = cos (x + (-k) * (2 * pi) + (-1::int) * (2 * pi))"
   1.745          unfolding cos_periodic_int ..
   1.746        also have "\<dots> \<le> cos ((?ux - 2 * ?lpi))"
   1.747          using cos_monotone_minus_pi_0'[OF pi_x x_le_ux ux_0]
   1.748 -        by (simp only: real_of_float_minus real_of_int_minus real_of_one
   1.749 -            minus_one [symmetric] diff_minus mult_minus_left mult_1_left)
   1.750 +        by (simp only: real_of_float_minus real_of_int_minus real_of_one minus_one[symmetric]
   1.751 +            diff_minus mult_minus_left mult_1_left)
   1.752        also have "\<dots> = cos ((- (?ux - 2 * ?lpi)))"
   1.753 -        unfolding real_of_float_minus cos_minus ..
   1.754 +        unfolding real_of_float_uminus cos_minus ..
   1.755        also have "\<dots> \<le> (ub_cos prec (- (?ux - 2 * ?lpi)))"
   1.756          using lb_cos_minus[OF pi_ux ux_0] by simp
   1.757        finally show ?thesis unfolding u by (simp add: real_of_float_max)
   1.758 @@ -1363,7 +1345,7 @@
   1.759      case True note Cond = this with bnds 1 2 3 4
   1.760      have l: "l = Float -1 0"
   1.761        and u: "u = max (ub_cos prec (?lx + 2 * ?lpi)) (ub_cos prec (-?ux))"
   1.762 -      by (auto simp add: bnds_cos_def Let_def)
   1.763 +      by (auto simp add: bnds_cos_def Let_def simp del: neg_numeral_float_Float)
   1.764  
   1.765      have "cos x \<le> u"
   1.766      proof (cases "-pi < x - k * (2 * pi)")
   1.767 @@ -1374,17 +1356,17 @@
   1.768        case False hence "x - k * (2 * pi) \<le> -pi" by simp
   1.769        hence pi_x: "x - k * (2 * pi) + 2 * pi \<le> pi" by simp
   1.770  
   1.771 -      have "-2 * pi \<le> ?lx" using Cond lpi by (auto simp add: le_float_def)
   1.772 +      have "-2 * pi \<le> ?lx" using Cond lpi by (auto simp add: less_eq_float_def)
   1.773  
   1.774        hence "0 \<le> x - k * (2 * pi) + 2 * pi" using lx by simp
   1.775  
   1.776        have lx_0: "0 \<le> real (?lx + 2 * ?lpi)"
   1.777 -        using Cond lpi by (auto simp add: le_float_def)
   1.778 +        using Cond lpi by (auto simp add: less_eq_float_def)
   1.779  
   1.780        from 1 and Cond have "\<not> -?lpi \<le> ?lx" by auto
   1.781 -      hence "?lx + 2 * ?lpi \<le> ?lpi" by (auto simp add: le_float_def)
   1.782 +      hence "?lx + 2 * ?lpi \<le> ?lpi" by (auto simp add: less_eq_float_def)
   1.783        hence pi_lx: "(?lx + 2 * ?lpi) \<le> pi"
   1.784 -        using lpi[THEN le_imp_neg_le] by (auto simp add: le_float_def)
   1.785 +        using lpi[THEN le_imp_neg_le] by (auto simp add: less_eq_float_def)
   1.786  
   1.787        have lx_le_x: "(?lx + 2 * ?lpi) \<le> x - k * (2 * pi) + 2 * pi"
   1.788          using lx lpi by auto
   1.789 @@ -1394,7 +1376,7 @@
   1.790        also have "\<dots> \<le> cos ((?lx + 2 * ?lpi))"
   1.791          using cos_monotone_0_pi'[OF lx_0 lx_le_x pi_x]
   1.792          by (simp only: real_of_float_minus real_of_int_minus real_of_one
   1.793 -          minus_one [symmetric] diff_minus mult_minus_left mult_1_left)
   1.794 +          minus_one[symmetric] diff_minus mult_minus_left mult_1_left)
   1.795        also have "\<dots> \<le> (ub_cos prec (?lx + 2 * ?lpi))"
   1.796          using lb_cos[OF lx_0 pi_lx] by simp
   1.797        finally show ?thesis unfolding u by (simp add: real_of_float_max)
   1.798 @@ -1467,11 +1449,10 @@
   1.799  "lb_exp prec x = (if 0 < x then float_divl prec 1 (ub_exp prec (-x))
   1.800               else let
   1.801                  horner = (\<lambda> x. let  y = lb_exp_horner prec (get_even (prec + 2)) 1 1 x  in if y \<le> 0 then Float 1 -2 else y)
   1.802 -             in if x < - 1 then (case floor_fl x of (Float m e) \<Rightarrow> (horner (float_divl prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
   1.803 +             in if x < - 1 then (horner (float_divl prec x (- floor_fl x))) ^ nat (- int_floor_fl x)
   1.804                             else horner x)" |
   1.805  "ub_exp prec x = (if 0 < x    then float_divr prec 1 (lb_exp prec (-x))
   1.806 -             else if x < - 1  then (case floor_fl x of (Float m e) \<Rightarrow>
   1.807 -                                    (ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- Float m e))) ^ (nat (-m) * 2 ^ nat e))
   1.808 +             else if x < - 1  then ub_exp_horner prec (get_odd (prec + 2)) 1 1 (float_divr prec x (- floor_fl x)) ^ (nat (- int_floor_fl x))
   1.809                                else ub_exp_horner prec (get_odd (prec + 2)) 1 1 x)"
   1.810  by pat_completeness auto
   1.811  termination by (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if 0 < x then 1 else 0))", auto simp add: less_float_def)
   1.812 @@ -1483,24 +1464,24 @@
   1.813    have "1 / 4 = (Float 1 -2)" unfolding Float_num by auto
   1.814    also have "\<dots> \<le> lb_exp_horner 1 (get_even 4) 1 1 (- 1)"
   1.815      unfolding get_even_def eq4
   1.816 -    by (auto simp add: lapprox_posrat_def rapprox_posrat_def normfloat.simps)
   1.817 +    by (auto simp add: compute_lapprox_rat compute_rapprox_rat compute_lapprox_posrat compute_rapprox_posrat rat_precision_def compute_bitlen)
   1.818    also have "\<dots> \<le> exp (- 1 :: float)" using bnds_exp_horner[where x="- 1"] by auto
   1.819 -  finally show ?thesis unfolding real_of_float_minus real_of_float_1 .
   1.820 +  finally show ?thesis unfolding real_of_float_minus real_of_float_one by simp
   1.821  qed
   1.822  
   1.823  lemma lb_exp_pos: assumes "\<not> 0 < x" shows "0 < lb_exp prec x"
   1.824  proof -
   1.825    let "?lb_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
   1.826    let "?horner x" = "let  y = ?lb_horner x  in if y \<le> 0 then Float 1 -2 else y"
   1.827 -  have pos_horner: "\<And> x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \<le> 0", auto simp add: le_float_def less_float_def)
   1.828 +  have pos_horner: "\<And> x. 0 < ?horner x" unfolding Let_def by (cases "?lb_horner x \<le> 0", auto simp add: less_eq_float_def less_float_def)
   1.829    moreover { fix x :: float fix num :: nat
   1.830 -    have "0 < real (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def real_of_float_0] by (rule zero_less_power)
   1.831 -    also have "\<dots> = (?horner x) ^ num" using float_power by auto
   1.832 +    have "0 < real (?horner x) ^ num" using `0 < ?horner x`[unfolded less_float_def real_of_float_zero] by (rule zero_less_power)
   1.833 +    also have "\<dots> = (?horner x) ^ num" by auto
   1.834      finally have "0 < real ((?horner x) ^ num)" .
   1.835    }
   1.836    ultimately show ?thesis
   1.837      unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] Let_def
   1.838 -    by (cases "floor_fl x", cases "x < - 1", auto simp add: float_power le_float_def less_float_def)
   1.839 +    by (cases "floor_fl x", cases "x < - 1", auto simp add: less_eq_float_def less_float_def)
   1.840  qed
   1.841  
   1.842  lemma exp_boundaries': assumes "x \<le> 0"
   1.843 @@ -1509,7 +1490,7 @@
   1.844    let "?lb_exp_horner x" = "lb_exp_horner prec (get_even (prec + 2)) 1 1 x"
   1.845    let "?ub_exp_horner x" = "ub_exp_horner prec (get_odd (prec + 2)) 1 1 x"
   1.846  
   1.847 -  have "real x \<le> 0" and "\<not> x > 0" using `x \<le> 0` unfolding le_float_def less_float_def by auto
   1.848 +  have "real x \<le> 0" and "\<not> x > 0" using `x \<le> 0` unfolding less_eq_float_def less_float_def by auto
   1.849    show ?thesis
   1.850    proof (cases "x < - 1")
   1.851      case False hence "- 1 \<le> real x" unfolding less_float_def by auto
   1.852 @@ -1527,62 +1508,62 @@
   1.853    next
   1.854      case True
   1.855  
   1.856 -    obtain m e where Float_floor: "floor_fl x = Float m e" by (cases "floor_fl x", auto)
   1.857 -    let ?num = "nat (- m) * 2 ^ nat e"
   1.858 -
   1.859 -    have "real (floor_fl x) < - 1" using floor_fl `x < - 1` unfolding le_float_def less_float_def real_of_float_minus real_of_float_1 by (rule order_le_less_trans)
   1.860 -    hence "real (floor_fl x) < 0" unfolding Float_floor real_of_float_simp using zero_less_pow2[of xe] by auto
   1.861 -    hence "m < 0"
   1.862 -      unfolding less_float_def real_of_float_0 Float_floor real_of_float_simp
   1.863 -      unfolding pos_prod_lt[OF zero_less_pow2[of e], unfolded mult_commute] by auto
   1.864 -    hence "1 \<le> - m" by auto
   1.865 -    hence "0 < nat (- m)" by auto
   1.866 -    moreover
   1.867 -    have "0 \<le> e" using floor_pos_exp Float_floor[symmetric] by auto
   1.868 -    hence "(0::nat) < 2 ^ nat e" by auto
   1.869 -    ultimately have "0 < ?num"  by auto
   1.870 +    let ?num = "nat (- int_floor_fl x)"
   1.871 +
   1.872 +    have "real (int_floor_fl x) < - 1" using int_floor_fl `x < - 1` unfolding less_eq_float_def less_float_def real_of_float_uminus real_of_float_one
   1.873 +      by (rule order_le_less_trans)
   1.874 +    hence "real (int_floor_fl x) < 0" by simp
   1.875 +    hence "int_floor_fl x < 0" by auto
   1.876 +    hence "1 \<le> - int_floor_fl x" by auto
   1.877 +    hence "0 < nat (- int_floor_fl x)" by auto
   1.878 +    hence "0 < ?num"  by auto
   1.879      hence "real ?num \<noteq> 0" by auto
   1.880 -    have e_nat: "(nat e) = e" using `0 \<le> e` by auto
   1.881 -    have num_eq: "real ?num = - floor_fl x" using `0 < nat (- m)`
   1.882 -      unfolding Float_floor real_of_float_minus real_of_float_simp real_of_nat_mult pow2_int[of "nat e", unfolded e_nat] real_of_nat_power by auto
   1.883 -    have "0 < - floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] unfolding less_float_def num_eq[symmetric] real_of_float_0 real_of_nat_zero .
   1.884 -    hence "real (floor_fl x) < 0" unfolding less_float_def by auto
   1.885 -
   1.886 +    have num_eq: "real ?num = - int_floor_fl x" using `0 < nat (- int_floor_fl x)` by auto
   1.887 +    have "0 < - int_floor_fl x" using `0 < ?num`[unfolded real_of_nat_less_iff[symmetric]] by simp
   1.888 +    hence "real (int_floor_fl x) < 0" unfolding less_float_def by auto
   1.889 +    have fl_eq: "real (- int_floor_fl x) = real (- floor_fl x)"
   1.890 +      by (simp add: floor_fl_def int_floor_fl_def)
   1.891 +    from `0 < - int_floor_fl x` have "0 < real (- floor_fl x)"
   1.892 +      by (simp add: floor_fl_def int_floor_fl_def)
   1.893 +    from `real (int_floor_fl x) < 0` have "real (floor_fl x) < 0"
   1.894 +      by (simp add: floor_fl_def int_floor_fl_def)
   1.895      have "exp x \<le> ub_exp prec x"
   1.896      proof -
   1.897        have div_less_zero: "real (float_divr prec x (- floor_fl x)) \<le> 0"
   1.898 -        using float_divr_nonpos_pos_upper_bound[OF `x \<le> 0` `0 < - floor_fl x`] unfolding le_float_def real_of_float_0 .
   1.899 +        using float_divr_nonpos_pos_upper_bound[OF `real x \<le> 0` `0 < real (- floor_fl x)`]
   1.900 +        unfolding less_eq_float_def real_of_float_zero .
   1.901  
   1.902        have "exp x = exp (?num * (x / ?num))" using `real ?num \<noteq> 0` by auto
   1.903        also have "\<dots> = exp (x / ?num) ^ ?num" unfolding exp_real_of_nat_mult ..
   1.904 -      also have "\<dots> \<le> exp (float_divr prec x (- floor_fl x)) ^ ?num" unfolding num_eq
   1.905 +      also have "\<dots> \<le> exp (float_divr prec x (- floor_fl x)) ^ ?num" unfolding num_eq fl_eq
   1.906          by (rule power_mono, rule exp_le_cancel_iff[THEN iffD2], rule float_divr) auto
   1.907 -      also have "\<dots> \<le> (?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num" unfolding float_power
   1.908 +      also have "\<dots> \<le> (?ub_exp_horner (float_divr prec x (- floor_fl x))) ^ ?num"
   1.909 +        unfolding real_of_float_power
   1.910          by (rule power_mono, rule bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct2], auto)
   1.911 -      finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def .
   1.912 +      finally show ?thesis unfolding ub_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] floor_fl_def Let_def .
   1.913      qed
   1.914      moreover
   1.915      have "lb_exp prec x \<le> exp x"
   1.916      proof -
   1.917 -      let ?divl = "float_divl prec x (- Float m e)"
   1.918 +      let ?divl = "float_divl prec x (- floor_fl x)"
   1.919        let ?horner = "?lb_exp_horner ?divl"
   1.920  
   1.921        show ?thesis
   1.922        proof (cases "?horner \<le> 0")
   1.923 -        case False hence "0 \<le> real ?horner" unfolding le_float_def by auto
   1.924 +        case False hence "0 \<le> real ?horner" unfolding less_eq_float_def by auto
   1.925  
   1.926          have div_less_zero: "real (float_divl prec x (- floor_fl x)) \<le> 0"
   1.927            using `real (floor_fl x) < 0` `real x \<le> 0` by (auto intro!: order_trans[OF float_divl] divide_nonpos_neg)
   1.928  
   1.929          have "(?lb_exp_horner (float_divl prec x (- floor_fl x))) ^ ?num \<le>
   1.930 -          exp (float_divl prec x (- floor_fl x)) ^ ?num" unfolding float_power
   1.931 -          using `0 \<le> real ?horner`[unfolded Float_floor[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono)
   1.932 -        also have "\<dots> \<le> exp (x / ?num) ^ ?num" unfolding num_eq
   1.933 -          using float_divl by (auto intro!: power_mono simp del: real_of_float_minus)
   1.934 +          exp (float_divl prec x (- floor_fl x)) ^ ?num"
   1.935 +          using `0 \<le> real ?horner`[unfolded floor_fl_def[symmetric]] bnds_exp_horner[OF div_less_zero, unfolded atLeastAtMost_iff, THEN conjunct1] by (auto intro!: power_mono)
   1.936 +        also have "\<dots> \<le> exp (x / ?num) ^ ?num" unfolding num_eq fl_eq
   1.937 +          using float_divl by (auto intro!: power_mono simp del: real_of_float_uminus)
   1.938          also have "\<dots> = exp (?num * (x / ?num))" unfolding exp_real_of_nat_mult ..
   1.939          also have "\<dots> = exp x" using `real ?num \<noteq> 0` by auto
   1.940          finally show ?thesis
   1.941 -          unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_not_P[OF False] by auto
   1.942 +          unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_not_P[OF False] by auto
   1.943        next
   1.944          case True
   1.945          have "real (floor_fl x) \<noteq> 0" and "real (floor_fl x) \<le> 0" using `real (floor_fl x) < 0` by auto
   1.946 @@ -1592,9 +1573,9 @@
   1.947          have "Float 1 -2 \<le> exp (x / (- floor_fl x))" unfolding Float_num .
   1.948          hence "real (Float 1 -2) ^ ?num \<le> exp (x / (- floor_fl x)) ^ ?num"
   1.949            by (auto intro!: power_mono)
   1.950 -        also have "\<dots> = exp x" unfolding num_eq exp_real_of_nat_mult[symmetric] using `real (floor_fl x) \<noteq> 0` by auto
   1.951 +        also have "\<dots> = exp x" unfolding num_eq fl_eq exp_real_of_nat_mult[symmetric] using `real (floor_fl x) \<noteq> 0` by auto
   1.952          finally show ?thesis
   1.953 -          unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] float.cases Float_floor Let_def if_P[OF True] float_power .
   1.954 +          unfolding lb_exp.simps if_not_P[OF `\<not> 0 < x`] if_P[OF `x < - 1`] int_floor_fl_def Let_def if_P[OF True] real_of_float_power .
   1.955        qed
   1.956      qed
   1.957      ultimately show ?thesis by auto
   1.958 @@ -1605,10 +1586,10 @@
   1.959  proof -
   1.960    show ?thesis
   1.961    proof (cases "0 < x")
   1.962 -    case False hence "x \<le> 0" unfolding less_float_def le_float_def by auto
   1.963 +    case False hence "x \<le> 0" unfolding less_float_def less_eq_float_def by auto
   1.964      from exp_boundaries'[OF this] show ?thesis .
   1.965    next
   1.966 -    case True hence "-x \<le> 0" unfolding less_float_def le_float_def by auto
   1.967 +    case True hence "-x \<le> 0" unfolding less_float_def less_eq_float_def by auto
   1.968  
   1.969      have "lb_exp prec x \<le> exp x"
   1.970      proof -
   1.971 @@ -1630,9 +1611,9 @@
   1.972        have lb_exp: "lb_exp prec (-x) \<le> exp (- real x)" unfolding atLeastAtMost_iff real_of_float_minus by auto
   1.973  
   1.974        have "exp x \<le> (1 :: float) / lb_exp prec (-x)"
   1.975 -        using lb_exp[unfolded inverse_le_iff_le[OF exp_gt_zero lb_exp_pos[OF `\<not> 0 < -x`, unfolded less_float_def real_of_float_0],
   1.976 +        using lb_exp[unfolded inverse_le_iff_le[OF exp_gt_zero lb_exp_pos[OF `\<not> 0 < -x`, unfolded less_float_def real_of_float_zero],
   1.977                                                  symmetric]]
   1.978 -        unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide real_of_float_1 by auto
   1.979 +        unfolding exp_minus nonzero_inverse_inverse_eq[OF exp_not_eq_zero] inverse_eq_divide real_of_float_one by auto
   1.980        also have "\<dots> \<le> float_divr prec 1 (lb_exp prec (-x))" using float_divr .
   1.981        finally show ?thesis unfolding ub_exp.simps if_P[OF True] .
   1.982      qed
   1.983 @@ -1703,7 +1684,7 @@
   1.984  
   1.985    let "?s n" = "-1^n * (1 / real (1 + n)) * (real x)^(Suc n)"
   1.986  
   1.987 -  have "?lb \<le> setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 mult_assoc[symmetric] real_of_float_mult setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] ev
   1.988 +  have "?lb \<le> setsum ?s {0 ..< 2 * ev}" unfolding power_Suc2 mult_assoc[symmetric] real_of_float_times setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] ev
   1.989      using horner_bounds(1)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*ev",
   1.990        OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x`
   1.991      by (rule mult_right_mono)
   1.992 @@ -1711,7 +1692,7 @@
   1.993    finally show "?lb \<le> ?ln" .
   1.994  
   1.995    have "?ln \<le> setsum ?s {0 ..< 2 * od + 1}" using ln_bounds(2)[OF `0 \<le> real x` `real x < 1`] by auto
   1.996 -  also have "\<dots> \<le> ?ub" unfolding power_Suc2 mult_assoc[symmetric] real_of_float_mult setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] od
   1.997 +  also have "\<dots> \<le> ?ub" unfolding power_Suc2 mult_assoc[symmetric] real_of_float_times setsum_left_distrib[symmetric] unfolding mult_commute[of "real x"] od
   1.998      using horner_bounds(2)[where G="\<lambda> i k. Suc k" and F="\<lambda>x. x" and f="\<lambda>x. x" and lb="\<lambda>n i k x. lb_ln_horner prec n k x" and ub="\<lambda>n i k x. ub_ln_horner prec n k x" and j'=1 and n="2*od+1",
   1.999        OF `0 \<le> real x` refl lb_ln_horner.simps ub_ln_horner.simps] `0 \<le> real x`
  1.1000      by (rule mult_right_mono)
  1.1001 @@ -1747,28 +1728,27 @@
  1.1002      using ln_add[of "3 / 2" "1 / 2"] by auto
  1.1003    have lb3: "?lthird \<le> 1 / 3" using lapprox_rat[of prec 1 3] by auto
  1.1004    hence lb3_ub: "real ?lthird < 1" by auto
  1.1005 -  have lb3_lb: "0 \<le> real ?lthird" using lapprox_rat_bottom[of 1 3] by auto
  1.1006 +  have lb3_lb: "0 \<le> real ?lthird" using lapprox_rat_nonneg[of 1 3] by auto
  1.1007    have ub3: "1 / 3 \<le> ?uthird" using rapprox_rat[of 1 3] by auto
  1.1008    hence ub3_lb: "0 \<le> real ?uthird" by auto
  1.1009  
  1.1010    have lb2: "0 \<le> real (Float 1 -1)" and ub2: "real (Float 1 -1) < 1" unfolding Float_num by auto
  1.1011  
  1.1012    have "0 \<le> (1::int)" and "0 < (3::int)" by auto
  1.1013 -  have ub3_ub: "real ?uthird < 1" unfolding rapprox_rat.simps(2)[OF `0 \<le> 1` `0 < 3`]
  1.1014 -    by (rule rapprox_posrat_less1, auto)
  1.1015 +  have ub3_ub: "real ?uthird < 1" by (simp add: compute_rapprox_rat rapprox_posrat_less1)
  1.1016  
  1.1017    have third_gt0: "(0 :: real) < 1 / 3 + 1" by auto
  1.1018    have uthird_gt0: "0 < real ?uthird + 1" using ub3_lb by auto
  1.1019    have lthird_gt0: "0 < real ?lthird + 1" using lb3_lb by auto
  1.1020  
  1.1021 -  show ?ub_ln2 unfolding ub_ln2_def Let_def real_of_float_add ln2_sum Float_num(4)[symmetric]
  1.1022 +  show ?ub_ln2 unfolding ub_ln2_def Let_def real_of_float_plus ln2_sum Float_num(4)[symmetric]
  1.1023    proof (rule add_mono, fact ln_float_bounds(2)[OF lb2 ub2])
  1.1024      have "ln (1 / 3 + 1) \<le> ln (real ?uthird + 1)" unfolding ln_le_cancel_iff[OF third_gt0 uthird_gt0] using ub3 by auto
  1.1025      also have "\<dots> \<le> ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird"
  1.1026        using ln_float_bounds(2)[OF ub3_lb ub3_ub] .
  1.1027      finally show "ln (1 / 3 + 1) \<le> ?uthird * ub_ln_horner prec (get_odd prec) 1 ?uthird" .
  1.1028    qed
  1.1029 -  show ?lb_ln2 unfolding lb_ln2_def Let_def real_of_float_add ln2_sum Float_num(4)[symmetric]
  1.1030 +  show ?lb_ln2 unfolding lb_ln2_def Let_def real_of_float_plus ln2_sum Float_num(4)[symmetric]
  1.1031    proof (rule add_mono, fact ln_float_bounds(1)[OF lb2 ub2])
  1.1032      have "?lthird * lb_ln_horner prec (get_even prec) 1 ?lthird \<le> ln (real ?lthird + 1)"
  1.1033        using ln_float_bounds(1)[OF lb3_lb lb3_ub] .
  1.1034 @@ -1786,7 +1766,7 @@
  1.1035                   if x \<le> Float 3 -1 then Some (horner (x - 1))
  1.1036              else if x < Float 1 1  then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1))
  1.1037                                     else let l = bitlen (mantissa x) - 1 in
  1.1038 -                                        Some (ub_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" |
  1.1039 +                                        Some (ub_ln2 prec * (Float (exponent x + l) 0) + horner (Float (mantissa x) (- l) - 1)))" |
  1.1040  "lb_ln prec x = (if x \<le> 0          then None
  1.1041              else if x < 1          then Some (- the (ub_ln prec (float_divr prec 1 x)))
  1.1042              else let horner = \<lambda>x. x * lb_ln_horner prec (get_even prec) 1 x in
  1.1043 @@ -1794,41 +1774,117 @@
  1.1044              else if x < Float 1 1  then Some (horner (Float 1 -1) +
  1.1045                                                horner (max (x * lapprox_rat prec 2 3 - 1) 0))
  1.1046                                     else let l = bitlen (mantissa x) - 1 in
  1.1047 -                                        Some (lb_ln2 prec * (Float (scale x + l) 0) + horner (Float (mantissa x) (- l) - 1)))"
  1.1048 +                                        Some (lb_ln2 prec * (Float (exponent x + l) 0) + horner (Float (mantissa x) (- l) - 1)))"
  1.1049  by pat_completeness auto
  1.1050  
  1.1051  termination proof (relation "measure (\<lambda> v. let (prec, x) = sum_case id id v in (if x < 1 then 1 else 0))", auto)
  1.1052    fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divl (max prec (Suc 0)) 1 x < 1"
  1.1053 -  hence "0 < x" and "0 < max prec (Suc 0)" unfolding less_float_def le_float_def by auto
  1.1054 -  from float_divl_pos_less1_bound[OF `0 < x` `x < 1` `0 < max prec (Suc 0)`]
  1.1055 -  show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def le_float_def by auto
  1.1056 +  hence "0 < real x" "1 \<le> max prec (Suc 0)" "real x < 1"
  1.1057 +    unfolding less_float_def less_eq_float_def by auto
  1.1058 +  from float_divl_pos_less1_bound[OF `0 < real x` `real x < 1` `1 \<le> max prec (Suc 0)`]
  1.1059 +  show False using `float_divl (max prec (Suc 0)) 1 x < 1` unfolding less_float_def less_eq_float_def by auto
  1.1060  next
  1.1061    fix prec x assume "\<not> x \<le> 0" and "x < 1" and "float_divr prec 1 x < 1"
  1.1062 -  hence "0 < x" unfolding less_float_def le_float_def by auto
  1.1063 +  hence "0 < x" unfolding less_float_def less_eq_float_def by auto
  1.1064    from float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`, of prec]
  1.1065 -  show False using `float_divr prec 1 x < 1` unfolding less_float_def le_float_def by auto
  1.1066 +  show False using `float_divr prec 1 x < 1` unfolding less_float_def less_eq_float_def by auto
  1.1067 +qed
  1.1068 +
  1.1069 +lemma float_pos_eq_mantissa_pos:  "x > 0 \<longleftrightarrow> mantissa x > 0"
  1.1070 +  apply (subst Float_mantissa_exponent[of x, symmetric])
  1.1071 +  apply (auto simp add: zero_less_mult_iff zero_float_def powr_gt_zero[of 2 "exponent x"] dest: less_zeroE)
  1.1072 +  using powr_gt_zero[of 2 "exponent x"]
  1.1073 +  apply simp
  1.1074 +  done
  1.1075 +
  1.1076 +lemma Float_pos_eq_mantissa_pos:  "Float m e > 0 \<longleftrightarrow> m > 0"
  1.1077 +  apply (auto simp add: zero_less_mult_iff zero_float_def powr_gt_zero[of 2 "exponent x"] dest: less_zeroE)
  1.1078 +  using powr_gt_zero[of 2 "e"]
  1.1079 +  apply simp
  1.1080 +  done
  1.1081 +
  1.1082 +lemma Float_representation_aux:
  1.1083 +  fixes m e
  1.1084 +  defines "x \<equiv> Float m e"
  1.1085 +  assumes "x > 0"
  1.1086 +  shows "Float (exponent x + (bitlen (mantissa x) - 1)) 0 = Float (e + (bitlen m - 1)) 0" (is ?th1)
  1.1087 +    and "Float (mantissa x) (- (bitlen (mantissa x) - 1)) = Float m ( - (bitlen m - 1))"  (is ?th2)
  1.1088 +proof -
  1.1089 +  from assms have mantissa_pos: "m > 0" "mantissa x > 0"
  1.1090 +    using Float_pos_eq_mantissa_pos float_pos_eq_mantissa_pos by simp_all
  1.1091 +  thus ?th1 using bitlen_Float[of m e] assms by (simp add: less_float_def zero_less_mult_iff)
  1.1092 +  from assms have "x \<noteq> float_of 0" by (auto simp add: zero_float_def zero_less_mult_iff)
  1.1093 +  from denormalize_shift[OF assms(1) this] guess i . note i = this
  1.1094 +  from `x \<noteq> float_of 0` have "mantissa x \<noteq> 0" by (simp add: mantissa_noteq_0)
  1.1095 +  from assms
  1.1096 +  have "real (mantissa x) * 2 powr (1 - real (bitlen (mantissa x))) \<in> float"
  1.1097 +    using two_powr_int_float[of "1 - bitlen (mantissa x)"] by simp
  1.1098 +  moreover
  1.1099 +  have "2 powr (1 - (real (bitlen (mantissa x)) + real i)) =
  1.1100 +    2 powr (1 - (real (bitlen (mantissa x)))) * inverse (2 powr (real i))"
  1.1101 +    by (simp add: powr_minus[symmetric] powr_add[symmetric] field_simps)
  1.1102 +  hence "real (mantissa x) * 2 powr (1 - real (bitlen (mantissa x))) =
  1.1103 +    (real (mantissa x) * 2 ^ i) * 2 powr (1 - real (bitlen (mantissa x * 2 ^ i)))"
  1.1104 +    using `mantissa x > 0` by (simp add: powr_realpow)
  1.1105 +  ultimately
  1.1106 +  show ?th2
  1.1107 +    using two_powr_int_float[of "1 - bitlen (mantissa x)"] by (simp add: i)
  1.1108 +qed
  1.1109 +
  1.1110 +lemma compute_ln[code]:
  1.1111 +  fixes m e
  1.1112 +  defines "x \<equiv> Float m e"
  1.1113 +  shows "ub_ln prec x = (if x \<le> 0          then None
  1.1114 +              else if x < 1          then Some (- the (lb_ln prec (float_divl (max prec 1) 1 x)))
  1.1115 +            else let horner = \<lambda>x. x * ub_ln_horner prec (get_odd prec) 1 x in
  1.1116 +                 if x \<le> Float 3 -1 then Some (horner (x - 1))
  1.1117 +            else if x < Float 1 1  then Some (horner (Float 1 -1) + horner (x * rapprox_rat prec 2 3 - 1))
  1.1118 +                                   else let l = bitlen m - 1 in
  1.1119 +                                        Some (ub_ln2 prec * (Float (e + l) 0) + horner (Float m (- l) - 1)))"
  1.1120 +    (is ?th1)
  1.1121 +  and "lb_ln prec x = (if x \<le> 0          then None
  1.1122 +            else if x < 1          then Some (- the (ub_ln prec (float_divr prec 1 x)))
  1.1123 +            else let horner = \<lambda>x. x * lb_ln_horner prec (get_even prec) 1 x in
  1.1124 +                 if x \<le> Float 3 -1 then Some (horner (x - 1))
  1.1125 +            else if x < Float 1 1  then Some (horner (Float 1 -1) +
  1.1126 +                                              horner (max (x * lapprox_rat prec 2 3 - 1) 0))
  1.1127 +                                   else let l = bitlen m - 1 in
  1.1128 +                                        Some (lb_ln2 prec * (Float (e + l) 0) + horner (Float m (- l) - 1)))"
  1.1129 +    (is ?th2)
  1.1130 +proof -
  1.1131 +  from assms Float_pos_eq_mantissa_pos have "x > 0 \<Longrightarrow> m > 0" by simp
  1.1132 +  thus ?th1 ?th2 using Float_representation_aux[of m e] unfolding x_def[symmetric]
  1.1133 +    by (auto simp add: simp del: Float_def dest: not_leE)
  1.1134  qed
  1.1135  
  1.1136  lemma ln_shifted_float: assumes "0 < m" shows "ln (Float m e) = ln 2 * (e + (bitlen m - 1)) + ln (Float m (- (bitlen m - 1)))"
  1.1137  proof -
  1.1138    let ?B = "2^nat (bitlen m - 1)"
  1.1139 +  def bl \<equiv> "bitlen m - 1"
  1.1140    have "0 < real m" and "\<And>X. (0 :: real) < 2^X" and "0 < (2 :: real)" and "m \<noteq> 0" using assms by auto
  1.1141 -  hence "0 \<le> bitlen m - 1" using bitlen_ge1[OF `m \<noteq> 0`] by auto
  1.1142 +  hence "0 \<le> bl" by (simp add: bitlen_def bl_def)
  1.1143    show ?thesis
  1.1144    proof (cases "0 \<le> e")
  1.1145 -    case True
  1.1146 -    show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
  1.1147 -      unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`]
  1.1148 -      unfolding real_of_float_ge0_exp[OF True] ln_mult[OF `0 < real m` `0 < 2^nat e`]
  1.1149 -      ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` True by auto
  1.1150 +    case True 
  1.1151 +    thus ?thesis
  1.1152 +      unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl`
  1.1153 +      apply (simp add: ln_mult)
  1.1154 +      apply (cases "e=0")
  1.1155 +        apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr)
  1.1156 +        apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr field_simps)
  1.1157 +      done
  1.1158    next
  1.1159      case False hence "0 < -e" by auto
  1.1160 +    have lne: "ln (2 powr real e) = ln (inverse (2 powr - e))" by (simp add: powr_minus)
  1.1161      hence pow_gt0: "(0::real) < 2^nat (-e)" by auto
  1.1162      hence inv_gt0: "(0::real) < inverse (2^nat (-e))" by auto
  1.1163 -    show ?thesis unfolding normalized_float[OF `m \<noteq> 0`]
  1.1164 -      unfolding ln_div[OF `0 < real m` `0 < ?B`] real_of_int_add ln_realpow[OF `0 < 2`]
  1.1165 -      unfolding real_of_float_nge0_exp[OF False] ln_mult[OF `0 < real m` inv_gt0] ln_inverse[OF pow_gt0]
  1.1166 -      ln_realpow[OF `0 < 2`] algebra_simps using `0 \<le> bitlen m - 1` False by auto
  1.1167 +    show ?thesis using False unfolding bl_def[symmetric] using `0 < real m` `0 \<le> bl`
  1.1168 +      apply (simp add: ln_mult lne)
  1.1169 +      apply (cases "e=0")
  1.1170 +        apply (cases "bl = 0", simp_all add: powr_minus ln_inverse ln_powr)
  1.1171 +        apply (simp add: ln_inverse lne)
  1.1172 +        apply (cases "bl = 0", simp_all add: ln_inverse ln_powr field_simps)
  1.1173 +      done
  1.1174    qed
  1.1175  qed
  1.1176  
  1.1177 @@ -1838,10 +1894,10 @@
  1.1178  proof (cases "x < Float 1 1")
  1.1179    case True
  1.1180    hence "real (x - 1) < 1" and "real x < 2" unfolding less_float_def Float_num by auto
  1.1181 -  have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` unfolding less_float_def le_float_def by auto
  1.1182 +  have "\<not> x \<le> 0" and "\<not> x < 1" using `1 \<le> x` unfolding less_float_def less_eq_float_def by auto
  1.1183    hence "0 \<le> real (x - 1)" using `1 \<le> x` unfolding less_float_def Float_num by auto
  1.1184  
  1.1185 -  have [simp]: "(Float 3 -1) = 3 / 2" by (simp add: real_of_float_def pow2_def)
  1.1186 +  have [simp]: "(Float 3 -1) = 3 / 2" by simp
  1.1187  
  1.1188    show ?thesis
  1.1189    proof (cases "x \<le> Float 3 -1")
  1.1190 @@ -1850,7 +1906,7 @@
  1.1191        using ln_float_bounds[OF `0 \<le> real (x - 1)` `real (x - 1) < 1`, of prec] `\<not> x \<le> 0` `\<not> x < 1` True
  1.1192        by auto
  1.1193    next
  1.1194 -    case False hence *: "3 / 2 < x" by (auto simp add: le_float_def)
  1.1195 +    case False hence *: "3 / 2 < x" by (auto simp add: less_eq_float_def)
  1.1196  
  1.1197      with ln_add[of "3 / 2" "x - 3 / 2"]
  1.1198      have add: "ln x = ln (3 / 2) + ln (real x * 2 / 3)"
  1.1199 @@ -1891,7 +1947,7 @@
  1.1200          by (rule order_trans[OF lapprox_rat], simp)
  1.1201  
  1.1202        have low: "0 \<le> real (lapprox_rat prec 2 3)"
  1.1203 -        using lapprox_rat_bottom[of 2 3 prec] by simp
  1.1204 +        using lapprox_rat_nonneg[of 2 3 prec] by simp
  1.1205  
  1.1206        have "?lb_horner ?max
  1.1207          \<le> ln (real ?max + 1)"
  1.1208 @@ -1907,7 +1963,7 @@
  1.1209          show "0 < real x * 2/3" using * by auto
  1.1210          show "real ?max + 1 \<le> real x * 2/3" using * up
  1.1211            by (cases "0 < real x * real (lapprox_posrat prec 2 3) - 1",
  1.1212 -              auto simp add: real_of_float_max min_max.sup_absorb1)
  1.1213 +              auto simp add: max_def)
  1.1214        qed
  1.1215        finally have "?lb_horner (Float 1 -1) + ?lb_horner ?max
  1.1216          \<le> ln x"
  1.1217 @@ -1919,55 +1975,61 @@
  1.1218  next
  1.1219    case False
  1.1220    hence "\<not> x \<le> 0" and "\<not> x < 1" "0 < x" "\<not> x \<le> Float 3 -1"
  1.1221 -    using `1 \<le> x` unfolding less_float_def le_float_def real_of_float_simp pow2_def
  1.1222 +    using `1 \<le> x` unfolding less_float_def less_eq_float_def
  1.1223      by auto
  1.1224    show ?thesis
  1.1225 -  proof (cases x)
  1.1226 -    case (Float m e)
  1.1227 +  proof -
  1.1228 +    def m \<equiv> "mantissa x"
  1.1229 +    def e \<equiv> "exponent x"
  1.1230 +    from Float_mantissa_exponent[of x] have Float: "x = Float m e" by (simp add: m_def e_def)
  1.1231      let ?s = "Float (e + (bitlen m - 1)) 0"
  1.1232      let ?x = "Float m (- (bitlen m - 1))"
  1.1233  
  1.1234 -    have "0 < m" and "m \<noteq> 0" using float_pos_m_pos `0 < x` Float by auto
  1.1235 +    have "0 < m" and "m \<noteq> 0" using `0 < x` Float powr_gt_zero[of 2 e]
  1.1236 +      by (auto simp: less_float_def less_eq_float_def zero_less_mult_iff)
  1.1237 +    def bl \<equiv> "bitlen m - 1" hence "bl \<ge> 0" using `m > 0` by (simp add: bitlen_def)
  1.1238 +    have "1 \<le> Float m e" using `1 \<le> x` Float unfolding less_eq_float_def by auto
  1.1239 +    from bitlen_div[OF `0 < m`] float_gt1_scale[OF `1 \<le> Float m e`] `bl \<ge> 0`
  1.1240 +    have x_bnds: "0 \<le> real (?x - 1)" "real (?x - 1) < 1"
  1.1241 +      unfolding bl_def[symmetric]
  1.1242 +      by (auto simp: powr_realpow[symmetric] field_simps inverse_eq_divide)
  1.1243 +         (auto simp : powr_minus field_simps inverse_eq_divide)
  1.1244  
  1.1245      {
  1.1246        have "lb_ln2 prec * ?s \<le> ln 2 * (e + (bitlen m - 1))" (is "?lb2 \<le> _")
  1.1247 -        unfolding real_of_float_mult real_of_float_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right
  1.1248 +        unfolding nat_0 power_0 mult_1_right real_of_float_times
  1.1249          using lb_ln2[of prec]
  1.1250 -      proof (rule mult_right_mono)
  1.1251 -        have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
  1.1252 -        from float_gt1_scale[OF this]
  1.1253 -        show "0 \<le> real (e + (bitlen m - 1))" by auto
  1.1254 -      qed
  1.1255 +      proof (rule mult_mono)
  1.1256 +        from float_gt1_scale[OF `1 \<le> Float m e`]
  1.1257 +        show "0 \<le> real (Float (e + (bitlen m - 1)) 0)" by simp
  1.1258 +      qed auto
  1.1259        moreover
  1.1260 -      from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
  1.1261 -      have "0 \<le> real (?x - 1)" and "real (?x - 1) < 1" by auto
  1.1262 -      from ln_float_bounds(1)[OF this]
  1.1263 +      from ln_float_bounds(1)[OF x_bnds]
  1.1264        have "(?x - 1) * lb_ln_horner prec (get_even prec) 1 (?x - 1) \<le> ln ?x" (is "?lb_horner \<le> _") by auto
  1.1265        ultimately have "?lb2 + ?lb_horner \<le> ln x"
  1.1266          unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
  1.1267      }
  1.1268      moreover
  1.1269      {
  1.1270 -      from bitlen_div[OF `0 < m`, unfolded normalized_float[OF `m \<noteq> 0`, symmetric]]
  1.1271 -      have "0 \<le> real (?x - 1)" and "real (?x - 1) < 1" by auto
  1.1272 -      from ln_float_bounds(2)[OF this]
  1.1273 +      from ln_float_bounds(2)[OF x_bnds]
  1.1274        have "ln ?x \<le> (?x - 1) * ub_ln_horner prec (get_odd prec) 1 (?x - 1)" (is "_ \<le> ?ub_horner") by auto
  1.1275        moreover
  1.1276        have "ln 2 * (e + (bitlen m - 1)) \<le> ub_ln2 prec * ?s" (is "_ \<le> ?ub2")
  1.1277 -        unfolding real_of_float_mult real_of_float_ge0_exp[OF order_refl] nat_0 power_0 mult_1_right
  1.1278 +        unfolding nat_0 power_0 mult_1_right real_of_float_times
  1.1279          using ub_ln2[of prec]
  1.1280 -      proof (rule mult_right_mono)
  1.1281 -        have "1 \<le> Float m e" using `1 \<le> x` Float unfolding le_float_def by auto
  1.1282 -        from float_gt1_scale[OF this]
  1.1283 +      proof (rule mult_mono)
  1.1284 +        from float_gt1_scale[OF `1 \<le> Float m e`]
  1.1285          show "0 \<le> real (e + (bitlen m - 1))" by auto
  1.1286 -      qed
  1.1287 +      next
  1.1288 +        have "0 \<le> ln 2" by simp
  1.1289 +        thus "0 \<le> real (ub_ln2 prec)" using ub_ln2[of prec] by arith
  1.1290 +      qed auto
  1.1291        ultimately have "ln x \<le> ?ub2 + ?ub_horner"
  1.1292          unfolding Float ln_shifted_float[OF `0 < m`, of e] by auto
  1.1293      }
  1.1294      ultimately show ?thesis unfolding lb_ln.simps unfolding ub_ln.simps
  1.1295        unfolding if_not_P[OF `\<not> x \<le> 0`] if_not_P[OF `\<not> x < 1`] if_not_P[OF False] if_not_P[OF `\<not> x \<le> Float 3 -1`] Let_def
  1.1296 -      unfolding scale.simps[of m e, unfolded Float[symmetric]] mantissa.simps[of m e, unfolded Float[symmetric]] real_of_float_add
  1.1297 -      by auto
  1.1298 +      unfolding real_of_float_plus e_def[symmetric] m_def[symmetric] by simp
  1.1299    qed
  1.1300  qed
  1.1301  
  1.1302 @@ -1975,33 +2037,33 @@
  1.1303    shows "the (lb_ln prec x) \<le> ln x \<and> ln x \<le> the (ub_ln prec x)"
  1.1304    (is "?lb \<le> ?ln \<and> ?ln \<le> ?ub")
  1.1305  proof (cases "x < 1")
  1.1306 -  case False hence "1 \<le> x" unfolding less_float_def le_float_def by auto
  1.1307 +  case False hence "1 \<le> x" unfolding less_float_def less_eq_float_def by auto
  1.1308    show ?thesis using ub_ln_lb_ln_bounds'[OF `1 \<le> x`] .
  1.1309  next
  1.1310 -  case True have "\<not> x \<le> 0" using `0 < x` unfolding less_float_def le_float_def by auto
  1.1311 -
  1.1312 +  case True have "\<not> x \<le> 0" using `0 < x` unfolding less_float_def less_eq_float_def by auto
  1.1313 +  from True have "real x < 1" by (simp add: less_float_def)
  1.1314    have "0 < real x" and "real x \<noteq> 0" using `0 < x` unfolding less_float_def by auto
  1.1315    hence A: "0 < 1 / real x" by auto
  1.1316  
  1.1317    {
  1.1318      let ?divl = "float_divl (max prec 1) 1 x"
  1.1319 -    have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
  1.1320 -    hence B: "0 < real ?divl" unfolding le_float_def by auto
  1.1321 +    have A': "1 \<le> ?divl" using float_divl_pos_less1_bound[OF `0 < real x` `real x < 1`] unfolding less_eq_float_def less_float_def by auto
  1.1322 +    hence B: "0 < real ?divl" unfolding less_eq_float_def by auto
  1.1323  
  1.1324      have "ln ?divl \<le> ln (1 / x)" unfolding ln_le_cancel_iff[OF B A] using float_divl[of _ 1 x] by auto
  1.1325      hence "ln x \<le> - ln ?divl" unfolding nonzero_inverse_eq_divide[OF `real x \<noteq> 0`, symmetric] ln_inverse[OF `0 < real x`] by auto
  1.1326      from this ub_ln_lb_ln_bounds'[OF A', THEN conjunct1, THEN le_imp_neg_le]
  1.1327 -    have "?ln \<le> - the (lb_ln prec ?divl)" unfolding real_of_float_minus by (rule order_trans)
  1.1328 +    have "?ln \<le> - the (lb_ln prec ?divl)" unfolding real_of_float_uminus by (rule order_trans)
  1.1329    } moreover
  1.1330    {
  1.1331      let ?divr = "float_divr prec 1 x"
  1.1332 -    have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding le_float_def less_float_def by auto
  1.1333 -    hence B: "0 < real ?divr" unfolding le_float_def by auto
  1.1334 +    have A': "1 \<le> ?divr" using float_divr_pos_less1_lower_bound[OF `0 < x` `x < 1`] unfolding less_eq_float_def less_float_def by auto
  1.1335 +    hence B: "0 < real ?divr" unfolding less_eq_float_def by auto
  1.1336  
  1.1337      have "ln (1 / x) \<le> ln ?divr" unfolding ln_le_cancel_iff[OF A B] using float_divr[of 1 x] by auto
  1.1338      hence "- ln ?divr \<le> ln x" unfolding nonzero_inverse_eq_divide[OF `real x \<noteq> 0`, symmetric] ln_inverse[OF `0 < real x`] by auto
  1.1339      from ub_ln_lb_ln_bounds'[OF A', THEN conjunct2, THEN le_imp_neg_le] this
  1.1340 -    have "- the (ub_ln prec ?divr) \<le> ?ln" unfolding real_of_float_minus by (rule order_trans)
  1.1341 +    have "- the (ub_ln prec ?divr) \<le> ?ln" unfolding real_of_float_uminus by (rule order_trans)
  1.1342    }
  1.1343    ultimately show ?thesis unfolding lb_ln.simps[where x=x]  ub_ln.simps[where x=x]
  1.1344      unfolding if_not_P[OF `\<not> x \<le> 0`] if_P[OF True] by auto
  1.1345 @@ -2012,7 +2074,7 @@
  1.1346  proof -
  1.1347    have "0 < x"
  1.1348    proof (rule ccontr)
  1.1349 -    assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
  1.1350 +    assume "\<not> 0 < x" hence "x \<le> 0" unfolding less_eq_float_def less_float_def by auto
  1.1351      thus False using assms by auto
  1.1352    qed
  1.1353    thus "0 < real x" unfolding less_float_def by auto
  1.1354 @@ -2025,7 +2087,7 @@
  1.1355  proof -
  1.1356    have "0 < x"
  1.1357    proof (rule ccontr)
  1.1358 -    assume "\<not> 0 < x" hence "x \<le> 0" unfolding le_float_def less_float_def by auto
  1.1359 +    assume "\<not> 0 < x" hence "x \<le> 0" unfolding less_eq_float_def less_float_def by auto
  1.1360      thus False using assms by auto
  1.1361    qed
  1.1362    thus "0 < real x" unfolding less_float_def by auto
  1.1363 @@ -2174,12 +2236,12 @@
  1.1364    unfolding bounded_by_def by auto
  1.1365  
  1.1366  fun approx approx' :: "nat \<Rightarrow> floatarith \<Rightarrow> (float * float) option list \<Rightarrow> (float * float) option" where
  1.1367 -"approx' prec a bs          = (case (approx prec a bs) of Some (l, u) \<Rightarrow> Some (round_down prec l, round_up prec u) | None \<Rightarrow> None)" |
  1.1368 +"approx' prec a bs          = (case (approx prec a bs) of Some (l, u) \<Rightarrow> Some (float_round_down prec l, float_round_up prec u) | None \<Rightarrow> None)" |
  1.1369  "approx prec (Add a b) bs   = lift_bin' (approx' prec a bs) (approx' prec b bs) (\<lambda> l1 u1 l2 u2. (l1 + l2, u1 + u2))" |
  1.1370  "approx prec (Minus a) bs   = lift_un' (approx' prec a bs) (\<lambda> l u. (-u, -l))" |
  1.1371  "approx prec (Mult a b) bs  = lift_bin' (approx' prec a bs) (approx' prec b bs)
  1.1372 -                                    (\<lambda> 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,
  1.1373 -                                                     float_pprt a2 * float_pprt b2 + float_pprt a1 * float_nprt b2 + float_nprt a2 * float_pprt b1 + float_nprt a1 * float_nprt b1))" |
  1.1374 +                                    (\<lambda> a1 a2 b1 b2. (nprt a1 * pprt b2 + nprt a2 * nprt b2 + pprt a1 * pprt b1 + pprt a2 * nprt b1,
  1.1375 +                                                     pprt a2 * pprt b2 + pprt a1 * nprt b2 + nprt a2 * pprt b1 + nprt a1 * nprt b1))" |
  1.1376  "approx prec (Inverse a) bs = lift_un (approx' prec a bs) (\<lambda> l u. if (0 < l \<or> u < 0) then (Some (float_divl prec 1 u), Some (float_divr prec 1 l)) else (None, None))" |
  1.1377  "approx prec (Cos a) bs     = lift_un' (approx' prec a bs) (bnds_cos prec)" |
  1.1378  "approx prec Pi bs          = Some (lb_pi prec, ub_pi prec)" |
  1.1379 @@ -2233,11 +2295,11 @@
  1.1380  proof -
  1.1381    obtain l' u' where S: "Some (l', u') = approx prec a vs"
  1.1382      using approx' unfolding approx'.simps by (cases "approx prec a vs", auto)
  1.1383 -  have l': "l = round_down prec l'" and u': "u = round_up prec u'"
  1.1384 +  have l': "l = float_round_down prec l'" and u': "u = float_round_up prec u'"
  1.1385      using approx' unfolding approx'.simps S[symmetric] by auto
  1.1386    show ?thesis unfolding l' u'
  1.1387 -    using order_trans[OF Pa[OF S, THEN conjunct2] round_up[of u']]
  1.1388 -    using order_trans[OF round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto
  1.1389 +    using order_trans[OF Pa[OF S, THEN conjunct2] float_round_up[of u']]
  1.1390 +    using order_trans[OF float_round_down[of _ l'] Pa[OF S, THEN conjunct1]] by auto
  1.1391  qed
  1.1392  
  1.1393  lemma lift_bin':
  1.1394 @@ -2394,11 +2456,11 @@
  1.1395    case (Mult a b)
  1.1396    from lift_bin'[OF Mult.prems[unfolded approx.simps]] Mult.hyps
  1.1397    obtain l1 u1 l2 u2
  1.1398 -    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"
  1.1399 -    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"
  1.1400 +    where l: "l = nprt l1 * pprt u2 + nprt u1 * nprt u2 + pprt l1 * pprt l2 + pprt u1 * nprt l2"
  1.1401 +    and u: "u = pprt u1 * pprt u2 + pprt l1 * nprt u2 + nprt u1 * pprt l2 + nprt l1 * nprt l2"
  1.1402      and "l1 \<le> interpret_floatarith a xs" and "interpret_floatarith a xs \<le> u1"
  1.1403      and "l2 \<le> interpret_floatarith b xs" and "interpret_floatarith b xs \<le> u2" unfolding fst_conv snd_conv by blast
  1.1404 -  thus ?case unfolding interpret_floatarith.simps l u real_of_float_add real_of_float_mult real_of_float_nprt real_of_float_pprt
  1.1405 +  thus ?case unfolding interpret_floatarith.simps l u
  1.1406      using mult_le_prts mult_ge_prts by auto
  1.1407  next
  1.1408    case (Inverse a)
  1.1409 @@ -2443,7 +2505,7 @@
  1.1410    from lift_un'[OF Abs.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Abs.hyps
  1.1411    obtain l1 u1 where l': "l = (if l1 < 0 \<and> 0 < u1 then 0 else min \<bar>l1\<bar> \<bar>u1\<bar>)" and u': "u = max \<bar>l1\<bar> \<bar>u1\<bar>"
  1.1412      and l1: "l1 \<le> interpret_floatarith x xs" and u1: "interpret_floatarith x xs \<le> u1" by blast
  1.1413 -  thus ?case unfolding l' u' by (cases "l1 < 0 \<and> 0 < u1", auto simp add: real_of_float_min real_of_float_max real_of_float_abs less_float_def)
  1.1414 +  thus ?case unfolding l' u' by (cases "l1 < 0 \<and> 0 < u1", auto simp add: real_of_float_min real_of_float_max less_float_def)
  1.1415  next
  1.1416    case (Min a b)
  1.1417    from lift_bin'[OF Min.prems[unfolded approx.simps], unfolded fst_conv snd_conv] Min.hyps
  1.1418 @@ -2527,7 +2589,7 @@
  1.1419  
  1.1420    let ?m = "(l + u) * Float 1 -1"
  1.1421    have "real l \<le> ?m" and "?m \<le> real u"
  1.1422 -    unfolding le_float_def using Suc.prems by auto
  1.1423 +    unfolding less_eq_float_def using Suc.prems by auto
  1.1424  
  1.1425    with `x \<in> { l .. u }`
  1.1426    have "x \<in> { l .. ?m} \<or> x \<in> { ?m .. u }" by auto
  1.1427 @@ -2576,7 +2638,7 @@
  1.1428    then obtain n
  1.1429      where x_eq: "x = Var n" by (cases x) auto
  1.1430  
  1.1431 -  with Assign.prems obtain l u' l' u
  1.1432 +  with Assign.prems obtain l u
  1.1433      where bnd_eq: "Some (l, u) = approx prec a vs"
  1.1434      and x_eq: "x = Var n"
  1.1435      and approx_form': "approx_form' prec f (ss ! n) n l u vs ss"
  1.1436 @@ -2612,7 +2674,7 @@
  1.1437      and inequality: "u \<le> l'"
  1.1438      by (cases "approx prec a vs", auto,
  1.1439        cases "approx prec b vs", auto)
  1.1440 -  from inequality[unfolded le_float_def] approx[OF LessEqual.prems(2) l_eq] approx[OF LessEqual.prems(2) u_eq]
  1.1441 +  from inequality[unfolded less_eq_float_def] approx[OF LessEqual.prems(2) l_eq] approx[OF LessEqual.prems(2) u_eq]
  1.1442    show ?case by auto
  1.1443  next
  1.1444    case (AtLeastAtMost x a b)
  1.1445 @@ -2624,7 +2686,7 @@
  1.1446      by (cases "approx prec x vs", auto,
  1.1447        cases "approx prec a vs", auto,
  1.1448        cases "approx prec b vs", auto, blast)
  1.1449 -  from inequality[unfolded le_float_def] approx[OF AtLeastAtMost.prems(2) l_eq] approx[OF AtLeastAtMost.prems(2) u_eq] approx[OF AtLeastAtMost.prems(2) x_eq]
  1.1450 +  from inequality[unfolded less_eq_float_def] approx[OF AtLeastAtMost.prems(2) l_eq] approx[OF AtLeastAtMost.prems(2) u_eq] approx[OF AtLeastAtMost.prems(2) x_eq]
  1.1451    show ?case by auto
  1.1452  qed
  1.1453  
  1.1454 @@ -2685,12 +2747,14 @@
  1.1455      by (auto intro!: DERIV_intros
  1.1456               simp add: algebra_simps power2_eq_square)
  1.1457  next case (Cos a) thus ?case
  1.1458 -  by (auto intro!: DERIV_intros
  1.1459 +  apply (auto intro!: DERIV_intros
  1.1460             simp del: interpret_floatarith.simps(5)
  1.1461             simp add: interpret_floatarith_sin interpret_floatarith.simps(5)[of a])
  1.1462 +  apply (simp add: cos_sin_eq)
  1.1463 +  done
  1.1464  next case (Power a n) thus ?case
  1.1465    by (cases n, auto intro!: DERIV_intros
  1.1466 -                    simp del: power_Suc simp add: real_eq_of_nat)
  1.1467 +                    simp del: power_Suc)
  1.1468  next case (Ln a) thus ?case
  1.1469      by (auto intro!: DERIV_intros simp add: divide_inverse)
  1.1470  next case (Var i) thus ?case using `n < length vs` by auto
  1.1471 @@ -3056,7 +3120,7 @@
  1.1472      by (auto simp add: Let_def lazy_conj)
  1.1473  
  1.1474    have m_l: "real l \<le> ?m" and m_u: "?m \<le> real u"
  1.1475 -    unfolding le_float_def using Suc.prems by auto
  1.1476 +    unfolding less_eq_float_def using Suc.prems by auto
  1.1477  
  1.1478    with `x \<in> { l .. u }`
  1.1479    have "x \<in> { l .. ?m} \<or> x \<in> { ?m .. u }" by auto
  1.1480 @@ -3118,7 +3182,7 @@
  1.1481    from approx_tse[OF this _ _ _ _ tse[symmetric], of l' u'] x'
  1.1482    have "ly \<le> interpret_floatarith a [x] - interpret_floatarith b [x]"
  1.1483      by (auto simp add: diff_minus)
  1.1484 -  from order_trans[OF `0 \<le> ly`[unfolded le_float_def] this]
  1.1485 +  from order_trans[OF `0 \<le> ly`[unfolded less_eq_float_def] this]
  1.1486    show ?thesis by auto
  1.1487  qed
  1.1488  
     2.1 --- a/src/HOL/Library/Float.thy	Wed Apr 18 14:29:20 2012 +0200
     2.2 +++ b/src/HOL/Library/Float.thy	Wed Apr 18 14:29:21 2012 +0200
     2.3 @@ -1,509 +1,925 @@
     2.4 -(*  Title:      HOL/Library/Float.thy
     2.5 -    Author:     Steven Obua 2008
     2.6 -    Author:     Johannes Hoelzl, TU Muenchen <hoelzl@in.tum.de> 2008 / 2009
     2.7 -*)
     2.8 -
     2.9  header {* Floating-Point Numbers *}
    2.10  
    2.11  theory Float
    2.12 -imports Complex_Main Lattice_Algebras
    2.13 +imports Complex_Main "~~/src/HOL/Library/Lattice_Algebras"
    2.14  begin
    2.15  
    2.16 -definition pow2 :: "int \<Rightarrow> real" where
    2.17 -  [simp]: "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
    2.18 -
    2.19 -datatype float = Float int int
    2.20 -
    2.21 -primrec of_float :: "float \<Rightarrow> real" where
    2.22 -  "of_float (Float a b) = real a * pow2 b"
    2.23 -
    2.24 -defs (overloaded)
    2.25 -  real_of_float_def [code_unfold]: "real == of_float"
    2.26 -
    2.27 -declare [[coercion "% x . Float x 0"]]
    2.28 -declare [[coercion "real::float\<Rightarrow>real"]]
    2.29 -
    2.30 -primrec mantissa :: "float \<Rightarrow> int" where
    2.31 -  "mantissa (Float a b) = a"
    2.32 -
    2.33 -primrec scale :: "float \<Rightarrow> int" where
    2.34 -  "scale (Float a b) = b"
    2.35 -
    2.36 -instantiation float :: zero
    2.37 -begin
    2.38 -definition zero_float where "0 = Float 0 0"
    2.39 -instance ..
    2.40 -end
    2.41 -
    2.42 -instantiation float :: one
    2.43 -begin
    2.44 -definition one_float where "1 = Float 1 0"
    2.45 -instance ..
    2.46 -end
    2.47 -
    2.48 -lemma real_of_float_simp[simp]: "real (Float a b) = real a * pow2 b"
    2.49 -  unfolding real_of_float_def using of_float.simps .
    2.50 -
    2.51 -lemma real_of_float_neg_exp: "e < 0 \<Longrightarrow> real (Float m e) = real m * inverse (2^nat (-e))" by auto
    2.52 -lemma real_of_float_nge0_exp: "\<not> 0 \<le> e \<Longrightarrow> real (Float m e) = real m * inverse (2^nat (-e))" by auto
    2.53 -lemma real_of_float_ge0_exp: "0 \<le> e \<Longrightarrow> real (Float m e) = real m * (2^nat e)" by auto
    2.54 -
    2.55 -lemma Float_num[simp]: shows
    2.56 -   "real (Float 1 0) = 1" and "real (Float 1 1) = 2" and "real (Float 1 2) = 4" and
    2.57 -   "real (Float 1 -1) = 1/2" and "real (Float 1 -2) = 1/4" and "real (Float 1 -3) = 1/8" and
    2.58 -   "real (Float -1 0) = -1" and "real (Float (numeral n) 0) = numeral n"
    2.59 +typedef float = "{m * 2 powr e | (m :: int) (e :: int). True }"
    2.60 +  morphisms real_of_float float_of
    2.61    by auto
    2.62  
    2.63 -lemma float_number_of_int[simp]: "real (Float n 0) = real n"
    2.64 -  by simp
    2.65 +declare [[coercion "real::float\<Rightarrow>real"]]
    2.66 +
    2.67 +lemmas float_of_inject[simp]
    2.68 +lemmas float_of_cases2 = float_of_cases[case_product float_of_cases]
    2.69 +lemmas float_of_cases3 = float_of_cases2[case_product float_of_cases]
    2.70 +
    2.71 +defs (overloaded)
    2.72 +  real_of_float_def[code_unfold]: "real == real_of_float"
    2.73  
    2.74 -lemma pow2_0[simp]: "pow2 0 = 1" by simp
    2.75 -lemma pow2_1[simp]: "pow2 1 = 2" by simp
    2.76 -lemma pow2_neg: "pow2 x = inverse (pow2 (-x))" by simp
    2.77 +lemma real_of_float_eq[simp]:
    2.78 +  fixes f1 f2 :: float shows "real f1 = real f2 \<longleftrightarrow> f1 = f2"
    2.79 +  unfolding real_of_float_def real_of_float_inject ..
    2.80 +
    2.81 +lemma float_of_real[simp]: "float_of (real x) = x"
    2.82 +  unfolding real_of_float_def by (rule real_of_float_inverse)
    2.83  
    2.84 -lemma pow2_powr: "pow2 a = 2 powr a"
    2.85 -  by (simp add: powr_realpow[symmetric] powr_minus)
    2.86 +lemma real_float[simp]: "x \<in> float \<Longrightarrow> real (float_of x) = x"
    2.87 +  unfolding real_of_float_def by (rule float_of_inverse)
    2.88  
    2.89 -declare pow2_def[simp del]
    2.90 +subsection {* Real operations preserving the representation as floating point number *}
    2.91 +
    2.92 +lemma floatI: fixes m e :: int shows "m * 2 powr e = x \<Longrightarrow> x \<in> float"
    2.93 +  by (auto simp: float_def)
    2.94  
    2.95 -lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
    2.96 -  by (simp add: pow2_powr powr_add)
    2.97 +lemma zero_float[simp]: "0 \<in> float" by (auto simp: float_def)
    2.98 +lemma one_float[simp]: "1 \<in> float" by (intro floatI[of 1 0]) simp
    2.99 +lemma numeral_float[simp]: "numeral i \<in> float" by (intro floatI[of "numeral i" 0]) simp  
   2.100 +lemma neg_numeral_float[simp]: "neg_numeral i \<in> float" by (intro floatI[of "neg_numeral i" 0]) simp
   2.101 +lemma real_of_int_float[simp]: "real (x :: int) \<in> float" by (intro floatI[of x 0]) simp
   2.102 +lemma real_of_nat_float[simp]: "real (x ::nat) \<in> float" by (intro floatI[of x 0]) simp
   2.103 +lemma two_powr_int_float[simp]: "2 powr (real (i::int)) \<in> float" by (intro floatI[of 1 i]) simp
   2.104 +lemma two_powr_nat_float[simp]: "2 powr (real (i::nat)) \<in> float" by (intro floatI[of 1 i]) simp
   2.105 +lemma two_powr_minus_int_float[simp]: "2 powr - (real (i::int)) \<in> float" by (intro floatI[of 1 "-i"]) simp
   2.106 +lemma two_powr_minus_nat_float[simp]: "2 powr - (real (i::nat)) \<in> float" by (intro floatI[of 1 "-i"]) simp
   2.107 +lemma two_powr_numeral_float[simp]: "2 powr numeral i \<in> float" by (intro floatI[of 1 "numeral i"]) simp
   2.108 +lemma two_powr_neg_numeral_float[simp]: "2 powr neg_numeral i \<in> float" by (intro floatI[of 1 "neg_numeral i"]) simp
   2.109 +lemma two_pow_float[simp]: "2 ^ n \<in> float" by (intro floatI[of 1 "n"]) (simp add: powr_realpow)
   2.110 +lemma real_of_float_float[simp]: "real (f::float) \<in> float" by (cases f) simp
   2.111  
   2.112 -lemma pow2_diff: "pow2 (a - b) = pow2 a / pow2 b"
   2.113 -  by (simp add: pow2_powr powr_divide2)
   2.114 -  
   2.115 -lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
   2.116 -  by (simp add: pow2_add)
   2.117 -
   2.118 -lemma float_components[simp]: "Float (mantissa f) (scale f) = f" by (cases f) auto
   2.119 -
   2.120 -lemma float_split: "\<exists> a b. x = Float a b" by (cases x) auto
   2.121 +lemma plus_float[simp]: "r \<in> float \<Longrightarrow> p \<in> float \<Longrightarrow> r + p \<in> float"
   2.122 +  unfolding float_def
   2.123 +proof (safe, simp)
   2.124 +  fix e1 m1 e2 m2 :: int
   2.125 +  { fix e1 m1 e2 m2 :: int assume "e1 \<le> e2"
   2.126 +    then have "m1 * 2 powr e1 + m2 * 2 powr e2 = (m1 + m2 * 2 ^ nat (e2 - e1)) * 2 powr e1"
   2.127 +      by (simp add: powr_realpow[symmetric] powr_divide2[symmetric] field_simps)
   2.128 +    then have "\<exists>(m::int) (e::int). m1 * 2 powr e1 + m2 * 2 powr e2 = m * 2 powr e"
   2.129 +      by blast }
   2.130 +  note * = this
   2.131 +  show "\<exists>(m::int) (e::int). m1 * 2 powr e1 + m2 * 2 powr e2 = m * 2 powr e"
   2.132 +  proof (cases e1 e2 rule: linorder_le_cases)
   2.133 +    assume "e2 \<le> e1" from *[OF this, of m2 m1] show ?thesis by (simp add: ac_simps)
   2.134 +  qed (rule *)
   2.135 +qed
   2.136  
   2.137 -lemma float_split2: "(\<forall> a b. x \<noteq> Float a b) = False" by (auto simp add: float_split)
   2.138 +lemma uminus_float[simp]: "x \<in> float \<Longrightarrow> -x \<in> float"
   2.139 +  apply (auto simp: float_def)
   2.140 +  apply (rule_tac x="-x" in exI)
   2.141 +  apply (rule_tac x="xa" in exI)
   2.142 +  apply (simp add: field_simps)
   2.143 +  done
   2.144  
   2.145 -lemma float_zero[simp]: "real (Float 0 e) = 0" by simp
   2.146 +lemma times_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> x * y \<in> float"
   2.147 +  apply (auto simp: float_def)
   2.148 +  apply (rule_tac x="x * xa" in exI)
   2.149 +  apply (rule_tac x="xb + xc" in exI)
   2.150 +  apply (simp add: powr_add)
   2.151 +  done
   2.152  
   2.153 -lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
   2.154 -by arith
   2.155 +lemma minus_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> x - y \<in> float"
   2.156 +  unfolding ab_diff_minus by (intro uminus_float plus_float)
   2.157 +
   2.158 +lemma abs_float[simp]: "x \<in> float \<Longrightarrow> abs x \<in> float"
   2.159 +  by (cases x rule: linorder_cases[of 0]) auto
   2.160 +
   2.161 +lemma sgn_of_float[simp]: "x \<in> float \<Longrightarrow> sgn x \<in> float"
   2.162 +  by (cases x rule: linorder_cases[of 0]) (auto intro!: uminus_float)
   2.163  
   2.164 -function normfloat :: "float \<Rightarrow> float" where
   2.165 -  "normfloat (Float a b) =
   2.166 -    (if a \<noteq> 0 \<and> even a then normfloat (Float (a div 2) (b+1))
   2.167 -     else if a=0 then Float 0 0 else Float a b)"
   2.168 -by pat_completeness auto
   2.169 -termination by (relation "measure (nat o abs o mantissa)") (auto intro: abs_div_2_less)
   2.170 -declare normfloat.simps[simp del]
   2.171 +lemma div_power_2_float[simp]: "x \<in> float \<Longrightarrow> x / 2^d \<in> float"
   2.172 +  apply (auto simp add: float_def)
   2.173 +  apply (rule_tac x="x" in exI)
   2.174 +  apply (rule_tac x="xa - d" in exI)
   2.175 +  apply (simp add: powr_realpow[symmetric] field_simps powr_add[symmetric])
   2.176 +  done
   2.177 +
   2.178 +lemma div_power_2_int_float[simp]: "x \<in> float \<Longrightarrow> x / (2::int)^d \<in> float"
   2.179 +  apply (auto simp add: float_def)
   2.180 +  apply (rule_tac x="x" in exI)
   2.181 +  apply (rule_tac x="xa - d" in exI)
   2.182 +  apply (simp add: powr_realpow[symmetric] field_simps powr_add[symmetric])
   2.183 +  done
   2.184  
   2.185 -theorem normfloat[symmetric, simp]: "real f = real (normfloat f)"
   2.186 -proof (induct f rule: normfloat.induct)
   2.187 -  case (1 a b)
   2.188 -  have real2: "2 = real (2::int)"
   2.189 -    by auto
   2.190 -  show ?case
   2.191 -    apply (subst normfloat.simps)
   2.192 -    apply auto
   2.193 -    apply (subst 1[symmetric])
   2.194 -    apply (auto simp add: pow2_add even_def)
   2.195 -    done
   2.196 +lemma div_numeral_Bit0_float[simp]:
   2.197 +  assumes x: "x / numeral n \<in> float" shows "x / (numeral (Num.Bit0 n)) \<in> float"
   2.198 +proof -
   2.199 +  have "(x / numeral n) / 2^1 \<in> float"
   2.200 +    by (intro x div_power_2_float)
   2.201 +  also have "(x / numeral n) / 2^1 = x / (numeral (Num.Bit0 n))"
   2.202 +    by (induct n) auto
   2.203 +  finally show ?thesis .
   2.204 +qed
   2.205 +
   2.206 +lemma div_neg_numeral_Bit0_float[simp]:
   2.207 +  assumes x: "x / numeral n \<in> float" shows "x / (neg_numeral (Num.Bit0 n)) \<in> float"
   2.208 +proof -
   2.209 +  have "- (x / numeral (Num.Bit0 n)) \<in> float" using x by simp
   2.210 +  also have "- (x / numeral (Num.Bit0 n)) = x / neg_numeral (Num.Bit0 n)"
   2.211 +    unfolding neg_numeral_def by (simp del: minus_numeral)
   2.212 +  finally show ?thesis .
   2.213  qed
   2.214  
   2.215 -lemma pow2_neq_zero[simp]: "pow2 x \<noteq> 0"
   2.216 -  by (auto simp add: pow2_def)
   2.217 +subsection {* Arithmetic operations on floating point numbers *}
   2.218 +
   2.219 +instantiation float :: ring_1
   2.220 +begin
   2.221 +
   2.222 +definition [simp]: "(0::float) = float_of 0"
   2.223 +
   2.224 +definition [simp]: "(1::float) = float_of 1"
   2.225 +
   2.226 +definition "(x + y::float) = float_of (real x + real y)"
   2.227 +
   2.228 +lemma float_plus_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> float_of x + float_of y = float_of (x + y)"
   2.229 +  by (simp add: plus_float_def)
   2.230  
   2.231 -lemma pow2_int: "pow2 (int c) = 2^c"
   2.232 -  by (simp add: pow2_def)
   2.233 +definition "(-x::float) = float_of (- real x)"
   2.234 +
   2.235 +lemma uminus_of_float[simp]: "x \<in> float \<Longrightarrow> - float_of x = float_of (- x)"
   2.236 +  by (simp add: uminus_float_def)
   2.237 +
   2.238 +definition "(x - y::float) = float_of (real x - real y)"
   2.239  
   2.240 -lemma zero_less_pow2[simp]: "0 < pow2 x"
   2.241 -  by (simp add: pow2_powr)
   2.242 +lemma float_minus_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> float_of x - float_of y = float_of (x - y)"
   2.243 +  by (simp add: minus_float_def)
   2.244 +
   2.245 +definition "(x * y::float) = float_of (real x * real y)"
   2.246 +
   2.247 +lemma float_times_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> float_of x * float_of y = float_of (x * y)"
   2.248 +  by (simp add: times_float_def)
   2.249  
   2.250 -lemma normfloat_imp_odd_or_zero:
   2.251 -  "normfloat f = Float a b \<Longrightarrow> odd a \<or> (a = 0 \<and> b = 0)"
   2.252 -proof (induct f rule: normfloat.induct)
   2.253 -  case (1 u v)
   2.254 -  from 1 have ab: "normfloat (Float u v) = Float a b" by auto
   2.255 -  {
   2.256 -    assume eu: "even u"
   2.257 -    assume z: "u \<noteq> 0"
   2.258 -    have "normfloat (Float u v) = normfloat (Float (u div 2) (v + 1))"
   2.259 -      apply (subst normfloat.simps)
   2.260 -      by (simp add: eu z)
   2.261 -    with ab have "normfloat (Float (u div 2) (v + 1)) = Float a b" by simp
   2.262 -    with 1 eu z have ?case by auto
   2.263 -  }
   2.264 -  note case1 = this
   2.265 -  {
   2.266 -    assume "odd u \<or> u = 0"
   2.267 -    then have ou: "\<not> (u \<noteq> 0 \<and> even u)" by auto
   2.268 -    have "normfloat (Float u v) = (if u = 0 then Float 0 0 else Float u v)"
   2.269 -      apply (subst normfloat.simps)
   2.270 -      apply (simp add: ou)
   2.271 -      done
   2.272 -    with ab have "Float a b = (if u = 0 then Float 0 0 else Float u v)" by auto
   2.273 -    then have ?case
   2.274 -      apply (case_tac "u=0")
   2.275 -      apply (auto)
   2.276 -      by (insert ou, auto)
   2.277 -  }
   2.278 -  note case2 = this
   2.279 -  show ?case
   2.280 -    apply (case_tac "odd u \<or> u = 0")
   2.281 -    apply (rule case2)
   2.282 -    apply simp
   2.283 -    apply (rule case1)
   2.284 -    apply auto
   2.285 -    done
   2.286 +instance
   2.287 +proof
   2.288 +  fix a b c :: float
   2.289 +  show "0 + a = a"
   2.290 +    by (cases a rule: float_of_cases) simp
   2.291 +  show "1 * a = a"
   2.292 +    by (cases a rule: float_of_cases) simp
   2.293 +  show "a * 1 = a"
   2.294 +    by (cases a rule: float_of_cases) simp
   2.295 +  show "-a + a = 0"
   2.296 +    by (cases a rule: float_of_cases) simp
   2.297 +  show "a + b = b + a"
   2.298 +    by (cases a b rule: float_of_cases2) (simp add: ac_simps)
   2.299 +  show "a - b = a + -b"
   2.300 +    by (cases a b rule: float_of_cases2) (simp add: field_simps)
   2.301 +  show "a + b + c = a + (b + c)"
   2.302 +    by (cases a b c rule: float_of_cases3) (simp add: ac_simps)
   2.303 +  show "a * b * c = a * (b * c)"
   2.304 +    by (cases a b c rule: float_of_cases3) (simp add: ac_simps)
   2.305 +  show "(a + b) * c = a * c + b * c"
   2.306 +    by (cases a b c rule: float_of_cases3) (simp add: field_simps)
   2.307 +  show "a * (b + c) = a * b + a * c"
   2.308 +    by (cases a b c rule: float_of_cases3) (simp add: field_simps)
   2.309 +  show "0 \<noteq> (1::float)" by simp
   2.310  qed
   2.311 +end
   2.312  
   2.313 -lemma float_eq_odd_helper: 
   2.314 -  assumes odd: "odd a'"
   2.315 -    and floateq: "real (Float a b) = real (Float a' b')"
   2.316 -  shows "b \<le> b'"
   2.317 -proof - 
   2.318 -  from odd have "a' \<noteq> 0" by auto
   2.319 -  with floateq have a': "real a' = real a * pow2 (b - b')"
   2.320 -    by (simp add: pow2_diff field_simps)
   2.321 +lemma real_of_float_uminus[simp]:
   2.322 +  fixes f g::float shows "real (- g) = - real g"
   2.323 +  by (simp add: uminus_float_def)
   2.324 +
   2.325 +lemma real_of_float_plus[simp]:
   2.326 +  fixes f g::float shows "real (f + g) = real f + real g"
   2.327 +  by (simp add: plus_float_def)
   2.328 +
   2.329 +lemma real_of_float_minus[simp]:
   2.330 +  fixes f g::float shows "real (f - g) = real f - real g"
   2.331 +  by (simp add: minus_float_def)
   2.332 +
   2.333 +lemma real_of_float_times[simp]:
   2.334 +  fixes f g::float shows "real (f * g) = real f * real g"
   2.335 +  by (simp add: times_float_def)
   2.336 +
   2.337 +lemma real_of_float_zero[simp]: "real (0::float) = 0" by simp
   2.338 +lemma real_of_float_one[simp]: "real (1::float) = 1" by simp
   2.339 +
   2.340 +lemma real_of_float_power[simp]: fixes f::float shows "real (f^n) = real f^n"
   2.341 +  by (induct n) simp_all
   2.342  
   2.343 -  {
   2.344 -    assume bcmp: "b > b'"
   2.345 -    then obtain c :: nat where "b - b' = int c + 1"
   2.346 -      by atomize_elim arith
   2.347 -    with a' have "real a' = real (a * 2^c * 2)"
   2.348 -      by (simp add: pow2_def nat_add_distrib)
   2.349 -    with odd have False
   2.350 -      unfolding real_of_int_inject by simp
   2.351 -  }
   2.352 -  then show ?thesis by arith
   2.353 -qed
   2.354 +instantiation float :: linorder
   2.355 +begin
   2.356 +
   2.357 +definition "x \<le> (y::float) \<longleftrightarrow> real x \<le> real y"
   2.358 +
   2.359 +lemma float_le_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> float_of x \<le> float_of y \<longleftrightarrow> x \<le> y"
   2.360 +  by (simp add: less_eq_float_def)
   2.361 +
   2.362 +definition "x < (y::float) \<longleftrightarrow> real x < real y"
   2.363 +
   2.364 +lemma float_less_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> float_of x < float_of y \<longleftrightarrow> x < y"
   2.365 +  by (simp add: less_float_def)
   2.366  
   2.367 -lemma float_eq_odd: 
   2.368 -  assumes odd1: "odd a"
   2.369 -    and odd2: "odd a'"
   2.370 -    and floateq: "real (Float a b) = real (Float a' b')"
   2.371 -  shows "a = a' \<and> b = b'"
   2.372 -proof -
   2.373 -  from 
   2.374 -     float_eq_odd_helper[OF odd2 floateq] 
   2.375 -     float_eq_odd_helper[OF odd1 floateq[symmetric]]
   2.376 -  have beq: "b = b'" by arith
   2.377 -  with floateq show ?thesis by auto
   2.378 +instance
   2.379 +proof
   2.380 +  fix a b c :: float
   2.381 +  show "a \<le> a"
   2.382 +    by (cases a rule: float_of_cases) simp
   2.383 +  show "a < b \<longleftrightarrow> a \<le> b \<and> \<not> b \<le> a"
   2.384 +    by (cases a b rule: float_of_cases2) auto
   2.385 +  show "a \<le> b \<or> b \<le> a"
   2.386 +    by (cases a b rule: float_of_cases2) auto
   2.387 +  { assume "a \<le> b" "b \<le> a" then show "a = b"
   2.388 +    by (cases a b rule: float_of_cases2) auto }
   2.389 +  { assume "a \<le> b" "b \<le> c" then show "a \<le> c"
   2.390 +    by (cases a b c rule: float_of_cases3) auto }
   2.391  qed
   2.392 +end
   2.393 +
   2.394 +lemma real_of_float_min: fixes a b::float shows "real (min a b) = min (real a) (real b)"
   2.395 +  by (simp add: min_def less_eq_float_def)
   2.396 +
   2.397 +lemma real_of_float_max: fixes a b::float shows "real (max a b) = max (real a) (real b)"
   2.398 +  by (simp add: max_def less_eq_float_def)
   2.399 +
   2.400 +instantiation float :: linordered_ring
   2.401 +begin
   2.402 +
   2.403 +definition "(abs x :: float) = float_of (abs (real x))"
   2.404  
   2.405 -theorem normfloat_unique:
   2.406 -  assumes real_of_float_eq: "real f = real g"
   2.407 -  shows "normfloat f = normfloat g"
   2.408 -proof - 
   2.409 -  from float_split[of "normfloat f"] obtain a b where normf:"normfloat f = Float a b" by auto
   2.410 -  from float_split[of "normfloat g"] obtain a' b' where normg:"normfloat g = Float a' b'" by auto
   2.411 -  have "real (normfloat f) = real (normfloat g)"
   2.412 -    by (simp add: real_of_float_eq)
   2.413 -  then have float_eq: "real (Float a b) = real (Float a' b')"
   2.414 -    by (simp add: normf normg)
   2.415 -  have ab: "odd a \<or> (a = 0 \<and> b = 0)" by (rule normfloat_imp_odd_or_zero[OF normf])
   2.416 -  have ab': "odd a' \<or> (a' = 0 \<and> b' = 0)" by (rule normfloat_imp_odd_or_zero[OF normg])
   2.417 -  {
   2.418 -    assume odd: "odd a"
   2.419 -    then have "a \<noteq> 0" by (simp add: even_def) arith
   2.420 -    with float_eq have "a' \<noteq> 0" by auto
   2.421 -    with ab' have "odd a'" by simp
   2.422 -    from odd this float_eq have "a = a' \<and> b = b'" by (rule float_eq_odd)
   2.423 -  }
   2.424 -  note odd_case = this
   2.425 -  {
   2.426 -    assume even: "even a"
   2.427 -    with ab have a0: "a = 0" by simp
   2.428 -    with float_eq have a0': "a' = 0" by auto 
   2.429 -    from a0 a0' ab ab' have "a = a' \<and> b = b'" by auto
   2.430 -  }
   2.431 -  note even_case = this
   2.432 -  from odd_case even_case show ?thesis
   2.433 -    apply (simp add: normf normg)
   2.434 -    apply (case_tac "even a")
   2.435 -    apply auto
   2.436 +lemma float_abs[simp]: "x \<in> float \<Longrightarrow> abs (float_of x) = float_of (abs x)"
   2.437 +  by (simp add: abs_float_def)
   2.438 +
   2.439 +instance
   2.440 +proof
   2.441 +  fix a b c :: float
   2.442 +  show "\<bar>a\<bar> = (if a < 0 then - a else a)"
   2.443 +    by (cases a rule: float_of_cases) simp
   2.444 +  assume "a \<le> b"
   2.445 +  then show "c + a \<le> c + b"
   2.446 +    by (cases a b c rule: float_of_cases3) simp
   2.447 +  assume "0 \<le> c"
   2.448 +  with `a \<le> b` show "c * a \<le> c * b"
   2.449 +    by (cases a b c rule: float_of_cases3) (auto intro: mult_left_mono)
   2.450 +  from `0 \<le> c` `a \<le> b` show "a * c \<le> b * c"
   2.451 +    by (cases a b c rule: float_of_cases3) (auto intro: mult_right_mono)
   2.452 +qed
   2.453 +end
   2.454 +
   2.455 +lemma real_of_abs_float[simp]: fixes f::float shows "real (abs f) = abs (real f)"
   2.456 +  unfolding abs_float_def by simp
   2.457 +
   2.458 +instance float :: dense_linorder
   2.459 +proof
   2.460 +  fix a b :: float
   2.461 +  show "\<exists>c. a < c"
   2.462 +    apply (intro exI[of _ "a + 1"])
   2.463 +    apply (cases a rule: float_of_cases)
   2.464 +    apply simp
   2.465 +    done
   2.466 +  show "\<exists>c. c < a"
   2.467 +    apply (intro exI[of _ "a - 1"])
   2.468 +    apply (cases a rule: float_of_cases)
   2.469 +    apply simp
   2.470 +    done
   2.471 +  assume "a < b"
   2.472 +  then show "\<exists>c. a < c \<and> c < b"
   2.473 +    apply (intro exI[of _ "(b + a) * float_of (1/2)"])
   2.474 +    apply (cases a b rule: float_of_cases2)
   2.475 +    apply simp
   2.476      done
   2.477  qed
   2.478  
   2.479 -instantiation float :: plus
   2.480 -begin
   2.481 -fun plus_float where
   2.482 -[simp del]: "(Float a_m a_e) + (Float b_m b_e) = 
   2.483 -     (if a_e \<le> b_e then Float (a_m + b_m * 2^(nat(b_e - a_e))) a_e 
   2.484 -                   else Float (a_m * 2^(nat (a_e - b_e)) + b_m) b_e)"
   2.485 -instance ..
   2.486 -end
   2.487 -
   2.488 -instantiation float :: uminus
   2.489 -begin
   2.490 -primrec uminus_float where [simp del]: "uminus_float (Float m e) = Float (-m) e"
   2.491 -instance ..
   2.492 -end
   2.493 -
   2.494 -instantiation float :: minus
   2.495 +instantiation float :: linordered_idom
   2.496  begin
   2.497 -definition minus_float where "(z::float) - w = z + (- w)"
   2.498 -instance ..
   2.499 -end
   2.500 +
   2.501 +definition "sgn x = float_of (sgn (real x))"
   2.502  
   2.503 -instantiation float :: times
   2.504 -begin
   2.505 -fun times_float where [simp del]: "(Float a_m a_e) * (Float b_m b_e) = Float (a_m * b_m) (a_e + b_e)"
   2.506 -instance ..
   2.507 -end
   2.508 +lemma sgn_float[simp]: "x \<in> float \<Longrightarrow> sgn (float_of x) = float_of (sgn x)"
   2.509 +  by (simp add: sgn_float_def)
   2.510  
   2.511 -primrec float_pprt :: "float \<Rightarrow> float" where
   2.512 -  "float_pprt (Float a e) = (if 0 <= a then (Float a e) else 0)"
   2.513 -
   2.514 -primrec float_nprt :: "float \<Rightarrow> float" where
   2.515 -  "float_nprt (Float a e) = (if 0 <= a then 0 else (Float a e))" 
   2.516 -
   2.517 -instantiation float :: ord
   2.518 -begin
   2.519 -definition le_float_def: "z \<le> (w :: float) \<equiv> real z \<le> real w"
   2.520 -definition less_float_def: "z < (w :: float) \<equiv> real z < real w"
   2.521 -instance ..
   2.522 +instance
   2.523 +proof
   2.524 +  fix a b c :: float
   2.525 +  show "sgn a = (if a = 0 then 0 else if 0 < a then 1 else - 1)"
   2.526 +    by (cases a rule: float_of_cases) simp
   2.527 +  show "a * b = b * a"
   2.528 +    by (cases a b rule: float_of_cases2) (simp add: field_simps)
   2.529 +  show "1 * a = a" "(a + b) * c = a * c + b * c"
   2.530 +    by (simp_all add: field_simps del: one_float_def)
   2.531 +  assume "a < b" "0 < c" then show "c * a < c * b"
   2.532 +    by (cases a b c rule: float_of_cases3) (simp add: field_simps)
   2.533 +qed
   2.534  end
   2.535  
   2.536 -lemma real_of_float_add[simp]: "real (a + b) = real a + real (b :: float)"
   2.537 -  by (cases a, cases b) (simp add: algebra_simps plus_float.simps, 
   2.538 -      auto simp add: pow2_int[symmetric] pow2_add[symmetric])
   2.539 -
   2.540 -lemma real_of_float_minus[simp]: "real (- a) = - real (a :: float)"
   2.541 -  by (cases a) simp
   2.542 -
   2.543 -lemma real_of_float_sub[simp]: "real (a - b) = real a - real (b :: float)"
   2.544 -  by (cases a, cases b) (simp add: minus_float_def)
   2.545 -
   2.546 -lemma real_of_float_mult[simp]: "real (a*b) = real a * real (b :: float)"
   2.547 -  by (cases a, cases b) (simp add: times_float.simps pow2_add)
   2.548 -
   2.549 -lemma real_of_float_0[simp]: "real (0 :: float) = 0"
   2.550 -  by (auto simp add: zero_float_def)
   2.551 +definition Float :: "int \<Rightarrow> int \<Rightarrow> float" where
   2.552 +  [simp, code del]: "Float m e = float_of (m * 2 powr e)"
   2.553  
   2.554 -lemma real_of_float_1[simp]: "real (1 :: float) = 1"
   2.555 -  by (auto simp add: one_float_def)
   2.556 +lemma real_of_float_Float[code]: "real_of_float (Float m e) =
   2.557 +  (if e \<ge> 0 then m * 2 ^ nat e else m * inverse (2 ^ nat (- e)))"
   2.558 +by (auto simp add: powr_realpow[symmetric] powr_minus real_of_float_def[symmetric])
   2.559  
   2.560 -lemma zero_le_float:
   2.561 -  "(0 <= real (Float a b)) = (0 <= a)"
   2.562 -  apply auto
   2.563 -  apply (auto simp add: zero_le_mult_iff)
   2.564 -  apply (insert zero_less_pow2[of b])
   2.565 -  apply (simp_all)
   2.566 -  done
   2.567 +code_datatype Float
   2.568  
   2.569 -lemma float_le_zero:
   2.570 -  "(real (Float a b) <= 0) = (a <= 0)"
   2.571 -  apply auto
   2.572 -  apply (auto simp add: mult_le_0_iff)
   2.573 -  apply (insert zero_less_pow2[of b])
   2.574 -  apply auto
   2.575 -  done
   2.576 +lemma real_Float: "real (Float m e) = m * 2 powr e" by simp
   2.577  
   2.578 -lemma zero_less_float:
   2.579 -  "(0 < real (Float a b)) = (0 < a)"
   2.580 -  apply auto
   2.581 -  apply (auto simp add: zero_less_mult_iff)
   2.582 -  apply (insert zero_less_pow2[of b])
   2.583 -  apply (simp_all)
   2.584 -  done
   2.585 -
   2.586 -lemma float_less_zero:
   2.587 -  "(real (Float a b) < 0) = (a < 0)"
   2.588 -  apply auto
   2.589 -  apply (auto simp add: mult_less_0_iff)
   2.590 -  apply (insert zero_less_pow2[of b])
   2.591 -  apply (simp_all)
   2.592 -  done
   2.593 -
   2.594 -declare real_of_float_simp[simp del]
   2.595 +definition normfloat :: "float \<Rightarrow> float" where
   2.596 +  [simp]: "normfloat x = x"
   2.597  
   2.598 -lemma real_of_float_pprt[simp]: "real (float_pprt a) = pprt (real a)"
   2.599 -  by (cases a) (auto simp add: zero_le_float float_le_zero)
   2.600 -
   2.601 -lemma real_of_float_nprt[simp]: "real (float_nprt a) = nprt (real a)"
   2.602 -  by (cases a) (auto simp add: zero_le_float float_le_zero)
   2.603 +lemma compute_normfloat[code]: "normfloat (Float m e) =
   2.604 +  (if m mod 2 = 0 \<and> m \<noteq> 0 then normfloat (Float (m div 2) (e + 1))
   2.605 +                           else if m = 0 then 0 else Float m e)"
   2.606 +  by (simp del: real_of_int_add split: prod.split)
   2.607 +     (auto simp add: powr_add zmod_eq_0_iff)
   2.608  
   2.609 -instance float :: ab_semigroup_add
   2.610 -proof (intro_classes)
   2.611 -  fix a b c :: float
   2.612 -  show "a + b + c = a + (b + c)"
   2.613 -    by (cases a, cases b, cases c)
   2.614 -      (auto simp add: algebra_simps power_add[symmetric] plus_float.simps)
   2.615 -next
   2.616 -  fix a b :: float
   2.617 -  show "a + b = b + a"
   2.618 -    by (cases a, cases b) (simp add: plus_float.simps)
   2.619 -qed
   2.620 +lemma compute_zero[code_unfold, code]: "0 = Float 0 0"
   2.621 +  by simp
   2.622 +
   2.623 +lemma compute_one[code_unfold, code]: "1 = Float 1 0"
   2.624 +  by simp
   2.625  
   2.626  instance float :: numeral ..
   2.627  
   2.628 -lemma Float_add_same_scale: "Float x e + Float y e = Float (x + y) e"
   2.629 -  by (simp add: plus_float.simps)
   2.630 +lemma float_of_numeral[simp]: "numeral k = float_of (numeral k)"
   2.631 +  by (induct k)
   2.632 +     (simp_all only: numeral.simps one_float_def float_plus_float numeral_float one_float plus_float)
   2.633 +
   2.634 +lemma float_of_neg_numeral[simp]: "neg_numeral k = float_of (neg_numeral k)"
   2.635 +  by (simp add: minus_numeral[symmetric] del: minus_numeral)
   2.636  
   2.637 -(* FIXME: define other constant for code_unfold_post *)
   2.638 -lemma numeral_float_Float (*[code_unfold_post]*):
   2.639 -  "numeral k = Float (numeral k) 0"
   2.640 -  by (induct k, simp_all only: numeral.simps one_float_def
   2.641 -    Float_add_same_scale)
   2.642 +lemma
   2.643 +  shows float_numeral[simp]: "real (numeral x :: float) = numeral x"
   2.644 +    and float_neg_numeral[simp]: "real (neg_numeral x :: float) = neg_numeral x"
   2.645 +  by simp_all
   2.646  
   2.647 -lemma float_number_of[simp]: "real (numeral x :: float) = numeral x"
   2.648 -  by (simp only: numeral_float_Float Float_num)
   2.649 +subsection {* Represent floats as unique mantissa and exponent *}
   2.650  
   2.651  
   2.652 -instance float :: comm_monoid_mult
   2.653 -proof (intro_classes)
   2.654 -  fix a b c :: float
   2.655 -  show "a * b * c = a * (b * c)"
   2.656 -    by (cases a, cases b, cases c) (simp add: times_float.simps)
   2.657 -next
   2.658 -  fix a b :: float
   2.659 -  show "a * b = b * a"
   2.660 -    by (cases a, cases b) (simp add: times_float.simps)
   2.661 -next
   2.662 -  fix a :: float
   2.663 -  show "1 * a = a"
   2.664 -    by (cases a) (simp add: times_float.simps one_float_def)
   2.665 +lemma int_induct_abs[case_names less]:
   2.666 +  fixes j :: int
   2.667 +  assumes H: "\<And>n. (\<And>i. \<bar>i\<bar> < \<bar>n\<bar> \<Longrightarrow> P i) \<Longrightarrow> P n"
   2.668 +  shows "P j"
   2.669 +proof (induct "nat \<bar>j\<bar>" arbitrary: j rule: less_induct)
   2.670 +  case less show ?case by (rule H[OF less]) simp
   2.671 +qed
   2.672 +
   2.673 +lemma int_cancel_factors:
   2.674 +  fixes n :: int assumes "1 < r" shows "n = 0 \<or> (\<exists>k i. n = k * r ^ i \<and> \<not> r dvd k)"
   2.675 +proof (induct n rule: int_induct_abs)
   2.676 +  case (less n)
   2.677 +  { fix m assume n: "n \<noteq> 0" "n = m * r"
   2.678 +    then have "\<bar>m \<bar> < \<bar>n\<bar>"
   2.679 +      by (metis abs_dvd_iff abs_ge_self assms comm_semiring_1_class.normalizing_semiring_rules(7)
   2.680 +                dvd_imp_le_int dvd_refl dvd_triv_right linorder_neq_iff linorder_not_le
   2.681 +                mult_eq_0_iff zdvd_mult_cancel1)
   2.682 +    from less[OF this] n have "\<exists>k i. n = k * r ^ Suc i \<and> \<not> r dvd k" by auto }
   2.683 +  then show ?case
   2.684 +    by (metis comm_semiring_1_class.normalizing_semiring_rules(12,7) dvdE power_0)
   2.685 +qed
   2.686 +
   2.687 +lemma mult_powr_eq_mult_powr_iff_asym:
   2.688 +  fixes m1 m2 e1 e2 :: int
   2.689 +  assumes m1: "\<not> 2 dvd m1" and "e1 \<le> e2"
   2.690 +  shows "m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2"
   2.691 +proof
   2.692 +  have "m1 \<noteq> 0" using m1 unfolding dvd_def by auto
   2.693 +  assume eq: "m1 * 2 powr e1 = m2 * 2 powr e2"
   2.694 +  with `e1 \<le> e2` have "m1 = m2 * 2 powr nat (e2 - e1)"
   2.695 +    by (simp add: powr_divide2[symmetric] field_simps)
   2.696 +  also have "\<dots> = m2 * 2^nat (e2 - e1)"
   2.697 +    by (simp add: powr_realpow)
   2.698 +  finally have m1_eq: "m1 = m2 * 2^nat (e2 - e1)"
   2.699 +    unfolding real_of_int_inject .
   2.700 +  with m1 have "m1 = m2"
   2.701 +    by (cases "nat (e2 - e1)") (auto simp add: dvd_def)
   2.702 +  then show "m1 = m2 \<and> e1 = e2"
   2.703 +    using eq `m1 \<noteq> 0` by (simp add: powr_inj)
   2.704 +qed simp
   2.705 +
   2.706 +lemma mult_powr_eq_mult_powr_iff:
   2.707 +  fixes m1 m2 e1 e2 :: int
   2.708 +  shows "\<not> 2 dvd m1 \<Longrightarrow> \<not> 2 dvd m2 \<Longrightarrow> m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2"
   2.709 +  using mult_powr_eq_mult_powr_iff_asym[of m1 e1 e2 m2]
   2.710 +  using mult_powr_eq_mult_powr_iff_asym[of m2 e2 e1 m1]
   2.711 +  by (cases e1 e2 rule: linorder_le_cases) auto
   2.712 +
   2.713 +lemma floatE_normed:
   2.714 +  assumes x: "x \<in> float"
   2.715 +  obtains (zero) "x = 0"
   2.716 +   | (powr) m e :: int where "x = m * 2 powr e" "\<not> 2 dvd m" "x \<noteq> 0"
   2.717 +proof atomize_elim
   2.718 +  { assume "x \<noteq> 0"
   2.719 +    from x obtain m e :: int where x: "x = m * 2 powr e" by (auto simp: float_def)
   2.720 +    with `x \<noteq> 0` int_cancel_factors[of 2 m] obtain k i where "m = k * 2 ^ i" "\<not> 2 dvd k"
   2.721 +      by auto
   2.722 +    with `\<not> 2 dvd k` x have "\<exists>(m::int) (e::int). x = m * 2 powr e \<and> \<not> (2::int) dvd m"
   2.723 +      by (rule_tac exI[of _ "k"], rule_tac exI[of _ "e + int i"])
   2.724 +         (simp add: powr_add powr_realpow) }
   2.725 +  then show "x = 0 \<or> (\<exists>(m::int) (e::int). x = m * 2 powr e \<and> \<not> (2::int) dvd m \<and> x \<noteq> 0)"
   2.726 +    by blast
   2.727 +qed
   2.728 +
   2.729 +lemma float_normed_cases:
   2.730 +  fixes f :: float
   2.731 +  obtains (zero) "f = 0"
   2.732 +   | (powr) m e :: int where "real f = m * 2 powr e" "\<not> 2 dvd m" "f \<noteq> 0"
   2.733 +proof (atomize_elim, induct f)
   2.734 +  case (float_of y) then show ?case
   2.735 +    by (cases rule: floatE_normed) auto
   2.736 +qed
   2.737 +
   2.738 +definition mantissa :: "float \<Rightarrow> int" where
   2.739 +  "mantissa f = fst (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0)
   2.740 +   \<or> (f \<noteq> 0 \<and> real f = real (fst p) * 2 powr real (snd p) \<and> \<not> 2 dvd fst p))"
   2.741 +
   2.742 +definition exponent :: "float \<Rightarrow> int" where
   2.743 +  "exponent f = snd (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0)
   2.744 +   \<or> (f \<noteq> 0 \<and> real f = real (fst p) * 2 powr real (snd p) \<and> \<not> 2 dvd fst p))"
   2.745 +
   2.746 +lemma 
   2.747 +  shows exponent_0[simp]: "exponent (float_of 0) = 0" (is ?E)
   2.748 +    and mantissa_0[simp]: "mantissa (float_of 0) = 0" (is ?M)
   2.749 +proof -
   2.750 +  have "\<And>p::int \<times> int. fst p = 0 \<and> snd p = 0 \<longleftrightarrow> p = (0, 0)" by auto
   2.751 +  then show ?E ?M
   2.752 +    by (auto simp add: mantissa_def exponent_def)
   2.753  qed
   2.754  
   2.755 -(* Floats do NOT form a cancel_semigroup_add: *)
   2.756 -lemma "0 + Float 0 1 = 0 + Float 0 2"
   2.757 -  by (simp add: plus_float.simps zero_float_def)
   2.758 +lemma
   2.759 +  shows mantissa_exponent: "real f = mantissa f * 2 powr exponent f" (is ?E)
   2.760 +    and mantissa_not_dvd: "f \<noteq> (float_of 0) \<Longrightarrow> \<not> 2 dvd mantissa f" (is "_ \<Longrightarrow> ?D")
   2.761 +proof cases
   2.762 +  assume [simp]: "f \<noteq> (float_of 0)"
   2.763 +  have "f = mantissa f * 2 powr exponent f \<and> \<not> 2 dvd mantissa f"
   2.764 +  proof (cases f rule: float_normed_cases)
   2.765 +    case (powr m e)
   2.766 +    then have "\<exists>p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0)
   2.767 +     \<or> (f \<noteq> 0 \<and> real f = real (fst p) * 2 powr real (snd p) \<and> \<not> 2 dvd fst p)"
   2.768 +      by auto
   2.769 +    then show ?thesis
   2.770 +      unfolding exponent_def mantissa_def
   2.771 +      by (rule someI2_ex) simp
   2.772 +  qed simp
   2.773 +  then show ?E ?D by auto
   2.774 +qed simp
   2.775 +
   2.776 +lemma mantissa_noteq_0: "f \<noteq> float_of 0 \<Longrightarrow> mantissa f \<noteq> 0"
   2.777 +  using mantissa_not_dvd[of f] by auto
   2.778 +
   2.779 +lemma Float_mantissa_exponent: "Float (mantissa f) (exponent f) = f"
   2.780 +  unfolding real_of_float_eq[symmetric] mantissa_exponent[of f] by simp
   2.781 +
   2.782 +lemma Float_cases[case_names Float, cases type: float]:
   2.783 +  fixes f :: float
   2.784 +  obtains (Float) m e :: int where "f = Float m e"
   2.785 +  using Float_mantissa_exponent[symmetric]
   2.786 +  by (atomize_elim) auto
   2.787  
   2.788 -instance float :: comm_semiring
   2.789 -proof (intro_classes)
   2.790 -  fix a b c :: float
   2.791 -  show "(a + b) * c = a * c + b * c"
   2.792 -    by (cases a, cases b, cases c) (simp add: plus_float.simps times_float.simps algebra_simps)
   2.793 +lemma 
   2.794 +  fixes m e :: int
   2.795 +  defines "f \<equiv> float_of (m * 2 powr e)"
   2.796 +  assumes dvd: "\<not> 2 dvd m"
   2.797 +  shows mantissa_float: "mantissa f = m" (is "?M")
   2.798 +    and exponent_float: "m \<noteq> 0 \<Longrightarrow> exponent f = e" (is "_ \<Longrightarrow> ?E")
   2.799 +proof cases
   2.800 +  assume "m = 0" with dvd show "mantissa f = m" by auto
   2.801 +next
   2.802 +  assume "m \<noteq> 0"
   2.803 +  then have f_not_0: "f \<noteq> float_of 0" by (simp add: f_def)
   2.804 +  from mantissa_exponent[of f]
   2.805 +  have "m * 2 powr e = mantissa f * 2 powr exponent f"
   2.806 +    by (auto simp add: f_def)
   2.807 +  then show "?M" "?E"
   2.808 +    using mantissa_not_dvd[OF f_not_0] dvd
   2.809 +    by (auto simp: mult_powr_eq_mult_powr_iff)
   2.810 +qed
   2.811 +
   2.812 +lemma denormalize_shift:
   2.813 +  assumes f_def: "f \<equiv> Float m e" and not_0: "f \<noteq> float_of 0"
   2.814 +  obtains i where "m = mantissa f * 2 ^ i" "e = exponent f - i"
   2.815 +proof
   2.816 +  from mantissa_exponent[of f] f_def
   2.817 +  have "m * 2 powr e = mantissa f * 2 powr exponent f"
   2.818 +    by simp
   2.819 +  then have eq: "m = mantissa f * 2 powr (exponent f - e)"
   2.820 +    by (simp add: powr_divide2[symmetric] field_simps)
   2.821 +  moreover
   2.822 +  have "e \<le> exponent f"
   2.823 +  proof (rule ccontr)
   2.824 +    assume "\<not> e \<le> exponent f"
   2.825 +    then have pos: "exponent f < e" by simp
   2.826 +    then have "2 powr (exponent f - e) = 2 powr - real (e - exponent f)"
   2.827 +      by simp
   2.828 +    also have "\<dots> = 1 / 2^nat (e - exponent f)"
   2.829 +      using pos by (simp add: powr_realpow[symmetric] powr_divide2[symmetric])
   2.830 +    finally have "m * 2^nat (e - exponent f) = real (mantissa f)"
   2.831 +      using eq by simp
   2.832 +    then have "mantissa f = m * 2^nat (e - exponent f)"
   2.833 +      unfolding real_of_int_inject by simp
   2.834 +    with `exponent f < e` have "2 dvd mantissa f"
   2.835 +      apply (intro dvdI[where k="m * 2^(nat (e-exponent f)) div 2"])
   2.836 +      apply (cases "nat (e - exponent f)")
   2.837 +      apply auto
   2.838 +      done
   2.839 +    then show False using mantissa_not_dvd[OF not_0] by simp
   2.840 +  qed
   2.841 +  ultimately have "real m = mantissa f * 2^nat (exponent f - e)"
   2.842 +    by (simp add: powr_realpow[symmetric])
   2.843 +  with `e \<le> exponent f`
   2.844 +  show "m = mantissa f * 2 ^ nat (exponent f - e)" "e = exponent f - nat (exponent f - e)"
   2.845 +    unfolding real_of_int_inject by auto
   2.846  qed
   2.847  
   2.848 -(* Floats do NOT form an order, because "(x < y) = (x <= y & x <> y)" does NOT hold *)
   2.849 +subsection {* Compute arithmetic operations *}
   2.850 +
   2.851 +lemma compute_float_numeral[code_abbrev]: "Float (numeral k) 0 = numeral k"
   2.852 +  by simp
   2.853 +
   2.854 +lemma compute_float_neg_numeral[code_abbrev]: "Float (neg_numeral k) 0 = neg_numeral k"
   2.855 +  by simp
   2.856 +
   2.857 +lemma compute_float_uminus[code]: "- Float m1 e1 = Float (- m1) e1"
   2.858 +  by simp
   2.859 +
   2.860 +lemma compute_float_times[code]: "Float m1 e1 * Float m2 e2 = Float (m1 * m2) (e1 + e2)"
   2.861 +  by (simp add: field_simps powr_add)
   2.862 +
   2.863 +lemma compute_float_plus[code]: "Float m1 e1 + Float m2 e2 =
   2.864 +  (if e1 \<le> e2 then Float (m1 + m2 * 2^nat (e2 - e1)) e1
   2.865 +              else Float (m2 + m1 * 2^nat (e1 - e2)) e2)"
   2.866 +  by (simp add: field_simps)
   2.867 +     (simp add: powr_realpow[symmetric] powr_divide2[symmetric])
   2.868 +
   2.869 +lemma compute_float_minus[code]: fixes f g::float shows "f - g = f + (-g)" by simp
   2.870 +
   2.871 +lemma compute_float_sgn[code]: "sgn (Float m1 e1) = (if 0 < m1 then 1 else if m1 < 0 then -1 else 0)"
   2.872 +  by (simp add: sgn_times)
   2.873 +
   2.874 +definition is_float_pos :: "float \<Rightarrow> bool" where
   2.875 +  "is_float_pos f \<longleftrightarrow> 0 < f"
   2.876 +
   2.877 +lemma compute_is_float_pos[code]: "is_float_pos (Float m e) \<longleftrightarrow> 0 < m"
   2.878 +  by (auto simp add: is_float_pos_def zero_less_mult_iff) (simp add: not_le[symmetric])
   2.879 +
   2.880 +lemma compute_float_less[code]: "a < b \<longleftrightarrow> is_float_pos (b - a)"
   2.881 +  by (simp add: is_float_pos_def field_simps del: zero_float_def)
   2.882 +
   2.883 +definition is_float_nonneg :: "float \<Rightarrow> bool" where
   2.884 +  "is_float_nonneg f \<longleftrightarrow> 0 \<le> f"
   2.885 +
   2.886 +lemma compute_is_float_nonneg[code]: "is_float_nonneg (Float m e) \<longleftrightarrow> 0 \<le> m"
   2.887 +  by (auto simp add: is_float_nonneg_def zero_le_mult_iff) (simp add: not_less[symmetric])
   2.888 +
   2.889 +lemma compute_float_le[code]: "a \<le> b \<longleftrightarrow> is_float_nonneg (b - a)"
   2.890 +  by (simp add: is_float_nonneg_def field_simps del: zero_float_def)
   2.891 +
   2.892 +definition is_float_zero :: "float \<Rightarrow> bool" where
   2.893 +  "is_float_zero f \<longleftrightarrow> 0 = f"
   2.894 +
   2.895 +lemma compute_is_float_zero[code]: "is_float_zero (Float m e) \<longleftrightarrow> 0 = m"
   2.896 +  by (auto simp add: is_float_zero_def)
   2.897  
   2.898 -instance float :: zero_neq_one
   2.899 -proof (intro_classes)
   2.900 -  show "(0::float) \<noteq> 1"
   2.901 -    by (simp add: zero_float_def one_float_def)
   2.902 +lemma compute_float_abs[code]: "abs (Float m e) = Float (abs m) e" by (simp add: abs_mult)
   2.903 +
   2.904 +instantiation float :: equal
   2.905 +begin
   2.906 +
   2.907 +definition "equal_float (f1 :: float) f2 \<longleftrightarrow> is_float_zero (f1 - f2)"
   2.908 +
   2.909 +instance proof qed (auto simp: equal_float_def is_float_zero_def simp del: zero_float_def)
   2.910 +end
   2.911 +
   2.912 +subsection {* Rounding Real numbers *}
   2.913 +
   2.914 +definition round_down :: "int \<Rightarrow> real \<Rightarrow> real" where
   2.915 +  "round_down prec x = floor (x * 2 powr prec) * 2 powr -prec"
   2.916 +
   2.917 +definition round_up :: "int \<Rightarrow> real \<Rightarrow> real" where
   2.918 +  "round_up prec x = ceiling (x * 2 powr prec) * 2 powr -prec"
   2.919 +
   2.920 +lemma round_down_float[simp]: "round_down prec x \<in> float"
   2.921 +  unfolding round_down_def
   2.922 +  by (auto intro!: times_float simp: real_of_int_minus[symmetric] simp del: real_of_int_minus)
   2.923 +
   2.924 +lemma round_up_float[simp]: "round_up prec x \<in> float"
   2.925 +  unfolding round_up_def
   2.926 +  by (auto intro!: times_float simp: real_of_int_minus[symmetric] simp del: real_of_int_minus)
   2.927 +
   2.928 +lemma round_up: "x \<le> round_up prec x"
   2.929 +  by (simp add: powr_minus_divide le_divide_eq round_up_def)
   2.930 +
   2.931 +lemma round_down: "round_down prec x \<le> x"
   2.932 +  by (simp add: powr_minus_divide divide_le_eq round_down_def)
   2.933 +
   2.934 +lemma round_up_0[simp]: "round_up p 0 = 0"
   2.935 +  unfolding round_up_def by simp
   2.936 +
   2.937 +lemma round_down_0[simp]: "round_down p 0 = 0"
   2.938 +  unfolding round_down_def by simp
   2.939 +
   2.940 +lemma round_up_diff_round_down:
   2.941 +  "round_up prec x - round_down prec x \<le> 2 powr -prec"
   2.942 +proof -
   2.943 +  have "round_up prec x - round_down prec x =
   2.944 +    (ceiling (x * 2 powr prec) - floor (x * 2 powr prec)) * 2 powr -prec"
   2.945 +    by (simp add: round_up_def round_down_def field_simps)
   2.946 +  also have "\<dots> \<le> 1 * 2 powr -prec"
   2.947 +    by (rule mult_mono)
   2.948 +       (auto simp del: real_of_int_diff
   2.949 +             simp: real_of_int_diff[symmetric] real_of_int_le_one_cancel_iff ceiling_diff_floor_le_1)
   2.950 +  finally show ?thesis by simp
   2.951  qed
   2.952  
   2.953 -lemma float_le_simp: "((x::float) \<le> y) = (0 \<le> y - x)"
   2.954 -  by (auto simp add: le_float_def)
   2.955 +lemma round_down_shift: "round_down p (x * 2 powr k) = 2 powr k * round_down (p + k) x"
   2.956 +  unfolding round_down_def
   2.957 +  by (simp add: powr_add powr_mult field_simps powr_divide2[symmetric])
   2.958 +    (simp add: powr_add[symmetric])
   2.959  
   2.960 -lemma float_less_simp: "((x::float) < y) = (0 < y - x)"
   2.961 -  by (auto simp add: less_float_def)
   2.962 +lemma round_up_shift: "round_up p (x * 2 powr k) = 2 powr k * round_up (p + k) x"
   2.963 +  unfolding round_up_def
   2.964 +  by (simp add: powr_add powr_mult field_simps powr_divide2[symmetric])
   2.965 +    (simp add: powr_add[symmetric])
   2.966 +
   2.967 +subsection {* Rounding Floats *}
   2.968  
   2.969 -lemma real_of_float_min: "real (min x y :: float) = min (real x) (real y)" unfolding min_def le_float_def by auto
   2.970 -lemma real_of_float_max: "real (max a b :: float) = max (real a) (real b)" unfolding max_def le_float_def by auto
   2.971 +definition float_up :: "int \<Rightarrow> float \<Rightarrow> float" where
   2.972 +  "float_up prec x = float_of (round_up prec (real x))"
   2.973 +
   2.974 +lemma float_up_float: 
   2.975 +  "x \<in> float \<Longrightarrow> float_up prec (float_of x) = float_of (round_up prec x)"
   2.976 +  unfolding float_up_def by simp
   2.977  
   2.978 -lemma float_power: "real (x ^ n :: float) = real x ^ n"
   2.979 -  by (induct n) simp_all
   2.980 +lemma float_up_correct:
   2.981 +  shows "real (float_up e f) - real f \<in> {0..2 powr -e}"
   2.982 +unfolding atLeastAtMost_iff
   2.983 +proof
   2.984 +  have "round_up e f - f \<le> round_up e f - round_down e f" using round_down by simp
   2.985 +  also have "\<dots> \<le> 2 powr -e" using round_up_diff_round_down by simp
   2.986 +  finally show "real (float_up e f) - real f \<le> 2 powr real (- e)"
   2.987 +    by (simp add: float_up_def)
   2.988 +qed (simp add: algebra_simps float_up_def round_up)
   2.989  
   2.990 -lemma zero_le_pow2[simp]: "0 \<le> pow2 s"
   2.991 -  apply (subgoal_tac "0 < pow2 s")
   2.992 -  apply (auto simp only:)
   2.993 -  apply auto
   2.994 -  done
   2.995 +definition float_down :: "int \<Rightarrow> float \<Rightarrow> float" where
   2.996 +  "float_down prec x = float_of (round_down prec (real x))"
   2.997 +
   2.998 +lemma float_down_float: 
   2.999 +  "x \<in> float \<Longrightarrow> float_down prec (float_of x) = float_of (round_down prec x)"
  2.1000 +  unfolding float_down_def by simp
  2.1001  
  2.1002 -lemma pow2_less_0_eq_False[simp]: "(pow2 s < 0) = False"
  2.1003 -  apply auto
  2.1004 -  apply (subgoal_tac "0 \<le> pow2 s")
  2.1005 -  apply simp
  2.1006 -  apply simp
  2.1007 -  done
  2.1008 +lemma float_down_correct:
  2.1009 +  shows "real f - real (float_down e f) \<in> {0..2 powr -e}"
  2.1010 +unfolding atLeastAtMost_iff
  2.1011 +proof
  2.1012 +  have "f - round_down e f \<le> round_up e f - round_down e f" using round_up by simp
  2.1013 +  also have "\<dots> \<le> 2 powr -e" using round_up_diff_round_down by simp
  2.1014 +  finally show "real f - real (float_down e f) \<le> 2 powr real (- e)"
  2.1015 +    by (simp add: float_down_def)
  2.1016 +qed (simp add: algebra_simps float_down_def round_down)
  2.1017 +
  2.1018 +lemma round_down_Float_id:
  2.1019 +  assumes "p + e \<ge> 0"
  2.1020 +  shows "round_down p (Float m e) = Float m e"
  2.1021 +proof -
  2.1022 +  from assms have r: "real e + real p = real (nat (e + p))" by simp
  2.1023 +  have r: "\<lfloor>real (Float m e) * 2 powr real p\<rfloor> = real (Float m e) * 2 powr real p"
  2.1024 +    by (auto intro: exI[where x="m*2^nat (e+p)"]
  2.1025 +      simp add: ac_simps powr_add[symmetric] r powr_realpow)
  2.1026 +  show ?thesis using assms
  2.1027 +    unfolding round_down_def floor_divide_eq_div r
  2.1028 +    by (simp add: ac_simps powr_add[symmetric])
  2.1029 +qed
  2.1030  
  2.1031 -lemma pow2_le_0_eq_False[simp]: "(pow2 s \<le> 0) = False"
  2.1032 -  apply auto
  2.1033 -  apply (subgoal_tac "0 < pow2 s")
  2.1034 -  apply simp
  2.1035 -  apply simp
  2.1036 -  done
  2.1037 +lemma compute_float_down[code]:
  2.1038 +  "float_down p (Float m e) =
  2.1039 +    (if p + e < 0 then Float (m div 2^nat (-(p + e))) (-p) else Float m e)"
  2.1040 +proof cases
  2.1041 +  assume "p + e < 0"
  2.1042 +  hence "real ((2::int) ^ nat (-(p + e))) = 2 powr (-(p + e))"
  2.1043 +    using powr_realpow[of 2 "nat (-(p + e))"] by simp
  2.1044 +  also have "... = 1 / 2 powr p / 2 powr e"
  2.1045 +  unfolding powr_minus_divide real_of_int_minus by (simp add: powr_add)
  2.1046 +  finally show ?thesis
  2.1047 +    unfolding float_down_def round_down_def floor_divide_eq_div[symmetric]
  2.1048 +    using `p + e < 0` by (simp add: ac_simps)
  2.1049 +next
  2.1050 +  assume "\<not> p + e < 0" with round_down_Float_id show ?thesis by (simp add: float_down_def)
  2.1051 +qed
  2.1052  
  2.1053 -lemma float_pos_m_pos: "0 < Float m e \<Longrightarrow> 0 < m"
  2.1054 -  unfolding less_float_def real_of_float_simp real_of_float_0 zero_less_mult_iff
  2.1055 -  by auto
  2.1056 +lemma ceil_divide_floor_conv:
  2.1057 +assumes "b \<noteq> 0"
  2.1058 +shows "\<lceil>real a / real b\<rceil> = (if b dvd a then a div b else \<lfloor>real a / real b\<rfloor> + 1)"
  2.1059 +proof cases
  2.1060 +  assume "\<not> b dvd a"
  2.1061 +  hence "a mod b \<noteq> 0" by auto
  2.1062 +  hence ne: "real (a mod b) / real b \<noteq> 0" using `b \<noteq> 0` by auto
  2.1063 +  have "\<lceil>real a / real b\<rceil> = \<lfloor>real a / real b\<rfloor> + 1"
  2.1064 +  apply (rule ceiling_eq) apply (auto simp: floor_divide_eq_div[symmetric])
  2.1065 +  proof -
  2.1066 +    have "real \<lfloor>real a / real b\<rfloor> \<le> real a / real b" by simp
  2.1067 +    moreover have "real \<lfloor>real a / real b\<rfloor> \<noteq> real a / real b"
  2.1068 +    apply (subst (2) real_of_int_div_aux) unfolding floor_divide_eq_div using ne `b \<noteq> 0` by auto
  2.1069 +    ultimately show "real \<lfloor>real a / real b\<rfloor> < real a / real b" by arith
  2.1070 +  qed
  2.1071 +  thus ?thesis using `\<not> b dvd a` by simp
  2.1072 +qed (simp add: ceiling_def real_of_int_minus[symmetric] divide_minus_left[symmetric]
  2.1073 +  floor_divide_eq_div dvd_neg_div del: divide_minus_left real_of_int_minus)
  2.1074  
  2.1075 -lemma float_pos_less1_e_neg: assumes "0 < Float m e" and "Float m e < 1" shows "e < 0"
  2.1076 +lemma round_up_Float_id:
  2.1077 +  assumes "p + e \<ge> 0"
  2.1078 +  shows "round_up p (Float m e) = Float m e"
  2.1079  proof -
  2.1080 -  have "0 < m" using float_pos_m_pos `0 < Float m e` by auto
  2.1081 -  hence "0 \<le> real m" and "1 \<le> real m" by auto
  2.1082 -  
  2.1083 -  show "e < 0"
  2.1084 -  proof (rule ccontr)
  2.1085 -    assume "\<not> e < 0" hence "0 \<le> e" by auto
  2.1086 -    hence "1 \<le> pow2 e" unfolding pow2_def by auto
  2.1087 -    from mult_mono[OF `1 \<le> real m` this `0 \<le> real m`]
  2.1088 -    have "1 \<le> Float m e" by (simp add: le_float_def real_of_float_simp)
  2.1089 -    thus False using `Float m e < 1` unfolding less_float_def le_float_def by auto
  2.1090 +  from assms have r1: "real e + real p = real (nat (e + p))" by simp
  2.1091 +  have r: "\<lceil>real (Float m e) * 2 powr real p\<rceil> = real (Float m e) * 2 powr real p"
  2.1092 +    by (auto simp add: ac_simps powr_add[symmetric] r1 powr_realpow
  2.1093 +      intro: exI[where x="m*2^nat (e+p)"])
  2.1094 +  show ?thesis using assms
  2.1095 +    unfolding float_up_def round_up_def floor_divide_eq_div Let_def r
  2.1096 +    by (simp add: ac_simps powr_add[symmetric])
  2.1097 +qed
  2.1098 +
  2.1099 +lemma compute_float_up[code]:
  2.1100 +  "float_up p (Float m e) =
  2.1101 +    (let P = 2^nat (-(p + e)); r = m mod P in
  2.1102 +      if p + e < 0 then Float (m div P + (if r = 0 then 0 else 1)) (-p) else Float m e)"
  2.1103 +proof cases
  2.1104 +  assume "p + e < 0"
  2.1105 +  hence "real ((2::int) ^ nat (-(p + e))) = 2 powr (-(p + e))"
  2.1106 +    using powr_realpow[of 2 "nat (-(p + e))"] by simp
  2.1107 +  also have "... = 1 / 2 powr p / 2 powr e"
  2.1108 +  unfolding powr_minus_divide real_of_int_minus by (simp add: powr_add)
  2.1109 +  finally have twopow_rewrite:
  2.1110 +    "real ((2::int) ^ nat (- (p + e))) = 1 / 2 powr real p / 2 powr real e" .
  2.1111 +  with `p + e < 0` have powr_rewrite:
  2.1112 +    "2 powr real e * 2 powr real p = 1 / real ((2::int) ^ nat (- (p + e)))"
  2.1113 +    unfolding powr_divide2 by simp
  2.1114 +  show ?thesis
  2.1115 +  proof cases
  2.1116 +    assume "2^nat (-(p + e)) dvd m"
  2.1117 +    with `p + e < 0` twopow_rewrite show ?thesis
  2.1118 +      by (auto simp: ac_simps float_up_def round_up_def floor_divide_eq_div dvd_eq_mod_eq_0)
  2.1119 +  next
  2.1120 +    assume ndvd: "\<not> 2 ^ nat (- (p + e)) dvd m"
  2.1121 +    have one_div: "real m * (1 / real ((2::int) ^ nat (- (p + e)))) =
  2.1122 +      real m / real ((2::int) ^ nat (- (p + e)))"
  2.1123 +      by (simp add: field_simps)
  2.1124 +    have "real \<lceil>real m * (2 powr real e * 2 powr real p)\<rceil> =
  2.1125 +      real \<lfloor>real m * (2 powr real e * 2 powr real p)\<rfloor> + 1"
  2.1126 +      using ndvd unfolding powr_rewrite one_div
  2.1127 +      by (subst ceil_divide_floor_conv) (auto simp: field_simps)
  2.1128 +    thus ?thesis using `p + e < 0` twopow_rewrite
  2.1129 +      by (auto simp: ac_simps Let_def float_up_def round_up_def floor_divide_eq_div[symmetric])
  2.1130    qed
  2.1131 +next
  2.1132 +  assume "\<not> p + e < 0" with round_up_Float_id show ?thesis by (simp add: float_up_def)
  2.1133  qed
  2.1134  
  2.1135 -lemma float_less1_mantissa_bound: assumes "0 < Float m e" "Float m e < 1" shows "m < 2^(nat (-e))"
  2.1136 +lemmas real_of_ints =
  2.1137 +  real_of_int_zero
  2.1138 +  real_of_one
  2.1139 +  real_of_int_add
  2.1140 +  real_of_int_minus
  2.1141 +  real_of_int_diff
  2.1142 +  real_of_int_mult
  2.1143 +  real_of_int_power
  2.1144 +  real_numeral
  2.1145 +lemmas real_of_nats =
  2.1146 +  real_of_nat_zero
  2.1147 +  real_of_nat_one
  2.1148 +  real_of_nat_1
  2.1149 +  real_of_nat_add
  2.1150 +  real_of_nat_mult
  2.1151 +  real_of_nat_power
  2.1152 +
  2.1153 +lemmas int_of_reals = real_of_ints[symmetric]
  2.1154 +lemmas nat_of_reals = real_of_nats[symmetric]
  2.1155 +
  2.1156 +lemma two_real_int: "(2::real) = real (2::int)" by simp
  2.1157 +lemma two_real_nat: "(2::real) = real (2::nat)" by simp
  2.1158 +
  2.1159 +lemma mult_cong: "a = c ==> b = d ==> a*b = c*d" by simp
  2.1160 +
  2.1161 +subsection {* Compute bitlen of integers *}
  2.1162 +
  2.1163 +definition bitlen::"int => int"
  2.1164 +where "bitlen a = (if a > 0 then \<lfloor>log 2 a\<rfloor> + 1 else 0)"
  2.1165 +
  2.1166 +lemma bitlen_nonneg: "0 \<le> bitlen x"
  2.1167  proof -
  2.1168 -  have "e < 0" using float_pos_less1_e_neg assms by auto
  2.1169 -  have "\<And>x. (0::real) < 2^x" by auto
  2.1170 -  have "real m < 2^(nat (-e))" using `Float m e < 1`
  2.1171 -    unfolding less_float_def real_of_float_neg_exp[OF `e < 0`] real_of_float_1
  2.1172 -          real_mult_less_iff1[of _ _ 1, OF `0 < 2^(nat (-e))`, symmetric] 
  2.1173 -          mult_assoc by auto
  2.1174 -  thus ?thesis unfolding real_of_int_less_iff[symmetric] by auto
  2.1175 +  {
  2.1176 +    assume "0 > x"
  2.1177 +    have "-1 = log 2 (inverse 2)" by (subst log_inverse) simp_all
  2.1178 +    also have "... < log 2 (-x)" using `0 > x` by auto
  2.1179 +    finally have "-1 < log 2 (-x)" .
  2.1180 +  } thus "0 \<le> bitlen x" unfolding bitlen_def by (auto intro!: add_nonneg_nonneg)
  2.1181 +qed
  2.1182 +
  2.1183 +lemma bitlen_bounds:
  2.1184 +  assumes "x > 0"
  2.1185 +  shows "2 ^ nat (bitlen x - 1) \<le> x \<and> x < 2 ^ nat (bitlen x)"
  2.1186 +proof
  2.1187 +  have "(2::real) ^ nat \<lfloor>log 2 (real x)\<rfloor> = 2 powr real (floor (log 2 (real x)))"
  2.1188 +    using powr_realpow[symmetric, of 2 "nat \<lfloor>log 2 (real x)\<rfloor>"] `x > 0`
  2.1189 +    using real_nat_eq_real[of "floor (log 2 (real x))"]
  2.1190 +    by simp
  2.1191 +  also have "... \<le> 2 powr log 2 (real x)"
  2.1192 +    by simp
  2.1193 +  also have "... = real x"
  2.1194 +    using `0 < x` by simp
  2.1195 +  finally have "2 ^ nat \<lfloor>log 2 (real x)\<rfloor> \<le> real x" by simp
  2.1196 +  thus "2 ^ nat (bitlen x - 1) \<le> x" using `x > 0`
  2.1197 +    by (simp add: bitlen_def)
  2.1198 +next
  2.1199 +  have "x \<le> 2 powr (log 2 x)" using `x > 0` by simp
  2.1200 +  also have "... < 2 ^ nat (\<lfloor>log 2 (real x)\<rfloor> + 1)"
  2.1201 +    apply (simp add: powr_realpow[symmetric])
  2.1202 +    using `x > 0` by simp
  2.1203 +  finally show "x < 2 ^ nat (bitlen x)" using `x > 0`
  2.1204 +    by (simp add: bitlen_def ac_simps int_of_reals del: real_of_ints)
  2.1205 +qed
  2.1206 +
  2.1207 +lemma bitlen_pow2[simp]:
  2.1208 +  assumes "b > 0"
  2.1209 +  shows "bitlen (b * 2 ^ c) = bitlen b + c"
  2.1210 +proof -
  2.1211 +  from assms have "b * 2 ^ c > 0" by (auto intro: mult_pos_pos)
  2.1212 +  thus ?thesis
  2.1213 +    using floor_add[of "log 2 b" c] assms
  2.1214 +    by (auto simp add: log_mult log_nat_power bitlen_def)
  2.1215  qed
  2.1216  
  2.1217 -function bitlen :: "int \<Rightarrow> int" where
  2.1218 -"bitlen 0 = 0" | 
  2.1219 -"bitlen -1 = 1" | 
  2.1220 -"0 < x \<Longrightarrow> bitlen x = 1 + (bitlen (x div 2))" | 
  2.1221 -"x < -1 \<Longrightarrow> bitlen x = 1 + (bitlen (x div 2))"
  2.1222 -  apply (case_tac "x = 0 \<or> x = -1 \<or> x < -1 \<or> x > 0")
  2.1223 -  apply auto
  2.1224 -  done
  2.1225 -termination by (relation "measure (nat o abs)", auto)
  2.1226 -
  2.1227 -lemma bitlen_ge0: "0 \<le> bitlen x" by (induct x rule: bitlen.induct, auto)
  2.1228 -lemma bitlen_ge1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> bitlen x" by (induct x rule: bitlen.induct, auto simp add: bitlen_ge0)
  2.1229 +lemma bitlen_Float:
  2.1230 +fixes m e
  2.1231 +defines "f \<equiv> Float m e"
  2.1232 +shows "bitlen (\<bar>mantissa f\<bar>) + exponent f = (if m = 0 then 0 else bitlen \<bar>m\<bar> + e)"
  2.1233 +proof cases
  2.1234 +  assume "m \<noteq> 0" hence "f \<noteq> float_of 0" by (simp add: f_def) hence "mantissa f \<noteq> 0"
  2.1235 +    by (simp add: mantissa_noteq_0)
  2.1236 +  moreover
  2.1237 +  from f_def[THEN denormalize_shift, OF `f \<noteq> float_of 0`] guess i .
  2.1238 +  ultimately show ?thesis by (simp add: abs_mult)
  2.1239 +qed (simp add: f_def bitlen_def)
  2.1240  
  2.1241 -lemma bitlen_bounds': assumes "0 < x" shows "2^nat (bitlen x - 1) \<le> x \<and> x + 1 \<le> 2^nat (bitlen x)" (is "?P x")
  2.1242 -  using `0 < x`
  2.1243 -proof (induct x rule: bitlen.induct)
  2.1244 -  fix x
  2.1245 -  assume "0 < x" and hyp: "0 < x div 2 \<Longrightarrow> ?P (x div 2)" hence "0 \<le> x" and "x \<noteq> 0" by auto
  2.1246 -  { fix x have "0 \<le> 1 + bitlen x" using bitlen_ge0[of x] by auto } note gt0_pls1 = this
  2.1247 +lemma compute_bitlen[code]:
  2.1248 +  shows "bitlen x = (if x > 0 then bitlen (x div 2) + 1 else 0)"
  2.1249 +proof -
  2.1250 +  { assume "2 \<le> x"
  2.1251 +    then have "\<lfloor>log 2 (x div 2)\<rfloor> + 1 = \<lfloor>log 2 (x - x mod 2)\<rfloor>"
  2.1252 +      by (simp add: log_mult zmod_zdiv_equality')
  2.1253 +    also have "\<dots> = \<lfloor>log 2 (real x)\<rfloor>"
  2.1254 +    proof cases
  2.1255 +      assume "x mod 2 = 0" then show ?thesis by simp
  2.1256 +    next
  2.1257 +      def n \<equiv> "\<lfloor>log 2 (real x)\<rfloor>"
  2.1258 +      then have "0 \<le> n"
  2.1259 +        using `2 \<le> x` by simp
  2.1260 +      assume "x mod 2 \<noteq> 0"
  2.1261 +      with `2 \<le> x` have "x mod 2 = 1" "\<not> 2 dvd x" by (auto simp add: dvd_eq_mod_eq_0)
  2.1262 +      with `2 \<le> x` have "x \<noteq> 2^nat n" by (cases "nat n") auto
  2.1263 +      moreover
  2.1264 +      { have "real (2^nat n :: int) = 2 powr (nat n)"
  2.1265 +          by (simp add: powr_realpow)
  2.1266 +        also have "\<dots> \<le> 2 powr (log 2 x)"
  2.1267 +          using `2 \<le> x` by (simp add: n_def del: powr_log_cancel)
  2.1268 +        finally have "2^nat n \<le> x" using `2 \<le> x` by simp }
  2.1269 +      ultimately have "2^nat n \<le> x - 1" by simp
  2.1270 +      then have "2^nat n \<le> real (x - 1)"
  2.1271 +        unfolding real_of_int_le_iff[symmetric] by simp
  2.1272 +      { have "n = \<lfloor>log 2 (2^nat n)\<rfloor>"
  2.1273 +          using `0 \<le> n` by (simp add: log_nat_power)
  2.1274 +        also have "\<dots> \<le> \<lfloor>log 2 (x - 1)\<rfloor>"
  2.1275 +          using `2^nat n \<le> real (x - 1)` `0 \<le> n` `2 \<le> x` by (auto intro: floor_mono)
  2.1276 +        finally have "n \<le> \<lfloor>log 2 (x - 1)\<rfloor>" . }
  2.1277 +      moreover have "\<lfloor>log 2 (x - 1)\<rfloor> \<le> n"
  2.1278 +        using `2 \<le> x` by (auto simp add: n_def intro!: floor_mono)
  2.1279 +      ultimately show "\<lfloor>log 2 (x - x mod 2)\<rfloor> = \<lfloor>log 2 x\<rfloor>"
  2.1280 +        unfolding n_def `x mod 2 = 1` by auto
  2.1281 +    qed
  2.1282 +    finally have "\<lfloor>log 2 (x div 2)\<rfloor> + 1 = \<lfloor>log 2 x\<rfloor>" . }
  2.1283 +  moreover
  2.1284 +  { assume "x < 2" "0 < x"
  2.1285 +    then have "x = 1" by simp
  2.1286 +    then have "\<lfloor>log 2 (real x)\<rfloor> = 0" by simp }
  2.1287 +  ultimately show ?thesis
  2.1288 +    unfolding bitlen_def
  2.1289 +    by (auto simp: pos_imp_zdiv_pos_iff not_le)
  2.1290 +qed
  2.1291  
  2.1292 -  have "0 < (2::int)" by auto
  2.1293 -
  2.1294 -  show "?P x"
  2.1295 -  proof (cases "x = 1")
  2.1296 -    case True show "?P x" unfolding True by auto
  2.1297 +lemma float_gt1_scale: assumes "1 \<le> Float m e"
  2.1298 +  shows "0 \<le> e + (bitlen m - 1)"
  2.1299 +proof -
  2.1300 +  have "0 < Float m e" using assms by auto
  2.1301 +  hence "0 < m" using powr_gt_zero[of 2 e]
  2.1302 +    by (auto simp: less_float_def less_eq_float_def zero_less_mult_iff)
  2.1303 +  hence "m \<noteq> 0" by auto
  2.1304 +  show ?thesis
  2.1305 +  proof (cases "0 \<le> e")
  2.1306 +    case True thus ?thesis using `0 < m`  by (simp add: bitlen_def)
  2.1307    next
  2.1308 -    case False hence "2 \<le> x" using `0 < x` `x \<noteq> 1` by auto
  2.1309 -    hence "2 div 2 \<le> x div 2" by (rule zdiv_mono1, auto)
  2.1310 -    hence "0 < x div 2" and "x div 2 \<noteq> 0" by auto
  2.1311 -    hence bitlen_s1_ge0: "0 \<le> bitlen (x div 2) - 1" using bitlen_ge1[OF `x div 2 \<noteq> 0`] by auto
  2.1312 -
  2.1313 -    { from hyp[OF `0 < x div 2`]
  2.1314 -      have "2 ^ nat (bitlen (x div 2) - 1) \<le> x div 2" by auto
  2.1315 -      hence "2 ^ nat (bitlen (x div 2) - 1) * 2 \<le> x div 2 * 2" by (rule mult_right_mono, auto)
  2.1316 -      also have "\<dots> \<le> x" using `0 < x` by auto
  2.1317 -      finally have "2^nat (1 + bitlen (x div 2) - 1) \<le> x" unfolding power_Suc2[symmetric] Suc_nat_eq_nat_zadd1[OF bitlen_s1_ge0] by auto
  2.1318 -    } moreover
  2.1319 -    { have "x + 1 \<le> x - x mod 2 + 2"
  2.1320 -      proof -
  2.1321 -        have "x mod 2 < 2" using `0 < x` by auto
  2.1322 -        hence "x < x - x mod 2 +  2" unfolding algebra_simps by auto
  2.1323 -        thus ?thesis by auto
  2.1324 -      qed
  2.1325 -      also have "x - x mod 2 + 2 = (x div 2 + 1) * 2" unfolding algebra_simps using `0 < x` div_mod_equality[of x 2 0] by auto
  2.1326 -      also have "\<dots> \<le> 2^nat (bitlen (x div 2)) * 2" using hyp[OF `0 < x div 2`, THEN conjunct2] by (rule mult_right_mono, auto)
  2.1327 -      also have "\<dots> = 2^(1 + nat (bitlen (x div 2)))" unfolding power_Suc2[symmetric] by auto
  2.1328 -      finally have "x + 1 \<le> 2^(1 + nat (bitlen (x div 2)))" .
  2.1329 -    }
  2.1330 -    ultimately show ?thesis
  2.1331 -      unfolding bitlen.simps(3)[OF `0 < x`] nat_add_distrib[OF zero_le_one bitlen_ge0]
  2.1332 -      unfolding add_commute nat_add_distrib[OF zero_le_one gt0_pls1]
  2.1333 -      by auto
  2.1334 +    have "(1::int) < 2" by simp
  2.1335 +    case False let ?S = "2^(nat (-e))"
  2.1336 +    have "inverse (2 ^ nat (- e)) = 2 powr e" using assms False powr_realpow[of 2 "nat (-e)"]
  2.1337 +      by (auto simp: powr_minus field_simps inverse_eq_divide)
  2.1338 +    hence "1 \<le> real m * inverse ?S" using assms False powr_realpow[of 2 "nat (-e)"]
  2.1339 +      by (auto simp: powr_minus)
  2.1340 +    hence "1 * ?S \<le> real m * inverse ?S * ?S" by (rule mult_right_mono, auto)
  2.1341 +    hence "?S \<le> real m" unfolding mult_assoc by auto
  2.1342 +    hence "?S \<le> m" unfolding real_of_int_le_iff[symmetric] by auto
  2.1343 +    from this bitlen_bounds[OF `0 < m`, THEN conjunct2]
  2.1344 +    have "nat (-e) < (nat (bitlen m))" unfolding power_strict_increasing_iff[OF `1 < 2`, symmetric] by (rule order_le_less_trans)
  2.1345 +    hence "-e < bitlen m" using False by auto
  2.1346 +    thus ?thesis by auto
  2.1347    qed
  2.1348 -next
  2.1349 -  fix x :: int assume "x < -1" and "0 < x" hence False by auto
  2.1350 -  thus "?P x" by auto
  2.1351 -qed auto
  2.1352 -
  2.1353 -lemma bitlen_bounds: assumes "0 < x" shows "2^nat (bitlen x - 1) \<le> x \<and> x < 2^nat (bitlen x)"
  2.1354 -  using bitlen_bounds'[OF `0<x`] by auto
  2.1355 +qed
  2.1356  
  2.1357  lemma bitlen_div: assumes "0 < m" shows "1 \<le> real m / 2^nat (bitlen m - 1)" and "real m / 2^nat (bitlen m - 1) < 2"
  2.1358  proof -
  2.1359 @@ -514,840 +930,571 @@
  2.1360    thus "1 \<le> real m / ?B" by auto
  2.1361  
  2.1362    have "m \<noteq> 0" using assms by auto
  2.1363 -  have "0 \<le> bitlen m - 1" using bitlen_ge1[OF `m \<noteq> 0`] by auto
  2.1364 +  have "0 \<le> bitlen m - 1" using `0 < m` by (auto simp: bitlen_def)
  2.1365  
  2.1366    have "m < 2^nat(bitlen m)" using bitlen_bounds[OF `0 <m`] ..
  2.1367 -  also have "\<dots> = 2^nat(bitlen m - 1 + 1)" using bitlen_ge1[OF `m \<noteq> 0`] by auto
  2.1368 +  also have "\<dots> = 2^nat(bitlen m - 1 + 1)" using `0 < m` by (auto simp: bitlen_def)
  2.1369    also have "\<dots> = ?B * 2" unfolding nat_add_distrib[OF `0 \<le> bitlen m - 1` zero_le_one] by auto
  2.1370    finally have "real m < 2 * ?B" unfolding real_of_int_less_iff[symmetric] by auto
  2.1371    hence "real m / ?B < 2 * ?B / ?B" by (rule divide_strict_right_mono, auto)
  2.1372    thus "real m / ?B < 2" by auto
  2.1373  qed
  2.1374  
  2.1375 -lemma float_gt1_scale: assumes "1 \<le> Float m e"
  2.1376 -  shows "0 \<le> e + (bitlen m - 1)"
  2.1377 -proof (cases "0 \<le> e")
  2.1378 -  have "0 < Float m e" using assms unfolding less_float_def le_float_def by auto
  2.1379 -  hence "0 < m" using float_pos_m_pos by auto
  2.1380 -  hence "m \<noteq> 0" by auto
  2.1381 -  case True with bitlen_ge1[OF `m \<noteq> 0`] show ?thesis by auto
  2.1382 -next
  2.1383 -  have "0 < Float m e" using assms unfolding less_float_def le_float_def by auto
  2.1384 -  hence "0 < m" using float_pos_m_pos by auto
  2.1385 -  hence "m \<noteq> 0" and "1 < (2::int)" by auto
  2.1386 -  case False let ?S = "2^(nat (-e))"
  2.1387 -  have "1 \<le> real m * inverse ?S" using assms unfolding le_float_def real_of_float_nge0_exp[OF False] by auto
  2.1388 -  hence "1 * ?S \<le> real m * inverse ?S * ?S" by (rule mult_right_mono, auto)
  2.1389 -  hence "?S \<le> real m" unfolding mult_assoc by auto
  2.1390 -  hence "?S \<le> m" unfolding real_of_int_le_iff[symmetric] by auto
  2.1391 -  from this bitlen_bounds[OF `0 < m`, THEN conjunct2]
  2.1392 -  have "nat (-e) < (nat (bitlen m))" unfolding power_strict_increasing_iff[OF `1 < 2`, symmetric] by (rule order_le_less_trans)
  2.1393 -  hence "-e < bitlen m" using False bitlen_ge0 by auto
  2.1394 -  thus ?thesis by auto
  2.1395 -qed
  2.1396 +subsection {* Approximation of positive rationals *}
  2.1397 +
  2.1398 +lemma zdiv_zmult_twopow_eq: fixes a b::int shows "a div b div (2 ^ n) = a div (b * 2 ^ n)"
  2.1399 +by (simp add: zdiv_zmult2_eq)
  2.1400  
  2.1401 -lemma normalized_float: assumes "m \<noteq> 0" shows "real (Float m (- (bitlen m - 1))) = real m / 2^nat (bitlen m - 1)"
  2.1402 -proof (cases "- (bitlen m - 1) = 0")
  2.1403 -  case True show ?thesis unfolding real_of_float_simp pow2_def using True by auto
  2.1404 -next
  2.1405 -  case False hence P: "\<not> 0 \<le> - (bitlen m - 1)" using bitlen_ge1[OF `m \<noteq> 0`] by auto
  2.1406 -  show ?thesis unfolding real_of_float_nge0_exp[OF P] divide_inverse by auto
  2.1407 -qed
  2.1408 +lemma div_mult_twopow_eq: fixes a b::nat shows "a div ((2::nat) ^ n) div b = a div (b * 2 ^ n)"
  2.1409 +  by (cases "b=0") (simp_all add: div_mult2_eq[symmetric] ac_simps)
  2.1410  
  2.1411 -(* BROKEN
  2.1412 -lemma bitlen_Pls: "bitlen (Int.Pls) = Int.Pls" by (subst Pls_def, subst Pls_def, simp)
  2.1413 +lemma real_div_nat_eq_floor_of_divide:
  2.1414 +  fixes a b::nat
  2.1415 +  shows "a div b = real (floor (a/b))"
  2.1416 +by (metis floor_divide_eq_div real_of_int_of_nat_eq zdiv_int)
  2.1417  
  2.1418 -lemma bitlen_Min: "bitlen (Int.Min) = Int.Bit1 Int.Pls" by (subst Min_def, simp add: Bit1_def) 
  2.1419 +definition "rat_precision prec x y = int prec - (bitlen x - bitlen y)"
  2.1420  
  2.1421 -lemma bitlen_B0: "bitlen (Int.Bit0 b) = (if iszero b then Int.Pls else Int.succ (bitlen b))"
  2.1422 -  apply (auto simp add: iszero_def succ_def)
  2.1423 -  apply (simp add: Bit0_def Pls_def)
  2.1424 -  apply (subst Bit0_def)
  2.1425 -  apply simp
  2.1426 -  apply (subgoal_tac "0 < 2 * b \<or> 2 * b < -1")
  2.1427 -  apply auto
  2.1428 -  done
  2.1429 +definition lapprox_posrat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float" where
  2.1430 +  "lapprox_posrat prec x y = float_of (round_down (rat_precision prec x y) (x / y))"
  2.1431  
  2.1432 -lemma bitlen_B1: "bitlen (Int.Bit1 b) = (if iszero (Int.succ b) then Int.Bit1 Int.Pls else Int.succ (bitlen b))"
  2.1433 -proof -
  2.1434 -  have h: "! x. (2*x + 1) div 2 = (x::int)"
  2.1435 -    by arith    
  2.1436 -  show ?thesis
  2.1437 -    apply (auto simp add: iszero_def succ_def)
  2.1438 -    apply (subst Bit1_def)+
  2.1439 -    apply simp
  2.1440 -    apply (subgoal_tac "2 * b + 1 = -1")
  2.1441 -    apply (simp only:)
  2.1442 -    apply simp_all
  2.1443 -    apply (subst Bit1_def)
  2.1444 -    apply simp
  2.1445 -    apply (subgoal_tac "0 < 2 * b + 1 \<or> 2 * b + 1 < -1")
  2.1446 -    apply (auto simp add: h)
  2.1447 -    done
  2.1448 -qed
  2.1449 +lemma compute_lapprox_posrat[code]:
  2.1450 +  fixes prec x y 
  2.1451 +  shows "lapprox_posrat prec x y = 
  2.1452 +   (let 
  2.1453 +       l = rat_precision prec x y;
  2.1454 +       d = if 0 \<le> l then x * 2^nat l div y else x div 2^nat (- l) div y
  2.1455 +    in normfloat (Float d (- l)))"
  2.1456 +    unfolding lapprox_posrat_def div_mult_twopow_eq
  2.1457 +    by (simp add: round_down_def powr_int real_div_nat_eq_floor_of_divide
  2.1458 +                  field_simps Let_def
  2.1459 +             del: two_powr_minus_int_float)
  2.1460  
  2.1461 -lemma bitlen_number_of: "bitlen (number_of w) = number_of (bitlen w)"
  2.1462 -  by (simp add: number_of_is_id)
  2.1463 -BH *)
  2.1464 -
  2.1465 -lemma [code]: "bitlen x = 
  2.1466 -     (if x = 0  then 0 
  2.1467 - else if x = -1 then 1 
  2.1468 -                else (1 + (bitlen (x div 2))))"
  2.1469 -  by (cases "x = 0 \<or> x = -1 \<or> 0 < x") auto
  2.1470 -
  2.1471 -definition lapprox_posrat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
  2.1472 -where
  2.1473 -  "lapprox_posrat prec x y = 
  2.1474 -   (let 
  2.1475 -       l = nat (int prec + bitlen y - bitlen x) ;
  2.1476 -       d = (x * 2^l) div y
  2.1477 -    in normfloat (Float d (- (int l))))"
  2.1478 -
  2.1479 -lemma pow2_minus: "pow2 (-x) = inverse (pow2 x)"
  2.1480 -  unfolding pow2_neg[of "-x"] by auto
  2.1481 +definition rapprox_posrat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float" where
  2.1482 +  "rapprox_posrat prec x y = float_of (round_up (rat_precision prec x y) (x / y))"
  2.1483  
  2.1484 -lemma lapprox_posrat: 
  2.1485 -  assumes x: "0 \<le> x"
  2.1486 -  and y: "0 < y"
  2.1487 -  shows "real (lapprox_posrat prec x y) \<le> real x / real y"
  2.1488 -proof -
  2.1489 -  let ?l = "nat (int prec + bitlen y - bitlen x)"
  2.1490 -  
  2.1491 -  have "real (x * 2^?l div y) * inverse (2^?l) \<le> (real (x * 2^?l) / real y) * inverse (2^?l)" 
  2.1492 -    by (rule mult_right_mono, fact real_of_int_div4, simp)
  2.1493 -  also have "\<dots> \<le> (real x / real y) * 2^?l * inverse (2^?l)" by auto
  2.1494 -  finally have "real (x * 2^?l div y) * inverse (2^?l) \<le> real x / real y" unfolding mult_assoc by auto
  2.1495 -  thus ?thesis unfolding lapprox_posrat_def Let_def normfloat real_of_float_simp
  2.1496 -    unfolding pow2_minus pow2_int minus_minus .
  2.1497 -qed
  2.1498 -
  2.1499 -lemma real_of_int_div_mult: 
  2.1500 -  fixes x y c :: int assumes "0 < y" and "0 < c"
  2.1501 -  shows "real (x div y) \<le> real (x * c div y) * inverse (real c)"
  2.1502 -proof -
  2.1503 -  have "c * (x div y) + 0 \<le> c * x div y" unfolding zdiv_zmult1_eq[of c x y]
  2.1504 -    by (rule add_left_mono, 
  2.1505 -        auto intro!: mult_nonneg_nonneg 
  2.1506 -             simp add: pos_imp_zdiv_nonneg_iff[OF `0 < y`] `0 < c`[THEN less_imp_le] pos_mod_sign[OF `0 < y`])
  2.1507 -  hence "real (x div y) * real c \<le> real (x * c div y)" 
  2.1508 -    unfolding real_of_int_mult[symmetric] real_of_int_le_iff mult_commute by auto
  2.1509 -  hence "real (x div y) * real c * inverse (real c) \<le> real (x * c div y) * inverse (real c)"
  2.1510 -    using `0 < c` by auto
  2.1511 -  thus ?thesis unfolding mult_assoc using `0 < c` by auto
  2.1512 -qed
  2.1513 -
  2.1514 -lemma lapprox_posrat_bottom: assumes "0 < y"
  2.1515 -  shows "real (x div y) \<le> real (lapprox_posrat n x y)" 
  2.1516 -proof -
  2.1517 -  have pow: "\<And>x. (0::int) < 2^x" by auto
  2.1518 -  show ?thesis
  2.1519 -    unfolding lapprox_posrat_def Let_def real_of_float_add normfloat real_of_float_simp pow2_minus pow2_int
  2.1520 -    using real_of_int_div_mult[OF `0 < y` pow] by auto
  2.1521 -qed
  2.1522 -
  2.1523 -lemma lapprox_posrat_nonneg: assumes "0 \<le> x" and "0 < y"
  2.1524 -  shows "0 \<le> real (lapprox_posrat n x y)" 
  2.1525 -proof -
  2.1526 +(* TODO: optimize using zmod_zmult2_eq, pdivmod ? *)
  2.1527 +lemma compute_rapprox_posrat[code]:
  2.1528 +  fixes prec x y
  2.1529 +  defines "l \<equiv> rat_precision prec x y"
  2.1530 +  shows "rapprox_posrat prec x y = (let
  2.1531 +     l = l ;
  2.1532 +     X = if 0 \<le> l then (x * 2^nat l, y) else (x, y * 2^nat(-l)) ;
  2.1533 +     d = fst X div snd X ;
  2.1534 +     m = fst X mod snd X
  2.1535 +   in normfloat (Float (d + (if m = 0 \<or> y = 0 then 0 else 1)) (- l)))"
  2.1536 +proof (cases "y = 0")
  2.1537 +  assume "y = 0" thus ?thesis by (simp add: rapprox_posrat_def Let_def)
  2.1538 +next
  2.1539 +  assume "y \<noteq> 0"
  2.1540    show ?thesis
  2.1541 -    unfolding lapprox_posrat_def Let_def real_of_float_add normfloat real_of_float_simp pow2_minus pow2_int
  2.1542 -    using pos_imp_zdiv_nonneg_iff[OF `0 < y`] assms by (auto intro!: mult_nonneg_nonneg)
  2.1543 -qed
  2.1544 -
  2.1545 -definition rapprox_posrat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
  2.1546 -where
  2.1547 -  "rapprox_posrat prec x y = (let
  2.1548 -     l = nat (int prec + bitlen y - bitlen x) ;
  2.1549 -     X = x * 2^l ;
  2.1550 -     d = X div y ;
  2.1551 -     m = X mod y
  2.1552 -   in normfloat (Float (d + (if m = 0 then 0 else 1)) (- (int l))))"
  2.1553 -
  2.1554 -lemma rapprox_posrat:
  2.1555 -  assumes x: "0 \<le> x"
  2.1556 -  and y: "0 < y"
  2.1557 -  shows "real x / real y \<le> real (rapprox_posrat prec x y)"
  2.1558 -proof -
  2.1559 -  let ?l = "nat (int prec + bitlen y - bitlen x)" let ?X = "x * 2^?l"
  2.1560 -  show ?thesis 
  2.1561 -  proof (cases "?X mod y = 0")
  2.1562 -    case True hence "y dvd ?X" using `0 < y` by auto
  2.1563 -    from real_of_int_div[OF this]
  2.1564 -    have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
  2.1565 -    also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
  2.1566 -    finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
  2.1567 -    thus ?thesis unfolding rapprox_posrat_def Let_def normfloat if_P[OF True] 
  2.1568 -      unfolding real_of_float_simp pow2_minus pow2_int minus_minus by auto
  2.1569 -  next
  2.1570 -    case False
  2.1571 -    have "0 \<le> real y" and "real y \<noteq> 0" using `0 < y` by auto
  2.1572 -    have "0 \<le> real y * 2^?l" by (rule mult_nonneg_nonneg, rule `0 \<le> real y`, auto)
  2.1573 -
  2.1574 -    have "?X = y * (?X div y) + ?X mod y" by auto
  2.1575 -    also have "\<dots> \<le> y * (?X div y) + y" by (rule add_mono, auto simp add: pos_mod_bound[OF `0 < y`, THEN less_imp_le])
  2.1576 -    also have "\<dots> = y * (?X div y + 1)" unfolding right_distrib by auto
  2.1577 -    finally have "real ?X \<le> real y * real (?X div y + 1)" unfolding real_of_int_le_iff real_of_int_mult[symmetric] .
  2.1578 -    hence "real ?X / (real y * 2^?l) \<le> real y * real (?X div y + 1) / (real y * 2^?l)" 
  2.1579 -      by (rule divide_right_mono, simp only: `0 \<le> real y * 2^?l`)
  2.1580 -    also have "\<dots> = real y * real (?X div y + 1) / real y / 2^?l" by auto
  2.1581 -    also have "\<dots> = real (?X div y + 1) * inverse (2^?l)" unfolding nonzero_mult_divide_cancel_left[OF `real y \<noteq> 0`] 
  2.1582 -      unfolding divide_inverse ..
  2.1583 -    finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
  2.1584 -      unfolding pow2_minus pow2_int minus_minus by auto
  2.1585 +  proof (cases "0 \<le> l")
  2.1586 +    assume "0 \<le> l"
  2.1587 +    def x' == "x * 2 ^ nat l"
  2.1588 +    have "int x * 2 ^ nat l = x'" by (simp add: x'_def int_mult int_power)
  2.1589 +    moreover have "real x * 2 powr real l = real x'"
  2.1590 +      by (simp add: powr_realpow[symmetric] `0 \<le> l` x'_def)
  2.1591 +    ultimately show ?thesis
  2.1592 +      unfolding rapprox_posrat_def round_up_def l_def[symmetric]
  2.1593 +      using ceil_divide_floor_conv[of y x'] powr_realpow[of 2 "nat l"] `0 \<le> l` `y \<noteq> 0`
  2.1594 +      by (simp add: Let_def floor_divide_eq_div[symmetric] dvd_eq_mod_eq_0 int_of_reals
  2.1595 +               del: real_of_ints)
  2.1596 +   next
  2.1597 +    assume "\<not> 0 \<le> l"
  2.1598 +    def y' == "y * 2 ^ nat (- l)"
  2.1599 +    from `y \<noteq> 0` have "y' \<noteq> 0" by (simp add: y'_def)
  2.1600 +    have "int y * 2 ^ nat (- l) = y'" by (simp add: y'_def int_mult int_power)
  2.1601 +    moreover have "real x * real (2::int) powr real l / real y = x / real y'"
  2.1602 +      using `\<not> 0 \<le> l`
  2.1603 +      by (simp add: powr_realpow[symmetric] powr_minus y'_def field_simps inverse_eq_divide)
  2.1604 +    ultimately show ?thesis
  2.1605 +      using ceil_divide_floor_conv[of y' x] `\<not> 0 \<le> l` `y' \<noteq> 0` `y \<noteq> 0`
  2.1606 +      by (simp add: rapprox_posrat_def l_def round_up_def ceil_divide_floor_conv
  2.1607 +                    floor_divide_eq_div[symmetric] dvd_eq_mod_eq_0 int_of_reals
  2.1608 +               del: real_of_ints)
  2.1609    qed
  2.1610  qed
  2.1611  
  2.1612 -lemma rapprox_posrat_le1: assumes "0 \<le> x" and "0 < y" and "x \<le> y"
  2.1613 -  shows "real (rapprox_posrat n x y) \<le> 1"
  2.1614 +
  2.1615 +lemma rat_precision_pos:
  2.1616 +  assumes "0 \<le> x" and "0 < y" and "2 * x < y" and "0 < n"
  2.1617 +  shows "rat_precision n (int x) (int y) > 0"
  2.1618  proof -
  2.1619 -  let ?l = "nat (int n + bitlen y - bitlen x)" let ?X = "x * 2^?l"
  2.1620 -  show ?thesis
  2.1621 -  proof (cases "?X mod y = 0")
  2.1622 -    case True hence "y dvd ?X" using `0 < y` by auto
  2.1623 -    from real_of_int_div[OF this]
  2.1624 -    have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
  2.1625 -    also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
  2.1626 -    finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
  2.1627 -    also have "real x / real y \<le> 1" using `0 \<le> x` and `0 < y` and `x \<le> y` by auto
  2.1628 -    finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat if_P[OF True]
  2.1629 -      unfolding real_of_float_simp pow2_minus pow2_int minus_minus by auto
  2.1630 -  next
  2.1631 -    case False
  2.1632 -    have "x \<noteq> y"
  2.1633 -    proof (rule ccontr)
  2.1634 -      assume "\<not> x \<noteq> y" hence "x = y" by auto
  2.1635 -      have "?X mod y = 0" unfolding `x = y` using mod_mult_self1_is_0 by auto
  2.1636 -      thus False using False by auto
  2.1637 -    qed
  2.1638 -    hence "x < y" using `x \<le> y` by auto
  2.1639 -    hence "real x / real y < 1" using `0 < y` and `0 \<le> x` by auto
  2.1640 -
  2.1641 -    from real_of_int_div4[of "?X" y]
  2.1642 -    have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_numeral .
  2.1643 -    also have "\<dots> < 1 * 2^?l" using `real x / real y < 1` by (rule mult_strict_right_mono, auto)
  2.1644 -    finally have "?X div y < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
  2.1645 -    hence "?X div y + 1 \<le> 2^?l" by auto
  2.1646 -    hence "real (?X div y + 1) * inverse (2^?l) \<le> 2^?l * inverse (2^?l)"
  2.1647 -      unfolding real_of_int_le_iff[of _ "2^?l", symmetric] real_of_int_power real_numeral
  2.1648 -      by (rule mult_right_mono, auto)
  2.1649 -    hence "real (?X div y + 1) * inverse (2^?l) \<le> 1" by auto
  2.1650 -    thus ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
  2.1651 -      unfolding pow2_minus pow2_int minus_minus by auto
  2.1652 -  qed
  2.1653 +  { assume "0 < x" hence "log 2 x + 1 = log 2 (2 * x)" by (simp add: log_mult) }
  2.1654 +  hence "bitlen (int x) < bitlen (int y)" using assms
  2.1655 +    by (simp add: bitlen_def del: floor_add_one)
  2.1656 +      (auto intro!: floor_mono simp add: floor_add_one[symmetric] simp del: floor_add floor_add_one)
  2.1657 +  thus ?thesis
  2.1658 +    using assms by (auto intro!: pos_add_strict simp add: field_simps rat_precision_def)
  2.1659  qed
  2.1660  
  2.1661 -lemma zdiv_greater_zero: fixes a b :: int assumes "0 < a" and "a \<le> b"
  2.1662 -  shows "0 < b div a"
  2.1663 -proof (rule ccontr)
  2.1664 -  have "0 \<le> b" using assms by auto
  2.1665 -  assume "\<not> 0 < b div a" hence "b div a = 0" using `0 \<le> b`[unfolded pos_imp_zdiv_nonneg_iff[OF `0<a`, of b, symmetric]] by auto
  2.1666 -  have "b = a * (b div a) + b mod a" by auto
  2.1667 -  hence "b = b mod a" unfolding `b div a = 0` by auto
  2.1668 -  hence "b < a" using `0 < a`[THEN pos_mod_bound, of b] by auto
  2.1669 -  thus False using `a \<le> b` by auto
  2.1670 +lemma power_aux: assumes "x > 0" shows "(2::int) ^ nat (x - 1) \<le> 2 ^ nat x - 1"
  2.1671 +proof -
  2.1672 +  def y \<equiv> "nat (x - 1)" moreover
  2.1673 +  have "(2::int) ^ y \<le> (2 ^ (y + 1)) - 1" by simp
  2.1674 +  ultimately show ?thesis using assms by simp
  2.1675  qed
  2.1676  
  2.1677  lemma rapprox_posrat_less1: assumes "0 \<le> x" and "0 < y" and "2 * x < y" and "0 < n"
  2.1678    shows "real (rapprox_posrat n x y) < 1"
  2.1679 -proof (cases "x = 0")
  2.1680 -  case True thus ?thesis unfolding rapprox_posrat_def True Let_def normfloat real_of_float_simp by auto
  2.1681 -next
  2.1682 -  case False hence "0 < x" using `0 \<le> x` by auto
  2.1683 -  hence "x < y" using assms by auto
  2.1684 -  
  2.1685 -  let ?l = "nat (int n + bitlen y - bitlen x)" let ?X = "x * 2^?l"
  2.1686 -  show ?thesis
  2.1687 -  proof (cases "?X mod y = 0")
  2.1688 -    case True hence "y dvd ?X" using `0 < y` by auto
  2.1689 -    from real_of_int_div[OF this]
  2.1690 -    have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
  2.1691 -    also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
  2.1692 -    finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
  2.1693 -    also have "real x / real y < 1" using `0 \<le> x` and `0 < y` and `x < y` by auto
  2.1694 -    finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_P[OF True]
  2.1695 -      unfolding pow2_minus pow2_int minus_minus by auto
  2.1696 -  next
  2.1697 -    case False
  2.1698 -    hence "(real x / real y) < 1 / 2" using `0 < y` and `0 \<le> x` `2 * x < y` by auto
  2.1699 -
  2.1700 -    have "0 < ?X div y"
  2.1701 -    proof -
  2.1702 -      have "2^nat (bitlen x - 1) \<le> y" and "y < 2^nat (bitlen y)"
  2.1703 -        using bitlen_bounds[OF `0 < x`, THEN conjunct1] bitlen_bounds[OF `0 < y`, THEN conjunct2] `x < y` by auto
  2.1704 -      hence "(2::int)^nat (bitlen x - 1) < 2^nat (bitlen y)" by (rule order_le_less_trans)
  2.1705 -      hence "bitlen x \<le> bitlen y" by auto
  2.1706 -      hence len_less: "nat (bitlen x - 1) \<le> nat (int (n - 1) + bitlen y)" by auto
  2.1707 -
  2.1708 -      have "x \<noteq> 0" and "y \<noteq> 0" using `0 < x` `0 < y` by auto
  2.1709 -
  2.1710 -      have exp_eq: "nat (int (n - 1) + bitlen y) - nat (bitlen x - 1) = ?l"
  2.1711 -        using `bitlen x \<le> bitlen y` bitlen_ge1[OF `x \<noteq> 0`] bitlen_ge1[OF `y \<noteq> 0`] `0 < n` by auto
  2.1712 -
  2.1713 -      have "y * 2^nat (bitlen x - 1) \<le> y * x" 
  2.1714 -        using bitlen_bounds[OF `0 < x`, THEN conjunct1] `0 < y`[THEN less_imp_le] by (rule mult_left_mono)
  2.1715 -      also have "\<dots> \<le> 2^nat (bitlen y) * x" using bitlen_bounds[OF `0 < y`, THEN conjunct2, THEN less_imp_le] `0 \<le> x` by (rule mult_right_mono)
  2.1716 -      also have "\<dots> \<le> x * 2^nat (int (n - 1) + bitlen y)" unfolding mult_commute[of x] by (rule mult_right_mono, auto simp add: `0 \<le> x`)
  2.1717 -      finally have "real y * 2^nat (bitlen x - 1) * inverse (2^nat (bitlen x - 1)) \<le> real x * 2^nat (int (n - 1) + bitlen y) * inverse (2^nat (bitlen x - 1))"
  2.1718 -        unfolding real_of_int_le_iff[symmetric] by auto
  2.1719 -      hence "real y \<le> real x * (2^nat (int (n - 1) + bitlen y) / (2^nat (bitlen x - 1)))" 
  2.1720 -        unfolding mult_assoc divide_inverse by auto
  2.1721 -      also have "\<dots> = real x * (2^(nat (int (n - 1) + bitlen y) - nat (bitlen x - 1)))" using power_diff[of "2::real", OF _ len_less] by auto
  2.1722 -      finally have "y \<le> x * 2^?l" unfolding exp_eq unfolding real_of_int_le_iff[symmetric] by auto
  2.1723 -      thus ?thesis using zdiv_greater_zero[OF `0 < y`] by auto
  2.1724 -    qed
  2.1725 -
  2.1726 -    from real_of_int_div4[of "?X" y]
  2.1727 -    have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_numeral .
  2.1728 -    also have "\<dots> < 1/2 * 2^?l" using `real x / real y < 1/2` by (rule mult_strict_right_mono, auto)
  2.1729 -    finally have "?X div y * 2 < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
  2.1730 -    hence "?X div y + 1 < 2^?l" using `0 < ?X div y` by auto
  2.1731 -    hence "real (?X div y + 1) * inverse (2^?l) < 2^?l * inverse (2^?l)"
  2.1732 -      unfolding real_of_int_less_iff[of _ "2^?l", symmetric] real_of_int_power real_numeral
  2.1733 -      by (rule mult_strict_right_mono, auto)
  2.1734 -    hence "real (?X div y + 1) * inverse (2^?l) < 1" by auto
  2.1735 -    thus ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
  2.1736 -      unfolding pow2_minus pow2_int minus_minus by auto
  2.1737 -  qed
  2.1738 +proof -
  2.1739 +  have powr1: "2 powr real (rat_precision n (int x) (int y)) = 
  2.1740 +    2 ^ nat (rat_precision n (int x) (int y))" using rat_precision_pos[of x y n] assms
  2.1741 +    by (simp add: powr_realpow[symmetric])
  2.1742 +  have "x * 2 powr real (rat_precision n (int x) (int y)) / y = (x / y) *
  2.1743 +     2 powr real (rat_precision n (int x) (int y))" by simp
  2.1744 +  also have "... < (1 / 2) * 2 powr real (rat_precision n (int x) (int y))"
  2.1745 +    apply (rule mult_strict_right_mono) by (insert assms) auto
  2.1746 +  also have "\<dots> = 2 powr real (rat_precision n (int x) (int y) - 1)"
  2.1747 +    by (simp add: powr_add diff_def powr_neg_numeral)
  2.1748 +  also have "\<dots> = 2 ^ nat (rat_precision n (int x) (int y) - 1)"
  2.1749 +    using rat_precision_pos[of x y n] assms by (simp add: powr_realpow[symmetric])
  2.1750 +  also have "\<dots> \<le> 2 ^ nat (rat_precision n (int x) (int y)) - 1"
  2.1751 +    unfolding int_of_reals real_of_int_le_iff
  2.1752 +    using rat_precision_pos[OF assms] by (rule power_aux)
  2.1753 +  finally show ?thesis unfolding rapprox_posrat_def
  2.1754 +    apply (simp add: round_up_def)
  2.1755 +    apply (simp add: round_up_def field_simps powr_minus inverse_eq_divide)
  2.1756 +    unfolding powr1
  2.1757 +    unfolding int_of_reals real_of_int_less_iff
  2.1758 +    unfolding ceiling_less_eq using rat_precision_pos[of x y n] assms apply simp done
  2.1759  qed
  2.1760  
  2.1761 -lemma approx_rat_pattern: fixes P and ps :: "nat * int * int"
  2.1762 -  assumes Y: "\<And>y prec x. \<lbrakk>y = 0; ps = (prec, x, 0)\<rbrakk> \<Longrightarrow> P" 
  2.1763 -  and A: "\<And>x y prec. \<lbrakk>0 \<le> x; 0 < y; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
  2.1764 -  and B: "\<And>x y prec. \<lbrakk>x < 0; 0 < y; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
  2.1765 -  and C: "\<And>x y prec. \<lbrakk>x < 0; y < 0; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
  2.1766 -  and D: "\<And>x y prec. \<lbrakk>0 \<le> x; y < 0; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
  2.1767 -  shows P
  2.1768 -proof -
  2.1769 -  obtain prec x y where [simp]: "ps = (prec, x, y)" by (cases ps) auto
  2.1770 -  from Y have "y = 0 \<Longrightarrow> P" by auto
  2.1771 -  moreover {
  2.1772 -    assume "0 < y"
  2.1773 -    have P
  2.1774 -    proof (cases "0 \<le> x")
  2.1775 -      case True
  2.1776 -      with A and `0 < y` show P by auto
  2.1777 -    next
  2.1778 -      case False
  2.1779 -      with B and `0 < y` show P by auto
  2.1780 -    qed
  2.1781 -  } 
  2.1782 -  moreover {
  2.1783 -    assume "y < 0"
  2.1784 -    have P
  2.1785 -    proof (cases "0 \<le> x")
  2.1786 -      case True
  2.1787 -      with D and `y < 0` show P by auto
  2.1788 -    next
  2.1789 -      case False
  2.1790 -      with C and `y < 0` show P by auto
  2.1791 -    qed
  2.1792 -  }
  2.1793 -  ultimately show P by (cases "y = 0 \<or> 0 < y \<or> y < 0") auto
  2.1794 -qed
  2.1795 -
  2.1796 -function lapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
  2.1797 -where
  2.1798 -  "y = 0 \<Longrightarrow> lapprox_rat prec x y = 0"
  2.1799 -| "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> lapprox_rat prec x y = lapprox_posrat prec x y"
  2.1800 -| "x < 0 \<Longrightarrow> 0 < y \<Longrightarrow> lapprox_rat prec x y = - (rapprox_posrat prec (-x) y)"
  2.1801 -| "x < 0 \<Longrightarrow> y < 0 \<Longrightarrow> lapprox_rat prec x y = lapprox_posrat prec (-x) (-y)"
  2.1802 -| "0 \<le> x \<Longrightarrow> y < 0 \<Longrightarrow> lapprox_rat prec x y = - (rapprox_posrat prec x (-y))"
  2.1803 -apply simp_all by (rule approx_rat_pattern)
  2.1804 -termination by lexicographic_order
  2.1805 +definition lapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float" where
  2.1806 +  "lapprox_rat prec x y = float_of (round_down (rat_precision prec \<bar>x\<bar> \<bar>y\<bar>) (x / y))"
  2.1807  
  2.1808  lemma compute_lapprox_rat[code]:
  2.1809 -      "lapprox_rat prec x y = (if y = 0 then 0 else if 0 \<le> x then (if 0 < y then lapprox_posrat prec x y else - (rapprox_posrat prec x (-y))) 
  2.1810 -                                                             else (if 0 < y then - (rapprox_posrat prec (-x) y) else lapprox_posrat prec (-x) (-y)))"
  2.1811 -  by auto
  2.1812 -            
  2.1813 -lemma lapprox_rat: "real (lapprox_rat prec x y) \<le> real x / real y"
  2.1814 -proof -      
  2.1815 -  have h[rule_format]: "! a b b'. b' \<le> b \<longrightarrow> a \<le> b' \<longrightarrow> a \<le> (b::real)" by auto
  2.1816 -  show ?thesis
  2.1817 -    apply (case_tac "y = 0")
  2.1818 -    apply simp
  2.1819 -    apply (case_tac "0 \<le> x \<and> 0 < y")
  2.1820 -    apply (simp add: lapprox_posrat)
  2.1821 -    apply (case_tac "x < 0 \<and> 0 < y")
  2.1822 -    apply simp
  2.1823 -    apply (subst minus_le_iff)   
  2.1824 -    apply (rule h[OF rapprox_posrat])
  2.1825 -    apply (simp_all)
  2.1826 -    apply (case_tac "x < 0 \<and> y < 0")
  2.1827 -    apply simp
  2.1828 -    apply (rule h[OF _ lapprox_posrat])
  2.1829 -    apply (simp_all)
  2.1830 -    apply (case_tac "0 \<le> x \<and> y < 0")
  2.1831 -    apply (simp)
  2.1832 -    apply (subst minus_le_iff)   
  2.1833 -    apply (rule h[OF rapprox_posrat])
  2.1834 -    apply simp_all
  2.1835 -    apply arith
  2.1836 -    done
  2.1837 +  "lapprox_rat prec x y =
  2.1838 +    (if y = 0 then 0
  2.1839 +    else if 0 \<le> x then
  2.1840 +      (if 0 < y then lapprox_posrat prec (nat x) (nat y)
  2.1841 +      else - (rapprox_posrat prec (nat x) (nat (-y)))) 
  2.1842 +      else (if 0 < y
  2.1843 +        then - (rapprox_posrat prec (nat (-x)) (nat y))
  2.1844 +        else lapprox_posrat prec (nat (-x)) (nat (-y))))"
  2.1845 +  apply (cases "y = 0")
  2.1846 +  apply (simp add: lapprox_posrat_def rapprox_posrat_def round_down_def lapprox_rat_def)
  2.1847 +  apply (auto simp: lapprox_rat_def lapprox_posrat_def rapprox_posrat_def round_up_def round_down_def
  2.1848 +        ceiling_def real_of_float_uminus[symmetric] ac_simps int_of_reals simp del: real_of_ints)
  2.1849 +  apply (auto simp: ac_simps)
  2.1850 +  done
  2.1851 +
  2.1852 +definition rapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float" where
  2.1853 +  "rapprox_rat prec x y = float_of (round_up (rat_precision prec \<bar>x\<bar> \<bar>y\<bar>) (x / y))"
  2.1854 +
  2.1855 +lemma compute_rapprox_rat[code]:
  2.1856 +  "rapprox_rat prec x y =
  2.1857 +    (if y = 0 then 0
  2.1858 +    else if 0 \<le> x then
  2.1859 +      (if 0 < y then rapprox_posrat prec (nat x) (nat y)
  2.1860 +      else - (lapprox_posrat prec (nat x) (nat (-y)))) 
  2.1861 +      else (if 0 < y
  2.1862 +        then - (lapprox_posrat prec (nat (-x)) (nat y))
  2.1863 +        else rapprox_posrat prec (nat (-x)) (nat (-y))))"
  2.1864 +  apply (cases "y = 0", simp add: lapprox_posrat_def rapprox_posrat_def round_up_def rapprox_rat_def)
  2.1865 +  apply (auto simp: rapprox_rat_def lapprox_posrat_def rapprox_posrat_def round_up_def round_down_def
  2.1866 +        ceiling_def real_of_float_uminus[symmetric] ac_simps int_of_reals simp del: real_of_ints)
  2.1867 +  apply (auto simp: ac_simps)
  2.1868 +  done
  2.1869 +
  2.1870 +subsection {* Division *}
  2.1871 +
  2.1872 +definition div_precision
  2.1873 +where "div_precision prec x y =
  2.1874 +  rat_precision prec \<bar>mantissa x\<bar> \<bar>mantissa y\<bar> - exponent x + exponent y"
  2.1875 +
  2.1876 +definition float_divl :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
  2.1877 +where "float_divl prec a b =
  2.1878 +  float_of (round_down (div_precision prec a b) (a / b))"
  2.1879 +
  2.1880 +lemma compute_float_divl[code]:
  2.1881 +  fixes m1 s1 m2 s2
  2.1882 +  defines "f1 \<equiv> Float m1 s1" and "f2 \<equiv> Float m2 s2"
  2.1883 +  shows "float_divl prec f1 f2 = lapprox_rat prec m1 m2 * Float 1 (s1 - s2)"
  2.1884 +proof cases
  2.1885 +  assume "f1 \<noteq> 0 \<and> f2 \<noteq> 0"
  2.1886 +  then have "f1 \<noteq> float_of 0" "f2 \<noteq> float_of 0" by auto
  2.1887 +  with mantissa_not_dvd[of f1] mantissa_not_dvd[of f2]
  2.1888 +  have "mantissa f1 \<noteq> 0" "mantissa f2 \<noteq> 0"
  2.1889 +    by (auto simp add: dvd_def)  
  2.1890 +  then have pos: "0 < \<bar>mantissa f1\<bar>" "0 < \<bar>mantissa f2\<bar>"
  2.1891 +    by simp_all
  2.1892 +  moreover from f1_def[THEN denormalize_shift, OF `f1 \<noteq> float_of 0`] guess i . note i = this
  2.1893 +  moreover from f2_def[THEN denormalize_shift, OF `f2 \<noteq> float_of 0`] guess j . note j = this
  2.1894 +  moreover have "(real (mantissa f1) * 2 ^ i / (real (mantissa f2) * 2 ^ j))
  2.1895 +    = (real (mantissa f1) / real (mantissa f2)) * 2 powr (int i - int j)"
  2.1896 +    by (simp add: powr_divide2[symmetric] powr_realpow)
  2.1897 +  moreover have "real f1 / real f2 = real (mantissa f1) / real (mantissa f2) * 2 powr real (exponent f1 - exponent f2)"
  2.1898 +    unfolding mantissa_exponent by (simp add: powr_divide2[symmetric])
  2.1899 +  moreover have "rat_precision prec (\<bar>mantissa f1\<bar> * 2 ^ i) (\<bar>mantissa f2\<bar> * 2 ^ j) =
  2.1900 +    rat_precision prec \<bar>mantissa f1\<bar> \<bar>mantissa f2\<bar> + j - i"
  2.1901 +    using pos by (simp add: rat_precision_def)
  2.1902 +  ultimately show ?thesis
  2.1903 +    unfolding float_divl_def lapprox_rat_def div_precision_def
  2.1904 +    by (simp add: abs_mult round_down_shift powr_divide2[symmetric]
  2.1905 +                del: int_nat_eq real_of_int_diff times_divide_eq_left )
  2.1906 +       (simp add: field_simps powr_divide2[symmetric] powr_add)
  2.1907 +next
  2.1908 +  assume "\<not> (f1 \<noteq> 0 \<and> f2 \<noteq> 0)" then show ?thesis
  2.1909 +    by (auto simp add: float_divl_def f1_def f2_def lapprox_rat_def)
  2.1910 +qed  
  2.1911 +
  2.1912 +definition float_divr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
  2.1913 +where "float_divr prec a b =
  2.1914 +  float_of (round_up (div_precision prec a b) (a / b))"
  2.1915 +
  2.1916 +lemma compute_float_divr[code]:
  2.1917 +  fixes m1 s1 m2 s2
  2.1918 +  defines "f1 \<equiv> Float m1 s1" and "f2 \<equiv> Float m2 s2"
  2.1919 +  shows "float_divr prec f1 f2 = rapprox_rat prec m1 m2 * Float 1 (s1 - s2)"
  2.1920 +proof cases
  2.1921 +  assume "f1 \<noteq> 0 \<and> f2 \<noteq> 0"
  2.1922 +  then have "f1 \<noteq> float_of 0" "f2 \<noteq> float_of 0" by auto
  2.1923 +  with mantissa_not_dvd[of f1] mantissa_not_dvd[of f2]
  2.1924 +  have "mantissa f1 \<noteq> 0" "mantissa f2 \<noteq> 0"
  2.1925 +    by (auto simp add: dvd_def)  
  2.1926 +  then have pos: "0 < \<bar>mantissa f1\<bar>" "0 < \<bar>mantissa f2\<bar>"
  2.1927 +    by simp_all
  2.1928 +  moreover from f1_def[THEN denormalize_shift, OF `f1 \<noteq> float_of 0`] guess i . note i = this
  2.1929 +  moreover from f2_def[THEN denormalize_shift, OF `f2 \<noteq> float_of 0`] guess j . note j = this
  2.1930 +  moreover have "(real (mantissa f1) * 2 ^ i / (real (mantissa f2) * 2 ^ j))
  2.1931 +    = (real (mantissa f1) / real (mantissa f2)) * 2 powr (int i - int j)"
  2.1932 +    by (simp add: powr_divide2[symmetric] powr_realpow)
  2.1933 +  moreover have "real f1 / real f2 = real (mantissa f1) / real (mantissa f2) * 2 powr real (exponent f1 - exponent f2)"
  2.1934 +    unfolding mantissa_exponent by (simp add: powr_divide2[symmetric])
  2.1935 +  moreover have "rat_precision prec (\<bar>mantissa f1\<bar> * 2 ^ i) (\<bar>mantissa f2\<bar> * 2 ^ j) =
  2.1936 +    rat_precision prec \<bar>mantissa f1\<bar> \<bar>mantissa f2\<bar> + j - i"
  2.1937 +    using pos by (simp add: rat_precision_def)
  2.1938 +  ultimately show ?thesis
  2.1939 +    unfolding float_divr_def rapprox_rat_def div_precision_def
  2.1940 +    by (simp add: abs_mult round_up_shift powr_divide2[symmetric]
  2.1941 +                del: int_nat_eq real_of_int_diff times_divide_eq_left)
  2.1942 +       (simp add: field_simps powr_divide2[symmetric] powr_add)
  2.1943 +next
  2.1944 +  assume "\<not> (f1 \<noteq> 0 \<and> f2 \<noteq> 0)" then show ?thesis
  2.1945 +    by (auto simp add: float_divr_def f1_def f2_def rapprox_rat_def)
  2.1946  qed
  2.1947  
  2.1948 -lemma lapprox_rat_bottom: assumes "0 \<le> x" and "0 < y"
  2.1949 -  shows "real (x div y) \<le> real (lapprox_rat n x y)" 
  2.1950 -  unfolding lapprox_rat.simps(2)[OF assms]  using lapprox_posrat_bottom[OF `0<y`] .
  2.1951 +subsection {* Lemmas needed by Approximate *}
  2.1952 +
  2.1953 +declare one_float_def[simp del] zero_float_def[simp del]
  2.1954 +
  2.1955 +lemma Float_num[simp]: shows
  2.1956 +   "real (Float 1 0) = 1" and "real (Float 1 1) = 2" and "real (Float 1 2) = 4" and
  2.1957 +   "real (Float 1 -1) = 1/2" and "real (Float 1 -2) = 1/4" and "real (Float 1 -3) = 1/8" and
  2.1958 +   "real (Float -1 0) = -1" and "real (Float (number_of n) 0) = number_of n"
  2.1959 +using two_powr_int_float[of 2] two_powr_int_float[of "-1"] two_powr_int_float[of "-2"] two_powr_int_float[of "-3"]
  2.1960 +using powr_realpow[of 2 2] powr_realpow[of 2 3]
  2.1961 +using powr_minus[of 2 1] powr_minus[of 2 2] powr_minus[of 2 3]
  2.1962 +by auto
  2.1963 +
  2.1964 +lemma real_of_Float_int[simp]: "real (Float n 0) = real n" by simp
  2.1965 +
  2.1966 +lemma float_zero[simp]: "real (Float 0 e) = 0" by simp
  2.1967 +
  2.1968 +lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
  2.1969 +by arith
  2.1970  
  2.1971 -function rapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
  2.1972 -where
  2.1973 -  "y = 0 \<Longrightarrow> rapprox_rat prec x y = 0"
  2.1974 -| "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> rapprox_rat prec x y = rapprox_posrat prec x y"
  2.1975 -| "x < 0 \<Longrightarrow> 0 < y \<Longrightarrow> rapprox_rat prec x y = - (lapprox_posrat prec (-x) y)"
  2.1976 -| "x < 0 \<Longrightarrow> y < 0 \<Longrightarrow> rapprox_rat prec x y = rapprox_posrat prec (-x) (-y)"
  2.1977 -| "0 \<le> x \<Longrightarrow> y < 0 \<Longrightarrow> rapprox_rat prec x y = - (lapprox_posrat prec x (-y))"
  2.1978 -apply simp_all by (rule approx_rat_pattern)
  2.1979 -termination by lexicographic_order
  2.1980 +lemma lapprox_rat:
  2.1981 +  shows "real (lapprox_rat prec x y) \<le> real x / real y"
  2.1982 +  using round_down by (simp add: lapprox_rat_def)
  2.1983  
  2.1984 -lemma compute_rapprox_rat[code]:
  2.1985 -      "rapprox_rat prec x y = (if y = 0 then 0 else if 0 \<le> x then (if 0 < y then rapprox_posrat prec x y else - (lapprox_posrat prec x (-y))) else 
  2.1986 -                                                                  (if 0 < y then - (lapprox_posrat prec (-x) y) else rapprox_posrat prec (-x) (-y)))"
  2.1987 -  by auto
  2.1988 +lemma mult_div_le: fixes a b:: int assumes "b > 0" shows "a \<ge> b * (a div b)"
  2.1989 +proof -
  2.1990 +  from zmod_zdiv_equality'[of a b]
  2.1991 +  have "a = b * (a div b) + a mod b" by simp
  2.1992 +  also have "... \<ge> b * (a div b) + 0" apply (rule add_left_mono) apply (rule pos_mod_sign)
  2.1993 +  using assms by simp
  2.1994 +  finally show ?thesis by simp
  2.1995 +qed
  2.1996 +
  2.1997 +lemma lapprox_rat_nonneg:
  2.1998 +  fixes n x y
  2.1999 +  defines "p == int n - ((bitlen \<bar>x\<bar>) - (bitlen \<bar>y\<bar>))"
  2.2000 +  assumes "0 \<le> x" "0 < y"
  2.2001 +  shows "0 \<le> real (lapprox_rat n x y)"
  2.2002 +using assms unfolding lapprox_rat_def p_def[symmetric] round_down_def real_of_int_minus[symmetric]
  2.2003 +   powr_int[of 2, simplified]
  2.2004 +  by (auto simp add: inverse_eq_divide intro!: mult_nonneg_nonneg divide_nonneg_pos mult_pos_pos)
  2.2005  
  2.2006  lemma rapprox_rat: "real x / real y \<le> real (rapprox_rat prec x y)"
  2.2007 -proof -      
  2.2008 -  have h[rule_format]: "! a b b'. b' \<le> b \<longrightarrow> a \<le> b' \<longrightarrow> a \<le> (b::real)" by auto
  2.2009 -  show ?thesis
  2.2010 -    apply (case_tac "y = 0")
  2.2011 -    apply simp
  2.2012 -    apply (case_tac "0 \<le> x \<and> 0 < y")
  2.2013 -    apply (simp add: rapprox_posrat)
  2.2014 -    apply (case_tac "x < 0 \<and> 0 < y")
  2.2015 -    apply simp
  2.2016 -    apply (subst le_minus_iff)   
  2.2017 -    apply (rule h[OF _ lapprox_posrat])
  2.2018 -    apply (simp_all)
  2.2019 -    apply (case_tac "x < 0 \<and> y < 0")
  2.2020 -    apply simp
  2.2021 -    apply (rule h[OF rapprox_posrat])
  2.2022 -    apply (simp_all)
  2.2023 -    apply (case_tac "0 \<le> x \<and> y < 0")
  2.2024 -    apply (simp)
  2.2025 -    apply (subst le_minus_iff)   
  2.2026 -    apply (rule h[OF _ lapprox_posrat])
  2.2027 -    apply simp_all
  2.2028 -    apply arith
  2.2029 -    done
  2.2030 +  using round_up by (simp add: rapprox_rat_def)
  2.2031 +
  2.2032 +lemma rapprox_rat_le1:
  2.2033 +  fixes n x y
  2.2034 +  assumes xy: "0 \<le> x" "0 < y" "x \<le> y"
  2.2035 +  shows "real (rapprox_rat n x y) \<le> 1"
  2.2036 +proof -
  2.2037 +  have "bitlen \<bar>x\<bar> \<le> bitlen \<bar>y\<bar>"
  2.2038 +    using xy unfolding bitlen_def by (auto intro!: floor_mono)
  2.2039 +  then have "0 \<le> rat_precision n \<bar>x\<bar> \<bar>y\<bar>" by (simp add: rat_precision_def)
  2.2040 +  have "real \<lceil>real x / real y * 2 powr real (rat_precision n \<bar>x\<bar> \<bar>y\<bar>)\<rceil>
  2.2041 +      \<le> real \<lceil>2 powr real (rat_precision n \<bar>x\<bar> \<bar>y\<bar>)\<rceil>"
  2.2042 +    using xy by (auto intro!: ceiling_mono simp: field_simps)
  2.2043 +  also have "\<dots> = 2 powr real (rat_precision n \<bar>x\<bar> \<bar>y\<bar>)"
  2.2044 +    using `0 \<le> rat_precision n \<bar>x\<bar> \<bar>y\<bar>`
  2.2045 +    by (auto intro!: exI[of _ "2^nat (rat_precision n \<bar>x\<bar> \<bar>y\<bar>)"] simp: powr_int)
  2.2046 +  finally show ?thesis
  2.2047 +    by (simp add: rapprox_rat_def round_up_def)
  2.2048 +       (simp add: powr_minus inverse_eq_divide)
  2.2049  qed
  2.2050  
  2.2051 -lemma rapprox_rat_le1: assumes "0 \<le> x" and "0 < y" and "x \<le> y"
  2.2052 -  shows "real (rapprox_rat n x y) \<le> 1"
  2.2053 -  unfolding rapprox_rat.simps(2)[OF `0 \<le> x` `0 < y`] using rapprox_posrat_le1[OF assms] .
  2.2054 -
  2.2055 -lemma rapprox_rat_neg: assumes "x < 0" and "0 < y"
  2.2056 -  shows "real (rapprox_rat n x y) \<le> 0"
  2.2057 -  unfolding rapprox_rat.simps(3)[OF assms] using lapprox_posrat_nonneg[of "-x" y n] assms by auto
  2.2058 -
  2.2059 -lemma rapprox_rat_nonneg_neg: assumes "0 \<le> x" and "y < 0"
  2.2060 -  shows "real (rapprox_rat n x y) \<le> 0"
  2.2061 -  unfolding rapprox_rat.simps(5)[OF assms] using lapprox_posrat_nonneg[of x "-y" n] assms by auto
  2.2062 +lemma rapprox_rat_nonneg_neg: 
  2.2063 +  "0 \<le> x \<Longrightarrow> y < 0 \<Longrightarrow> real (rapprox_rat n x y) \<le> 0"
  2.2064 +  unfolding rapprox_rat_def round_up_def
  2.2065 +  by (auto simp: field_simps mult_le_0_iff zero_le_mult_iff)
  2.2066  
  2.2067 -lemma rapprox_rat_nonpos_pos: assumes "x \<le> 0" and "0 < y"
  2.2068 -  shows "real (rapprox_rat n x y) \<le> 0"
  2.2069 -proof (cases "x = 0") 
  2.2070 -  case True
  2.2071 -  hence "0 \<le> x" by auto show ?thesis
  2.2072 -    unfolding rapprox_rat.simps(2)[OF `0 \<le> x` `0 < y`]
  2.2073 -    unfolding True rapprox_posrat_def Let_def
  2.2074 -    by auto
  2.2075 -next
  2.2076 -  case False
  2.2077 -  hence "x < 0" using assms by auto
  2.2078 -  show ?thesis using rapprox_rat_neg[OF `x < 0` `0 < y`] .
  2.2079 -qed
  2.2080 +lemma rapprox_rat_neg:
  2.2081 +  "x < 0 \<Longrightarrow> 0 < y \<Longrightarrow> real (rapprox_rat n x y) \<le> 0"
  2.2082 +  unfolding rapprox_rat_def round_up_def
  2.2083 +  by (auto simp: field_simps mult_le_0_iff)
  2.2084  
  2.2085 -fun float_divl :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
  2.2086 -where
  2.2087 -  "float_divl prec (Float m1 s1) (Float m2 s2) = 
  2.2088 -    (let
  2.2089 -       l = lapprox_rat prec m1 m2;
  2.2090 -       f = Float 1 (s1 - s2)
  2.2091 -     in
  2.2092 -       f * l)"     
  2.2093 +lemma rapprox_rat_nonpos_pos:
  2.2094 +  "x \<le> 0 \<Longrightarrow> 0 < y \<Longrightarrow> real (rapprox_rat n x y) \<le> 0"
  2.2095 +  unfolding rapprox_rat_def round_up_def
  2.2096 +  by (auto simp: field_simps mult_le_0_iff)
  2.2097  
  2.2098  lemma float_divl: "real (float_divl prec x y) \<le> real x / real y"
  2.2099 -  using lapprox_rat[of prec "mantissa x" "mantissa y"]
  2.2100 -  by (cases x y rule: float.exhaust[case_product float.exhaust])
  2.2101 -     (simp split: split_if_asm
  2.2102 -           add: real_of_float_simp pow2_diff field_simps le_divide_eq mult_less_0_iff zero_less_mult_iff)
  2.2103 +  using round_down by (simp add: float_divl_def)
  2.2104 +
  2.2105 +lemma float_divl_lower_bound:
  2.2106 +  fixes x y prec
  2.2107 +  defines "p == rat_precision prec \<bar>mantissa x\<bar> \<bar>mantissa y\<bar> - exponent x + exponent y"
  2.2108 +  assumes xy: "0 \<le> x" "0 < y" shows "0 \<le> real (float_divl prec x y)"
  2.2109 +  using xy unfolding float_divl_def p_def[symmetric] round_down_def
  2.2110 +  by (simp add: zero_le_mult_iff zero_le_divide_iff less_eq_float_def less_float_def)
  2.2111 +
  2.2112 +lemma exponent_1: "exponent 1 = 0"
  2.2113 +  using exponent_float[of 1 0] by (simp add: one_float_def)
  2.2114 +
  2.2115 +lemma mantissa_1: "mantissa 1 = 1"
  2.2116 +  using mantissa_float[of 1 0] by (simp add: one_float_def)
  2.2117  
  2.2118 -lemma float_divl_lower_bound: assumes "0 \<le> x" and "0 < y" shows "0 \<le> float_divl prec x y"
  2.2119 -proof (cases x, cases y)
  2.2120 -  fix xm xe ym ye :: int
  2.2121 -  assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
  2.2122 -  have "0 \<le> xm"
  2.2123 -    using `0 \<le> x`[unfolded x_eq le_float_def real_of_float_simp real_of_float_0 zero_le_mult_iff]
  2.2124 -    by auto
  2.2125 -  have "0 < ym"
  2.2126 -    using `0 < y`[unfolded y_eq less_float_def real_of_float_simp real_of_float_0 zero_less_mult_iff]
  2.2127 -    by auto
  2.2128 +lemma bitlen_1: "bitlen 1 = 1"
  2.2129 +  by (simp add: bitlen_def)
  2.2130 +
  2.2131 +lemma mantissa_eq_zero_iff: "mantissa x = 0 \<longleftrightarrow> x = 0"
  2.2132 +proof
  2.2133 +  assume "mantissa x = 0" hence z: "0 = real x" using mantissa_exponent by simp
  2.2134 +  show "x = 0" by (simp add: zero_float_def z)
  2.2135 +qed (simp add: zero_float_def)
  2.2136  
  2.2137 -  have "\<And>n. 0 \<le> real (Float 1 n)"
  2.2138 -    unfolding real_of_float_simp using zero_le_pow2 by auto
  2.2139 -  moreover have "0 \<le> real (lapprox_rat prec xm ym)"
  2.2140 -    apply (rule order_trans[OF _ lapprox_rat_bottom[OF `0 \<le> xm` `0 < ym`]])
  2.2141 -    apply (auto simp add: `0 \<le> xm` pos_imp_zdiv_nonneg_iff[OF `0 < ym`])
  2.2142 -    done
  2.2143 -  ultimately show "0 \<le> float_divl prec x y"
  2.2144 -    unfolding x_eq y_eq float_divl.simps Let_def le_float_def real_of_float_0
  2.2145 -    by (auto intro!: mult_nonneg_nonneg)
  2.2146 +lemma float_upper_bound: "x \<le> 2 powr (bitlen \<bar>mantissa x\<bar> + exponent x)"
  2.2147 +proof (cases "x = 0", simp)
  2.2148 +  assume "x \<noteq> 0" hence "mantissa x \<noteq> 0" using mantissa_eq_zero_iff by auto
  2.2149 +  have "x = mantissa x * 2 powr (exponent x)" by (rule mantissa_exponent)
  2.2150 +  also have "mantissa x \<le> \<bar>mantissa x\<bar>" by simp
  2.2151 +  also have "... \<le> 2 powr (bitlen \<bar>mantissa x\<bar>)"
  2.2152 +    using bitlen_bounds[of "\<bar>mantissa x\<bar>"] bitlen_nonneg `mantissa x \<noteq> 0`
  2.2153 +    by (simp add: powr_int) (simp only: two_real_int int_of_reals real_of_int_abs[symmetric]
  2.2154 +      real_of_int_le_iff less_imp_le)
  2.2155 +  finally show ?thesis by (simp add: powr_add)
  2.2156  qed
  2.2157  
  2.2158  lemma float_divl_pos_less1_bound:
  2.2159 -  assumes "0 < x" and "x < 1" and "0 < prec"
  2.2160 -  shows "1 \<le> float_divl prec 1 x"
  2.2161 -proof (cases x)
  2.2162 -  case (Float m e)
  2.2163 -  from `0 < x` `x < 1` have "0 < m" "e < 0"
  2.2164 -    using float_pos_m_pos float_pos_less1_e_neg unfolding Float by auto
  2.2165 -  let ?b = "nat (bitlen m)" and ?e = "nat (-e)"
  2.2166 -  have "1 \<le> m" and "m \<noteq> 0" using `0 < m` by auto
  2.2167 -  with bitlen_bounds[OF `0 < m`] have "m < 2^?b" and "(2::int) \<le> 2^?b" by auto
  2.2168 -  hence "1 \<le> bitlen m" using power_le_imp_le_exp[of "2::int" 1 ?b] by auto
  2.2169 -  hence pow_split: "nat (int prec + bitlen m - 1) = (prec - 1) + ?b" using `0 < prec` by auto
  2.2170 -  
  2.2171 -  have pow_not0: "\<And>x. (2::real)^x \<noteq> 0" by auto
  2.2172 -
  2.2173 -  from float_less1_mantissa_bound `0 < x` `x < 1` Float 
  2.2174 -  have "m < 2^?e" by auto
  2.2175 -  with bitlen_bounds[OF `0 < m`, THEN conjunct1] have "(2::int)^nat (bitlen m - 1) < 2^?e"
  2.2176 -    by (rule order_le_less_trans)
  2.2177 -  from power_less_imp_less_exp[OF _ this]
  2.2178 -  have "bitlen m \<le> - e" by auto
  2.2179 -  hence "(2::real)^?b \<le> 2^?e" by auto
  2.2180 -  hence "(2::real)^?b * inverse (2^?b) \<le> 2^?e * inverse (2^?b)"
  2.2181 -    by (rule mult_right_mono) auto
  2.2182 -  hence "(1::real) \<le> 2^?e * inverse (2^?b)" by auto
  2.2183 -  also
  2.2184 -  let ?d = "real (2 ^ nat (int prec + bitlen m - 1) div m) * inverse (2 ^ nat (int prec + bitlen m - 1))"
  2.2185 -  {
  2.2186 -    have "2^(prec - 1) * m \<le> 2^(prec - 1) * 2^?b"
  2.2187 -      using `m < 2^?b`[THEN less_imp_le] by (rule mult_left_mono) auto
  2.2188 -    also have "\<dots> = 2 ^ nat (int prec + bitlen m - 1)"
  2.2189 -      unfolding pow_split power_add by auto
  2.2190 -    finally have "2^(prec - 1) * m div m \<le> 2 ^ nat (int prec + bitlen m - 1) div m"
  2.2191 -      using `0 < m` by (rule zdiv_mono1)
  2.2192 -    hence "2^(prec - 1) \<le> 2 ^ nat (int prec + bitlen m - 1) div m"
  2.2193 -      unfolding div_mult_self2_is_id[OF `m \<noteq> 0`] .
  2.2194 -    hence "2^(prec - 1) * inverse (2 ^ nat (int prec + bitlen m - 1)) \<le> ?d"
  2.2195 -      unfolding real_of_int_le_iff[of "2^(prec - 1)", symmetric] by auto
  2.2196 -  }
  2.2197 -  from mult_left_mono[OF this [unfolded pow_split power_add inverse_mult_distrib mult_assoc[symmetric] right_inverse[OF pow_not0] mult_1_left], of "2^?e"]
  2.2198 -  have "2^?e * inverse (2^?b) \<le> 2^?e * ?d" unfolding pow_split power_add by auto
  2.2199 -  finally have "1 \<le> 2^?e * ?d" .
  2.2200 -  
  2.2201 -  have e_nat: "0 - e = int (nat (-e))" using `e < 0` by auto
  2.2202 -  have "bitlen 1 = 1" using bitlen.simps by auto
  2.2203 -  
  2.2204 -  show ?thesis 
  2.2205 -    unfolding one_float_def Float float_divl.simps Let_def
  2.2206 -      lapprox_rat.simps(2)[OF zero_le_one `0 < m`]
  2.2207 -      lapprox_posrat_def `bitlen 1 = 1`
  2.2208 -    unfolding le_float_def real_of_float_mult normfloat real_of_float_simp
  2.2209 -      pow2_minus pow2_int e_nat
  2.2210 -    using `1 \<le> 2^?e * ?d` by (auto simp add: pow2_def)
  2.2211 +  assumes "0 < real x" and "real x < 1" and "prec \<ge> 1"
  2.2212 +  shows "1 \<le> real (float_divl prec 1 x)"
  2.2213 +proof cases
  2.2214 +  assume nonneg: "div_precision prec 1 x \<ge> 0"
  2.2215 +  hence "2 powr real (div_precision prec 1 x) =
  2.2216 +    floor (real ((2::int) ^ nat (div_precision prec 1 x))) * floor (1::real)"
  2.2217 +    by (simp add: powr_int del: real_of_int_power) simp
  2.2218 +  also have "floor (1::real) \<le> floor (1 / x)" using assms by simp
  2.2219 +  also have "floor (real ((2::int) ^ nat (div_precision prec 1 x))) * floor (1 / x) \<le>
  2.2220 +    floor (real ((2::int) ^ nat (div_precision prec 1 x)) * (1 / x))"
  2.2221 +    by (rule le_mult_floor) (auto simp: assms less_imp_le)
  2.2222 +  finally have "2 powr real (div_precision prec 1 x) <=
  2.2223 +    floor (2 powr nat (div_precision prec 1 x) / x)" by (simp add: powr_realpow)
  2.2224 +  thus ?thesis
  2.2225 +    using assms nonneg
  2.2226 +    unfolding float_divl_def round_down_def
  2.2227 +    by simp (simp add: powr_minus inverse_eq_divide)
  2.2228 +next
  2.2229 +  assume neg: "\<not> 0 \<le> div_precision prec 1 x"
  2.2230 +  have "1 \<le> 1 * 2 powr (prec - 1)" using assms by (simp add: powr_realpow)
  2.2231 +  also have "... \<le> 2 powr (bitlen \<bar>mantissa x\<bar> + exponent x) / x * 2 powr (prec - 1)"
  2.2232 +    apply (rule mult_mono) using assms float_upper_bound
  2.2233 +    by (auto intro!: divide_nonneg_pos)
  2.2234 +  also have "2 powr (bitlen \<bar>mantissa x\<bar> + exponent x) / x * 2 powr (prec - 1) =
  2.2235 +    2 powr real (div_precision prec 1 x) / real x"
  2.2236 +    using assms
  2.2237 +    apply (simp add: div_precision_def rat_precision_def diff_diff_eq2
  2.2238 +    mantissa_1 exponent_1 bitlen_1 powr_add powr_minus real_of_nat_diff)
  2.2239 +    apply (simp only: diff_def powr_add)
  2.2240 +    apply simp
  2.2241 +    done
  2.2242 +  finally have "1 \<le> \<lfloor>2 powr real (div_precision prec 1 x) / real x\<rfloor>"
  2.2243 +    using floor_mono[of "1::real"] by simp thm mult_mono
  2.2244 +  hence "1 \<le> real \<lfloor>2 powr real (div_precision prec 1 x) / real x\<rfloor>"
  2.2245 +    by (metis floor_real_of_int one_le_floor)
  2.2246 +  hence "1 * 1 \<le>
  2.2247 +    real \<lfloor>2 powr real (div_precision prec 1 x) / real x\<rfloor> * 2 powr - real (div_precision prec 1 x)"
  2.2248 +  apply (rule mult_mono)
  2.2249 +    using assms neg
  2.2250 +    by (auto intro: divide_nonneg_pos mult_nonneg_nonneg simp: real_of_int_minus[symmetric] powr_int simp del: real_of_int_minus) find_theorems "real (- _)"
  2.2251 +  thus ?thesis
  2.2252 +    using assms neg
  2.2253 +    unfolding float_divl_def round_down_def
  2.2254 +    by simp
  2.2255  qed
  2.2256  
  2.2257 -fun float_divr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
  2.2258 -where
  2.2259 -  "float_divr prec (Float m1 s1) (Float m2 s2) = 
  2.2260 -    (let
  2.2261 -       r = rapprox_rat prec m1 m2;
  2.2262 -       f = Float 1 (s1 - s2)
  2.2263 -     in
  2.2264 -       f * r)"  
  2.2265 -
  2.2266  lemma float_divr: "real x / real y \<le> real (float_divr prec x y)"
  2.2267 -  using rapprox_rat[of "mantissa x" "mantissa y" prec]
  2.2268 -  by (cases x y rule: float.exhaust[case_product float.exhaust])
  2.2269 -     (simp split: split_if_asm
  2.2270 -           add: real_of_float_simp pow2_diff field_simps divide_le_eq mult_less_0_iff zero_less_mult_iff)
  2.2271 +  using round_up by (simp add: float_divr_def)
  2.2272  
  2.2273  lemma float_divr_pos_less1_lower_bound: assumes "0 < x" and "x < 1" shows "1 \<le> float_divr prec 1 x"
  2.2274  proof -
  2.2275    have "1 \<le> 1 / real x" using `0 < x` and `x < 1` unfolding less_float_def by auto
  2.2276    also have "\<dots> \<le> real (float_divr prec 1 x)" using float_divr[where x=1 and y=x] by auto
  2.2277 -  finally show ?thesis unfolding le_float_def by auto
  2.2278 -qed
  2.2279 -
  2.2280 -lemma float_divr_nonpos_pos_upper_bound: assumes "x \<le> 0" and "0 < y" shows "float_divr prec x y \<le> 0"
  2.2281 -proof (cases x, cases y)
  2.2282 -  fix xm xe ym ye :: int
  2.2283 -  assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
  2.2284 -  have "xm \<le> 0" using `x \<le> 0`[unfolded x_eq le_float_def real_of_float_simp real_of_float_0 mult_le_0_iff] by auto
  2.2285 -  have "0 < ym" using `0 < y`[unfolded y_eq less_float_def real_of_float_simp real_of_float_0 zero_less_mult_iff] by auto
  2.2286 -
  2.2287 -  have "\<And>n. 0 \<le> real (Float 1 n)" unfolding real_of_float_simp using zero_le_pow2 by auto
  2.2288 -  moreover have "real (rapprox_rat prec xm ym) \<le> 0" using rapprox_rat_nonpos_pos[OF `xm \<le> 0` `0 < ym`] .
  2.2289 -  ultimately show "float_divr prec x y \<le> 0"
  2.2290 -    unfolding x_eq y_eq float_divr.simps Let_def le_float_def real_of_float_0 real_of_float_mult by (auto intro!: mult_nonneg_nonpos)
  2.2291 -qed
  2.2292 -
  2.2293 -lemma float_divr_nonneg_neg_upper_bound: assumes "0 \<le> x" and "y < 0" shows "float_divr prec x y \<le> 0"
  2.2294 -proof (cases x, cases y)
  2.2295 -  fix xm xe ym ye :: int
  2.2296 -  assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
  2.2297 -  have "0 \<le> xm" using `0 \<le> x`[unfolded x_eq le_float_def real_of_float_simp real_of_float_0 zero_le_mult_iff] by auto
  2.2298 -  have "ym < 0" using `y < 0`[unfolded y_eq less_float_def real_of_float_simp real_of_float_0 mult_less_0_iff] by auto
  2.2299 -  hence "0 < - ym" by auto
  2.2300 -
  2.2301 -  have "\<And>n. 0 \<le> real (Float 1 n)" unfolding real_of_float_simp using zero_le_pow2 by auto
  2.2302 -  moreover have "real (rapprox_rat prec xm ym) \<le> 0" using rapprox_rat_nonneg_neg[OF `0 \<le> xm` `ym < 0`] .
  2.2303 -  ultimately show "float_divr prec x y \<le> 0"
  2.2304 -    unfolding x_eq y_eq float_divr.simps Let_def le_float_def real_of_float_0 real_of_float_mult by (auto intro!: mult_nonneg_nonpos)
  2.2305 -qed
  2.2306 -
  2.2307 -primrec round_down :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.2308 -"round_down prec (Float m e) = (let d = bitlen m - int prec in
  2.2309 -     if 0 < d then let P = 2^nat d ; n = m div P in Float n (e + d)
  2.2310 -              else Float m e)"
  2.2311 -
  2.2312 -primrec round_up :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.2313 -"round_up prec (Float m e) = (let d = bitlen m - int prec in
  2.2314 -  if 0 < d then let P = 2^nat d ; n = m div P ; r = m mod P in Float (n + (if r = 0 then 0 else 1)) (e + d) 
  2.2315 -           else Float m e)"
  2.2316 -
  2.2317 -lemma round_up: "real x \<le> real (round_up prec x)"
  2.2318 -proof (cases x)
  2.2319 -  case (Float m e)
  2.2320 -  let ?d = "bitlen m - int prec"
  2.2321 -  let ?p = "(2::int)^nat ?d"
  2.2322 -  have "0 < ?p" by auto
  2.2323 -  show "?thesis"
  2.2324 -  proof (cases "0 < ?d")
  2.2325 -    case True
  2.2326 -    hence pow_d: "pow2 ?d = real ?p" using pow2_int[symmetric] by simp
  2.2327 -    show ?thesis
  2.2328 -    proof (cases "m mod ?p = 0")
  2.2329 -      case True
  2.2330 -      have m: "m = m div ?p * ?p + 0" unfolding True[symmetric] using mod_div_equality [symmetric] .
  2.2331 -      have "real (Float m e) = real (Float (m div ?p) (e + ?d))" unfolding real_of_float_simp arg_cong[OF m, of real]
  2.2332 -        by (auto simp add: pow2_add `0 < ?d` pow_d)
  2.2333 -      thus ?thesis
  2.2334 -        unfolding Float round_up.simps Let_def if_P[OF `m mod ?p = 0`] if_P[OF `0 < ?d`]
  2.2335 -        by auto
  2.2336 -    next
  2.2337 -      case False
  2.2338 -      have "m = m div ?p * ?p + m mod ?p" unfolding mod_div_equality ..
  2.2339 -      also have "\<dots> \<le> (m div ?p + 1) * ?p" unfolding left_distrib mult_1 by (rule add_left_mono, rule pos_mod_bound[OF `0 < ?p`, THEN less_imp_le])
  2.2340 -      finally have "real (Float m e) \<le> real (Float (m div ?p + 1) (e + ?d))" unfolding real_of_float_simp add_commute[of e]
  2.2341 -        unfolding pow2_add mult_assoc[symmetric] real_of_int_le_iff[of m, symmetric]
  2.2342 -        by (auto intro!: mult_mono simp add: pow2_add `0 < ?d` pow_d)
  2.2343 -      thus ?thesis
  2.2344 -        unfolding Float round_up.simps Let_def if_not_P[OF `\<not> m mod ?p = 0`] if_P[OF `0 < ?d`] .
  2.2345 -    qed
  2.2346 -  next
  2.2347 -    case False
  2.2348 -    show ?thesis
  2.2349 -      unfolding Float round_up.simps Let_def if_not_P[OF False] .. 
  2.2350 -  qed
  2.2351 -qed
  2.2352 -
  2.2353 -lemma round_down: "real (round_down prec x) \<le> real x"
  2.2354 -proof (cases x)
  2.2355 -  case (Float m e)
  2.2356 -  let ?d = "bitlen m - int prec"
  2.2357 -  let ?p = "(2::int)^nat ?d"
  2.2358 -  have "0 < ?p" by auto
  2.2359 -  show "?thesis"
  2.2360 -  proof (cases "0 < ?d")
  2.2361 -    case True
  2.2362 -    hence pow_d: "pow2 ?d = real ?p" using pow2_int[symmetric] by simp
  2.2363 -    have "m div ?p * ?p \<le> m div ?p * ?p + m mod ?p" by (auto simp add: pos_mod_bound[OF `0 < ?p`, THEN less_imp_le])
  2.2364 -    also have "\<dots> \<le> m" unfolding mod_div_equality ..
  2.2365 -    finally have "real (Float (m div ?p) (e + ?d)) \<le> real (Float m e)" unfolding real_of_float_simp add_commute[of e]
  2.2366 -      unfolding pow2_add mult_assoc[symmetric] real_of_int_le_iff[of _ m, symmetric]
  2.2367 -      by (auto intro!: mult_mono simp add: pow2_add `0 < ?d` pow_d)
  2.2368 -    thus ?thesis
  2.2369 -      unfolding Float round_down.simps Let_def if_P[OF `0 < ?d`] .
  2.2370 -  next
  2.2371 -    case False
  2.2372 -    show ?thesis
  2.2373 -      unfolding Float round_down.simps Let_def if_not_P[OF False] .. 
  2.2374 -  qed
  2.2375 +  finally show ?thesis unfolding less_eq_float_def by auto
  2.2376  qed
  2.2377  
  2.2378 -definition lb_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  2.2379 -  "lb_mult prec x y =
  2.2380 -    (case normfloat (x * y) of Float m e \<Rightarrow>
  2.2381 -      let
  2.2382 -        l = bitlen m - int prec
  2.2383 -      in if l > 0 then Float (m div (2^nat l)) (e + l)
  2.2384 -                  else Float m e)"
  2.2385 +lemma float_divr_nonpos_pos_upper_bound:
  2.2386 +  assumes "real x \<le> 0" and "0 < real y"
  2.2387 +  shows "real (float_divr prec x y) \<le> 0"
  2.2388 +using assms
  2.2389 +unfolding float_divr_def round_up_def
  2.2390 +by (auto simp: field_simps mult_le_0_iff divide_le_0_iff)
  2.2391  
  2.2392 -definition ub_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  2.2393 -  "ub_mult prec x y =
  2.2394 -    (case normfloat (x * y) of Float m e \<Rightarrow>
  2.2395 -      let
  2.2396 -        l = bitlen m - int prec
  2.2397 -      in if l > 0 then Float (m div (2^nat l) + 1) (e + l)
  2.2398 -                  else Float m e)"
  2.2399 +lemma float_divr_nonneg_neg_upper_bound:
  2.2400 +  assumes "0 \<le> real x" and "real y < 0"
  2.2401 +  shows "real (float_divr prec x y) \<le> 0"
  2.2402 +using assms
  2.2403 +unfolding float_divr_def round_up_def
  2.2404 +by (auto simp: field_simps mult_le_0_iff zero_le_mult_iff divide_le_0_iff)
  2.2405 +
  2.2406 +definition "round_prec p f = int p - (bitlen \<bar>mantissa f\<bar> + exponent f)"
  2.2407 +
  2.2408 +definition float_round_down :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.2409 +"float_round_down prec f = float_of (round_down (round_prec prec f) f)"
  2.2410 +
  2.2411 +definition float_round_up :: "nat \<Rightarrow> float \<Rightarrow> float" where
  2.2412 +"float_round_up prec f = float_of (round_up (round_prec prec f) f)"
  2.2413  
  2.2414 -lemma lb_mult: "real (lb_mult prec x y) \<le> real (x * y)"
  2.2415 -proof (cases "normfloat (x * y)")
  2.2416 -  case (Float m e)
  2.2417 -  hence "odd m \<or> (m = 0 \<and> e = 0)" by (rule normfloat_imp_odd_or_zero)
  2.2418 -  let ?l = "bitlen m - int prec"
  2.2419 -  have "real (lb_mult prec x y) \<le> real (normfloat (x * y))"
  2.2420 -  proof (cases "?l > 0")
  2.2421 -    case False thus ?thesis unfolding lb_mult_def Float Let_def float.cases by auto
  2.2422 -  next
  2.2423 -    case True
  2.2424 -    have "real (m div 2^(nat ?l)) * pow2 ?l \<le> real m"
  2.2425 -    proof -
  2.2426 -      have "real (m div 2^(nat ?l)) * pow2 ?l = real (2^(nat ?l) * (m div 2^(nat ?l)))" unfolding real_of_int_mult real_of_int_power real_numeral unfolding pow2_int[symmetric] 
  2.2427 -        using `?l > 0` by auto
  2.2428 -      also have "\<dots> \<le> real (2^(nat ?l) * (m div 2^(nat ?l)) + m mod 2^(nat ?l))" unfolding real_of_int_add by auto
  2.2429 -      also have "\<dots> = real m" unfolding zmod_zdiv_equality[symmetric] ..
  2.2430 -      finally show ?thesis by auto
  2.2431 -    qed
  2.2432 -    thus ?thesis unfolding lb_mult_def Float Let_def float.cases if_P[OF True] real_of_float_simp pow2_add mult_commute mult_assoc by auto
  2.2433 -  qed
  2.2434 -  also have "\<dots> = real (x * y)" unfolding normfloat ..
  2.2435 -  finally show ?thesis .
  2.2436 +lemma compute_float_round_down[code]:
  2.2437 +fixes prec m e
  2.2438 +defines "d == bitlen (abs m) - int prec"
  2.2439 +defines "P == 2^nat d"
  2.2440 +defines "f == Float m e"
  2.2441 +shows "float_round_down prec f = (let d = d in
  2.2442 +    if 0 < d then let P = P ; n = m div P in Float n (e + d)
  2.2443 +             else f)"
  2.2444 +  unfolding float_round_down_def float_down_def[symmetric]
  2.2445 +    compute_float_down f_def Let_def P_def round_prec_def d_def bitlen_Float
  2.2446 +  by (simp add: field_simps)
  2.2447 +  
  2.2448 +lemma compute_float_round_up[code]:
  2.2449 +fixes prec m e
  2.2450 +defines "d == bitlen (abs m) - int prec"
  2.2451 +defines "P == 2^nat d"
  2.2452 +defines "f == Float m e"
  2.2453 +shows "float_round_up prec f = (let d = d in
  2.2454 +  if 0 < d then let P = P ; n = m div P ; r = m mod P in Float (n + (if r = 0 then 0 else 1)) (e + d)
  2.2455 +           else f)"
  2.2456 +  unfolding float_round_up_def float_up_def[symmetric]
  2.2457 +    compute_float_up f_def Let_def P_def round_prec_def d_def bitlen_Float
  2.2458 +  by (simp add: field_simps)
  2.2459 +
  2.2460 +lemma float_round_up: "real x \<le> real (float_round_up prec x)"
  2.2461 +  using round_up
  2.2462 +  by (simp add: float_round_up_def)
  2.2463 +
  2.2464 +lemma float_round_down: "real (float_round_down prec x) \<le> real x"
  2.2465 +  using round_down
  2.2466 +  by (simp add: float_round_down_def)
  2.2467 +
  2.2468 +instantiation float :: lattice_ab_group_add
  2.2469 +begin
  2.2470 +
  2.2471 +definition inf_float::"float\<Rightarrow>float\<Rightarrow>float"
  2.2472 +where "inf_float a b = min a b"
  2.2473 +
  2.2474 +definition sup_float::"float\<Rightarrow>float\<Rightarrow>float"
  2.2475 +where "sup_float a b = max a b"
  2.2476 +
  2.2477 +instance
  2.2478 +proof
  2.2479 +  fix x y :: float show "inf x y \<le> x" unfolding inf_float_def by simp
  2.2480 +  show "inf x y \<le> y" unfolding inf_float_def by simp
  2.2481 +  show "x \<le> sup x y" unfolding sup_float_def by simp
  2.2482 +  show "y \<le> sup x y" unfolding sup_float_def by simp
  2.2483 +  fix z::float
  2.2484 +  assume "x \<le> y" "x \<le> z" thus "x \<le> inf y z" unfolding inf_float_def by simp
  2.2485 +  next fix x y z :: float
  2.2486 +  assume "y \<le> x" "z \<le> x" thus "sup y z \<le> x" unfolding sup_float_def by simp
  2.2487  qed
  2.2488  
  2.2489 -lemma ub_mult: "real (x * y) \<le> real (ub_mult prec x y)"
  2.2490 -proof (cases "normfloat (x * y)")
  2.2491 -  case (Float m e)
  2.2492 -  hence "odd m \<or> (m = 0 \<and> e = 0)" by (rule normfloat_imp_odd_or_zero)
  2.2493 -  let ?l = "bitlen m - int prec"
  2.2494 -  have "real (x * y) = real (normfloat (x * y))" unfolding normfloat ..
  2.2495 -  also have "\<dots> \<le> real (ub_mult prec x y)"
  2.2496 -  proof (cases "?l > 0")
  2.2497 -    case False thus ?thesis unfolding ub_mult_def Float Let_def float.cases by auto
  2.2498 -  next
  2.2499 -    case True
  2.2500 -    have "real m \<le> real (m div 2^(nat ?l) + 1) * pow2 ?l"
  2.2501 -    proof -
  2.2502 -      have "m mod 2^(nat ?l) < 2^(nat ?l)" by (rule pos_mod_bound) auto
  2.2503 -      hence mod_uneq: "real (m mod 2^(nat ?l)) \<le> 1 * 2^(nat ?l)" unfolding mult_1 real_of_int_less_iff[symmetric] by auto
  2.2504 -      
  2.2505 -      have "real m = real (2^(nat ?l) * (m div 2^(nat ?l)) + m mod 2^(nat ?l))" unfolding zmod_zdiv_equality[symmetric] ..
  2.2506 -      also have "\<dots> = real (m div 2^(nat ?l)) * 2^(nat ?l) + real (m mod 2^(nat ?l))" unfolding real_of_int_add by auto
  2.2507 -      also have "\<dots> \<le> (real (m div 2^(nat ?l)) + 1) * 2^(nat ?l)" unfolding left_distrib using mod_uneq by auto
  2.2508 -      finally show ?thesis unfolding pow2_int[symmetric] using True by auto
  2.2509 -    qed
  2.2510 -    thus ?thesis unfolding ub_mult_def Float Let_def float.cases if_P[OF True] real_of_float_simp pow2_add mult_commute mult_assoc by auto
  2.2511 -  qed
  2.2512 -  finally show ?thesis .
  2.2513 -qed
  2.2514 -
  2.2515 -primrec float_abs :: "float \<Rightarrow> float" where
  2.2516 -  "float_abs (Float m e) = Float \<bar>m\<bar> e"
  2.2517 -
  2.2518 -instantiation float :: abs
  2.2519 -begin
  2.2520 -definition abs_float_def: "\<bar>x\<bar> = float_abs x"
  2.2521 -instance ..
  2.2522  end
  2.2523  
  2.2524 -lemma real_of_float_abs: "real \<bar>x :: float\<bar> = \<bar>real x\<bar>" 
  2.2525 -proof (cases x)
  2.2526 -  case (Float m e)
  2.2527 -  have "\<bar>real m\<bar> * pow2 e = \<bar>real m * pow2 e\<bar>" unfolding abs_mult by auto
  2.2528 -  thus ?thesis unfolding Float abs_float_def float_abs.simps real_of_float_simp by auto
  2.2529 -qed
  2.2530 +lemma Float_le_zero_iff: "Float a b \<le> 0 \<longleftrightarrow> a \<le> 0"
  2.2531 + apply (auto simp: zero_float_def mult_le_0_iff)
  2.2532 + using powr_gt_zero[of 2 b] by simp
  2.2533 +
  2.2534 +(* TODO: how to use as code equation? -> pprt_float?! *)
  2.2535 +lemma compute_pprt[code]: "pprt (Float a e) = (if a <= 0 then 0 else (Float a e))"
  2.2536 +unfolding pprt_def sup_float_def max_def Float_le_zero_iff ..
  2.2537  
  2.2538 -primrec floor_fl :: "float \<Rightarrow> float" where
  2.2539 -  "floor_fl (Float m e) = (if 0 \<le> e then Float m e
  2.2540 -                                  else Float (m div (2 ^ (nat (-e)))) 0)"
  2.2541 +(* TODO: how to use as code equation? *)
  2.2542 +lemma compute_nprt[code]: "nprt (Float a e) = (if a <= 0 then (Float a e) else 0)"
  2.2543 +unfolding nprt_def inf_float_def min_def Float_le_zero_iff ..
  2.2544 +
  2.2545 +lemma of_float_pprt[simp]: fixes a::float shows "real (pprt a) = pprt (real a)"
  2.2546 +  unfolding pprt_def sup_float_def max_def sup_real_def by (auto simp: less_eq_float_def)
  2.2547 +
  2.2548 +lemma of_float_nprt[simp]: fixes a::float shows "real (nprt a) = nprt (real a)"
  2.2549 +  unfolding nprt_def inf_float_def min_def inf_real_def by (auto simp: less_eq_float_def)
  2.2550 +
  2.2551 +definition int_floor_fl :: "float \<Rightarrow> int" where
  2.2552 +  "int_floor_fl f = floor f"
  2.2553  
  2.2554 -lemma floor_fl: "real (floor_fl x) \<le> real x"
  2.2555 -proof (cases x)
  2.2556 -  case (Float m e)
  2.2557 -  show ?thesis
  2.2558 -  proof (cases "0 \<le> e")
  2.2559 -    case False
  2.2560 -    hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
  2.2561 -    have "real (Float (m div (2 ^ (nat (-e)))) 0) = real (m div 2 ^ (nat (-e)))" unfolding real_of_float_simp by auto
  2.2562 -    also have "\<dots> \<le> real m / real ((2::int) ^ (nat (-e)))" using real_of_int_div4 .
  2.2563 -    also have "\<dots> = real m * inverse (2 ^ (nat (-e)))" unfolding real_of_int_power real_numeral divide_inverse ..
  2.2564 -    also have "\<dots> = real (Float m e)" unfolding real_of_float_simp me_eq pow2_int pow2_neg[of e] ..
  2.2565 -    finally show ?thesis unfolding Float floor_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
  2.2566 -  next
  2.2567 -    case True thus ?thesis unfolding Float by auto
  2.2568 -  qed
  2.2569 -qed
  2.2570 +lemma compute_int_floor_fl[code]:
  2.2571 +  shows "int_floor_fl (Float m e) = (if 0 \<le> e then m * 2 ^ nat e
  2.2572 +                                  else m div (2 ^ (nat (-e))))"
  2.2573 +  by (simp add: int_floor_fl_def powr_int int_of_reals floor_divide_eq_div del: real_of_ints)
  2.2574 +
  2.2575 +definition floor_fl :: "float \<Rightarrow> float" where
  2.2576 +  "floor_fl f = float_of (floor f)"
  2.2577 +
  2.2578 +lemma compute_floor_fl[code]:
  2.2579 +  shows "floor_fl (Float m e) = (if 0 \<le> e then Float m e
  2.2580 +                                  else Float (m div (2 ^ (nat (-e)))) 0)"
  2.2581 +  by (simp add: floor_fl_def powr_int int_of_reals floor_divide_eq_div del: real_of_ints)
  2.2582  
  2.2583 -lemma floor_pos_exp: assumes floor: "Float m e = floor_fl x" shows "0 \<le> e"
  2.2584 -proof (cases x)
  2.2585 -  case (Float mx me)
  2.2586 -  from floor[unfolded Float floor_fl.simps] show ?thesis by (cases "0 \<le> me", auto)
  2.2587 -qed
  2.2588 +lemma floor_fl: "real (floor_fl x) \<le> real x" by (simp add: floor_fl_def)
  2.2589 +lemma int_floor_fl: "real (int_floor_fl x) \<le> real x" by (simp add: int_floor_fl_def)
  2.2590  
  2.2591 -declare floor_fl.simps[simp del]
  2.2592 +lemma floor_pos_exp: "exponent (floor_fl x) \<ge> 0"
  2.2593 +proof cases
  2.2594 +  assume nzero: "floor_fl x \<noteq> float_of 0"
  2.2595 +  have "floor_fl x \<equiv> Float \<lfloor>real x\<rfloor> 0" by (simp add: floor_fl_def)
  2.2596 +  from denormalize_shift[OF this nzero] guess i . note i = this
  2.2597 +  thus ?thesis by simp
  2.2598 +qed (simp add: floor_fl_def)
  2.2599  
  2.2600 -primrec ceiling_fl :: "float \<Rightarrow> float" where
  2.2601 +(* TODO: not used in approximation
  2.2602 +definition ceiling_fl :: "float_of \<Rightarrow> float" where
  2.2603 +  "ceiling_fl f = float_of (ceiling f)"
  2.2604 +
  2.2605 +lemma compute_ceiling_fl:
  2.2606    "ceiling_fl (Float m e) = (if 0 \<le> e then Float m e
  2.2607                                      else Float (m div (2 ^ (nat (-e))) + 1) 0)"
  2.2608  
  2.2609  lemma ceiling_fl: "real x \<le> real (ceiling_fl x)"
  2.2610 -proof (cases x)
  2.2611 -  case (Float m e)
  2.2612 -  show ?thesis
  2.2613 -  proof (cases "0 \<le> e")
  2.2614 -    case False
  2.2615 -    hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
  2.2616 -    have "real (Float m e) = real m * inverse (2 ^ (nat (-e)))" unfolding real_of_float_simp me_eq pow2_int pow2_neg[of e] ..
  2.2617 -    also have "\<dots> = real m / real ((2::int) ^ (nat (-e)))" unfolding real_of_int_power real_numeral divide_inverse ..
  2.2618 -    also have "\<dots> \<le> 1 + real (m div 2 ^ (nat (-e)))" using real_of_int_div3[unfolded diff_le_eq] .
  2.2619 -    also have "\<dots> = real (Float (m div (2 ^ (nat (-e))) + 1) 0)" unfolding real_of_float_simp by auto
  2.2620 -    finally show ?thesis unfolding Float ceiling_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
  2.2621 -  next
  2.2622 -    case True thus ?thesis unfolding Float by auto
  2.2623 -  qed
  2.2624 -qed
  2.2625  
  2.2626 -declare ceiling_fl.simps[simp del]
  2.2627 +definition lb_mod :: "nat \<Rightarrow> float_of \<Rightarrow> float_of \<Rightarrow> float_of \<Rightarrow> float" where
  2.2628 +"lb_mod prec x ub lb = x - ceiling_fl (float_divr prec x lb) * ub"
  2.2629  
  2.2630 -definition lb_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  2.2631 -  "lb_mod prec x ub lb = x - ceiling_fl (float_divr prec x lb) * ub"
  2.2632 -
  2.2633 -definition ub_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  2.2634 -  "ub_mod prec x ub lb = x - floor_fl (float_divl prec x ub) * lb"
  2.2635 +definition ub_mod :: "nat \<Rightarrow> float_of \<Rightarrow> float_of \<Rightarrow> float_of \<Rightarrow> float" where
  2.2636 +"ub_mod prec x ub lb = x - floor_fl (float_divl prec x ub) * lb"
  2.2637  
  2.2638  lemma lb_mod: fixes k :: int assumes "0 \<le> real x" and "real k * y \<le> real x" (is "?k * y \<le> ?x")
  2.2639    assumes "0 < real lb" "real lb \<le> y" (is "?lb \<le> y") "y \<le> real ub" (is "y \<le> ?ub")
  2.2640    shows "real (lb_mod prec x ub lb) \<le> ?x - ?k * y"
  2.2641 -proof -
  2.2642 -  have "?lb \<le> ?ub" using assms by auto
  2.2643 -  have "0 \<le> ?lb" and "?lb \<noteq> 0" using assms by auto
  2.2644 -  have "?k * y \<le> ?x" using assms by auto
  2.2645 -  also have "\<dots> \<le> ?x / ?lb * ?ub" by (metis mult_left_mono[OF `?lb \<le> ?ub` `0 \<le> ?x`] divide_right_mono[OF _ `0 \<le> ?lb` ] times_divide_eq_left nonzero_mult_divide_cancel_right[OF `?lb \<noteq> 0`])
  2.2646 -  also have "\<dots> \<le> real (ceiling_fl (float_divr prec x lb)) * ?ub" by (metis mult_right_mono order_trans `0 \<le> ?lb` `?lb \<le> ?ub` float_divr ceiling_fl)
  2.2647 -  finally show ?thesis unfolding lb_mod_def real_of_float_sub real_of_float_mult by auto
  2.2648 -qed
  2.2649  
  2.2650 -lemma ub_mod:
  2.2651 -  fixes k :: int and x :: float
  2.2652 -  assumes "0 \<le> real x" and "real x \<le> real k * y" (is "?x \<le> ?k * y")
  2.2653 +lemma ub_mod: fixes k :: int and x :: float_of assumes "0 \<le> real x" and "real x \<le> real k * y" (is "?x \<le> ?k * y")
  2.2654    assumes "0 < real lb" "real lb \<le> y" (is "?lb \<le> y") "y \<le> real ub" (is "y \<le> ?ub")
  2.2655    shows "?x - ?k * y \<le> real (ub_mod prec x ub lb)"
  2.2656 -proof -
  2.2657 -  have "?lb \<le> ?ub" using assms by auto
  2.2658 -  hence "0 \<le> ?lb" and "0 \<le> ?ub" and "?ub \<noteq> 0" using assms by auto
  2.2659 -  have "real (floor_fl (float_divl prec x ub)) * ?lb \<le> ?x / ?ub * ?lb" by (metis mult_right_mono order_trans `0 \<le> ?lb` `?lb \<le> ?ub` float_divl floor_fl)
  2.2660 -  also have "\<dots> \<le> ?x" by (metis mult_left_mono[OF `?lb \<le> ?ub` `0 \<le> ?x`] divide_right_mono[OF _ `0 \<le> ?ub` ] times_divide_eq_left nonzero_mult_divide_cancel_right[OF `?ub \<noteq> 0`])
  2.2661 -  also have "\<dots> \<le> ?k * y" using assms by auto
  2.2662 -  finally show ?thesis unfolding ub_mod_def real_of_float_sub real_of_float_mult by auto
  2.2663 -qed
  2.2664  
  2.2665 -lemma le_float_def'[code]: "f \<le> g = (case f - g of Float a b \<Rightarrow> a \<le> 0)"
  2.2666 -proof -
  2.2667 -  have le_transfer: "(f \<le> g) = (real (f - g) \<le> 0)" by (auto simp add: le_float_def)
  2.2668 -  from float_split[of "f - g"] obtain a b where f_diff_g: "f - g = Float a b" by auto
  2.2669 -  with le_transfer have le_transfer': "f \<le> g = (real (Float a b) \<le> 0)" by simp
  2.2670 -  show ?thesis by (simp add: le_transfer' f_diff_g float_le_zero)
  2.2671 -qed
  2.2672 -
  2.2673 -lemma less_float_def'[code]: "f < g = (case f - g of Float a b \<Rightarrow> a < 0)"
  2.2674 -proof -
  2.2675 -  have less_transfer: "(f < g) = (real (f - g) < 0)" by (auto simp add: less_float_def)
  2.2676 -  from float_split[of "f - g"] obtain a b where f_diff_g: "f - g = Float a b" by auto
  2.2677 -  with less_transfer have less_transfer': "f < g = (real (Float a b) < 0)" by simp
  2.2678 -  show ?thesis by (simp add: less_transfer' f_diff_g float_less_zero)
  2.2679 -qed
  2.2680 +*)
  2.2681  
  2.2682  end
  2.2683 +