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