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