src/HOL/Library/Float.thy
author wenzelm
Thu Feb 16 22:53:24 2012 +0100 (2012-02-16)
changeset 46507 1b24c24017dd
parent 46028 9f113cdf3d66
child 46573 8c4c5c8dcf7a
permissions -rw-r--r--
tuned proofs;
     1 (*  Title:      HOL/Library/Float.thy
     2     Author:     Steven Obua 2008
     3     Author:     Johannes Hoelzl, TU Muenchen <hoelzl@in.tum.de> 2008 / 2009
     4 *)
     5 
     6 header {* Floating-Point Numbers *}
     7 
     8 theory Float
     9 imports Complex_Main Lattice_Algebras
    10 begin
    11 
    12 definition
    13   pow2 :: "int \<Rightarrow> real" where
    14   [simp]: "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
    15 
    16 datatype float = Float int int
    17 
    18 primrec of_float :: "float \<Rightarrow> real" where
    19   "of_float (Float a b) = real a * pow2 b"
    20 
    21 defs (overloaded)
    22   real_of_float_def [code_unfold]: "real == of_float"
    23 
    24 declare [[coercion "% x . Float x 0"]]
    25 declare [[coercion "real::float\<Rightarrow>real"]]
    26 
    27 primrec mantissa :: "float \<Rightarrow> int" where
    28   "mantissa (Float a b) = a"
    29 
    30 primrec scale :: "float \<Rightarrow> int" where
    31   "scale (Float a b) = b"
    32 
    33 instantiation float :: zero begin
    34 definition zero_float where "0 = Float 0 0"
    35 instance ..
    36 end
    37 
    38 instantiation float :: one begin
    39 definition one_float where "1 = Float 1 0"
    40 instance ..
    41 end
    42 
    43 instantiation float :: number begin
    44 definition number_of_float where "number_of n = Float n 0"
    45 instance ..
    46 end
    47 
    48 lemma number_of_float_Float:
    49   "number_of k = Float (number_of k) 0"
    50   by (simp add: number_of_float_def number_of_is_id)
    51 
    52 declare number_of_float_Float [symmetric, code_abbrev]
    53 
    54 lemma real_of_float_simp[simp]: "real (Float a b) = real a * pow2 b"
    55   unfolding real_of_float_def using of_float.simps .
    56 
    57 lemma real_of_float_neg_exp: "e < 0 \<Longrightarrow> real (Float m e) = real m * inverse (2^nat (-e))" by auto
    58 lemma real_of_float_nge0_exp: "\<not> 0 \<le> e \<Longrightarrow> real (Float m e) = real m * inverse (2^nat (-e))" by auto
    59 lemma real_of_float_ge0_exp: "0 \<le> e \<Longrightarrow> real (Float m e) = real m * (2^nat e)" by auto
    60 
    61 lemma Float_num[simp]: shows
    62    "real (Float 1 0) = 1" and "real (Float 1 1) = 2" and "real (Float 1 2) = 4" and
    63    "real (Float 1 -1) = 1/2" and "real (Float 1 -2) = 1/4" and "real (Float 1 -3) = 1/8" and
    64    "real (Float -1 0) = -1" and "real (Float (number_of n) 0) = number_of n"
    65   by auto
    66 
    67 lemma float_number_of[simp]: "real (number_of x :: float) = number_of x"
    68   by (simp only:number_of_float_def Float_num[unfolded number_of_is_id])
    69 
    70 lemma float_number_of_int[simp]: "real (Float n 0) = real n"
    71   by simp
    72 
    73 lemma pow2_0[simp]: "pow2 0 = 1" by simp
    74 lemma pow2_1[simp]: "pow2 1 = 2" by simp
    75 lemma pow2_neg: "pow2 x = inverse (pow2 (-x))" by simp
    76 
    77 lemma pow2_powr: "pow2 a = 2 powr a"
    78   by (simp add: powr_realpow[symmetric] powr_minus)
    79 
    80 declare pow2_def[simp del]
    81 
    82 lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
    83   by (simp add: pow2_powr powr_add)
    84 
    85 lemma pow2_diff: "pow2 (a - b) = pow2 a / pow2 b"
    86   by (simp add: pow2_powr powr_divide2)
    87   
    88 lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
    89   by (simp add: pow2_add)
    90 
    91 lemma float_components[simp]: "Float (mantissa f) (scale f) = f" by (cases f) auto
    92 
    93 lemma float_split: "\<exists> a b. x = Float a b" by (cases x) auto
    94 
    95 lemma float_split2: "(\<forall> a b. x \<noteq> Float a b) = False" by (auto simp add: float_split)
    96 
    97 lemma float_zero[simp]: "real (Float 0 e) = 0" by simp
    98 
    99 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
   100 by arith
   101 
   102 function normfloat :: "float \<Rightarrow> float" where
   103   "normfloat (Float a b) =
   104     (if a \<noteq> 0 \<and> even a then normfloat (Float (a div 2) (b+1))
   105      else if a=0 then Float 0 0 else Float a b)"
   106 by pat_completeness auto
   107 termination by (relation "measure (nat o abs o mantissa)") (auto intro: abs_div_2_less)
   108 declare normfloat.simps[simp del]
   109 
   110 theorem normfloat[symmetric, simp]: "real f = real (normfloat f)"
   111 proof (induct f rule: normfloat.induct)
   112   case (1 a b)
   113   have real2: "2 = real (2::int)"
   114     by auto
   115   show ?case
   116     apply (subst normfloat.simps)
   117     apply auto
   118     apply (subst 1[symmetric])
   119     apply (auto simp add: pow2_add even_def)
   120     done
   121 qed
   122 
   123 lemma pow2_neq_zero[simp]: "pow2 x \<noteq> 0"
   124   by (auto simp add: pow2_def)
   125 
   126 lemma pow2_int: "pow2 (int c) = 2^c"
   127 by (simp add: pow2_def)
   128 
   129 lemma zero_less_pow2[simp]:
   130   "0 < pow2 x"
   131   by (simp add: pow2_powr)
   132 
   133 lemma normfloat_imp_odd_or_zero: "normfloat f = Float a b \<Longrightarrow> odd a \<or> (a = 0 \<and> b = 0)"
   134 proof (induct f rule: normfloat.induct)
   135   case (1 u v)
   136   from 1 have ab: "normfloat (Float u v) = Float a b" by auto
   137   {
   138     assume eu: "even u"
   139     assume z: "u \<noteq> 0"
   140     have "normfloat (Float u v) = normfloat (Float (u div 2) (v + 1))"
   141       apply (subst normfloat.simps)
   142       by (simp add: eu z)
   143     with ab have "normfloat (Float (u div 2) (v + 1)) = Float a b" by simp
   144     with 1 eu z have ?case by auto
   145   }
   146   note case1 = this
   147   {
   148     assume "odd u \<or> u = 0"
   149     then have ou: "\<not> (u \<noteq> 0 \<and> even u)" by auto
   150     have "normfloat (Float u v) = (if u = 0 then Float 0 0 else Float u v)"
   151       apply (subst normfloat.simps)
   152       apply (simp add: ou)
   153       done
   154     with ab have "Float a b = (if u = 0 then Float 0 0 else Float u v)" by auto
   155     then have ?case
   156       apply (case_tac "u=0")
   157       apply (auto)
   158       by (insert ou, auto)
   159   }
   160   note case2 = this
   161   show ?case
   162     apply (case_tac "odd u \<or> u = 0")
   163     apply (rule case2)
   164     apply simp
   165     apply (rule case1)
   166     apply auto
   167     done
   168 qed
   169 
   170 lemma float_eq_odd_helper: 
   171   assumes odd: "odd a'"
   172   and floateq: "real (Float a b) = real (Float a' b')"
   173   shows "b \<le> b'"
   174 proof - 
   175   from odd have "a' \<noteq> 0" by auto
   176   with floateq have a': "real a' = real a * pow2 (b - b')"
   177     by (simp add: pow2_diff field_simps)
   178 
   179   {
   180     assume bcmp: "b > b'"
   181     then have "\<exists>c::nat. b - b' = int c + 1"
   182       by arith
   183     then guess c ..
   184     with a' have "real a' = real (a * 2^c * 2)"
   185       by (simp add: pow2_def nat_add_distrib)
   186     with odd have False
   187       unfolding real_of_int_inject by simp
   188   }
   189   then show ?thesis by arith
   190 qed
   191 
   192 lemma float_eq_odd: 
   193   assumes odd1: "odd a"
   194   and odd2: "odd a'"
   195   and floateq: "real (Float a b) = real (Float a' b')"
   196   shows "a = a' \<and> b = b'"
   197 proof -
   198   from 
   199      float_eq_odd_helper[OF odd2 floateq] 
   200      float_eq_odd_helper[OF odd1 floateq[symmetric]]
   201   have beq: "b = b'" by arith
   202   with floateq show ?thesis by auto
   203 qed
   204 
   205 theorem normfloat_unique:
   206   assumes real_of_float_eq: "real f = real g"
   207   shows "normfloat f = normfloat g"
   208 proof - 
   209   from float_split[of "normfloat f"] obtain a b where normf:"normfloat f = Float a b" by auto
   210   from float_split[of "normfloat g"] obtain a' b' where normg:"normfloat g = Float a' b'" by auto
   211   have "real (normfloat f) = real (normfloat g)"
   212     by (simp add: real_of_float_eq)
   213   then have float_eq: "real (Float a b) = real (Float a' b')"
   214     by (simp add: normf normg)
   215   have ab: "odd a \<or> (a = 0 \<and> b = 0)" by (rule normfloat_imp_odd_or_zero[OF normf])
   216   have ab': "odd a' \<or> (a' = 0 \<and> b' = 0)" by (rule normfloat_imp_odd_or_zero[OF normg])
   217   {
   218     assume odd: "odd a"
   219     then have "a \<noteq> 0" by (simp add: even_def, arith)
   220     with float_eq have "a' \<noteq> 0" by auto
   221     with ab' have "odd a'" by simp
   222     from odd this float_eq have "a = a' \<and> b = b'" by (rule float_eq_odd)
   223   }
   224   note odd_case = this
   225   {
   226     assume even: "even a"
   227     with ab have a0: "a = 0" by simp
   228     with float_eq have a0': "a' = 0" by auto 
   229     from a0 a0' ab ab' have "a = a' \<and> b = b'" by auto
   230   }
   231   note even_case = this
   232   from odd_case even_case show ?thesis
   233     apply (simp add: normf normg)
   234     apply (case_tac "even a")
   235     apply auto
   236     done
   237 qed
   238 
   239 instantiation float :: plus begin
   240 fun plus_float where
   241 [simp del]: "(Float a_m a_e) + (Float b_m b_e) = 
   242      (if a_e \<le> b_e then Float (a_m + b_m * 2^(nat(b_e - a_e))) a_e 
   243                    else Float (a_m * 2^(nat (a_e - b_e)) + b_m) b_e)"
   244 instance ..
   245 end
   246 
   247 instantiation float :: uminus begin
   248 primrec uminus_float where [simp del]: "uminus_float (Float m e) = Float (-m) e"
   249 instance ..
   250 end
   251 
   252 instantiation float :: minus begin
   253 definition minus_float where [simp del]: "(z::float) - w = z + (- w)"
   254 instance ..
   255 end
   256 
   257 instantiation float :: times begin
   258 fun times_float where [simp del]: "(Float a_m a_e) * (Float b_m b_e) = Float (a_m * b_m) (a_e + b_e)"
   259 instance ..
   260 end
   261 
   262 primrec float_pprt :: "float \<Rightarrow> float" where
   263   "float_pprt (Float a e) = (if 0 <= a then (Float a e) else 0)"
   264 
   265 primrec float_nprt :: "float \<Rightarrow> float" where
   266   "float_nprt (Float a e) = (if 0 <= a then 0 else (Float a e))" 
   267 
   268 instantiation float :: ord begin
   269 definition le_float_def: "z \<le> (w :: float) \<equiv> real z \<le> real w"
   270 definition less_float_def: "z < (w :: float) \<equiv> real z < real w"
   271 instance ..
   272 end
   273 
   274 lemma real_of_float_add[simp]: "real (a + b) = real a + real (b :: float)"
   275   by (cases a, cases b) (simp add: algebra_simps plus_float.simps, 
   276       auto simp add: pow2_int[symmetric] pow2_add[symmetric])
   277 
   278 lemma real_of_float_minus[simp]: "real (- a) = - real (a :: float)"
   279   by (cases a) (simp add: uminus_float.simps)
   280 
   281 lemma real_of_float_sub[simp]: "real (a - b) = real a - real (b :: float)"
   282   by (cases a, cases b) (simp add: minus_float_def)
   283 
   284 lemma real_of_float_mult[simp]: "real (a*b) = real a * real (b :: float)"
   285   by (cases a, cases b) (simp add: times_float.simps pow2_add)
   286 
   287 lemma real_of_float_0[simp]: "real (0 :: float) = 0"
   288   by (auto simp add: zero_float_def float_zero)
   289 
   290 lemma real_of_float_1[simp]: "real (1 :: float) = 1"
   291   by (auto simp add: one_float_def)
   292 
   293 lemma zero_le_float:
   294   "(0 <= real (Float a b)) = (0 <= a)"
   295   apply auto
   296   apply (auto simp add: zero_le_mult_iff)
   297   apply (insert zero_less_pow2[of b])
   298   apply (simp_all)
   299   done
   300 
   301 lemma float_le_zero:
   302   "(real (Float a b) <= 0) = (a <= 0)"
   303   apply auto
   304   apply (auto simp add: mult_le_0_iff)
   305   apply (insert zero_less_pow2[of b])
   306   apply auto
   307   done
   308 
   309 lemma zero_less_float:
   310   "(0 < real (Float a b)) = (0 < a)"
   311   apply auto
   312   apply (auto simp add: zero_less_mult_iff)
   313   apply (insert zero_less_pow2[of b])
   314   apply (simp_all)
   315   done
   316 
   317 lemma float_less_zero:
   318   "(real (Float a b) < 0) = (a < 0)"
   319   apply auto
   320   apply (auto simp add: mult_less_0_iff)
   321   apply (insert zero_less_pow2[of b])
   322   apply (simp_all)
   323   done
   324 
   325 declare real_of_float_simp[simp del]
   326 
   327 lemma real_of_float_pprt[simp]: "real (float_pprt a) = pprt (real a)"
   328   by (cases a) (auto simp add: zero_le_float float_le_zero)
   329 
   330 lemma real_of_float_nprt[simp]: "real (float_nprt a) = nprt (real a)"
   331   by (cases a) (auto simp add: zero_le_float float_le_zero)
   332 
   333 instance float :: ab_semigroup_add
   334 proof (intro_classes)
   335   fix a b c :: float
   336   show "a + b + c = a + (b + c)"
   337     by (cases a, cases b, cases c)
   338       (auto simp add: algebra_simps power_add[symmetric] plus_float.simps)
   339 next
   340   fix a b :: float
   341   show "a + b = b + a"
   342     by (cases a, cases b) (simp add: plus_float.simps)
   343 qed
   344 
   345 instance float :: comm_monoid_mult
   346 proof (intro_classes)
   347   fix a b c :: float
   348   show "a * b * c = a * (b * c)"
   349     by (cases a, cases b, cases c) (simp add: times_float.simps)
   350 next
   351   fix a b :: float
   352   show "a * b = b * a"
   353     by (cases a, cases b) (simp add: times_float.simps)
   354 next
   355   fix a :: float
   356   show "1 * a = a"
   357     by (cases a) (simp add: times_float.simps one_float_def)
   358 qed
   359 
   360 (* Floats do NOT form a cancel_semigroup_add: *)
   361 lemma "0 + Float 0 1 = 0 + Float 0 2"
   362   by (simp add: plus_float.simps zero_float_def)
   363 
   364 instance float :: comm_semiring
   365 proof (intro_classes)
   366   fix a b c :: float
   367   show "(a + b) * c = a * c + b * c"
   368     by (cases a, cases b, cases c) (simp add: plus_float.simps times_float.simps algebra_simps)
   369 qed
   370 
   371 (* Floats do NOT form an order, because "(x < y) = (x <= y & x <> y)" does NOT hold *)
   372 
   373 instance float :: zero_neq_one
   374 proof (intro_classes)
   375   show "(0::float) \<noteq> 1"
   376     by (simp add: zero_float_def one_float_def)
   377 qed
   378 
   379 lemma float_le_simp: "((x::float) \<le> y) = (0 \<le> y - x)"
   380   by (auto simp add: le_float_def)
   381 
   382 lemma float_less_simp: "((x::float) < y) = (0 < y - x)"
   383   by (auto simp add: less_float_def)
   384 
   385 lemma real_of_float_min: "real (min x y :: float) = min (real x) (real y)" unfolding min_def le_float_def by auto
   386 lemma real_of_float_max: "real (max a b :: float) = max (real a) (real b)" unfolding max_def le_float_def by auto
   387 
   388 lemma float_power: "real (x ^ n :: float) = real x ^ n"
   389   by (induct n) simp_all
   390 
   391 lemma zero_le_pow2[simp]: "0 \<le> pow2 s"
   392   apply (subgoal_tac "0 < pow2 s")
   393   apply (auto simp only:)
   394   apply auto
   395   done
   396 
   397 lemma pow2_less_0_eq_False[simp]: "(pow2 s < 0) = False"
   398   apply auto
   399   apply (subgoal_tac "0 \<le> pow2 s")
   400   apply simp
   401   apply simp
   402   done
   403 
   404 lemma pow2_le_0_eq_False[simp]: "(pow2 s \<le> 0) = False"
   405   apply auto
   406   apply (subgoal_tac "0 < pow2 s")
   407   apply simp
   408   apply simp
   409   done
   410 
   411 lemma float_pos_m_pos: "0 < Float m e \<Longrightarrow> 0 < m"
   412   unfolding less_float_def real_of_float_simp real_of_float_0 zero_less_mult_iff
   413   by auto
   414 
   415 lemma float_pos_less1_e_neg: assumes "0 < Float m e" and "Float m e < 1" shows "e < 0"
   416 proof -
   417   have "0 < m" using float_pos_m_pos `0 < Float m e` by auto
   418   hence "0 \<le> real m" and "1 \<le> real m" by auto
   419   
   420   show "e < 0"
   421   proof (rule ccontr)
   422     assume "\<not> e < 0" hence "0 \<le> e" by auto
   423     hence "1 \<le> pow2 e" unfolding pow2_def by auto
   424     from mult_mono[OF `1 \<le> real m` this `0 \<le> real m`]
   425     have "1 \<le> Float m e" by (simp add: le_float_def real_of_float_simp)
   426     thus False using `Float m e < 1` unfolding less_float_def le_float_def by auto
   427   qed
   428 qed
   429 
   430 lemma float_less1_mantissa_bound: assumes "0 < Float m e" "Float m e < 1" shows "m < 2^(nat (-e))"
   431 proof -
   432   have "e < 0" using float_pos_less1_e_neg assms by auto
   433   have "\<And>x. (0::real) < 2^x" by auto
   434   have "real m < 2^(nat (-e))" using `Float m e < 1`
   435     unfolding less_float_def real_of_float_neg_exp[OF `e < 0`] real_of_float_1
   436           real_mult_less_iff1[of _ _ 1, OF `0 < 2^(nat (-e))`, symmetric] 
   437           mult_assoc by auto
   438   thus ?thesis unfolding real_of_int_less_iff[symmetric] by auto
   439 qed
   440 
   441 function bitlen :: "int \<Rightarrow> int" where
   442 "bitlen 0 = 0" | 
   443 "bitlen -1 = 1" | 
   444 "0 < x \<Longrightarrow> bitlen x = 1 + (bitlen (x div 2))" | 
   445 "x < -1 \<Longrightarrow> bitlen x = 1 + (bitlen (x div 2))"
   446   apply (case_tac "x = 0 \<or> x = -1 \<or> x < -1 \<or> x > 0")
   447   apply auto
   448   done
   449 termination by (relation "measure (nat o abs)", auto)
   450 
   451 lemma bitlen_ge0: "0 \<le> bitlen x" by (induct x rule: bitlen.induct, auto)
   452 lemma bitlen_ge1: "x \<noteq> 0 \<Longrightarrow> 1 \<le> bitlen x" by (induct x rule: bitlen.induct, auto simp add: bitlen_ge0)
   453 
   454 lemma bitlen_bounds': assumes "0 < x" shows "2^nat (bitlen x - 1) \<le> x \<and> x + 1 \<le> 2^nat (bitlen x)" (is "?P x")
   455   using `0 < x`
   456 proof (induct x rule: bitlen.induct)
   457   fix x
   458   assume "0 < x" and hyp: "0 < x div 2 \<Longrightarrow> ?P (x div 2)" hence "0 \<le> x" and "x \<noteq> 0" by auto
   459   { fix x have "0 \<le> 1 + bitlen x" using bitlen_ge0[of x] by auto } note gt0_pls1 = this
   460 
   461   have "0 < (2::int)" by auto
   462 
   463   show "?P x"
   464   proof (cases "x = 1")
   465     case True show "?P x" unfolding True by auto
   466   next
   467     case False hence "2 \<le> x" using `0 < x` `x \<noteq> 1` by auto
   468     hence "2 div 2 \<le> x div 2" by (rule zdiv_mono1, auto)
   469     hence "0 < x div 2" and "x div 2 \<noteq> 0" by auto
   470     hence bitlen_s1_ge0: "0 \<le> bitlen (x div 2) - 1" using bitlen_ge1[OF `x div 2 \<noteq> 0`] by auto
   471 
   472     { from hyp[OF `0 < x div 2`]
   473       have "2 ^ nat (bitlen (x div 2) - 1) \<le> x div 2" by auto
   474       hence "2 ^ nat (bitlen (x div 2) - 1) * 2 \<le> x div 2 * 2" by (rule mult_right_mono, auto)
   475       also have "\<dots> \<le> x" using `0 < x` by auto
   476       finally have "2^nat (1 + bitlen (x div 2) - 1) \<le> x" unfolding power_Suc2[symmetric] Suc_nat_eq_nat_zadd1[OF bitlen_s1_ge0] by auto
   477     } moreover
   478     { have "x + 1 \<le> x - x mod 2 + 2"
   479       proof -
   480         have "x mod 2 < 2" using `0 < x` by auto
   481         hence "x < x - x mod 2 +  2" unfolding algebra_simps by auto
   482         thus ?thesis by auto
   483       qed
   484       also have "x - x mod 2 + 2 = (x div 2 + 1) * 2" unfolding algebra_simps using `0 < x` zdiv_zmod_equality2[of x 2 0] by auto
   485       also have "\<dots> \<le> 2^nat (bitlen (x div 2)) * 2" using hyp[OF `0 < x div 2`, THEN conjunct2] by (rule mult_right_mono, auto)
   486       also have "\<dots> = 2^(1 + nat (bitlen (x div 2)))" unfolding power_Suc2[symmetric] by auto
   487       finally have "x + 1 \<le> 2^(1 + nat (bitlen (x div 2)))" .
   488     }
   489     ultimately show ?thesis
   490       unfolding bitlen.simps(3)[OF `0 < x`] nat_add_distrib[OF zero_le_one bitlen_ge0]
   491       unfolding add_commute nat_add_distrib[OF zero_le_one gt0_pls1]
   492       by auto
   493   qed
   494 next
   495   fix x :: int assume "x < -1" and "0 < x" hence False by auto
   496   thus "?P x" by auto
   497 qed auto
   498 
   499 lemma bitlen_bounds: assumes "0 < x" shows "2^nat (bitlen x - 1) \<le> x \<and> x < 2^nat (bitlen x)"
   500   using bitlen_bounds'[OF `0<x`] by auto
   501 
   502 lemma bitlen_div: assumes "0 < m" shows "1 \<le> real m / 2^nat (bitlen m - 1)" and "real m / 2^nat (bitlen m - 1) < 2"
   503 proof -
   504   let ?B = "2^nat(bitlen m - 1)"
   505 
   506   have "?B \<le> m" using bitlen_bounds[OF `0 <m`] ..
   507   hence "1 * ?B \<le> real m" unfolding real_of_int_le_iff[symmetric] by auto
   508   thus "1 \<le> real m / ?B" by auto
   509 
   510   have "m \<noteq> 0" using assms by auto
   511   have "0 \<le> bitlen m - 1" using bitlen_ge1[OF `m \<noteq> 0`] by auto
   512 
   513   have "m < 2^nat(bitlen m)" using bitlen_bounds[OF `0 <m`] ..
   514   also have "\<dots> = 2^nat(bitlen m - 1 + 1)" using bitlen_ge1[OF `m \<noteq> 0`] by auto
   515   also have "\<dots> = ?B * 2" unfolding nat_add_distrib[OF `0 \<le> bitlen m - 1` zero_le_one] by auto
   516   finally have "real m < 2 * ?B" unfolding real_of_int_less_iff[symmetric] by auto
   517   hence "real m / ?B < 2 * ?B / ?B" by (rule divide_strict_right_mono, auto)
   518   thus "real m / ?B < 2" by auto
   519 qed
   520 
   521 lemma float_gt1_scale: assumes "1 \<le> Float m e"
   522   shows "0 \<le> e + (bitlen m - 1)"
   523 proof (cases "0 \<le> e")
   524   have "0 < Float m e" using assms unfolding less_float_def le_float_def by auto
   525   hence "0 < m" using float_pos_m_pos by auto
   526   hence "m \<noteq> 0" by auto
   527   case True with bitlen_ge1[OF `m \<noteq> 0`] show ?thesis by auto
   528 next
   529   have "0 < Float m e" using assms unfolding less_float_def le_float_def by auto
   530   hence "0 < m" using float_pos_m_pos by auto
   531   hence "m \<noteq> 0" and "1 < (2::int)" by auto
   532   case False let ?S = "2^(nat (-e))"
   533   have "1 \<le> real m * inverse ?S" using assms unfolding le_float_def real_of_float_nge0_exp[OF False] by auto
   534   hence "1 * ?S \<le> real m * inverse ?S * ?S" by (rule mult_right_mono, auto)
   535   hence "?S \<le> real m" unfolding mult_assoc by auto
   536   hence "?S \<le> m" unfolding real_of_int_le_iff[symmetric] by auto
   537   from this bitlen_bounds[OF `0 < m`, THEN conjunct2]
   538   have "nat (-e) < (nat (bitlen m))" unfolding power_strict_increasing_iff[OF `1 < 2`, symmetric] by (rule order_le_less_trans)
   539   hence "-e < bitlen m" using False bitlen_ge0 by auto
   540   thus ?thesis by auto
   541 qed
   542 
   543 lemma normalized_float: assumes "m \<noteq> 0" shows "real (Float m (- (bitlen m - 1))) = real m / 2^nat (bitlen m - 1)"
   544 proof (cases "- (bitlen m - 1) = 0")
   545   case True show ?thesis unfolding real_of_float_simp pow2_def using True by auto
   546 next
   547   case False hence P: "\<not> 0 \<le> - (bitlen m - 1)" using bitlen_ge1[OF `m \<noteq> 0`] by auto
   548   show ?thesis unfolding real_of_float_nge0_exp[OF P] divide_inverse by auto
   549 qed
   550 
   551 lemma bitlen_Pls: "bitlen (Int.Pls) = Int.Pls" by (subst Pls_def, subst Pls_def, simp)
   552 
   553 lemma bitlen_Min: "bitlen (Int.Min) = Int.Bit1 Int.Pls" by (subst Min_def, simp add: Bit1_def) 
   554 
   555 lemma bitlen_B0: "bitlen (Int.Bit0 b) = (if iszero b then Int.Pls else Int.succ (bitlen b))"
   556   apply (auto simp add: iszero_def succ_def)
   557   apply (simp add: Bit0_def Pls_def)
   558   apply (subst Bit0_def)
   559   apply simp
   560   apply (subgoal_tac "0 < 2 * b \<or> 2 * b < -1")
   561   apply auto
   562   done
   563 
   564 lemma bitlen_B1: "bitlen (Int.Bit1 b) = (if iszero (Int.succ b) then Int.Bit1 Int.Pls else Int.succ (bitlen b))"
   565 proof -
   566   have h: "! x. (2*x + 1) div 2 = (x::int)"
   567     by arith    
   568   show ?thesis
   569     apply (auto simp add: iszero_def succ_def)
   570     apply (subst Bit1_def)+
   571     apply simp
   572     apply (subgoal_tac "2 * b + 1 = -1")
   573     apply (simp only:)
   574     apply simp_all
   575     apply (subst Bit1_def)
   576     apply simp
   577     apply (subgoal_tac "0 < 2 * b + 1 \<or> 2 * b + 1 < -1")
   578     apply (auto simp add: h)
   579     done
   580 qed
   581 
   582 lemma bitlen_number_of: "bitlen (number_of w) = number_of (bitlen w)"
   583   by (simp add: number_of_is_id)
   584 
   585 lemma [code]: "bitlen x = 
   586      (if x = 0  then 0 
   587  else if x = -1 then 1 
   588                 else (1 + (bitlen (x div 2))))"
   589   by (cases "x = 0 \<or> x = -1 \<or> 0 < x") auto
   590 
   591 definition lapprox_posrat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
   592 where
   593   "lapprox_posrat prec x y = 
   594    (let 
   595        l = nat (int prec + bitlen y - bitlen x) ;
   596        d = (x * 2^l) div y
   597     in normfloat (Float d (- (int l))))"
   598 
   599 lemma pow2_minus: "pow2 (-x) = inverse (pow2 x)"
   600   unfolding pow2_neg[of "-x"] by auto
   601 
   602 lemma lapprox_posrat: 
   603   assumes x: "0 \<le> x"
   604   and y: "0 < y"
   605   shows "real (lapprox_posrat prec x y) \<le> real x / real y"
   606 proof -
   607   let ?l = "nat (int prec + bitlen y - bitlen x)"
   608   
   609   have "real (x * 2^?l div y) * inverse (2^?l) \<le> (real (x * 2^?l) / real y) * inverse (2^?l)" 
   610     by (rule mult_right_mono, fact real_of_int_div4, simp)
   611   also have "\<dots> \<le> (real x / real y) * 2^?l * inverse (2^?l)" by auto
   612   finally have "real (x * 2^?l div y) * inverse (2^?l) \<le> real x / real y" unfolding mult_assoc by auto
   613   thus ?thesis unfolding lapprox_posrat_def Let_def normfloat real_of_float_simp
   614     unfolding pow2_minus pow2_int minus_minus .
   615 qed
   616 
   617 lemma real_of_int_div_mult: 
   618   fixes x y c :: int assumes "0 < y" and "0 < c"
   619   shows "real (x div y) \<le> real (x * c div y) * inverse (real c)"
   620 proof -
   621   have "c * (x div y) + 0 \<le> c * x div y" unfolding zdiv_zmult1_eq[of c x y]
   622     by (rule add_left_mono, 
   623         auto intro!: mult_nonneg_nonneg 
   624              simp add: pos_imp_zdiv_nonneg_iff[OF `0 < y`] `0 < c`[THEN less_imp_le] pos_mod_sign[OF `0 < y`])
   625   hence "real (x div y) * real c \<le> real (x * c div y)" 
   626     unfolding real_of_int_mult[symmetric] real_of_int_le_iff mult_commute by auto
   627   hence "real (x div y) * real c * inverse (real c) \<le> real (x * c div y) * inverse (real c)"
   628     using `0 < c` by auto
   629   thus ?thesis unfolding mult_assoc using `0 < c` by auto
   630 qed
   631 
   632 lemma lapprox_posrat_bottom: assumes "0 < y"
   633   shows "real (x div y) \<le> real (lapprox_posrat n x y)" 
   634 proof -
   635   have pow: "\<And>x. (0::int) < 2^x" by auto
   636   show ?thesis
   637     unfolding lapprox_posrat_def Let_def real_of_float_add normfloat real_of_float_simp pow2_minus pow2_int
   638     using real_of_int_div_mult[OF `0 < y` pow] by auto
   639 qed
   640 
   641 lemma lapprox_posrat_nonneg: assumes "0 \<le> x" and "0 < y"
   642   shows "0 \<le> real (lapprox_posrat n x y)" 
   643 proof -
   644   show ?thesis
   645     unfolding lapprox_posrat_def Let_def real_of_float_add normfloat real_of_float_simp pow2_minus pow2_int
   646     using pos_imp_zdiv_nonneg_iff[OF `0 < y`] assms by (auto intro!: mult_nonneg_nonneg)
   647 qed
   648 
   649 definition rapprox_posrat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
   650 where
   651   "rapprox_posrat prec x y = (let
   652      l = nat (int prec + bitlen y - bitlen x) ;
   653      X = x * 2^l ;
   654      d = X div y ;
   655      m = X mod y
   656    in normfloat (Float (d + (if m = 0 then 0 else 1)) (- (int l))))"
   657 
   658 lemma rapprox_posrat:
   659   assumes x: "0 \<le> x"
   660   and y: "0 < y"
   661   shows "real x / real y \<le> real (rapprox_posrat prec x y)"
   662 proof -
   663   let ?l = "nat (int prec + bitlen y - bitlen x)" let ?X = "x * 2^?l"
   664   show ?thesis 
   665   proof (cases "?X mod y = 0")
   666     case True hence "y \<noteq> 0" and "y dvd ?X" using `0 < y` by auto
   667     from real_of_int_div[OF this]
   668     have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
   669     also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
   670     finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
   671     thus ?thesis unfolding rapprox_posrat_def Let_def normfloat if_P[OF True] 
   672       unfolding real_of_float_simp pow2_minus pow2_int minus_minus by auto
   673   next
   674     case False
   675     have "0 \<le> real y" and "real y \<noteq> 0" using `0 < y` by auto
   676     have "0 \<le> real y * 2^?l" by (rule mult_nonneg_nonneg, rule `0 \<le> real y`, auto)
   677 
   678     have "?X = y * (?X div y) + ?X mod y" by auto
   679     also have "\<dots> \<le> y * (?X div y) + y" by (rule add_mono, auto simp add: pos_mod_bound[OF `0 < y`, THEN less_imp_le])
   680     also have "\<dots> = y * (?X div y + 1)" unfolding right_distrib by auto
   681     finally have "real ?X \<le> real y * real (?X div y + 1)" unfolding real_of_int_le_iff real_of_int_mult[symmetric] .
   682     hence "real ?X / (real y * 2^?l) \<le> real y * real (?X div y + 1) / (real y * 2^?l)" 
   683       by (rule divide_right_mono, simp only: `0 \<le> real y * 2^?l`)
   684     also have "\<dots> = real y * real (?X div y + 1) / real y / 2^?l" by auto
   685     also have "\<dots> = real (?X div y + 1) * inverse (2^?l)" unfolding nonzero_mult_divide_cancel_left[OF `real y \<noteq> 0`] 
   686       unfolding divide_inverse ..
   687     finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
   688       unfolding pow2_minus pow2_int minus_minus by auto
   689   qed
   690 qed
   691 
   692 lemma rapprox_posrat_le1: assumes "0 \<le> x" and "0 < y" and "x \<le> y"
   693   shows "real (rapprox_posrat n x y) \<le> 1"
   694 proof -
   695   let ?l = "nat (int n + bitlen y - bitlen x)" let ?X = "x * 2^?l"
   696   show ?thesis
   697   proof (cases "?X mod y = 0")
   698     case True hence "y \<noteq> 0" and "y dvd ?X" using `0 < y` by auto
   699     from real_of_int_div[OF this]
   700     have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
   701     also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
   702     finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
   703     also have "real x / real y \<le> 1" using `0 \<le> x` and `0 < y` and `x \<le> y` by auto
   704     finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat if_P[OF True]
   705       unfolding real_of_float_simp pow2_minus pow2_int minus_minus by auto
   706   next
   707     case False
   708     have "x \<noteq> y"
   709     proof (rule ccontr)
   710       assume "\<not> x \<noteq> y" hence "x = y" by auto
   711       have "?X mod y = 0" unfolding `x = y` using mod_mult_self1_is_0 by auto
   712       thus False using False by auto
   713     qed
   714     hence "x < y" using `x \<le> y` by auto
   715     hence "real x / real y < 1" using `0 < y` and `0 \<le> x` by auto
   716 
   717     from real_of_int_div4[of "?X" y]
   718     have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_number_of .
   719     also have "\<dots> < 1 * 2^?l" using `real x / real y < 1` by (rule mult_strict_right_mono, auto)
   720     finally have "?X div y < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
   721     hence "?X div y + 1 \<le> 2^?l" by auto
   722     hence "real (?X div y + 1) * inverse (2^?l) \<le> 2^?l * inverse (2^?l)"
   723       unfolding real_of_int_le_iff[of _ "2^?l", symmetric] real_of_int_power real_number_of
   724       by (rule mult_right_mono, auto)
   725     hence "real (?X div y + 1) * inverse (2^?l) \<le> 1" by auto
   726     thus ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
   727       unfolding pow2_minus pow2_int minus_minus by auto
   728   qed
   729 qed
   730 
   731 lemma zdiv_greater_zero: fixes a b :: int assumes "0 < a" and "a \<le> b"
   732   shows "0 < b div a"
   733 proof (rule ccontr)
   734   have "0 \<le> b" using assms by auto
   735   assume "\<not> 0 < b div a" hence "b div a = 0" using `0 \<le> b`[unfolded pos_imp_zdiv_nonneg_iff[OF `0<a`, of b, symmetric]] by auto
   736   have "b = a * (b div a) + b mod a" by auto
   737   hence "b = b mod a" unfolding `b div a = 0` by auto
   738   hence "b < a" using `0 < a`[THEN pos_mod_bound, of b] by auto
   739   thus False using `a \<le> b` by auto
   740 qed
   741 
   742 lemma rapprox_posrat_less1: assumes "0 \<le> x" and "0 < y" and "2 * x < y" and "0 < n"
   743   shows "real (rapprox_posrat n x y) < 1"
   744 proof (cases "x = 0")
   745   case True thus ?thesis unfolding rapprox_posrat_def True Let_def normfloat real_of_float_simp by auto
   746 next
   747   case False hence "0 < x" using `0 \<le> x` by auto
   748   hence "x < y" using assms by auto
   749   
   750   let ?l = "nat (int n + bitlen y - bitlen x)" let ?X = "x * 2^?l"
   751   show ?thesis
   752   proof (cases "?X mod y = 0")
   753     case True hence "y \<noteq> 0" and "y dvd ?X" using `0 < y` by auto
   754     from real_of_int_div[OF this]
   755     have "real (?X div y) * inverse (2 ^ ?l) = real ?X / real y * inverse (2 ^ ?l)" by auto
   756     also have "\<dots> = real x / real y * (2^?l * inverse (2^?l))" by auto
   757     finally have "real (?X div y) * inverse (2^?l) = real x / real y" by auto
   758     also have "real x / real y < 1" using `0 \<le> x` and `0 < y` and `x < y` by auto
   759     finally show ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_P[OF True]
   760       unfolding pow2_minus pow2_int minus_minus by auto
   761   next
   762     case False
   763     hence "(real x / real y) < 1 / 2" using `0 < y` and `0 \<le> x` `2 * x < y` by auto
   764 
   765     have "0 < ?X div y"
   766     proof -
   767       have "2^nat (bitlen x - 1) \<le> y" and "y < 2^nat (bitlen y)"
   768         using bitlen_bounds[OF `0 < x`, THEN conjunct1] bitlen_bounds[OF `0 < y`, THEN conjunct2] `x < y` by auto
   769       hence "(2::int)^nat (bitlen x - 1) < 2^nat (bitlen y)" by (rule order_le_less_trans)
   770       hence "bitlen x \<le> bitlen y" by auto
   771       hence len_less: "nat (bitlen x - 1) \<le> nat (int (n - 1) + bitlen y)" by auto
   772 
   773       have "x \<noteq> 0" and "y \<noteq> 0" using `0 < x` `0 < y` by auto
   774 
   775       have exp_eq: "nat (int (n - 1) + bitlen y) - nat (bitlen x - 1) = ?l"
   776         using `bitlen x \<le> bitlen y` bitlen_ge1[OF `x \<noteq> 0`] bitlen_ge1[OF `y \<noteq> 0`] `0 < n` by auto
   777 
   778       have "y * 2^nat (bitlen x - 1) \<le> y * x" 
   779         using bitlen_bounds[OF `0 < x`, THEN conjunct1] `0 < y`[THEN less_imp_le] by (rule mult_left_mono)
   780       also have "\<dots> \<le> 2^nat (bitlen y) * x" using bitlen_bounds[OF `0 < y`, THEN conjunct2, THEN less_imp_le] `0 \<le> x` by (rule mult_right_mono)
   781       also have "\<dots> \<le> x * 2^nat (int (n - 1) + bitlen y)" unfolding mult_commute[of x] by (rule mult_right_mono, auto simp add: `0 \<le> x`)
   782       finally have "real y * 2^nat (bitlen x - 1) * inverse (2^nat (bitlen x - 1)) \<le> real x * 2^nat (int (n - 1) + bitlen y) * inverse (2^nat (bitlen x - 1))"
   783         unfolding real_of_int_le_iff[symmetric] by auto
   784       hence "real y \<le> real x * (2^nat (int (n - 1) + bitlen y) / (2^nat (bitlen x - 1)))" 
   785         unfolding mult_assoc divide_inverse by auto
   786       also have "\<dots> = real x * (2^(nat (int (n - 1) + bitlen y) - nat (bitlen x - 1)))" using power_diff[of "2::real", OF _ len_less] by auto
   787       finally have "y \<le> x * 2^?l" unfolding exp_eq unfolding real_of_int_le_iff[symmetric] by auto
   788       thus ?thesis using zdiv_greater_zero[OF `0 < y`] by auto
   789     qed
   790 
   791     from real_of_int_div4[of "?X" y]
   792     have "real (?X div y) \<le> (real x / real y) * 2^?l" unfolding real_of_int_mult times_divide_eq_left real_of_int_power real_number_of .
   793     also have "\<dots> < 1/2 * 2^?l" using `real x / real y < 1/2` by (rule mult_strict_right_mono, auto)
   794     finally have "?X div y * 2 < 2^?l" unfolding real_of_int_less_iff[of _ "2^?l", symmetric] by auto
   795     hence "?X div y + 1 < 2^?l" using `0 < ?X div y` by auto
   796     hence "real (?X div y + 1) * inverse (2^?l) < 2^?l * inverse (2^?l)"
   797       unfolding real_of_int_less_iff[of _ "2^?l", symmetric] real_of_int_power real_number_of
   798       by (rule mult_strict_right_mono, auto)
   799     hence "real (?X div y + 1) * inverse (2^?l) < 1" by auto
   800     thus ?thesis unfolding rapprox_posrat_def Let_def normfloat real_of_float_simp if_not_P[OF False]
   801       unfolding pow2_minus pow2_int minus_minus by auto
   802   qed
   803 qed
   804 
   805 lemma approx_rat_pattern: fixes P and ps :: "nat * int * int"
   806   assumes Y: "\<And>y prec x. \<lbrakk>y = 0; ps = (prec, x, 0)\<rbrakk> \<Longrightarrow> P" 
   807   and A: "\<And>x y prec. \<lbrakk>0 \<le> x; 0 < y; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
   808   and B: "\<And>x y prec. \<lbrakk>x < 0; 0 < y; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
   809   and C: "\<And>x y prec. \<lbrakk>x < 0; y < 0; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
   810   and D: "\<And>x y prec. \<lbrakk>0 \<le> x; y < 0; ps = (prec, x, y)\<rbrakk> \<Longrightarrow> P"
   811   shows P
   812 proof -
   813   obtain prec x y where [simp]: "ps = (prec, x, y)" by (cases ps) auto
   814   from Y have "y = 0 \<Longrightarrow> P" by auto
   815   moreover {
   816     assume "0 < y"
   817     have P
   818     proof (cases "0 \<le> x")
   819       case True
   820       with A and `0 < y` show P by auto
   821     next
   822       case False
   823       with B and `0 < y` show P by auto
   824     qed
   825   } 
   826   moreover {
   827     assume "y < 0"
   828     have P
   829     proof (cases "0 \<le> x")
   830       case True
   831       with D and `y < 0` show P by auto
   832     next
   833       case False
   834       with C and `y < 0` show P by auto
   835     qed
   836   }
   837   ultimately show P by (cases "y = 0 \<or> 0 < y \<or> y < 0") auto
   838 qed
   839 
   840 function lapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
   841 where
   842   "y = 0 \<Longrightarrow> lapprox_rat prec x y = 0"
   843 | "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> lapprox_rat prec x y = lapprox_posrat prec x y"
   844 | "x < 0 \<Longrightarrow> 0 < y \<Longrightarrow> lapprox_rat prec x y = - (rapprox_posrat prec (-x) y)"
   845 | "x < 0 \<Longrightarrow> y < 0 \<Longrightarrow> lapprox_rat prec x y = lapprox_posrat prec (-x) (-y)"
   846 | "0 \<le> x \<Longrightarrow> y < 0 \<Longrightarrow> lapprox_rat prec x y = - (rapprox_posrat prec x (-y))"
   847 apply simp_all by (rule approx_rat_pattern)
   848 termination by lexicographic_order
   849 
   850 lemma compute_lapprox_rat[code]:
   851       "lapprox_rat prec x y = (if y = 0 then 0 else if 0 \<le> x then (if 0 < y then lapprox_posrat prec x y else - (rapprox_posrat prec x (-y))) 
   852                                                              else (if 0 < y then - (rapprox_posrat prec (-x) y) else lapprox_posrat prec (-x) (-y)))"
   853   by auto
   854             
   855 lemma lapprox_rat: "real (lapprox_rat prec x y) \<le> real x / real y"
   856 proof -      
   857   have h[rule_format]: "! a b b'. b' \<le> b \<longrightarrow> a \<le> b' \<longrightarrow> a \<le> (b::real)" by auto
   858   show ?thesis
   859     apply (case_tac "y = 0")
   860     apply simp
   861     apply (case_tac "0 \<le> x \<and> 0 < y")
   862     apply (simp add: lapprox_posrat)
   863     apply (case_tac "x < 0 \<and> 0 < y")
   864     apply simp
   865     apply (subst minus_le_iff)   
   866     apply (rule h[OF rapprox_posrat])
   867     apply (simp_all)
   868     apply (case_tac "x < 0 \<and> y < 0")
   869     apply simp
   870     apply (rule h[OF _ lapprox_posrat])
   871     apply (simp_all)
   872     apply (case_tac "0 \<le> x \<and> y < 0")
   873     apply (simp)
   874     apply (subst minus_le_iff)   
   875     apply (rule h[OF rapprox_posrat])
   876     apply simp_all
   877     apply arith
   878     done
   879 qed
   880 
   881 lemma lapprox_rat_bottom: assumes "0 \<le> x" and "0 < y"
   882   shows "real (x div y) \<le> real (lapprox_rat n x y)" 
   883   unfolding lapprox_rat.simps(2)[OF assms]  using lapprox_posrat_bottom[OF `0<y`] .
   884 
   885 function rapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float"
   886 where
   887   "y = 0 \<Longrightarrow> rapprox_rat prec x y = 0"
   888 | "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> rapprox_rat prec x y = rapprox_posrat prec x y"
   889 | "x < 0 \<Longrightarrow> 0 < y \<Longrightarrow> rapprox_rat prec x y = - (lapprox_posrat prec (-x) y)"
   890 | "x < 0 \<Longrightarrow> y < 0 \<Longrightarrow> rapprox_rat prec x y = rapprox_posrat prec (-x) (-y)"
   891 | "0 \<le> x \<Longrightarrow> y < 0 \<Longrightarrow> rapprox_rat prec x y = - (lapprox_posrat prec x (-y))"
   892 apply simp_all by (rule approx_rat_pattern)
   893 termination by lexicographic_order
   894 
   895 lemma compute_rapprox_rat[code]:
   896       "rapprox_rat prec x y = (if y = 0 then 0 else if 0 \<le> x then (if 0 < y then rapprox_posrat prec x y else - (lapprox_posrat prec x (-y))) else 
   897                                                                   (if 0 < y then - (lapprox_posrat prec (-x) y) else rapprox_posrat prec (-x) (-y)))"
   898   by auto
   899 
   900 lemma rapprox_rat: "real x / real y \<le> real (rapprox_rat prec x y)"
   901 proof -      
   902   have h[rule_format]: "! a b b'. b' \<le> b \<longrightarrow> a \<le> b' \<longrightarrow> a \<le> (b::real)" by auto
   903   show ?thesis
   904     apply (case_tac "y = 0")
   905     apply simp
   906     apply (case_tac "0 \<le> x \<and> 0 < y")
   907     apply (simp add: rapprox_posrat)
   908     apply (case_tac "x < 0 \<and> 0 < y")
   909     apply simp
   910     apply (subst le_minus_iff)   
   911     apply (rule h[OF _ lapprox_posrat])
   912     apply (simp_all)
   913     apply (case_tac "x < 0 \<and> y < 0")
   914     apply simp
   915     apply (rule h[OF rapprox_posrat])
   916     apply (simp_all)
   917     apply (case_tac "0 \<le> x \<and> y < 0")
   918     apply (simp)
   919     apply (subst le_minus_iff)   
   920     apply (rule h[OF _ lapprox_posrat])
   921     apply simp_all
   922     apply arith
   923     done
   924 qed
   925 
   926 lemma rapprox_rat_le1: assumes "0 \<le> x" and "0 < y" and "x \<le> y"
   927   shows "real (rapprox_rat n x y) \<le> 1"
   928   unfolding rapprox_rat.simps(2)[OF `0 \<le> x` `0 < y`] using rapprox_posrat_le1[OF assms] .
   929 
   930 lemma rapprox_rat_neg: assumes "x < 0" and "0 < y"
   931   shows "real (rapprox_rat n x y) \<le> 0"
   932   unfolding rapprox_rat.simps(3)[OF assms] using lapprox_posrat_nonneg[of "-x" y n] assms by auto
   933 
   934 lemma rapprox_rat_nonneg_neg: assumes "0 \<le> x" and "y < 0"
   935   shows "real (rapprox_rat n x y) \<le> 0"
   936   unfolding rapprox_rat.simps(5)[OF assms] using lapprox_posrat_nonneg[of x "-y" n] assms by auto
   937 
   938 lemma rapprox_rat_nonpos_pos: assumes "x \<le> 0" and "0 < y"
   939   shows "real (rapprox_rat n x y) \<le> 0"
   940 proof (cases "x = 0") 
   941   case True
   942   hence "0 \<le> x" by auto show ?thesis
   943     unfolding rapprox_rat.simps(2)[OF `0 \<le> x` `0 < y`]
   944     unfolding True rapprox_posrat_def Let_def
   945     by auto
   946 next
   947   case False
   948   hence "x < 0" using assms by auto
   949   show ?thesis using rapprox_rat_neg[OF `x < 0` `0 < y`] .
   950 qed
   951 
   952 fun float_divl :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
   953 where
   954   "float_divl prec (Float m1 s1) (Float m2 s2) = 
   955     (let
   956        l = lapprox_rat prec m1 m2;
   957        f = Float 1 (s1 - s2)
   958      in
   959        f * l)"     
   960 
   961 lemma float_divl: "real (float_divl prec x y) \<le> real x / real y"
   962   using lapprox_rat[of prec "mantissa x" "mantissa y"]
   963   by (cases x y rule: float.exhaust[case_product float.exhaust])
   964      (simp split: split_if_asm
   965            add: real_of_float_simp pow2_diff field_simps le_divide_eq mult_less_0_iff zero_less_mult_iff)
   966 
   967 lemma float_divl_lower_bound: assumes "0 \<le> x" and "0 < y" shows "0 \<le> float_divl prec x y"
   968 proof (cases x, cases y)
   969   fix xm xe ym ye :: int
   970   assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
   971   have "0 \<le> xm"
   972     using `0 \<le> x`[unfolded x_eq le_float_def real_of_float_simp real_of_float_0 zero_le_mult_iff]
   973     by auto
   974   have "0 < ym"
   975     using `0 < y`[unfolded y_eq less_float_def real_of_float_simp real_of_float_0 zero_less_mult_iff]
   976     by auto
   977 
   978   have "\<And>n. 0 \<le> real (Float 1 n)"
   979     unfolding real_of_float_simp using zero_le_pow2 by auto
   980   moreover have "0 \<le> real (lapprox_rat prec xm ym)"
   981     apply (rule order_trans[OF _ lapprox_rat_bottom[OF `0 \<le> xm` `0 < ym`]])
   982     apply (auto simp add: `0 \<le> xm` pos_imp_zdiv_nonneg_iff[OF `0 < ym`])
   983     done
   984   ultimately show "0 \<le> float_divl prec x y"
   985     unfolding x_eq y_eq float_divl.simps Let_def le_float_def real_of_float_0
   986     by (auto intro!: mult_nonneg_nonneg)
   987 qed
   988 
   989 lemma float_divl_pos_less1_bound:
   990   assumes "0 < x" and "x < 1" and "0 < prec"
   991   shows "1 \<le> float_divl prec 1 x"
   992 proof (cases x)
   993   case (Float m e)
   994   from `0 < x` `x < 1` have "0 < m" "e < 0"
   995     using float_pos_m_pos float_pos_less1_e_neg unfolding Float by auto
   996   let ?b = "nat (bitlen m)" and ?e = "nat (-e)"
   997   have "1 \<le> m" and "m \<noteq> 0" using `0 < m` by auto
   998   with bitlen_bounds[OF `0 < m`] have "m < 2^?b" and "(2::int) \<le> 2^?b" by auto
   999   hence "1 \<le> bitlen m" using power_le_imp_le_exp[of "2::int" 1 ?b] by auto
  1000   hence pow_split: "nat (int prec + bitlen m - 1) = (prec - 1) + ?b" using `0 < prec` by auto
  1001   
  1002   have pow_not0: "\<And>x. (2::real)^x \<noteq> 0" by auto
  1003 
  1004   from float_less1_mantissa_bound `0 < x` `x < 1` Float 
  1005   have "m < 2^?e" by auto
  1006   with bitlen_bounds[OF `0 < m`, THEN conjunct1] have "(2::int)^nat (bitlen m - 1) < 2^?e"
  1007     by (rule order_le_less_trans)
  1008   from power_less_imp_less_exp[OF _ this]
  1009   have "bitlen m \<le> - e" by auto
  1010   hence "(2::real)^?b \<le> 2^?e" by auto
  1011   hence "(2::real)^?b * inverse (2^?b) \<le> 2^?e * inverse (2^?b)"
  1012     by (rule mult_right_mono) auto
  1013   hence "(1::real) \<le> 2^?e * inverse (2^?b)" by auto
  1014   also
  1015   let ?d = "real (2 ^ nat (int prec + bitlen m - 1) div m) * inverse (2 ^ nat (int prec + bitlen m - 1))"
  1016   {
  1017     have "2^(prec - 1) * m \<le> 2^(prec - 1) * 2^?b"
  1018       using `m < 2^?b`[THEN less_imp_le] by (rule mult_left_mono) auto
  1019     also have "\<dots> = 2 ^ nat (int prec + bitlen m - 1)"
  1020       unfolding pow_split power_add by auto
  1021     finally have "2^(prec - 1) * m div m \<le> 2 ^ nat (int prec + bitlen m - 1) div m"
  1022       using `0 < m` by (rule zdiv_mono1)
  1023     hence "2^(prec - 1) \<le> 2 ^ nat (int prec + bitlen m - 1) div m"
  1024       unfolding div_mult_self2_is_id[OF `m \<noteq> 0`] .
  1025     hence "2^(prec - 1) * inverse (2 ^ nat (int prec + bitlen m - 1)) \<le> ?d"
  1026       unfolding real_of_int_le_iff[of "2^(prec - 1)", symmetric] by auto
  1027   }
  1028   from mult_left_mono[OF this [unfolded pow_split power_add inverse_mult_distrib mult_assoc[symmetric] right_inverse[OF pow_not0] mult_1_left], of "2^?e"]
  1029   have "2^?e * inverse (2^?b) \<le> 2^?e * ?d" unfolding pow_split power_add by auto
  1030   finally have "1 \<le> 2^?e * ?d" .
  1031   
  1032   have e_nat: "0 - e = int (nat (-e))" using `e < 0` by auto
  1033   have "bitlen 1 = 1" using bitlen.simps by auto
  1034   
  1035   show ?thesis 
  1036     unfolding one_float_def Float float_divl.simps Let_def
  1037       lapprox_rat.simps(2)[OF zero_le_one `0 < m`]
  1038       lapprox_posrat_def `bitlen 1 = 1`
  1039     unfolding le_float_def real_of_float_mult normfloat real_of_float_simp
  1040       pow2_minus pow2_int e_nat
  1041     using `1 \<le> 2^?e * ?d` by (auto simp add: pow2_def)
  1042 qed
  1043 
  1044 fun float_divr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float"
  1045 where
  1046   "float_divr prec (Float m1 s1) (Float m2 s2) = 
  1047     (let
  1048        r = rapprox_rat prec m1 m2;
  1049        f = Float 1 (s1 - s2)
  1050      in
  1051        f * r)"  
  1052 
  1053 lemma float_divr: "real x / real y \<le> real (float_divr prec x y)"
  1054   using rapprox_rat[of "mantissa x" "mantissa y" prec]
  1055   by (cases x y rule: float.exhaust[case_product float.exhaust])
  1056      (simp split: split_if_asm
  1057            add: real_of_float_simp pow2_diff field_simps divide_le_eq mult_less_0_iff zero_less_mult_iff)
  1058 
  1059 lemma float_divr_pos_less1_lower_bound: assumes "0 < x" and "x < 1" shows "1 \<le> float_divr prec 1 x"
  1060 proof -
  1061   have "1 \<le> 1 / real x" using `0 < x` and `x < 1` unfolding less_float_def by auto
  1062   also have "\<dots> \<le> real (float_divr prec 1 x)" using float_divr[where x=1 and y=x] by auto
  1063   finally show ?thesis unfolding le_float_def by auto
  1064 qed
  1065 
  1066 lemma float_divr_nonpos_pos_upper_bound: assumes "x \<le> 0" and "0 < y" shows "float_divr prec x y \<le> 0"
  1067 proof (cases x, cases y)
  1068   fix xm xe ym ye :: int
  1069   assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
  1070   have "xm \<le> 0" using `x \<le> 0`[unfolded x_eq le_float_def real_of_float_simp real_of_float_0 mult_le_0_iff] by auto
  1071   have "0 < ym" using `0 < y`[unfolded y_eq less_float_def real_of_float_simp real_of_float_0 zero_less_mult_iff] by auto
  1072 
  1073   have "\<And>n. 0 \<le> real (Float 1 n)" unfolding real_of_float_simp using zero_le_pow2 by auto
  1074   moreover have "real (rapprox_rat prec xm ym) \<le> 0" using rapprox_rat_nonpos_pos[OF `xm \<le> 0` `0 < ym`] .
  1075   ultimately show "float_divr prec x y \<le> 0"
  1076     unfolding x_eq y_eq float_divr.simps Let_def le_float_def real_of_float_0 real_of_float_mult by (auto intro!: mult_nonneg_nonpos)
  1077 qed
  1078 
  1079 lemma float_divr_nonneg_neg_upper_bound: assumes "0 \<le> x" and "y < 0" shows "float_divr prec x y \<le> 0"
  1080 proof (cases x, cases y)
  1081   fix xm xe ym ye :: int
  1082   assume x_eq: "x = Float xm xe" and y_eq: "y = Float ym ye"
  1083   have "0 \<le> xm" using `0 \<le> x`[unfolded x_eq le_float_def real_of_float_simp real_of_float_0 zero_le_mult_iff] by auto
  1084   have "ym < 0" using `y < 0`[unfolded y_eq less_float_def real_of_float_simp real_of_float_0 mult_less_0_iff] by auto
  1085   hence "0 < - ym" by auto
  1086 
  1087   have "\<And>n. 0 \<le> real (Float 1 n)" unfolding real_of_float_simp using zero_le_pow2 by auto
  1088   moreover have "real (rapprox_rat prec xm ym) \<le> 0" using rapprox_rat_nonneg_neg[OF `0 \<le> xm` `ym < 0`] .
  1089   ultimately show "float_divr prec x y \<le> 0"
  1090     unfolding x_eq y_eq float_divr.simps Let_def le_float_def real_of_float_0 real_of_float_mult by (auto intro!: mult_nonneg_nonpos)
  1091 qed
  1092 
  1093 primrec round_down :: "nat \<Rightarrow> float \<Rightarrow> float" where
  1094 "round_down prec (Float m e) = (let d = bitlen m - int prec in
  1095      if 0 < d then let P = 2^nat d ; n = m div P in Float n (e + d)
  1096               else Float m e)"
  1097 
  1098 primrec round_up :: "nat \<Rightarrow> float \<Rightarrow> float" where
  1099 "round_up prec (Float m e) = (let d = bitlen m - int prec in
  1100   if 0 < d then let P = 2^nat d ; n = m div P ; r = m mod P in Float (n + (if r = 0 then 0 else 1)) (e + d) 
  1101            else Float m e)"
  1102 
  1103 lemma round_up: "real x \<le> real (round_up prec x)"
  1104 proof (cases x)
  1105   case (Float m e)
  1106   let ?d = "bitlen m - int prec"
  1107   let ?p = "(2::int)^nat ?d"
  1108   have "0 < ?p" by auto
  1109   show "?thesis"
  1110   proof (cases "0 < ?d")
  1111     case True
  1112     hence pow_d: "pow2 ?d = real ?p" using pow2_int[symmetric] by simp
  1113     show ?thesis
  1114     proof (cases "m mod ?p = 0")
  1115       case True
  1116       have m: "m = m div ?p * ?p + 0" unfolding True[symmetric] using zdiv_zmod_equality2[where k=0, unfolded monoid_add_class.add_0_right, symmetric] .
  1117       have "real (Float m e) = real (Float (m div ?p) (e + ?d))" unfolding real_of_float_simp arg_cong[OF m, of real]
  1118         by (auto simp add: pow2_add `0 < ?d` pow_d)
  1119       thus ?thesis
  1120         unfolding Float round_up.simps Let_def if_P[OF `m mod ?p = 0`] if_P[OF `0 < ?d`]
  1121         by auto
  1122     next
  1123       case False
  1124       have "m = m div ?p * ?p + m mod ?p" unfolding zdiv_zmod_equality2[where k=0, unfolded monoid_add_class.add_0_right] ..
  1125       also have "\<dots> \<le> (m div ?p + 1) * ?p" unfolding left_distrib mult_1 by (rule add_left_mono, rule pos_mod_bound[OF `0 < ?p`, THEN less_imp_le])
  1126       finally have "real (Float m e) \<le> real (Float (m div ?p + 1) (e + ?d))" unfolding real_of_float_simp add_commute[of e]
  1127         unfolding pow2_add mult_assoc[symmetric] real_of_int_le_iff[of m, symmetric]
  1128         by (auto intro!: mult_mono simp add: pow2_add `0 < ?d` pow_d)
  1129       thus ?thesis
  1130         unfolding Float round_up.simps Let_def if_not_P[OF `\<not> m mod ?p = 0`] if_P[OF `0 < ?d`] .
  1131     qed
  1132   next
  1133     case False
  1134     show ?thesis
  1135       unfolding Float round_up.simps Let_def if_not_P[OF False] .. 
  1136   qed
  1137 qed
  1138 
  1139 lemma round_down: "real (round_down prec x) \<le> real x"
  1140 proof (cases x)
  1141   case (Float m e)
  1142   let ?d = "bitlen m - int prec"
  1143   let ?p = "(2::int)^nat ?d"
  1144   have "0 < ?p" by auto
  1145   show "?thesis"
  1146   proof (cases "0 < ?d")
  1147     case True
  1148     hence pow_d: "pow2 ?d = real ?p" using pow2_int[symmetric] by simp
  1149     have "m div ?p * ?p \<le> m div ?p * ?p + m mod ?p" by (auto simp add: pos_mod_bound[OF `0 < ?p`, THEN less_imp_le])
  1150     also have "\<dots> \<le> m" unfolding zdiv_zmod_equality2[where k=0, unfolded monoid_add_class.add_0_right] ..
  1151     finally have "real (Float (m div ?p) (e + ?d)) \<le> real (Float m e)" unfolding real_of_float_simp add_commute[of e]
  1152       unfolding pow2_add mult_assoc[symmetric] real_of_int_le_iff[of _ m, symmetric]
  1153       by (auto intro!: mult_mono simp add: pow2_add `0 < ?d` pow_d)
  1154     thus ?thesis
  1155       unfolding Float round_down.simps Let_def if_P[OF `0 < ?d`] .
  1156   next
  1157     case False
  1158     show ?thesis
  1159       unfolding Float round_down.simps Let_def if_not_P[OF False] .. 
  1160   qed
  1161 qed
  1162 
  1163 definition lb_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  1164 "lb_mult prec x y = (case normfloat (x * y) of Float m e \<Rightarrow> let
  1165     l = bitlen m - int prec
  1166   in if l > 0 then Float (m div (2^nat l)) (e + l)
  1167               else Float m e)"
  1168 
  1169 definition ub_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  1170 "ub_mult prec x y = (case normfloat (x * y) of Float m e \<Rightarrow> let
  1171     l = bitlen m - int prec
  1172   in if l > 0 then Float (m div (2^nat l) + 1) (e + l)
  1173               else Float m e)"
  1174 
  1175 lemma lb_mult: "real (lb_mult prec x y) \<le> real (x * y)"
  1176 proof (cases "normfloat (x * y)")
  1177   case (Float m e)
  1178   hence "odd m \<or> (m = 0 \<and> e = 0)" by (rule normfloat_imp_odd_or_zero)
  1179   let ?l = "bitlen m - int prec"
  1180   have "real (lb_mult prec x y) \<le> real (normfloat (x * y))"
  1181   proof (cases "?l > 0")
  1182     case False thus ?thesis unfolding lb_mult_def Float Let_def float.cases by auto
  1183   next
  1184     case True
  1185     have "real (m div 2^(nat ?l)) * pow2 ?l \<le> real m"
  1186     proof -
  1187       have "real (m div 2^(nat ?l)) * pow2 ?l = real (2^(nat ?l) * (m div 2^(nat ?l)))" unfolding real_of_int_mult real_of_int_power real_number_of unfolding pow2_int[symmetric] 
  1188         using `?l > 0` by auto
  1189       also have "\<dots> \<le> real (2^(nat ?l) * (m div 2^(nat ?l)) + m mod 2^(nat ?l))" unfolding real_of_int_add by auto
  1190       also have "\<dots> = real m" unfolding zmod_zdiv_equality[symmetric] ..
  1191       finally show ?thesis by auto
  1192     qed
  1193     thus ?thesis unfolding lb_mult_def Float Let_def float.cases if_P[OF True] real_of_float_simp pow2_add mult_commute mult_assoc by auto
  1194   qed
  1195   also have "\<dots> = real (x * y)" unfolding normfloat ..
  1196   finally show ?thesis .
  1197 qed
  1198 
  1199 lemma ub_mult: "real (x * y) \<le> real (ub_mult prec x y)"
  1200 proof (cases "normfloat (x * y)")
  1201   case (Float m e)
  1202   hence "odd m \<or> (m = 0 \<and> e = 0)" by (rule normfloat_imp_odd_or_zero)
  1203   let ?l = "bitlen m - int prec"
  1204   have "real (x * y) = real (normfloat (x * y))" unfolding normfloat ..
  1205   also have "\<dots> \<le> real (ub_mult prec x y)"
  1206   proof (cases "?l > 0")
  1207     case False thus ?thesis unfolding ub_mult_def Float Let_def float.cases by auto
  1208   next
  1209     case True
  1210     have "real m \<le> real (m div 2^(nat ?l) + 1) * pow2 ?l"
  1211     proof -
  1212       have "m mod 2^(nat ?l) < 2^(nat ?l)" by (rule pos_mod_bound) auto
  1213       hence mod_uneq: "real (m mod 2^(nat ?l)) \<le> 1 * 2^(nat ?l)" unfolding mult_1 real_of_int_less_iff[symmetric] by auto
  1214       
  1215       have "real m = real (2^(nat ?l) * (m div 2^(nat ?l)) + m mod 2^(nat ?l))" unfolding zmod_zdiv_equality[symmetric] ..
  1216       also have "\<dots> = real (m div 2^(nat ?l)) * 2^(nat ?l) + real (m mod 2^(nat ?l))" unfolding real_of_int_add by auto
  1217       also have "\<dots> \<le> (real (m div 2^(nat ?l)) + 1) * 2^(nat ?l)" unfolding left_distrib using mod_uneq by auto
  1218       finally show ?thesis unfolding pow2_int[symmetric] using True by auto
  1219     qed
  1220     thus ?thesis unfolding ub_mult_def Float Let_def float.cases if_P[OF True] real_of_float_simp pow2_add mult_commute mult_assoc by auto
  1221   qed
  1222   finally show ?thesis .
  1223 qed
  1224 
  1225 primrec float_abs :: "float \<Rightarrow> float" where
  1226   "float_abs (Float m e) = Float \<bar>m\<bar> e"
  1227 
  1228 instantiation float :: abs begin
  1229 definition abs_float_def: "\<bar>x\<bar> = float_abs x"
  1230 instance ..
  1231 end
  1232 
  1233 lemma real_of_float_abs: "real \<bar>x :: float\<bar> = \<bar>real x\<bar>" 
  1234 proof (cases x)
  1235   case (Float m e)
  1236   have "\<bar>real m\<bar> * pow2 e = \<bar>real m * pow2 e\<bar>" unfolding abs_mult by auto
  1237   thus ?thesis unfolding Float abs_float_def float_abs.simps real_of_float_simp by auto
  1238 qed
  1239 
  1240 primrec floor_fl :: "float \<Rightarrow> float" where
  1241   "floor_fl (Float m e) = (if 0 \<le> e then Float m e
  1242                                   else Float (m div (2 ^ (nat (-e)))) 0)"
  1243 
  1244 lemma floor_fl: "real (floor_fl x) \<le> real x"
  1245 proof (cases x)
  1246   case (Float m e)
  1247   show ?thesis
  1248   proof (cases "0 \<le> e")
  1249     case False
  1250     hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
  1251     have "real (Float (m div (2 ^ (nat (-e)))) 0) = real (m div 2 ^ (nat (-e)))" unfolding real_of_float_simp by auto
  1252     also have "\<dots> \<le> real m / real ((2::int) ^ (nat (-e)))" using real_of_int_div4 .
  1253     also have "\<dots> = real m * inverse (2 ^ (nat (-e)))" unfolding real_of_int_power real_number_of divide_inverse ..
  1254     also have "\<dots> = real (Float m e)" unfolding real_of_float_simp me_eq pow2_int pow2_neg[of e] ..
  1255     finally show ?thesis unfolding Float floor_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
  1256   next
  1257     case True thus ?thesis unfolding Float by auto
  1258   qed
  1259 qed
  1260 
  1261 lemma floor_pos_exp: assumes floor: "Float m e = floor_fl x" shows "0 \<le> e"
  1262 proof (cases x)
  1263   case (Float mx me)
  1264   from floor[unfolded Float floor_fl.simps] show ?thesis by (cases "0 \<le> me", auto)
  1265 qed
  1266 
  1267 declare floor_fl.simps[simp del]
  1268 
  1269 primrec ceiling_fl :: "float \<Rightarrow> float" where
  1270   "ceiling_fl (Float m e) = (if 0 \<le> e then Float m e
  1271                                     else Float (m div (2 ^ (nat (-e))) + 1) 0)"
  1272 
  1273 lemma ceiling_fl: "real x \<le> real (ceiling_fl x)"
  1274 proof (cases x)
  1275   case (Float m e)
  1276   show ?thesis
  1277   proof (cases "0 \<le> e")
  1278     case False
  1279     hence me_eq: "pow2 (-e) = pow2 (int (nat (-e)))" by auto
  1280     have "real (Float m e) = real m * inverse (2 ^ (nat (-e)))" unfolding real_of_float_simp me_eq pow2_int pow2_neg[of e] ..
  1281     also have "\<dots> = real m / real ((2::int) ^ (nat (-e)))" unfolding real_of_int_power real_number_of divide_inverse ..
  1282     also have "\<dots> \<le> 1 + real (m div 2 ^ (nat (-e)))" using real_of_int_div3[unfolded diff_le_eq] .
  1283     also have "\<dots> = real (Float (m div (2 ^ (nat (-e))) + 1) 0)" unfolding real_of_float_simp by auto
  1284     finally show ?thesis unfolding Float ceiling_fl.simps if_not_P[OF `\<not> 0 \<le> e`] .
  1285   next
  1286     case True thus ?thesis unfolding Float by auto
  1287   qed
  1288 qed
  1289 
  1290 declare ceiling_fl.simps[simp del]
  1291 
  1292 definition lb_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  1293 "lb_mod prec x ub lb = x - ceiling_fl (float_divr prec x lb) * ub"
  1294 
  1295 definition ub_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
  1296 "ub_mod prec x ub lb = x - floor_fl (float_divl prec x ub) * lb"
  1297 
  1298 lemma lb_mod: fixes k :: int assumes "0 \<le> real x" and "real k * y \<le> real x" (is "?k * y \<le> ?x")
  1299   assumes "0 < real lb" "real lb \<le> y" (is "?lb \<le> y") "y \<le> real ub" (is "y \<le> ?ub")
  1300   shows "real (lb_mod prec x ub lb) \<le> ?x - ?k * y"
  1301 proof -
  1302   have "?lb \<le> ?ub" using assms by auto
  1303   have "0 \<le> ?lb" and "?lb \<noteq> 0" using assms by auto
  1304   have "?k * y \<le> ?x" using assms by auto
  1305   also have "\<dots> \<le> ?x / ?lb * ?ub" by (metis mult_left_mono[OF `?lb \<le> ?ub` `0 \<le> ?x`] divide_right_mono[OF _ `0 \<le> ?lb` ] times_divide_eq_left nonzero_mult_divide_cancel_right[OF `?lb \<noteq> 0`])
  1306   also have "\<dots> \<le> real (ceiling_fl (float_divr prec x lb)) * ?ub" by (metis mult_right_mono order_trans `0 \<le> ?lb` `?lb \<le> ?ub` float_divr ceiling_fl)
  1307   finally show ?thesis unfolding lb_mod_def real_of_float_sub real_of_float_mult by auto
  1308 qed
  1309 
  1310 lemma ub_mod: fixes k :: int and x :: float assumes "0 \<le> real x" and "real x \<le> real k * y" (is "?x \<le> ?k * y")
  1311   assumes "0 < real lb" "real lb \<le> y" (is "?lb \<le> y") "y \<le> real ub" (is "y \<le> ?ub")
  1312   shows "?x - ?k * y \<le> real (ub_mod prec x ub lb)"
  1313 proof -
  1314   have "?lb \<le> ?ub" using assms by auto
  1315   hence "0 \<le> ?lb" and "0 \<le> ?ub" and "?ub \<noteq> 0" using assms by auto
  1316   have "real (floor_fl (float_divl prec x ub)) * ?lb \<le> ?x / ?ub * ?lb" by (metis mult_right_mono order_trans `0 \<le> ?lb` `?lb \<le> ?ub` float_divl floor_fl)
  1317   also have "\<dots> \<le> ?x" by (metis mult_left_mono[OF `?lb \<le> ?ub` `0 \<le> ?x`] divide_right_mono[OF _ `0 \<le> ?ub` ] times_divide_eq_left nonzero_mult_divide_cancel_right[OF `?ub \<noteq> 0`])
  1318   also have "\<dots> \<le> ?k * y" using assms by auto
  1319   finally show ?thesis unfolding ub_mod_def real_of_float_sub real_of_float_mult by auto
  1320 qed
  1321 
  1322 lemma le_float_def'[code]: "f \<le> g = (case f - g of Float a b \<Rightarrow> a \<le> 0)"
  1323 proof -
  1324   have le_transfer: "(f \<le> g) = (real (f - g) \<le> 0)" by (auto simp add: le_float_def)
  1325   from float_split[of "f - g"] obtain a b where f_diff_g: "f - g = Float a b" by auto
  1326   with le_transfer have le_transfer': "f \<le> g = (real (Float a b) \<le> 0)" by simp
  1327   show ?thesis by (simp add: le_transfer' f_diff_g float_le_zero)
  1328 qed
  1329 
  1330 lemma less_float_def'[code]: "f < g = (case f - g of Float a b \<Rightarrow> a < 0)"
  1331 proof -
  1332   have less_transfer: "(f < g) = (real (f - g) < 0)" by (auto simp add: less_float_def)
  1333   from float_split[of "f - g"] obtain a b where f_diff_g: "f - g = Float a b" by auto
  1334   with less_transfer have less_transfer': "f < g = (real (Float a b) < 0)" by simp
  1335   show ?thesis by (simp add: less_transfer' f_diff_g float_less_zero)
  1336 qed
  1337 
  1338 end