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