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