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