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