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