src/HOL/Library/Float.thy
author eberlm
Fri, 17 Jun 2016 11:33:52 +0200
changeset 63319 bc8793d7bd21
parent 63248 414e3550e9c0
child 63356 77332fed33c3
permissions -rw-r--r--
fps_from_poly → fps_of_poly
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47615
341fd902ef1c transfer now handles Let
hoelzl
parents: 47608
diff changeset
     1
(*  Title:      HOL/Library/Float.thy
341fd902ef1c transfer now handles Let
hoelzl
parents: 47608
diff changeset
     2
    Author:     Johannes Hölzl, Fabian Immler
341fd902ef1c transfer now handles Let
hoelzl
parents: 47608
diff changeset
     3
    Copyright   2012  TU München
341fd902ef1c transfer now handles Let
hoelzl
parents: 47608
diff changeset
     4
*)
341fd902ef1c transfer now handles Let
hoelzl
parents: 47608
diff changeset
     5
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
     6
section \<open>Floating-Point Numbers\<close>
29988
747f0c519090 add header
huffman
parents: 29804
diff changeset
     7
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20217
diff changeset
     8
theory Float
51542
738598beeb26 tuned imports;
wenzelm
parents: 51375
diff changeset
     9
imports Complex_Main Lattice_Algebras
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 20217
diff changeset
    10
begin
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
    11
49812
e3945ddcb9aa eliminated some remaining uses of typedef with implicit set definition;
wenzelm
parents: 47937
diff changeset
    12
definition "float = {m * 2 powr e | (m :: int) (e :: int). True}"
e3945ddcb9aa eliminated some remaining uses of typedef with implicit set definition;
wenzelm
parents: 47937
diff changeset
    13
49834
b27bbb021df1 discontinued obsolete typedef (open) syntax;
wenzelm
parents: 49812
diff changeset
    14
typedef float = float
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    15
  morphisms real_of_float float_of
49812
e3945ddcb9aa eliminated some remaining uses of typedef with implicit set definition;
wenzelm
parents: 47937
diff changeset
    16
  unfolding float_def by auto
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
    17
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    18
setup_lifting type_definition_float
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
    19
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    20
declare real_of_float [code_unfold]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    21
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    22
lemmas float_of_inject[simp]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    23
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    24
declare [[coercion "real_of_float :: float \<Rightarrow> real"]]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
    25
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
    26
lemma real_of_float_eq:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    27
  fixes f1 f2 :: float
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    28
  shows "f1 = f2 \<longleftrightarrow> real_of_float f1 = real_of_float f2"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    29
  unfolding real_of_float_inject ..
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    30
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    31
declare real_of_float_inverse[simp] float_of_inverse [simp]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    32
declare real_of_float [simp]
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
    33
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
    34
subsection \<open>Real operations preserving the representation as floating point number\<close>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    35
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    36
lemma floatI: fixes m e :: int shows "m * 2 powr e = x \<Longrightarrow> x \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    37
  by (auto simp: float_def)
19765
dfe940911617 misc cleanup;
wenzelm
parents: 16890
diff changeset
    38
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    39
lemma zero_float[simp]: "0 \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    40
  by (auto simp: float_def)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    41
lemma one_float[simp]: "1 \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    42
  by (intro floatI[of 1 0]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    43
lemma numeral_float[simp]: "numeral i \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    44
  by (intro floatI[of "numeral i" 0]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    45
lemma neg_numeral_float[simp]: "- numeral i \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    46
  by (intro floatI[of "- numeral i" 0]) simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    47
lemma real_of_int_float[simp]: "real_of_int (x :: int) \<in> float"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    48
  by (intro floatI[of x 0]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    49
lemma real_of_nat_float[simp]: "real (x :: nat) \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    50
  by (intro floatI[of x 0]) simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    51
lemma two_powr_int_float[simp]: "2 powr (real_of_int (i::int)) \<in> float"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    52
  by (intro floatI[of 1 i]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    53
lemma two_powr_nat_float[simp]: "2 powr (real (i::nat)) \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    54
  by (intro floatI[of 1 i]) simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    55
lemma two_powr_minus_int_float[simp]: "2 powr - (real_of_int (i::int)) \<in> float"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    56
  by (intro floatI[of 1 "-i"]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    57
lemma two_powr_minus_nat_float[simp]: "2 powr - (real (i::nat)) \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    58
  by (intro floatI[of 1 "-i"]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    59
lemma two_powr_numeral_float[simp]: "2 powr numeral i \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    60
  by (intro floatI[of 1 "numeral i"]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    61
lemma two_powr_neg_numeral_float[simp]: "2 powr - numeral i \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    62
  by (intro floatI[of 1 "- numeral i"]) simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    63
lemma two_pow_float[simp]: "2 ^ n \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    64
  by (intro floatI[of 1 "n"]) (simp add: powr_realpow)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    65
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
    66
45495
c55a07526dbe cleaned up float theories; removed duplicate definitions and theorems
hoelzl
parents: 44766
diff changeset
    67
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    68
lemma plus_float[simp]: "r \<in> float \<Longrightarrow> p \<in> float \<Longrightarrow> r + p \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    69
  unfolding float_def
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    70
proof (safe, simp)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    71
  have *: "\<exists>(m::int) (e::int). m1 * 2 powr e1 + m2 * 2 powr e2 = m * 2 powr e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    72
    if "e1 \<le> e2" for e1 m1 e2 m2 :: int
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    73
  proof -
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    74
    from that have "m1 * 2 powr e1 + m2 * 2 powr e2 = (m1 + m2 * 2 ^ nat (e2 - e1)) * 2 powr e1"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    75
      by (simp add: powr_realpow[symmetric] powr_divide2[symmetric] field_simps)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    76
    then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    77
      by blast
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    78
  qed
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    79
  fix e1 m1 e2 m2 :: int
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    80
  consider "e2 \<le> e1" | "e1 \<le> e2" by (rule linorder_le_cases)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    81
  then show "\<exists>(m::int) (e::int). m1 * 2 powr e1 + m2 * 2 powr e2 = m * 2 powr e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    82
  proof cases
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    83
    case 1
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    84
    from *[OF this, of m2 m1] show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    85
      by (simp add: ac_simps)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    86
  next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    87
    case 2
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    88
    then show ?thesis by (rule *)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
    89
  qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    90
qed
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
    91
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    92
lemma uminus_float[simp]: "x \<in> float \<Longrightarrow> -x \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    93
  apply (auto simp: float_def)
57492
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56777
diff changeset
    94
  apply hypsubst_thin
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
    95
  apply (rename_tac m e)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
    96
  apply (rule_tac x="-m" in exI)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
    97
  apply (rule_tac x="e" in exI)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    98
  apply (simp add: field_simps)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
    99
  done
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   100
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   101
lemma times_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> x * y \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   102
  apply (auto simp: float_def)
57492
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56777
diff changeset
   103
  apply hypsubst_thin
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   104
  apply (rename_tac mx my ex ey)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   105
  apply (rule_tac x="mx * my" in exI)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   106
  apply (rule_tac x="ex + ey" in exI)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   107
  apply (simp add: powr_add)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   108
  done
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   109
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   110
lemma minus_float[simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> x - y \<in> float"
54230
b1d955791529 more simplification rules on unary and binary minus
haftmann
parents: 53381
diff changeset
   111
  using plus_float [of x "- y"] by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   112
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
   113
lemma abs_float[simp]: "x \<in> float \<Longrightarrow> \<bar>x\<bar> \<in> float"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   114
  by (cases x rule: linorder_cases[of 0]) auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   115
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   116
lemma sgn_of_float[simp]: "x \<in> float \<Longrightarrow> sgn x \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   117
  by (cases x rule: linorder_cases[of 0]) (auto intro!: uminus_float)
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21256
diff changeset
   118
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   119
lemma div_power_2_float[simp]: "x \<in> float \<Longrightarrow> x / 2^d \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   120
  apply (auto simp add: float_def)
57492
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56777
diff changeset
   121
  apply hypsubst_thin
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   122
  apply (rename_tac m e)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   123
  apply (rule_tac x="m" in exI)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   124
  apply (rule_tac x="e - d" in exI)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   125
  apply (simp add: powr_realpow[symmetric] field_simps powr_add[symmetric])
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   126
  done
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   127
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   128
lemma div_power_2_int_float[simp]: "x \<in> float \<Longrightarrow> x / (2::int)^d \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   129
  apply (auto simp add: float_def)
57492
74bf65a1910a Hypsubst preserves equality hypotheses
Thomas Sewell <thomas.sewell@nicta.com.au>
parents: 56777
diff changeset
   130
  apply hypsubst_thin
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   131
  apply (rename_tac m e)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   132
  apply (rule_tac x="m" in exI)
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   133
  apply (rule_tac x="e - d" in exI)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   134
  apply (simp add: powr_realpow[symmetric] field_simps powr_add[symmetric])
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   135
  done
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
   136
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   137
lemma div_numeral_Bit0_float[simp]:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   138
  assumes x: "x / numeral n \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   139
  shows "x / (numeral (Num.Bit0 n)) \<in> float"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   140
proof -
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   141
  have "(x / numeral n) / 2^1 \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   142
    by (intro x div_power_2_float)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   143
  also have "(x / numeral n) / 2^1 = x / (numeral (Num.Bit0 n))"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   144
    by (induct n) auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   145
  finally show ?thesis .
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   146
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   147
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   148
lemma div_neg_numeral_Bit0_float[simp]:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   149
  assumes x: "x / numeral n \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   150
  shows "x / (- numeral (Num.Bit0 n)) \<in> float"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   151
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   152
  have "- (x / numeral (Num.Bit0 n)) \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   153
    using x by simp
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   154
  also have "- (x / numeral (Num.Bit0 n)) = x / - numeral (Num.Bit0 n)"
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   155
    by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   156
  finally show ?thesis .
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   157
qed
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
   158
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   159
lemma power_float[simp]:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   160
  assumes "a \<in> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   161
  shows "a ^ b \<in> float"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   162
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   163
  from assms obtain m e :: int where "a = m * 2 powr e"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   164
    by (auto simp: float_def)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   165
  then show ?thesis
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   166
    by (auto intro!: floatI[where m="m^b" and e = "e*b"]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   167
      simp: power_mult_distrib powr_realpow[symmetric] powr_powr)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   168
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   169
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   170
lift_definition Float :: "int \<Rightarrow> int \<Rightarrow> float" is "\<lambda>(m::int) (e::int). m * 2 powr e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   171
  by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   172
declare Float.rep_eq[simp]
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   173
62419
c7d6f4dded19 compute_real_of_float has not been used as code equation
immler
parents: 62390
diff changeset
   174
code_datatype Float
c7d6f4dded19 compute_real_of_float has not been used as code equation
immler
parents: 62390
diff changeset
   175
47780
3357688660ff add code equation for real_of_float
hoelzl
parents: 47621
diff changeset
   176
lemma compute_real_of_float[code]:
3357688660ff add code equation for real_of_float
hoelzl
parents: 47621
diff changeset
   177
  "real_of_float (Float m e) = (if e \<ge> 0 then m * 2 ^ nat e else m / 2 ^ (nat (-e)))"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   178
  by (simp add: powr_int)
47780
3357688660ff add code equation for real_of_float
hoelzl
parents: 47621
diff changeset
   179
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   180
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   181
subsection \<open>Arithmetic operations on floating point numbers\<close>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   182
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   183
instantiation float :: "{ring_1, linorder, linordered_ring, linordered_idom, numeral, equal}"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   184
begin
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   185
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   186
lift_definition zero_float :: float is 0 by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   187
declare zero_float.rep_eq[simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   188
lift_definition one_float :: float is 1 by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   189
declare one_float.rep_eq[simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   190
lift_definition plus_float :: "float \<Rightarrow> float \<Rightarrow> float" is "op +" by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   191
declare plus_float.rep_eq[simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   192
lift_definition times_float :: "float \<Rightarrow> float \<Rightarrow> float" is "op *" by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   193
declare times_float.rep_eq[simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   194
lift_definition minus_float :: "float \<Rightarrow> float \<Rightarrow> float" is "op -" by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   195
declare minus_float.rep_eq[simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   196
lift_definition uminus_float :: "float \<Rightarrow> float" is "uminus" by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   197
declare uminus_float.rep_eq[simp]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   198
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   199
lift_definition abs_float :: "float \<Rightarrow> float" is abs by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   200
declare abs_float.rep_eq[simp]
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   201
lift_definition sgn_float :: "float \<Rightarrow> float" is sgn by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   202
declare sgn_float.rep_eq[simp]
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
   203
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 54784
diff changeset
   204
lift_definition equal_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "op = :: real \<Rightarrow> real \<Rightarrow> bool" .
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   205
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 54784
diff changeset
   206
lift_definition less_eq_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "op \<le>" .
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   207
declare less_eq_float.rep_eq[simp]
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 54784
diff changeset
   208
lift_definition less_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "op <" .
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   209
declare less_float.rep_eq[simp]
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
   210
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   211
instance
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   212
  by (standard; transfer; fastforce simp add: field_simps intro: mult_left_mono mult_right_mono)+
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   213
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   214
end
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   215
61639
6ef461bee3fa new conversion theorems for int, nat to float
paulson <lp15@cam.ac.uk>
parents: 61609
diff changeset
   216
lemma real_of_float [simp]: "real_of_float (of_nat n) = of_nat n"
6ef461bee3fa new conversion theorems for int, nat to float
paulson <lp15@cam.ac.uk>
parents: 61609
diff changeset
   217
by (induct n) simp_all
6ef461bee3fa new conversion theorems for int, nat to float
paulson <lp15@cam.ac.uk>
parents: 61609
diff changeset
   218
6ef461bee3fa new conversion theorems for int, nat to float
paulson <lp15@cam.ac.uk>
parents: 61609
diff changeset
   219
lemma real_of_float_of_int_eq [simp]: "real_of_float (of_int z) = of_int z"
6ef461bee3fa new conversion theorems for int, nat to float
paulson <lp15@cam.ac.uk>
parents: 61609
diff changeset
   220
  by (cases z rule: int_diff_cases) (simp_all add: of_rat_diff)
6ef461bee3fa new conversion theorems for int, nat to float
paulson <lp15@cam.ac.uk>
parents: 61609
diff changeset
   221
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   222
lemma Float_0_eq_0[simp]: "Float 0 e = 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   223
  by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   224
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   225
lemma real_of_float_power[simp]:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   226
  fixes f :: float
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   227
  shows "real_of_float (f^n) = real_of_float f^n"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   228
  by (induct n) simp_all
45495
c55a07526dbe cleaned up float theories; removed duplicate definitions and theorems
hoelzl
parents: 44766
diff changeset
   229
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   230
lemma
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   231
  fixes x y :: float
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   232
  shows real_of_float_min: "real_of_float (min x y) = min (real_of_float x) (real_of_float y)"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   233
    and real_of_float_max: "real_of_float (max x y) = max (real_of_float x) (real_of_float y)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   234
  by (simp_all add: min_def max_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   235
53215
5e47c31c6f7c renamed typeclass dense_linorder to unbounded_dense_linorder
hoelzl
parents: 51542
diff changeset
   236
instance float :: unbounded_dense_linorder
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   237
proof
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   238
  fix a b :: float
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   239
  show "\<exists>c. a < c"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   240
    apply (intro exI[of _ "a + 1"])
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   241
    apply transfer
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   242
    apply simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   243
    done
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   244
  show "\<exists>c. c < a"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   245
    apply (intro exI[of _ "a - 1"])
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   246
    apply transfer
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   247
    apply simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   248
    done
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   249
  show "\<exists>c. a < c \<and> c < b" if "a < b"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   250
    apply (rule exI[of _ "(a + b) * Float 1 (- 1)"])
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   251
    using that
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   252
    apply transfer
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   253
    apply (simp add: powr_minus)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   254
    done
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   255
qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   256
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   257
instantiation float :: lattice_ab_group_add
46573
8c4c5c8dcf7a misc tuning;
wenzelm
parents: 46028
diff changeset
   258
begin
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   259
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   260
definition inf_float :: "float \<Rightarrow> float \<Rightarrow> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   261
  where "inf_float a b = min a b"
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   262
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   263
definition sup_float :: "float \<Rightarrow> float \<Rightarrow> float"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   264
  where "sup_float a b = max a b"
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   265
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   266
instance
60679
ade12ef2773c tuned proofs;
wenzelm
parents: 60500
diff changeset
   267
  by (standard; transfer; simp add: inf_float_def sup_float_def real_of_float_min real_of_float_max)
ade12ef2773c tuned proofs;
wenzelm
parents: 60500
diff changeset
   268
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   269
end
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   270
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   271
lemma float_numeral[simp]: "real_of_float (numeral x :: float) = numeral x"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   272
  apply (induct x)
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   273
  apply simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   274
  apply (simp_all only: numeral_Bit0 numeral_Bit1 real_of_float_eq float_of_inverse
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   275
                  plus_float.rep_eq one_float.rep_eq plus_float numeral_float one_float)
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   276
  done
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   277
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
   278
lemma transfer_numeral [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55565
diff changeset
   279
  "rel_fun (op =) pcr_float (numeral :: _ \<Rightarrow> real) (numeral :: _ \<Rightarrow> float)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   280
  by (simp add: rel_fun_def float.pcr_cr_eq cr_float_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   281
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   282
lemma float_neg_numeral[simp]: "real_of_float (- numeral x :: float) = - numeral x"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   283
  by simp
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46670
diff changeset
   284
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
   285
lemma transfer_neg_numeral [transfer_rule]:
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 55565
diff changeset
   286
  "rel_fun (op =) pcr_float (- numeral :: _ \<Rightarrow> real) (- numeral :: _ \<Rightarrow> float)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   287
  by (simp add: rel_fun_def float.pcr_cr_eq cr_float_def)
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   288
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   289
lemma
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   290
  shows float_of_numeral[simp]: "numeral k = float_of (numeral k)"
54489
03ff4d1e6784 eliminiated neg_numeral in favour of - (numeral _)
haftmann
parents: 54230
diff changeset
   291
    and float_of_neg_numeral[simp]: "- numeral k = float_of (- numeral k)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   292
  unfolding real_of_float_eq by simp_all
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46670
diff changeset
   293
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   294
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   295
subsection \<open>Quickcheck\<close>
58987
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   296
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   297
instantiation float :: exhaustive
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   298
begin
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   299
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   300
definition exhaustive_float where
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   301
  "exhaustive_float f d =
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   302
    Quickcheck_Exhaustive.exhaustive (%x. Quickcheck_Exhaustive.exhaustive (%y. f (Float x y)) d) d"
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   303
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   304
instance ..
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   305
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   306
end
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   307
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   308
definition (in term_syntax) [code_unfold]:
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   309
  "valtermify_float x y = Code_Evaluation.valtermify Float {\<cdot>} x {\<cdot>} y"
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   310
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   311
instantiation float :: full_exhaustive
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   312
begin
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   313
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   314
definition full_exhaustive_float where
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   315
  "full_exhaustive_float f d =
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   316
    Quickcheck_Exhaustive.full_exhaustive
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   317
      (\<lambda>x. Quickcheck_Exhaustive.full_exhaustive (\<lambda>y. f (valtermify_float x y)) d) d"
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   318
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   319
instance ..
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   320
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   321
end
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   322
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   323
instantiation float :: random
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   324
begin
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   325
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   326
definition "Quickcheck_Random.random i =
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   327
  scomp (Quickcheck_Random.random (2 ^ nat_of_natural i))
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   328
    (\<lambda>man. scomp (Quickcheck_Random.random i) (\<lambda>exp. Pair (valtermify_float man exp)))"
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   329
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   330
instance ..
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   331
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   332
end
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   333
119680ebf37c quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
immler
parents: 58985
diff changeset
   334
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   335
subsection \<open>Represent floats as unique mantissa and exponent\<close>
47108
2a1953f0d20d merged fork with new numeral representation (see NEWS)
huffman
parents: 46670
diff changeset
   336
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   337
lemma int_induct_abs[case_names less]:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   338
  fixes j :: int
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   339
  assumes H: "\<And>n. (\<And>i. \<bar>i\<bar> < \<bar>n\<bar> \<Longrightarrow> P i) \<Longrightarrow> P n"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   340
  shows "P j"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   341
proof (induct "nat \<bar>j\<bar>" arbitrary: j rule: less_induct)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   342
  case less
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   343
  show ?case by (rule H[OF less]) simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   344
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   345
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   346
lemma int_cancel_factors:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   347
  fixes n :: int
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   348
  assumes "1 < r"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   349
  shows "n = 0 \<or> (\<exists>k i. n = k * r ^ i \<and> \<not> r dvd k)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   350
proof (induct n rule: int_induct_abs)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   351
  case (less n)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   352
  have "\<exists>k i. n = k * r ^ Suc i \<and> \<not> r dvd k" if "n \<noteq> 0" "n = m * r" for m
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   353
  proof -
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   354
    from that have "\<bar>m \<bar> < \<bar>n\<bar>"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   355
      using \<open>1 < r\<close> by (simp add: abs_mult)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   356
    from less[OF this] that show ?thesis by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   357
  qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   358
  then show ?case
59554
4044f53326c9 inlined rules to free user-space from technical names
haftmann
parents: 59487
diff changeset
   359
    by (metis dvd_def monoid_mult_class.mult.right_neutral mult.commute power_0)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   360
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   361
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   362
lemma mult_powr_eq_mult_powr_iff_asym:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   363
  fixes m1 m2 e1 e2 :: int
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   364
  assumes m1: "\<not> 2 dvd m1"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   365
    and "e1 \<le> e2"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   366
  shows "m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   367
  (is "?lhs \<longleftrightarrow> ?rhs")
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   368
proof
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   369
  show ?rhs if eq: ?lhs
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   370
  proof -
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   371
    have "m1 \<noteq> 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   372
      using m1 unfolding dvd_def by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   373
    from \<open>e1 \<le> e2\<close> eq have "m1 = m2 * 2 powr nat (e2 - e1)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   374
      by (simp add: powr_divide2[symmetric] field_simps)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   375
    also have "\<dots> = m2 * 2^nat (e2 - e1)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   376
      by (simp add: powr_realpow)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   377
    finally have m1_eq: "m1 = m2 * 2^nat (e2 - e1)"
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61639
diff changeset
   378
      by linarith
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   379
    with m1 have "m1 = m2"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   380
      by (cases "nat (e2 - e1)") (auto simp add: dvd_def)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   381
    then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   382
      using eq \<open>m1 \<noteq> 0\<close> by (simp add: powr_inj)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   383
  qed
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   384
  show ?lhs if ?rhs
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   385
    using that by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   386
qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   387
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   388
lemma mult_powr_eq_mult_powr_iff:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   389
  fixes m1 m2 e1 e2 :: int
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   390
  shows "\<not> 2 dvd m1 \<Longrightarrow> \<not> 2 dvd m2 \<Longrightarrow> m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   391
  using mult_powr_eq_mult_powr_iff_asym[of m1 e1 e2 m2]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   392
  using mult_powr_eq_mult_powr_iff_asym[of m2 e2 e1 m1]
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   393
  by (cases e1 e2 rule: linorder_le_cases) auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   394
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   395
lemma floatE_normed:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   396
  assumes x: "x \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   397
  obtains (zero) "x = 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   398
   | (powr) m e :: int where "x = m * 2 powr e" "\<not> 2 dvd m" "x \<noteq> 0"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   399
proof -
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   400
  {
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   401
    assume "x \<noteq> 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   402
    from x obtain m e :: int where x: "x = m * 2 powr e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   403
      by (auto simp: float_def)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   404
    with \<open>x \<noteq> 0\<close> int_cancel_factors[of 2 m] obtain k i where "m = k * 2 ^ i" "\<not> 2 dvd k"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   405
      by auto
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   406
    with \<open>\<not> 2 dvd k\<close> x have "\<exists>(m::int) (e::int). x = m * 2 powr e \<and> \<not> (2::int) dvd m"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   407
      by (rule_tac exI[of _ "k"], rule_tac exI[of _ "e + int i"])
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   408
        (simp add: powr_add powr_realpow)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   409
  }
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   410
  with that show thesis by blast
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   411
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   412
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   413
lemma float_normed_cases:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   414
  fixes f :: float
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   415
  obtains (zero) "f = 0"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   416
   | (powr) m e :: int where "real_of_float f = m * 2 powr e" "\<not> 2 dvd m" "f \<noteq> 0"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   417
proof (atomize_elim, induct f)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   418
  case (float_of y)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   419
  then show ?case
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   420
    by (cases rule: floatE_normed) (auto simp: zero_float_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   421
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   422
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   423
definition mantissa :: "float \<Rightarrow> int" where
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   424
  "mantissa f = fst (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   425
   \<or> (f \<noteq> 0 \<and> real_of_float f = real_of_int (fst p) * 2 powr real_of_int (snd p) \<and> \<not> 2 dvd fst p))"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   426
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   427
definition exponent :: "float \<Rightarrow> int" where
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   428
  "exponent f = snd (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   429
   \<or> (f \<noteq> 0 \<and> real_of_float f = real_of_int (fst p) * 2 powr real_of_int (snd p) \<and> \<not> 2 dvd fst p))"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   430
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
   431
lemma
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   432
  shows exponent_0[simp]: "exponent (float_of 0) = 0" (is ?E)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   433
    and mantissa_0[simp]: "mantissa (float_of 0) = 0" (is ?M)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   434
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   435
  have "\<And>p::int \<times> int. fst p = 0 \<and> snd p = 0 \<longleftrightarrow> p = (0, 0)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   436
    by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   437
  then show ?E ?M
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   438
    by (auto simp add: mantissa_def exponent_def zero_float_def)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   439
qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   440
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   441
lemma
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   442
  shows mantissa_exponent: "real_of_float f = mantissa f * 2 powr exponent f" (is ?E)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   443
    and mantissa_not_dvd: "f \<noteq> (float_of 0) \<Longrightarrow> \<not> 2 dvd mantissa f" (is "_ \<Longrightarrow> ?D")
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   444
proof cases
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   445
  assume [simp]: "f \<noteq> float_of 0"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   446
  have "f = mantissa f * 2 powr exponent f \<and> \<not> 2 dvd mantissa f"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   447
  proof (cases f rule: float_normed_cases)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   448
    case zero
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   449
    then show ?thesis by  (simp add: zero_float_def)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   450
  next
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   451
    case (powr m e)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   452
    then have "\<exists>p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0) \<or>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   453
      (f \<noteq> 0 \<and> real_of_float f = real_of_int (fst p) * 2 powr real_of_int (snd p) \<and> \<not> 2 dvd fst p)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   454
      by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   455
    then show ?thesis
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   456
      unfolding exponent_def mantissa_def
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   457
      by (rule someI2_ex) (simp add: zero_float_def)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   458
  qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   459
  then show ?E ?D by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   460
qed simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   461
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   462
lemma mantissa_noteq_0: "f \<noteq> float_of 0 \<Longrightarrow> mantissa f \<noteq> 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   463
  using mantissa_not_dvd[of f] by auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   464
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
   465
lemma
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   466
  fixes m e :: int
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   467
  defines "f \<equiv> float_of (m * 2 powr e)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   468
  assumes dvd: "\<not> 2 dvd m"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   469
  shows mantissa_float: "mantissa f = m" (is "?M")
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   470
    and exponent_float: "m \<noteq> 0 \<Longrightarrow> exponent f = e" (is "_ \<Longrightarrow> ?E")
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   471
proof cases
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   472
  assume "m = 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   473
  with dvd show "mantissa f = m" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   474
next
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   475
  assume "m \<noteq> 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   476
  then have f_not_0: "f \<noteq> float_of 0" by (simp add: f_def)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   477
  from mantissa_exponent[of f] have "m * 2 powr e = mantissa f * 2 powr exponent f"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   478
    by (auto simp add: f_def)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   479
  then show "?M" "?E"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   480
    using mantissa_not_dvd[OF f_not_0] dvd
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   481
    by (auto simp: mult_powr_eq_mult_powr_iff)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   482
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   483
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   484
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   485
subsection \<open>Compute arithmetic operations\<close>
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   486
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   487
lemma Float_mantissa_exponent: "Float (mantissa f) (exponent f) = f"
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   488
  unfolding real_of_float_eq mantissa_exponent[of f] by simp
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   489
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   490
lemma Float_cases [cases type: float]:
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   491
  fixes f :: float
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   492
  obtains (Float) m e :: int where "f = Float m e"
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   493
  using Float_mantissa_exponent[symmetric]
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   494
  by (atomize_elim) auto
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   495
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   496
lemma denormalize_shift:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   497
  assumes f_def: "f \<equiv> Float m e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   498
    and not_0: "f \<noteq> float_of 0"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   499
  obtains i where "m = mantissa f * 2 ^ i" "e = exponent f - i"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   500
proof
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   501
  from mantissa_exponent[of f] f_def
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   502
  have "m * 2 powr e = mantissa f * 2 powr exponent f"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   503
    by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   504
  then have eq: "m = mantissa f * 2 powr (exponent f - e)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   505
    by (simp add: powr_divide2[symmetric] field_simps)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   506
  moreover
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   507
  have "e \<le> exponent f"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   508
  proof (rule ccontr)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   509
    assume "\<not> e \<le> exponent f"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   510
    then have pos: "exponent f < e" by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   511
    then have "2 powr (exponent f - e) = 2 powr - real_of_int (e - exponent f)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   512
      by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   513
    also have "\<dots> = 1 / 2^nat (e - exponent f)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   514
      using pos by (simp add: powr_realpow[symmetric] powr_divide2[symmetric])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   515
    finally have "m * 2^nat (e - exponent f) = real_of_int (mantissa f)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   516
      using eq by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   517
    then have "mantissa f = m * 2^nat (e - exponent f)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   518
      by linarith
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   519
    with \<open>exponent f < e\<close> have "2 dvd mantissa f"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   520
      apply (intro dvdI[where k="m * 2^(nat (e-exponent f)) div 2"])
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   521
      apply (cases "nat (e - exponent f)")
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   522
      apply auto
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   523
      done
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   524
    then show False using mantissa_not_dvd[OF not_0] by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   525
  qed
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   526
  ultimately have "real_of_int m = mantissa f * 2^nat (exponent f - e)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   527
    by (simp add: powr_realpow[symmetric])
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   528
  with \<open>e \<le> exponent f\<close>
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61639
diff changeset
   529
  show "m = mantissa f * 2 ^ nat (exponent f - e)" 
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61639
diff changeset
   530
    by linarith
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61639
diff changeset
   531
  show "e = exponent f - nat (exponent f - e)"
61799
4cf66f21b764 isabelle update_cartouches -c -t;
wenzelm
parents: 61762
diff changeset
   532
    using \<open>e \<le> exponent f\<close> by auto
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   533
qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   534
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   535
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   536
begin
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   537
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   538
qualified lemma compute_float_zero[code_unfold, code]: "0 = Float 0 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   539
  by transfer simp
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   540
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   541
qualified lemma compute_float_one[code_unfold, code]: "1 = Float 1 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   542
  by transfer simp
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   543
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   544
lift_definition normfloat :: "float \<Rightarrow> float" is "\<lambda>x. x" .
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   545
lemma normloat_id[simp]: "normfloat x = x" by transfer rule
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   546
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   547
qualified lemma compute_normfloat[code]: "normfloat (Float m e) =
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   548
  (if m mod 2 = 0 \<and> m \<noteq> 0 then normfloat (Float (m div 2) (e + 1))
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   549
                           else if m = 0 then 0 else Float m e)"
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   550
  by transfer (auto simp add: powr_add zmod_eq_0_iff)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   551
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   552
qualified lemma compute_float_numeral[code_abbrev]: "Float (numeral k) 0 = numeral k"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   553
  by transfer simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   554
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   555
qualified lemma compute_float_neg_numeral[code_abbrev]: "Float (- numeral k) 0 = - numeral k"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   556
  by transfer simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   557
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   558
qualified lemma compute_float_uminus[code]: "- Float m1 e1 = Float (- m1) e1"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   559
  by transfer simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   560
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   561
qualified lemma compute_float_times[code]: "Float m1 e1 * Float m2 e2 = Float (m1 * m2) (e1 + e2)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   562
  by transfer (simp add: field_simps powr_add)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   563
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   564
qualified lemma compute_float_plus[code]: "Float m1 e1 + Float m2 e2 =
54783
25860d89a044 Float: prevent unnecessary large numbers when adding 0
immler
parents: 54782
diff changeset
   565
  (if m1 = 0 then Float m2 e2 else if m2 = 0 then Float m1 e1 else
25860d89a044 Float: prevent unnecessary large numbers when adding 0
immler
parents: 54782
diff changeset
   566
  if e1 \<le> e2 then Float (m1 + m2 * 2^nat (e2 - e1)) e1
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   567
              else Float (m2 + m1 * 2^nat (e1 - e2)) e2)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   568
  by transfer (simp add: field_simps powr_realpow[symmetric] powr_divide2[symmetric])
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   569
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   570
qualified lemma compute_float_minus[code]: fixes f g::float shows "f - g = f + (-g)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   571
  by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   572
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   573
qualified lemma compute_float_sgn[code]: "sgn (Float m1 e1) = (if 0 < m1 then 1 else if m1 < 0 then -1 else 0)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   574
  by transfer (simp add: sgn_times)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   575
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 54784
diff changeset
   576
lift_definition is_float_pos :: "float \<Rightarrow> bool" is "op < 0 :: real \<Rightarrow> bool" .
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   577
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   578
qualified lemma compute_is_float_pos[code]: "is_float_pos (Float m e) \<longleftrightarrow> 0 < m"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   579
  by transfer (auto simp add: zero_less_mult_iff not_le[symmetric, of _ 0])
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   580
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 54784
diff changeset
   581
lift_definition is_float_nonneg :: "float \<Rightarrow> bool" is "op \<le> 0 :: real \<Rightarrow> bool" .
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   582
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   583
qualified lemma compute_is_float_nonneg[code]: "is_float_nonneg (Float m e) \<longleftrightarrow> 0 \<le> m"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   584
  by transfer (auto simp add: zero_le_mult_iff not_less[symmetric, of _ 0])
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   585
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 54784
diff changeset
   586
lift_definition is_float_zero :: "float \<Rightarrow> bool"  is "op = 0 :: real \<Rightarrow> bool" .
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   587
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   588
qualified lemma compute_is_float_zero[code]: "is_float_zero (Float m e) \<longleftrightarrow> 0 = m"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   589
  by transfer (auto simp add: is_float_zero_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   590
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
   591
qualified lemma compute_float_abs[code]: "\<bar>Float m e\<bar> = Float \<bar>m\<bar> e"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   592
  by transfer (simp add: abs_mult)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   593
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   594
qualified lemma compute_float_eq[code]: "equal_class.equal f g = is_float_zero (f - g)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   595
  by transfer simp
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   596
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   597
end
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   598
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   599
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   600
subsection \<open>Lemmas for types @{typ real}, @{typ nat}, @{typ int}\<close>
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   601
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   602
lemmas real_of_ints =
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   603
  of_int_add
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   604
  of_int_minus
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   605
  of_int_diff
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   606
  of_int_mult
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   607
  of_int_power
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   608
  of_int_numeral of_int_neg_numeral
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   609
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   610
lemmas int_of_reals = real_of_ints[symmetric]
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   611
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   612
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   613
subsection \<open>Rounding Real Numbers\<close>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   614
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   615
definition round_down :: "int \<Rightarrow> real \<Rightarrow> real"
61942
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
   616
  where "round_down prec x = \<lfloor>x * 2 powr prec\<rfloor> * 2 powr -prec"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   617
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   618
definition round_up :: "int \<Rightarrow> real \<Rightarrow> real"
61942
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
   619
  where "round_up prec x = \<lceil>x * 2 powr prec\<rceil> * 2 powr -prec"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   620
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   621
lemma round_down_float[simp]: "round_down prec x \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   622
  unfolding round_down_def
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   623
  by (auto intro!: times_float simp: of_int_minus[symmetric] simp del: of_int_minus)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   624
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   625
lemma round_up_float[simp]: "round_up prec x \<in> float"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   626
  unfolding round_up_def
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   627
  by (auto intro!: times_float simp: of_int_minus[symmetric] simp del: of_int_minus)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   628
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   629
lemma round_up: "x \<le> round_up prec x"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   630
  by (simp add: powr_minus_divide le_divide_eq round_up_def ceiling_correct)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   631
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   632
lemma round_down: "round_down prec x \<le> x"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   633
  by (simp add: powr_minus_divide divide_le_eq round_down_def)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   634
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   635
lemma round_up_0[simp]: "round_up p 0 = 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   636
  unfolding round_up_def by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   637
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   638
lemma round_down_0[simp]: "round_down p 0 = 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   639
  unfolding round_down_def by simp
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   640
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   641
lemma round_up_diff_round_down:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   642
  "round_up prec x - round_down prec x \<le> 2 powr -prec"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   643
proof -
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   644
  have "round_up prec x - round_down prec x =
61942
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
   645
    (\<lceil>x * 2 powr prec\<rceil> - \<lfloor>x * 2 powr prec\<rfloor>) * 2 powr -prec"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   646
    by (simp add: round_up_def round_down_def field_simps)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   647
  also have "\<dots> \<le> 1 * 2 powr -prec"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   648
    by (rule mult_mono)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   649
       (auto simp del: of_int_diff
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   650
             simp: of_int_diff[symmetric] ceiling_diff_floor_le_1)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   651
  finally show ?thesis by simp
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   652
qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   653
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   654
lemma round_down_shift: "round_down p (x * 2 powr k) = 2 powr k * round_down (p + k) x"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   655
  unfolding round_down_def
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   656
  by (simp add: powr_add powr_mult field_simps powr_divide2[symmetric])
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   657
    (simp add: powr_add[symmetric])
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   658
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   659
lemma round_up_shift: "round_up p (x * 2 powr k) = 2 powr k * round_up (p + k) x"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   660
  unfolding round_up_def
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   661
  by (simp add: powr_add powr_mult field_simps powr_divide2[symmetric])
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   662
    (simp add: powr_add[symmetric])
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   663
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   664
lemma round_up_uminus_eq: "round_up p (-x) = - round_down p x"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   665
  and round_down_uminus_eq: "round_down p (-x) = - round_up p x"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   666
  by (auto simp: round_up_def round_down_def ceiling_def)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   667
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   668
lemma round_up_mono: "x \<le> y \<Longrightarrow> round_up p x \<le> round_up p y"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   669
  by (auto intro!: ceiling_mono simp: round_up_def)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   670
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   671
lemma round_up_le1:
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   672
  assumes "x \<le> 1" "prec \<ge> 0"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   673
  shows "round_up prec x \<le> 1"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   674
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   675
  have "real_of_int \<lceil>x * 2 powr prec\<rceil> \<le> real_of_int \<lceil>2 powr real_of_int prec\<rceil>"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   676
    using assms by (auto intro!: ceiling_mono)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   677
  also have "\<dots> = 2 powr prec" using assms by (auto simp: powr_int intro!: exI[where x="2^nat prec"])
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   678
  finally show ?thesis
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   679
    by (simp add: round_up_def) (simp add: powr_minus inverse_eq_divide)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   680
qed
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   681
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   682
lemma round_up_less1:
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   683
  assumes "x < 1 / 2" "p > 0"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   684
  shows "round_up p x < 1"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   685
proof -
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   686
  have "x * 2 powr p < 1 / 2 * 2 powr p"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   687
    using assms by simp
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   688
  also have "\<dots> \<le> 2 powr p - 1" using \<open>p > 0\<close>
58989
99831590def5 tuned proofs
immler
parents: 58987
diff changeset
   689
    by (auto simp: powr_divide2[symmetric] powr_int field_simps self_le_power)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   690
  finally show ?thesis using \<open>p > 0\<close>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   691
    by (simp add: round_up_def field_simps powr_minus powr_int ceiling_less_iff)
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   692
qed
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   693
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   694
lemma round_down_ge1:
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   695
  assumes x: "x \<ge> 1"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   696
  assumes prec: "p \<ge> - log 2 x"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   697
  shows "1 \<le> round_down p x"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   698
proof cases
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   699
  assume nonneg: "0 \<le> p"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   700
  have "2 powr p = real_of_int \<lfloor>2 powr real_of_int p\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   701
    using nonneg by (auto simp: powr_int)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   702
  also have "\<dots> \<le> real_of_int \<lfloor>x * 2 powr p\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   703
    using assms by (auto intro!: floor_mono)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   704
  finally show ?thesis
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   705
    by (simp add: round_down_def) (simp add: powr_minus inverse_eq_divide)
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   706
next
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   707
  assume neg: "\<not> 0 \<le> p"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   708
  have "x = 2 powr (log 2 x)"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   709
    using x by simp
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   710
  also have "2 powr (log 2 x) \<ge> 2 powr - p"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   711
    using prec by auto
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   712
  finally have x_le: "x \<ge> 2 powr -p" .
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   713
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   714
  from neg have "2 powr real_of_int p \<le> 2 powr 0"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   715
    by (intro powr_mono) auto
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
   716
  also have "\<dots> \<le> \<lfloor>2 powr 0::real\<rfloor>" by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   717
  also have "\<dots> \<le> \<lfloor>x * 2 powr (real_of_int p)\<rfloor>"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   718
    unfolding of_int_le_iff
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   719
    using x x_le by (intro floor_mono) (simp add: powr_minus_divide field_simps)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   720
  finally show ?thesis
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   721
    using prec x
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   722
    by (simp add: round_down_def powr_minus_divide pos_le_divide_eq)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   723
qed
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   724
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   725
lemma round_up_le0: "x \<le> 0 \<Longrightarrow> round_up p x \<le> 0"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   726
  unfolding round_up_def
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   727
  by (auto simp: field_simps mult_le_0_iff zero_le_mult_iff)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   728
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   729
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   730
subsection \<open>Rounding Floats\<close>
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   731
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   732
definition div_twopow :: "int \<Rightarrow> nat \<Rightarrow> int"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   733
  where [simp]: "div_twopow x n = x div (2 ^ n)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   734
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   735
definition mod_twopow :: "int \<Rightarrow> nat \<Rightarrow> int"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   736
  where [simp]: "mod_twopow x n = x mod (2 ^ n)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   737
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   738
lemma compute_div_twopow[code]:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   739
  "div_twopow x n = (if x = 0 \<or> x = -1 \<or> n = 0 then x else div_twopow (x div 2) (n - 1))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   740
  by (cases n) (auto simp: zdiv_zmult2_eq div_eq_minus1)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   741
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   742
lemma compute_mod_twopow[code]:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   743
  "mod_twopow x n = (if n = 0 then 0 else x mod 2 + 2 * mod_twopow (x div 2) (n - 1))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   744
  by (cases n) (auto simp: zmod_zmult2_eq)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   745
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   746
lift_definition float_up :: "int \<Rightarrow> float \<Rightarrow> float" is round_up by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   747
declare float_up.rep_eq[simp]
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   748
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   749
lemma round_up_correct: "round_up e f - f \<in> {0..2 powr -e}"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   750
  unfolding atLeastAtMost_iff
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   751
proof
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   752
  have "round_up e f - f \<le> round_up e f - round_down e f"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   753
    using round_down by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   754
  also have "\<dots> \<le> 2 powr -e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   755
    using round_up_diff_round_down by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   756
  finally show "round_up e f - f \<le> 2 powr - (real_of_int e)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   757
    by simp
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   758
qed (simp add: algebra_simps round_up)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   759
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   760
lemma float_up_correct: "real_of_float (float_up e f) - real_of_float f \<in> {0..2 powr -e}"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   761
  by transfer (rule round_up_correct)
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   762
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   763
lift_definition float_down :: "int \<Rightarrow> float \<Rightarrow> float" is round_down by simp
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
   764
declare float_down.rep_eq[simp]
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
   765
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   766
lemma round_down_correct: "f - (round_down e f) \<in> {0..2 powr -e}"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   767
  unfolding atLeastAtMost_iff
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   768
proof
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   769
  have "f - round_down e f \<le> round_up e f - round_down e f"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   770
    using round_up by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   771
  also have "\<dots> \<le> 2 powr -e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   772
    using round_up_diff_round_down by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   773
  finally show "f - round_down e f \<le> 2 powr - (real_of_int e)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   774
    by simp
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   775
qed (simp add: algebra_simps round_down)
24301
6c7f226b24c3 changed floatarith lemmas
obua
parents: 24124
diff changeset
   776
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   777
lemma float_down_correct: "real_of_float f - real_of_float (float_down e f) \<in> {0..2 powr -e}"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   778
  by transfer (rule round_down_correct)
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   779
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   780
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   781
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   782
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   783
qualified lemma compute_float_down[code]:
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   784
  "float_down p (Float m e) =
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
   785
    (if p + e < 0 then Float (div_twopow m (nat (-(p + e)))) (-p) else Float m e)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   786
proof (cases "p + e < 0")
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   787
  case True
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   788
  then have "real_of_int ((2::int) ^ nat (-(p + e))) = 2 powr (-(p + e))"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   789
    using powr_realpow[of 2 "nat (-(p + e))"] by simp
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   790
  also have "\<dots> = 1 / 2 powr p / 2 powr e"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   791
    unfolding powr_minus_divide of_int_minus by (simp add: powr_add)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   792
  finally show ?thesis
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   793
    using \<open>p + e < 0\<close>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   794
    apply transfer
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   795
    apply  (simp add: ac_simps round_down_def floor_divide_of_int_eq[symmetric])
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   796
    proof - (*FIXME*)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   797
      fix pa :: int and ea :: int and ma :: int
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   798
      assume a1: "2 ^ nat (- pa - ea) = 1 / (2 powr real_of_int pa * 2 powr real_of_int ea)"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   799
      assume "pa + ea < 0"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   800
      have "\<lfloor>real_of_int ma / real_of_int (int 2 ^ nat (- (pa + ea)))\<rfloor> = \<lfloor>real_of_float (Float ma (pa + ea))\<rfloor>"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   801
        using a1 by (simp add: powr_add)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   802
      thus "\<lfloor>real_of_int ma * (2 powr real_of_int pa * 2 powr real_of_int ea)\<rfloor> = ma div 2 ^ nat (- pa - ea)"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   803
        by (metis Float.rep_eq add_uminus_conv_diff floor_divide_of_int_eq minus_add_distrib of_int_simps(3) of_nat_numeral powr_add)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   804
    qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   805
next
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   806
  case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   807
  then have r: "real_of_int e + real_of_int p = real (nat (e + p))" by simp
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   808
  have r: "\<lfloor>(m * 2 powr e) * 2 powr real_of_int p\<rfloor> = (m * 2 powr e) * 2 powr real_of_int p"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   809
    by (auto intro: exI[where x="m*2^nat (e+p)"]
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
   810
             simp add: ac_simps powr_add[symmetric] r powr_realpow)
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
   811
  with \<open>\<not> p + e < 0\<close> show ?thesis
57862
8f074e6e22fc tuned proofs;
wenzelm
parents: 57512
diff changeset
   812
    by transfer (auto simp add: round_down_def field_simps powr_add powr_minus)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   813
qed
24301
6c7f226b24c3 changed floatarith lemmas
obua
parents: 24124
diff changeset
   814
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   815
lemma abs_round_down_le: "\<bar>f - (round_down e f)\<bar> \<le> 2 powr -e"
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   816
  using round_down_correct[of f e] by simp
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   817
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   818
lemma abs_round_up_le: "\<bar>f - (round_up e f)\<bar> \<le> 2 powr -e"
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   819
  using round_up_correct[of e f] by simp
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   820
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   821
lemma round_down_nonneg: "0 \<le> s \<Longrightarrow> 0 \<le> round_down p s"
56536
aefb4a8da31f made mult_nonneg_nonneg a simp rule
nipkow
parents: 56479
diff changeset
   822
  by (auto simp: round_down_def)
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
   823
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   824
lemma ceil_divide_floor_conv:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   825
  assumes "b \<noteq> 0"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   826
  shows "\<lceil>real_of_int a / real_of_int b\<rceil> = (if b dvd a then a div b else \<lfloor>real_of_int a / real_of_int b\<rfloor> + 1)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   827
proof (cases "b dvd a")
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   828
  case True
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   829
  then show ?thesis
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   830
    by (simp add: ceiling_def of_int_minus[symmetric] divide_minus_left[symmetric]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   831
      floor_divide_of_int_eq dvd_neg_div del: divide_minus_left of_int_minus)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   832
next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   833
  case False
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   834
  then have "a mod b \<noteq> 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   835
    by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   836
  then have ne: "real_of_int (a mod b) / real_of_int b \<noteq> 0"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   837
    using \<open>b \<noteq> 0\<close> by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   838
  have "\<lceil>real_of_int a / real_of_int b\<rceil> = \<lfloor>real_of_int a / real_of_int b\<rfloor> + 1"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   839
    apply (rule ceiling_eq)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   840
    apply (auto simp: floor_divide_of_int_eq[symmetric])
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   841
  proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   842
    have "real_of_int \<lfloor>real_of_int a / real_of_int b\<rfloor> \<le> real_of_int a / real_of_int b"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   843
      by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   844
    moreover have "real_of_int \<lfloor>real_of_int a / real_of_int b\<rfloor> \<noteq> real_of_int a / real_of_int b"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   845
      apply (subst (2) real_of_int_div_aux)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   846
      unfolding floor_divide_of_int_eq
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   847
      using ne \<open>b \<noteq> 0\<close> apply auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   848
      done
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
   849
    ultimately show "real_of_int \<lfloor>real_of_int a / real_of_int b\<rfloor> < real_of_int a / real_of_int b" by arith
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   850
  qed
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   851
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   852
    using \<open>\<not> b dvd a\<close> by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   853
qed
19765
dfe940911617 misc cleanup;
wenzelm
parents: 16890
diff changeset
   854
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   855
qualified lemma compute_float_up[code]: "float_up p x = - float_down p (-x)"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
   856
  by transfer (simp add: round_down_uminus_eq)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   857
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   858
end
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   859
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   860
definition floorlog :: "nat \<Rightarrow> nat \<Rightarrow> nat"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   861
  where "floorlog b a = (if a > 0 \<and> b > 1 then nat \<lfloor>log b a\<rfloor> + 1 else 0)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   862
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   863
lemma floorlog_nonneg: "0 \<le> floorlog b x"
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
   864
proof -
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   865
  have "-1 < log b (-x)" if "0 > x" "b > 1"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   866
  proof -
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   867
    have "-1 = log b (inverse b)" using that
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   868
      by (subst log_inverse) simp_all
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   869
    also have "\<dots> < log b (-x)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   870
      using \<open>0 > x\<close> by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   871
    finally show ?thesis .
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   872
  qed
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
   873
  then show ?thesis
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   874
    unfolding floorlog_def by (auto intro!: add_nonneg_nonneg)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   875
qed
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   876
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   877
lemma floorlog_bounds:
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   878
  assumes "x > 0" "b > 1"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   879
  shows "b ^ (floorlog b x - 1) \<le> x \<and> x < b ^ (floorlog b x)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   880
proof
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   881
  show "b ^ (floorlog b x - 1) \<le> x"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   882
  proof -
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   883
    have "(b::real) ^ nat \<lfloor>log b (real_of_int x)\<rfloor> = b powr real_of_int \<lfloor>log b (real_of_int x)\<rfloor>"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   884
      using powr_realpow[symmetric, of b "nat \<lfloor>log b (real_of_int x)\<rfloor>"] \<open>x > 0\<close> \<open>b > 1\<close>
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   885
      by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   886
    also have "\<dots> \<le> b powr log b (real_of_int x)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   887
      using \<open>b > 1\<close>
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   888
      by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   889
    also have "\<dots> = real_of_int x"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   890
      using \<open>0 < x\<close> \<open>b > 1\<close> by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   891
    finally have "b ^ nat \<lfloor>log b (real_of_int x)\<rfloor> \<le> real_of_int x"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   892
      by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   893
    then show ?thesis
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   894
      using \<open>0 < x\<close> \<open>b > 1\<close> of_nat_le_iff
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   895
      by (fastforce simp add: floorlog_def)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   896
  qed
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   897
  show "x < b ^ (floorlog b x)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   898
  proof -
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   899
    have "x \<le> b powr (log b x)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   900
      using \<open>x > 0\<close> \<open>b > 1\<close> by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   901
    also have "\<dots> < b powr (\<lfloor>log b x\<rfloor> + 1)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   902
      using assms
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   903
      by (intro powr_less_mono) auto
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   904
    also have "\<dots> = b ^ nat (\<lfloor>log b (real_of_int x)\<rfloor> + 1)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   905
      using assms
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   906
      by (simp add: powr_realpow[symmetric])
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   907
    finally
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   908
    have "x < b ^ nat (\<lfloor>log (real b) (real_of_int (int x))\<rfloor> + 1)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   909
      by (rule of_nat_less_imp_less)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   910
    then show ?thesis
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   911
      using \<open>x > 0\<close> \<open>b > 1\<close>
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   912
      by (simp add: floorlog_def nat_add_distrib)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   913
  qed
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   914
qed
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   915
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   916
lemma floorlog_power[simp]:
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   917
  assumes "a > 0" "b > 1"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   918
  shows "floorlog b (a * b ^ c) = floorlog b a + c"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   919
proof -
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   920
  have "\<lfloor>log (real b) (real a) + real c\<rfloor> = \<lfloor>log (real b) (real a)\<rfloor> + c"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   921
    by arith
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   922
  then show ?thesis using assms
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   923
    by (auto simp: floorlog_def log_mult powr_realpow[symmetric] nat_add_distrib)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   924
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   925
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   926
lemma
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   927
  floor_log_add_eqI:
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   928
  fixes a::nat and b::nat and r::real
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   929
  assumes "b > 1"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   930
  assumes "a \<ge> 1" "0 \<le> r" "r < 1"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   931
  shows "\<lfloor>log b (a + r)\<rfloor> = \<lfloor>log b a\<rfloor>"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   932
proof (rule floor_eq2)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   933
  have "log (real b) (real a) \<le> log (real b) (real a + r)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   934
    using assms by force
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   935
  then show "\<lfloor>log (real b) (real a)\<rfloor> \<le> log (real b) (real a + r)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   936
    by arith
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   937
next
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   938
  define l::int where "l = int b ^ (nat \<lfloor>log b a\<rfloor> + 1)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   939
  have l_def_real: "l = b powr (\<lfloor>log b a\<rfloor> + 1)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   940
    using assms by (simp add: l_def powr_add powr_real_of_int)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   941
  have "a < l"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   942
  proof -
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   943
    have "a = b powr (log b a)" using assms by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   944
    also have "\<dots> < b powr floor ((log b a) + 1)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   945
      using assms(1) by auto
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   946
    also have "\<dots> = l"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   947
      using assms
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   948
      by (simp add: l_def powr_real_of_int powr_add)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   949
    finally show ?thesis by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   950
  qed
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   951
  then have "a + r < l" using assms by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   952
  then have "log b (a + r) < log b l"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   953
    using assms by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   954
  also have "\<dots> = real_of_int \<lfloor>log b a\<rfloor> + 1"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   955
    using assms by (simp add: l_def_real)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   956
  finally show "log b (a + r) < real_of_int \<lfloor>log b a\<rfloor> + 1" .
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   957
qed
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   958
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   959
lemma
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   960
  divide_nat_diff_div_nat_less_one:
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   961
  fixes x b::nat shows "x / b - x div b < 1"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   962
  by (metis One_nat_def add_diff_cancel_left' divide_1 divide_self_if floor_divide_of_nat_eq
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   963
    less_eq_real_def mod_div_trivial nat.simps(3) of_nat_eq_0_iff real_of_nat_div3 real_of_nat_div_aux)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   964
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   965
context
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   966
begin
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   967
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   968
qualified lemma compute_floorlog[code]: "floorlog b x = (if x > 0 \<and> b > 1 then floorlog b (x div b) + 1 else 0)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   969
proof -
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   970
  {
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   971
    assume prems: "x > 0" "b > 1" "0 < x div b"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   972
    have "\<lfloor>log (real b) (real x)\<rfloor> = \<lfloor>log (real b) (x / b * b)\<rfloor>"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   973
      using prems by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   974
    also have "\<dots> = \<lfloor>log b (x / b) + log b b\<rfloor>"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   975
      using prems
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   976
      by (subst log_mult) auto
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   977
    also have "\<dots> = \<lfloor>log b (x / b)\<rfloor> + 1" using prems by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   978
    also have "\<lfloor>log b (x / b)\<rfloor> = \<lfloor>log b (x div b + (x / b - x div b))\<rfloor>"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   979
      by simp
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   980
    also have "\<dots> = \<lfloor>log b (x div b)\<rfloor>"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   981
      using prems real_of_nat_div4 divide_nat_diff_div_nat_less_one
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   982
      by (intro floor_log_add_eqI) auto
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   983
    finally have ?thesis using prems by (simp add: floorlog_def nat_add_distrib)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   984
  } then show ?thesis
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   985
    by (auto simp: floorlog_def div_eq_0_iff intro!: floor_eq2)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   986
qed
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   987
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   988
end
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   989
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   990
definition bitlen :: "int \<Rightarrow> int" where "bitlen a = floorlog 2 (nat a)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   991
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   992
lemma bitlen_alt_def: "bitlen a = (if a > 0 then \<lfloor>log 2 a\<rfloor> + 1 else 0)"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   993
  by (simp add: bitlen_def floorlog_def)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   994
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   995
lemma bitlen_nonneg: "0 \<le> bitlen x"
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   996
  using floorlog_nonneg by (simp add: bitlen_def)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
   997
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   998
lemma bitlen_bounds:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
   999
  assumes "x > 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1000
  shows "2 ^ nat (bitlen x - 1) \<le> x \<and> x < 2 ^ nat (bitlen x)"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1001
proof -
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1002
  from assms have "bitlen x \<ge> 1" by (auto simp: bitlen_alt_def)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1003
  with assms floorlog_bounds[of "nat x" 2] show ?thesis
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1004
    by (auto simp add: bitlen_def le_nat_iff nat_less_iff nat_diff_distrib)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1005
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1006
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1007
lemma bitlen_pow2[simp]:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1008
  assumes "b > 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1009
  shows "bitlen (b * 2 ^ c) = bitlen b + c"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1010
  using assms
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1011
  by (simp add: bitlen_def nat_mult_distrib nat_power_eq)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1012
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1013
lemma bitlen_Float:
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1014
  fixes m e
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1015
  defines "f \<equiv> Float m e"
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1016
  shows "bitlen (\<bar>mantissa f\<bar>) + exponent f = (if m = 0 then 0 else bitlen \<bar>m\<bar> + e)"
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1017
proof (cases "m = 0")
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1018
  case True
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1019
  then show ?thesis by (simp add: f_def bitlen_alt_def Float_def)
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1020
next
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1021
  case False
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1022
  then have "f \<noteq> float_of 0"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1023
    unfolding real_of_float_eq by (simp add: f_def)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1024
  then have "mantissa f \<noteq> 0"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1025
    by (simp add: mantissa_noteq_0)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1026
  moreover
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1027
  obtain i where "m = mantissa f * 2 ^ i" "e = exponent f - int i"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1028
    by (rule f_def[THEN denormalize_shift, OF \<open>f \<noteq> float_of 0\<close>])
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1029
  ultimately show ?thesis by (simp add: abs_mult)
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1030
qed
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1031
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1032
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1033
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1034
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1035
qualified lemma compute_bitlen[code]: "bitlen x = (if x > 0 then bitlen (x div 2) + 1 else 0)"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1036
  unfolding bitlen_def
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1037
  by (subst Float.compute_floorlog) (simp add: nat_div_distrib)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1038
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1039
end
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1040
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1041
lemma float_gt1_scale: assumes "1 \<le> Float m e"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1042
  shows "0 \<le> e + (bitlen m - 1)"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1043
proof -
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1044
  have "0 < Float m e" using assms by auto
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1045
  then have "0 < m" using powr_gt_zero[of 2 e]
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
  1046
    apply (auto simp: zero_less_mult_iff)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1047
    using not_le powr_ge_pzero apply blast
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1048
    done
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1049
  then have "m \<noteq> 0" by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1050
  show ?thesis
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1051
  proof (cases "0 \<le> e")
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1052
    case True
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1053
    then show ?thesis
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1054
      using \<open>0 < m\<close> by (simp add: bitlen_alt_def)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1055
  next
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1056
    case False
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1057
    have "(1::int) < 2" by simp
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1058
    let ?S = "2^(nat (-e))"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1059
    have "inverse (2 ^ nat (- e)) = 2 powr e"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1060
      using assms False powr_realpow[of 2 "nat (-e)"]
57862
8f074e6e22fc tuned proofs;
wenzelm
parents: 57512
diff changeset
  1061
      by (auto simp: powr_minus field_simps)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1062
    then have "1 \<le> real_of_int m * inverse ?S"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1063
      using assms False powr_realpow[of 2 "nat (-e)"]
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1064
      by (auto simp: powr_minus)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1065
    then have "1 * ?S \<le> real_of_int m * inverse ?S * ?S"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1066
      by (rule mult_right_mono) auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1067
    then have "?S \<le> real_of_int m"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1068
      unfolding mult.assoc by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1069
    then have "?S \<le> m"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1070
      unfolding of_int_le_iff[symmetric] by auto
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1071
    from this bitlen_bounds[OF \<open>0 < m\<close>, THEN conjunct2]
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1072
    have "nat (-e) < (nat (bitlen m))"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1073
      unfolding power_strict_increasing_iff[OF \<open>1 < 2\<close>, symmetric]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1074
      by (rule order_le_less_trans)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1075
    then have "-e < bitlen m"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1076
      using False by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1077
    then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1078
      by auto
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1079
  qed
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1080
qed
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1081
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1082
lemma bitlen_div:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1083
  assumes "0 < m"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1084
  shows "1 \<le> real_of_int m / 2^nat (bitlen m - 1)"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1085
    and "real_of_int m / 2^nat (bitlen m - 1) < 2"
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1086
proof -
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1087
  let ?B = "2^nat(bitlen m - 1)"
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1088
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1089
  have "?B \<le> m" using bitlen_bounds[OF \<open>0 <m\<close>] ..
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1090
  then have "1 * ?B \<le> real_of_int m"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1091
    unfolding of_int_le_iff[symmetric] by auto
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1092
  then show "1 \<le> real_of_int m / ?B"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1093
    by auto
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1094
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1095
  have "m \<noteq> 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1096
    using assms by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1097
  have "0 \<le> bitlen m - 1"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1098
    using \<open>0 < m\<close> by (auto simp: bitlen_alt_def)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  1099
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1100
  have "m < 2^nat(bitlen m)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1101
    using bitlen_bounds[OF \<open>0 <m\<close>] ..
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1102
  also have "\<dots> = 2^nat(bitlen m - 1 + 1)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1103
    using \<open>0 < m\<close> by (auto simp: bitlen_def)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1104
  also have "\<dots> = ?B * 2"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1105
    unfolding nat_add_distrib[OF \<open>0 \<le> bitlen m - 1\<close> zero_le_one] by auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1106
  finally have "real_of_int m < 2 * ?B"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1107
    by (metis (full_types) mult.commute power.simps(2) real_of_int_less_numeral_power_cancel_iff)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1108
  then have "real_of_int m / ?B < 2 * ?B / ?B"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1109
    by (rule divide_strict_right_mono) auto
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1110
  then show "real_of_int m / ?B < 2"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1111
    by auto
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1112
qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1113
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1114
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1115
subsection \<open>Truncating Real Numbers\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1116
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1117
definition truncate_down::"nat \<Rightarrow> real \<Rightarrow> real"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1118
  where "truncate_down prec x = round_down (prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>) x"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1119
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1120
lemma truncate_down: "truncate_down prec x \<le> x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1121
  using round_down by (simp add: truncate_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1122
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1123
lemma truncate_down_le: "x \<le> y \<Longrightarrow> truncate_down prec x \<le> y"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1124
  by (rule order_trans[OF truncate_down])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1125
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1126
lemma truncate_down_zero[simp]: "truncate_down prec 0 = 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1127
  by (simp add: truncate_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1128
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1129
lemma truncate_down_float[simp]: "truncate_down p x \<in> float"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1130
  by (auto simp: truncate_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1131
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1132
definition truncate_up::"nat \<Rightarrow> real \<Rightarrow> real"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1133
  where "truncate_up prec x = round_up (prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>) x"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1134
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1135
lemma truncate_up: "x \<le> truncate_up prec x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1136
  using round_up by (simp add: truncate_up_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1137
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1138
lemma truncate_up_le: "x \<le> y \<Longrightarrow> x \<le> truncate_up prec y"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1139
  by (rule order_trans[OF _ truncate_up])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1140
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1141
lemma truncate_up_zero[simp]: "truncate_up prec 0 = 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1142
  by (simp add: truncate_up_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1143
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1144
lemma truncate_up_uminus_eq: "truncate_up prec (-x) = - truncate_down prec x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1145
  and truncate_down_uminus_eq: "truncate_down prec (-x) = - truncate_up prec x"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1146
  by (auto simp: truncate_up_def round_up_def truncate_down_def round_down_def ceiling_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1147
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1148
lemma truncate_up_float[simp]: "truncate_up p x \<in> float"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1149
  by (auto simp: truncate_up_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1150
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1151
lemma mult_powr_eq: "0 < b \<Longrightarrow> b \<noteq> 1 \<Longrightarrow> 0 < x \<Longrightarrow> x * b powr y = b powr (y + log b x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1152
  by (simp_all add: powr_add)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1153
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1154
lemma truncate_down_pos:
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1155
  assumes "x > 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1156
  shows "truncate_down p x > 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1157
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1158
  have "0 \<le> log 2 x - real_of_int \<lfloor>log 2 x\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1159
    by (simp add: algebra_simps)
61762
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61649
diff changeset
  1160
  with assms
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1161
  show ?thesis
61762
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61649
diff changeset
  1162
    apply (auto simp: truncate_down_def round_down_def mult_powr_eq 
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1163
      intro!: ge_one_powr_ge_zero mult_pos_pos)
61762
d50b993b4fb9 Removal of redundant lemmas (diff_less_iff, diff_le_iff) and of the abbreviation Exp. Addition of some new material.
paulson <lp15@cam.ac.uk>
parents: 61649
diff changeset
  1164
    by linarith
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1165
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1166
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1167
lemma truncate_down_nonneg: "0 \<le> y \<Longrightarrow> 0 \<le> truncate_down prec y"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1168
  by (auto simp: truncate_down_def round_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1169
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1170
lemma truncate_down_ge1: "1 \<le> x \<Longrightarrow> 1 \<le> truncate_down p x"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1171
  apply (auto simp: truncate_down_def algebra_simps intro!: round_down_ge1)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1172
  apply linarith
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1173
  done
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1174
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1175
lemma truncate_up_nonpos: "x \<le> 0 \<Longrightarrow> truncate_up prec x \<le> 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1176
  by (auto simp: truncate_up_def round_up_def intro!: mult_nonpos_nonneg)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1177
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1178
lemma truncate_up_le1:
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1179
  assumes "x \<le> 1"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1180
  shows "truncate_up p x \<le> 1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1181
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1182
  consider "x \<le> 0" | "x > 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1183
    by arith
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1184
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1185
  proof cases
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1186
    case 1
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1187
    with truncate_up_nonpos[OF this, of p] show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1188
      by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1189
  next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1190
    case 2
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1191
    then have le: "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<le> 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1192
      using assms by (auto simp: log_less_iff)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1193
    from assms have "0 \<le> int p" by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1194
    from add_mono[OF this le]
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1195
    show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1196
      using assms by (simp add: truncate_up_def round_up_le1 add_mono)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1197
  qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1198
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1199
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1200
lemma truncate_down_shift_int: "truncate_down p (x * 2 powr real_of_int k) = truncate_down p x * 2 powr k"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1201
  by (cases "x = 0")
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1202
   (simp_all add: algebra_simps abs_mult log_mult truncate_down_def round_down_shift[of _ _ k, simplified])
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1203
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1204
lemma truncate_down_shift_nat: "truncate_down p (x * 2 powr real k) = truncate_down p x * 2 powr k"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1205
  by (metis of_int_of_nat_eq truncate_down_shift_int)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1206
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1207
lemma truncate_up_shift_int: "truncate_up p (x * 2 powr real_of_int k) = truncate_up p x * 2 powr k"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1208
  by (cases "x = 0")
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1209
   (simp_all add: algebra_simps abs_mult log_mult truncate_up_def round_up_shift[of _ _ k, simplified])
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1210
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1211
lemma truncate_up_shift_nat: "truncate_up p (x * 2 powr real k) = truncate_up p x * 2 powr k"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1212
  by (metis of_int_of_nat_eq truncate_up_shift_int)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1213
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1214
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1215
subsection \<open>Truncating Floats\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1216
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1217
lift_definition float_round_up :: "nat \<Rightarrow> float \<Rightarrow> float" is truncate_up
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1218
  by (simp add: truncate_up_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1219
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1220
lemma float_round_up: "real_of_float x \<le> real_of_float (float_round_up prec x)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1221
  using truncate_up by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1222
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1223
lemma float_round_up_zero[simp]: "float_round_up prec 0 = 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1224
  by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1225
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1226
lift_definition float_round_down :: "nat \<Rightarrow> float \<Rightarrow> float" is truncate_down
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1227
  by (simp add: truncate_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1228
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1229
lemma float_round_down: "real_of_float (float_round_down prec x) \<le> real_of_float x"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1230
  using truncate_down by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1231
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1232
lemma float_round_down_zero[simp]: "float_round_down prec 0 = 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1233
  by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1234
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1235
lemmas float_round_up_le = order_trans[OF _ float_round_up]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1236
  and float_round_down_le = order_trans[OF float_round_down]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1237
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1238
lemma minus_float_round_up_eq: "- float_round_up prec x = float_round_down prec (- x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1239
  and minus_float_round_down_eq: "- float_round_down prec x = float_round_up prec (- x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1240
  by (transfer, simp add: truncate_down_uminus_eq truncate_up_uminus_eq)+
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1241
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1242
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1243
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1244
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1245
qualified lemma compute_float_round_down[code]:
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1246
  "float_round_down prec (Float m e) = (let d = bitlen \<bar>m\<bar> - int prec - 1 in
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1247
    if 0 < d then Float (div_twopow m (nat d)) (e + d)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1248
             else Float m e)"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1249
  using Float.compute_float_down[of "Suc prec - bitlen \<bar>m\<bar> - e" m e, symmetric]
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1250
  by transfer
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1251
    (simp add: field_simps abs_mult log_mult bitlen_alt_def truncate_down_def
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1252
      cong del: if_weak_cong)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1253
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1254
qualified lemma compute_float_round_up[code]:
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1255
  "float_round_up prec x = - float_round_down prec (-x)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1256
  by transfer (simp add: truncate_down_uminus_eq)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1257
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1258
end
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1259
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1260
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1261
subsection \<open>Approximation of positive rationals\<close>
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1262
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1263
lemma div_mult_twopow_eq:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1264
  fixes a b :: nat
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1265
  shows "a div ((2::nat) ^ n) div b = a div (b * 2 ^ n)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1266
  by (cases "b = 0") (simp_all add: div_mult2_eq[symmetric] ac_simps)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1267
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1268
lemma real_div_nat_eq_floor_of_divide:
59984
4f1eccec320c conversion between division on nat/int and division in archmedean fields
haftmann
parents: 59554
diff changeset
  1269
  fixes a b :: nat
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1270
  shows "a div b = real_of_int \<lfloor>a / b\<rfloor>"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1271
  by (simp add: floor_divide_of_nat_eq [of a b])
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1272
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1273
definition "rat_precision prec x y =
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1274
  (let d = bitlen x - bitlen y in int prec - d +
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1275
  (if Float (abs x) 0 < Float (abs y) d then 1 else 0))"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1276
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1277
lemma floor_log_divide_eq:
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1278
  assumes "i > 0" "j > 0" "p > 1"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1279
  shows "\<lfloor>log p (i / j)\<rfloor> = floor (log p i) - floor (log p j) -
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1280
      (if i \<ge> j * p powr (floor (log p i) - floor (log p j)) then 0 else 1)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1281
proof -
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1282
  let ?l = "log p"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1283
  let ?fl = "\<lambda>x. floor (?l x)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1284
  have "\<lfloor>?l (i / j)\<rfloor> = \<lfloor>?l i - ?l j\<rfloor>" using assms
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1285
    by (auto simp: log_divide)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1286
  also have "\<dots> = floor (real_of_int (?fl i - ?fl j) + (?l i - ?fl i - (?l j - ?fl j)))"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1287
    (is "_ = floor (_ + ?r)")
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1288
    by (simp add: algebra_simps)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1289
  also note floor_add2
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1290
  also note \<open>p > 1\<close>
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1291
  note powr = powr_le_cancel_iff[symmetric, OF \<open>1 < p\<close>, THEN iffD2]
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1292
  note powr_strict = powr_less_cancel_iff[symmetric, OF \<open>1 < p\<close>, THEN iffD2]
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1293
  have "floor ?r = (if i \<ge> j * p powr (?fl i - ?fl j) then 0 else -1)" (is "_ = ?if")
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1294
    using assms
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1295
    by (linarith |
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1296
      auto
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1297
        intro!: floor_eq2
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1298
        intro: powr_strict powr
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1299
        simp: powr_divide2[symmetric] powr_add divide_simps algebra_simps)+
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1300
  finally
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1301
  show ?thesis by simp
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1302
qed
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1303
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1304
lemma truncate_down_rat_precision:
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1305
    "truncate_down prec (real x / real y) = round_down (rat_precision prec x y) (real x / real y)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1306
  and truncate_up_rat_precision:
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1307
    "truncate_up prec (real x / real y) = round_up (rat_precision prec x y) (real x / real y)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1308
  unfolding truncate_down_def truncate_up_def rat_precision_def
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1309
  by (cases x; cases y) (auto simp: floor_log_divide_eq algebra_simps bitlen_alt_def)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1310
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1311
lift_definition lapprox_posrat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1312
  is "\<lambda>prec (x::nat) (y::nat). truncate_down prec (x / y)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1313
  by simp
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  1314
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1315
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1316
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1317
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1318
qualified lemma compute_lapprox_posrat[code]:
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1319
  fixes prec x y
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1320
  shows "lapprox_posrat prec x y =
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1321
   (let
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1322
      l = rat_precision prec x y;
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1323
      d = if 0 \<le> l then x * 2^nat l div y else x div 2^nat (- l) div y
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1324
    in normfloat (Float d (- l)))"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1325
    unfolding div_mult_twopow_eq
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1326
    by transfer
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1327
      (simp add: round_down_def powr_int real_div_nat_eq_floor_of_divide field_simps Let_def
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1328
        truncate_down_rat_precision del: two_powr_minus_int_float)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1329
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1330
end
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1331
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1332
lift_definition rapprox_posrat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> float"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1333
  is "\<lambda>prec (x::nat) (y::nat). truncate_up prec (x / y)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1334
  by simp
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1335
60376
wenzelm
parents: 60017
diff changeset
  1336
context
wenzelm
parents: 60017
diff changeset
  1337
begin
wenzelm
parents: 60017
diff changeset
  1338
wenzelm
parents: 60017
diff changeset
  1339
qualified lemma compute_rapprox_posrat[code]:
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1340
  fixes prec x y
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1341
  defines "l \<equiv> rat_precision prec x y"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1342
  shows "rapprox_posrat prec x y = (let
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1343
     l = l ;
60868
dd18c33c001e direct bootstrap of integer division from natural division
haftmann
parents: 60698
diff changeset
  1344
     (r, s) = if 0 \<le> l then (x * 2^nat l, y) else (x, y * 2^nat(-l)) ;
dd18c33c001e direct bootstrap of integer division from natural division
haftmann
parents: 60698
diff changeset
  1345
     d = r div s ;
dd18c33c001e direct bootstrap of integer division from natural division
haftmann
parents: 60698
diff changeset
  1346
     m = r mod s
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1347
   in normfloat (Float (d + (if m = 0 \<or> y = 0 then 0 else 1)) (- l)))"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1348
proof (cases "y = 0")
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1349
  assume "y = 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1350
  then show ?thesis by transfer simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1351
next
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1352
  assume "y \<noteq> 0"
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1353
  show ?thesis
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1354
  proof (cases "0 \<le> l")
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1355
    case True
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62421
diff changeset
  1356
    define x' where "x' = x * 2 ^ nat l"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1357
    have "int x * 2 ^ nat l = x'"
62348
9a5f43dac883 dropped various legacy fact bindings
haftmann
parents: 61945
diff changeset
  1358
      by (simp add: x'_def of_nat_mult of_nat_power)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1359
    moreover have "real x * 2 powr l = real x'"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1360
      by (simp add: powr_realpow[symmetric] \<open>0 \<le> l\<close> x'_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1361
    ultimately show ?thesis
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1362
      using ceil_divide_floor_conv[of y x'] powr_realpow[of 2 "nat l"] \<open>0 \<le> l\<close> \<open>y \<noteq> 0\<close>
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1363
        l_def[symmetric, THEN meta_eq_to_obj_eq]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1364
      apply transfer
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1365
      apply (auto simp add: round_up_def truncate_up_rat_precision)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1366
      by (metis floor_divide_of_int_eq of_int_of_nat_eq)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1367
   next
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1368
    case False
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62421
diff changeset
  1369
    define y' where "y' = y * 2 ^ nat (- l)"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1370
    from \<open>y \<noteq> 0\<close> have "y' \<noteq> 0" by (simp add: y'_def)
62348
9a5f43dac883 dropped various legacy fact bindings
haftmann
parents: 61945
diff changeset
  1371
    have "int y * 2 ^ nat (- l) = y'" by (simp add: y'_def of_nat_mult of_nat_power)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1372
    moreover have "real x * real_of_int (2::int) powr real_of_int l / real y = x / real y'"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1373
      using \<open>\<not> 0 \<le> l\<close>
57862
8f074e6e22fc tuned proofs;
wenzelm
parents: 57512
diff changeset
  1374
      by (simp add: powr_realpow[symmetric] powr_minus y'_def field_simps)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1375
    ultimately show ?thesis
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1376
      using ceil_divide_floor_conv[of y' x] \<open>\<not> 0 \<le> l\<close> \<open>y' \<noteq> 0\<close> \<open>y \<noteq> 0\<close>
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1377
        l_def[symmetric, THEN meta_eq_to_obj_eq]
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1378
      apply transfer
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1379
      apply (auto simp add: round_up_def ceil_divide_floor_conv truncate_up_rat_precision)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1380
      by (metis floor_divide_of_int_eq of_int_of_nat_eq)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1381
  qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1382
qed
60376
wenzelm
parents: 60017
diff changeset
  1383
wenzelm
parents: 60017
diff changeset
  1384
end
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1385
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1386
lemma rat_precision_pos:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1387
  assumes "0 \<le> x"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1388
    and "0 < y"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1389
    and "2 * x < y"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1390
  shows "rat_precision n (int x) (int y) > 0"
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1391
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1392
  have "0 < x \<Longrightarrow> log 2 x + 1 = log 2 (2 * x)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1393
    by (simp add: log_mult)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1394
  then have "bitlen (int x) < bitlen (int y)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1395
    using assms
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1396
    by (simp add: bitlen_alt_def del: floor_add_one)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1397
      (auto intro!: floor_mono simp add: floor_add_one[symmetric] simp del: floor_add floor_add_one)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1398
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1399
    using assms
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1400
    by (auto intro!: pos_add_strict simp add: field_simps rat_precision_def)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1401
qed
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  1402
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  1403
lemma rapprox_posrat_less1:
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1404
  "0 \<le> x \<Longrightarrow> 0 < y \<Longrightarrow> 2 * x < y \<Longrightarrow> real_of_float (rapprox_posrat n x y) < 1"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1405
  by transfer (simp add: rat_precision_pos round_up_less1 truncate_up_rat_precision)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1406
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1407
lift_definition lapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float" is
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1408
  "\<lambda>prec (x::int) (y::int). truncate_down prec (x / y)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1409
  by simp
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  1410
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1411
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1412
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1413
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1414
qualified lemma compute_lapprox_rat[code]:
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1415
  "lapprox_rat prec x y =
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1416
   (if y = 0 then 0
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1417
    else if 0 \<le> x then
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1418
     (if 0 < y then lapprox_posrat prec (nat x) (nat y)
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  1419
      else - (rapprox_posrat prec (nat x) (nat (-y))))
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1420
      else (if 0 < y
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1421
        then - (rapprox_posrat prec (nat (-x)) (nat y))
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1422
        else lapprox_posrat prec (nat (-x)) (nat (-y))))"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1423
  by transfer (simp add: truncate_up_uminus_eq)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1424
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1425
lift_definition rapprox_rat :: "nat \<Rightarrow> int \<Rightarrow> int \<Rightarrow> float" is
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1426
  "\<lambda>prec (x::int) (y::int). truncate_up prec (x / y)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1427
  by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1428
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1429
lemma "rapprox_rat = rapprox_posrat"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1430
  by transfer auto
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1431
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1432
lemma "lapprox_rat = lapprox_posrat"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1433
  by transfer auto
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1434
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1435
qualified lemma compute_rapprox_rat[code]:
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1436
  "rapprox_rat prec x y = - lapprox_rat prec (-x) y"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1437
  by transfer (simp add: truncate_down_uminus_eq)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1438
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1439
qualified lemma compute_truncate_down[code]: "truncate_down p (Ratreal r) = (let (a, b) = quotient_of r in lapprox_rat p a b)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1440
  by transfer (auto split: prod.split simp: of_rat_divide dest!: quotient_of_div)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1441
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1442
qualified lemma compute_truncate_up[code]: "truncate_up p (Ratreal r) = (let (a, b) = quotient_of r in rapprox_rat p a b)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1443
  by transfer (auto split: prod.split simp:  of_rat_divide dest!: quotient_of_div)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1444
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1445
end
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1446
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1447
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1448
subsection \<open>Division\<close>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1449
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1450
definition "real_divl prec a b = truncate_down prec (a / b)"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  1451
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1452
definition "real_divr prec a b = truncate_up prec (a / b)"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  1453
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  1454
lift_definition float_divl :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is real_divl
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  1455
  by (simp add: real_divl_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1456
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1457
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1458
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1459
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1460
qualified lemma compute_float_divl[code]:
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1461
  "float_divl prec (Float m1 s1) (Float m2 s2) = lapprox_rat prec m1 m2 * Float 1 (s1 - s2)"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1462
  apply transfer
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1463
  unfolding real_divl_def of_int_1 mult_1 truncate_down_shift_int[symmetric]
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1464
  apply (simp add: powr_divide2[symmetric] powr_minus)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1465
  done
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1466
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  1467
lift_definition float_divr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is real_divr
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  1468
  by (simp add: real_divr_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1469
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1470
qualified lemma compute_float_divr[code]:
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  1471
  "float_divr prec x y = - float_divl prec (-x) y"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1472
  by transfer (simp add: real_divr_def real_divl_def truncate_down_uminus_eq)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1473
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1474
end
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  1475
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  1476
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1477
subsection \<open>Approximate Power\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1478
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1479
lemma div2_less_self[termination_simp]:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1480
  fixes n :: nat
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1481
  shows "odd n \<Longrightarrow> n div 2 < n"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1482
  by (simp add: odd_pos)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1483
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1484
fun power_down :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1485
where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1486
  "power_down p x 0 = 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1487
| "power_down p x (Suc n) =
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1488
    (if odd n then truncate_down (Suc p) ((power_down p x (Suc n div 2))\<^sup>2)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1489
     else truncate_down (Suc p) (x * power_down p x n))"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1490
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1491
fun power_up :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1492
where
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1493
  "power_up p x 0 = 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1494
| "power_up p x (Suc n) =
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1495
    (if odd n then truncate_up p ((power_up p x (Suc n div 2))\<^sup>2)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1496
     else truncate_up p (x * power_up p x n))"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1497
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1498
lift_definition power_up_fl :: "nat \<Rightarrow> float \<Rightarrow> nat \<Rightarrow> float" is power_up
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1499
  by (induct_tac rule: power_up.induct) simp_all
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1500
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1501
lift_definition power_down_fl :: "nat \<Rightarrow> float \<Rightarrow> nat \<Rightarrow> float" is power_down
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1502
  by (induct_tac rule: power_down.induct) simp_all
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1503
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1504
lemma power_float_transfer[transfer_rule]:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1505
  "(rel_fun pcr_float (rel_fun op = pcr_float)) op ^ op ^"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1506
  unfolding power_def
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1507
  by transfer_prover
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1508
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1509
lemma compute_power_up_fl[code]:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1510
  "power_up_fl p x 0 = 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1511
  "power_up_fl p x (Suc n) =
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1512
    (if odd n then float_round_up p ((power_up_fl p x (Suc n div 2))\<^sup>2)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1513
     else float_round_up p (x * power_up_fl p x n))"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1514
  and compute_power_down_fl[code]:
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1515
  "power_down_fl p x 0 = 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1516
  "power_down_fl p x (Suc n) =
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1517
    (if odd n then float_round_down (Suc p) ((power_down_fl p x (Suc n div 2))\<^sup>2)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1518
     else float_round_down (Suc p) (x * power_down_fl p x n))"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1519
  unfolding atomize_conj
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1520
  by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1521
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1522
lemma power_down_pos: "0 < x \<Longrightarrow> 0 < power_down p x n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1523
  by (induct p x n rule: power_down.induct)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1524
    (auto simp del: odd_Suc_div_two intro!: truncate_down_pos)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1525
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1526
lemma power_down_nonneg: "0 \<le> x \<Longrightarrow> 0 \<le> power_down p x n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1527
  by (induct p x n rule: power_down.induct)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1528
    (auto simp del: odd_Suc_div_two intro!: truncate_down_nonneg mult_nonneg_nonneg)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1529
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1530
lemma power_down: "0 \<le> x \<Longrightarrow> power_down p x n \<le> x ^ n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1531
proof (induct p x n rule: power_down.induct)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1532
  case (2 p x n)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1533
  {
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1534
    assume "odd n"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1535
    then have "(power_down p x (Suc n div 2)) ^ 2 \<le> (x ^ (Suc n div 2)) ^ 2"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1536
      using 2
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1537
      by (auto intro: power_mono power_down_nonneg simp del: odd_Suc_div_two)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1538
    also have "\<dots> = x ^ (Suc n div 2 * 2)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1539
      by (simp add: power_mult[symmetric])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1540
    also have "Suc n div 2 * 2 = Suc n"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1541
      using \<open>odd n\<close> by presburger
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1542
    finally have ?case
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1543
      using \<open>odd n\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1544
      by (auto intro!: truncate_down_le simp del: odd_Suc_div_two)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1545
  }
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1546
  then show ?case
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1547
    by (auto intro!: truncate_down_le mult_left_mono 2 mult_nonneg_nonneg power_down_nonneg)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1548
qed simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1549
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1550
lemma power_up: "0 \<le> x \<Longrightarrow> x ^ n \<le> power_up p x n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1551
proof (induct p x n rule: power_up.induct)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1552
  case (2 p x n)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1553
  {
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1554
    assume "odd n"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1555
    then have "Suc n = Suc n div 2 * 2"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1556
      using \<open>odd n\<close> even_Suc by presburger
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1557
    then have "x ^ Suc n \<le> (x ^ (Suc n div 2))\<^sup>2"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1558
      by (simp add: power_mult[symmetric])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1559
    also have "\<dots> \<le> (power_up p x (Suc n div 2))\<^sup>2"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1560
      using 2 \<open>odd n\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1561
      by (auto intro: power_mono simp del: odd_Suc_div_two )
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1562
    finally have ?case
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1563
      using \<open>odd n\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1564
      by (auto intro!: truncate_up_le simp del: odd_Suc_div_two )
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1565
  }
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1566
  then show ?case
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1567
    by (auto intro!: truncate_up_le mult_left_mono 2)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1568
qed simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1569
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1570
lemmas power_up_le = order_trans[OF _ power_up]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1571
  and power_up_less = less_le_trans[OF _ power_up]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1572
  and power_down_le = order_trans[OF power_down]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1573
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1574
lemma power_down_fl: "0 \<le> x \<Longrightarrow> power_down_fl p x n \<le> x ^ n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1575
  by transfer (rule power_down)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1576
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1577
lemma power_up_fl: "0 \<le> x \<Longrightarrow> x ^ n \<le> power_up_fl p x n"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1578
  by transfer (rule power_up)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1579
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1580
lemma real_power_up_fl: "real_of_float (power_up_fl p x n) = power_up p x n"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1581
  by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1582
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1583
lemma real_power_down_fl: "real_of_float (power_down_fl p x n) = power_down p x n"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1584
  by transfer simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1585
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1586
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1587
subsection \<open>Approximate Addition\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1588
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1589
definition "plus_down prec x y = truncate_down prec (x + y)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1590
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1591
definition "plus_up prec x y = truncate_up prec (x + y)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1592
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1593
lemma float_plus_down_float[intro, simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> plus_down p x y \<in> float"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1594
  by (simp add: plus_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1595
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1596
lemma float_plus_up_float[intro, simp]: "x \<in> float \<Longrightarrow> y \<in> float \<Longrightarrow> plus_up p x y \<in> float"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1597
  by (simp add: plus_up_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1598
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1599
lift_definition float_plus_down::"nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is plus_down ..
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1600
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1601
lift_definition float_plus_up::"nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is plus_up ..
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1602
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1603
lemma plus_down: "plus_down prec x y \<le> x + y"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1604
  and plus_up: "x + y \<le> plus_up prec x y"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1605
  by (auto simp: plus_down_def truncate_down plus_up_def truncate_up)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1606
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1607
lemma float_plus_down: "real_of_float (float_plus_down prec x y) \<le> x + y"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1608
  and float_plus_up: "x + y \<le> real_of_float (float_plus_up prec x y)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1609
  by (transfer, rule plus_down plus_up)+
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1610
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1611
lemmas plus_down_le = order_trans[OF plus_down]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1612
  and plus_up_le = order_trans[OF _ plus_up]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1613
  and float_plus_down_le = order_trans[OF float_plus_down]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1614
  and float_plus_up_le = order_trans[OF _ float_plus_up]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1615
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1616
lemma compute_plus_up[code]: "plus_up p x y = - plus_down p (-x) (-y)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1617
  using truncate_down_uminus_eq[of p "x + y"]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1618
  by (auto simp: plus_down_def plus_up_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1619
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1620
lemma truncate_down_log2_eqI:
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1621
  assumes "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> = \<lfloor>log 2 \<bar>y\<bar>\<rfloor>"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1622
  assumes "\<lfloor>x * 2 powr (p - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>)\<rfloor> = \<lfloor>y * 2 powr (p - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>)\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1623
  shows "truncate_down p x = truncate_down p y"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1624
  using assms by (auto simp: truncate_down_def round_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1625
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1626
lemma bitlen_eq_zero_iff: "bitlen x = 0 \<longleftrightarrow> x \<le> 0"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1627
  by (clarsimp simp add: bitlen_alt_def)
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1628
    (metis Float.compute_bitlen add.commute bitlen_alt_def bitlen_nonneg less_add_same_cancel2 not_less
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1629
      zero_less_one)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1630
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1631
lemma sum_neq_zeroI:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1632
  fixes a k :: real
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1633
  shows "\<bar>a\<bar> \<ge> k \<Longrightarrow> \<bar>b\<bar> < k \<Longrightarrow> a + b \<noteq> 0"
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1634
    and "\<bar>a\<bar> > k \<Longrightarrow> \<bar>b\<bar> \<le> k \<Longrightarrow> a + b \<noteq> 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1635
  by auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1636
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1637
lemma abs_real_le_2_powr_bitlen[simp]: "\<bar>real_of_int m2\<bar> < 2 powr real_of_int (bitlen \<bar>m2\<bar>)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1638
proof (cases "m2 = 0")
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1639
  case True
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1640
  then show ?thesis by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1641
next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1642
  case False
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1643
  then have "\<bar>m2\<bar> < 2 ^ nat (bitlen \<bar>m2\<bar>)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1644
    using bitlen_bounds[of "\<bar>m2\<bar>"]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1645
    by (auto simp: powr_add bitlen_nonneg)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1646
  then show ?thesis
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61639
diff changeset
  1647
    by (metis bitlen_nonneg powr_int of_int_abs real_of_int_less_numeral_power_cancel_iff zero_less_numeral)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1648
qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1649
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1650
lemma floor_sum_times_2_powr_sgn_eq:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1651
  fixes ai p q :: int
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1652
    and a b :: real
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1653
  assumes "a * 2 powr p = ai"
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1654
    and b_le_1: "\<bar>b * 2 powr (p + 1)\<bar> \<le> 1"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1655
    and leqp: "q \<le> p"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1656
  shows "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(2 * ai + sgn b) * 2 powr (q - p - 1)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1657
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1658
  consider "b = 0" | "b > 0" | "b < 0" by arith
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1659
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1660
  proof cases
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1661
    case 1
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1662
    then show ?thesis
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1663
      by (simp add: assms(1)[symmetric] powr_add[symmetric] algebra_simps powr_mult_base)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1664
  next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1665
    case 2
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1666
    then have "b * 2 powr p < \<bar>b * 2 powr (p + 1)\<bar>"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1667
      by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1668
    also note b_le_1
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1669
    finally have b_less_1: "b * 2 powr real_of_int p < 1" .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1670
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1671
    from b_less_1 \<open>b > 0\<close> have floor_eq: "\<lfloor>b * 2 powr real_of_int p\<rfloor> = 0" "\<lfloor>sgn b / 2\<rfloor> = 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1672
      by (simp_all add: floor_eq_iff)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1673
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1674
    have "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(a + b) * 2 powr p * 2 powr (q - p)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1675
      by (simp add: algebra_simps powr_realpow[symmetric] powr_add[symmetric])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1676
    also have "\<dots> = \<lfloor>(ai + b * 2 powr p) * 2 powr (q - p)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1677
      by (simp add: assms algebra_simps)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1678
    also have "\<dots> = \<lfloor>(ai + b * 2 powr p) / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1679
      using assms
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1680
      by (simp add: algebra_simps powr_realpow[symmetric] divide_powr_uminus powr_add[symmetric])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1681
    also have "\<dots> = \<lfloor>ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1682
      by (simp del: of_int_power add: floor_divide_real_eq_div floor_eq)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1683
    finally have "\<lfloor>(a + b) * 2 powr real_of_int q\<rfloor> = \<lfloor>real_of_int ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>" .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1684
    moreover
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1685
    {
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1686
      have "\<lfloor>(2 * ai + sgn b) * 2 powr (real_of_int (q - p) - 1)\<rfloor> = \<lfloor>(ai + sgn b / 2) * 2 powr (q - p)\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1687
        by (subst powr_divide2[symmetric]) (simp add: field_simps)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1688
      also have "\<dots> = \<lfloor>(ai + sgn b / 2) / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1689
        using leqp by (simp add: powr_realpow[symmetric] powr_divide2[symmetric])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1690
      also have "\<dots> = \<lfloor>ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1691
        by (simp del: of_int_power add: floor_divide_real_eq_div floor_eq)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1692
      finally
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1693
      have "\<lfloor>(2 * ai + (sgn b)) * 2 powr (real_of_int (q - p) - 1)\<rfloor> =
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1694
          \<lfloor>real_of_int ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>" .
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1695
    }
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1696
    ultimately show ?thesis by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1697
  next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1698
    case 3
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1699
    then have floor_eq: "\<lfloor>b * 2 powr (real_of_int p + 1)\<rfloor> = -1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1700
      using b_le_1
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1701
      by (auto simp: floor_eq_iff algebra_simps pos_divide_le_eq[symmetric] abs_if divide_powr_uminus
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  1702
        intro!: mult_neg_pos split: if_split_asm)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1703
    have "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(2*a + 2*b) * 2 powr p * 2 powr (q - p - 1)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1704
      by (simp add: algebra_simps powr_realpow[symmetric] powr_add[symmetric] powr_mult_base)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1705
    also have "\<dots> = \<lfloor>(2 * (a * 2 powr p) + 2 * b * 2 powr p) * 2 powr (q - p - 1)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1706
      by (simp add: algebra_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1707
    also have "\<dots> = \<lfloor>(2 * ai + b * 2 powr (p + 1)) / 2 powr (1 - q + p)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1708
      using assms by (simp add: algebra_simps powr_mult_base divide_powr_uminus)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1709
    also have "\<dots> = \<lfloor>(2 * ai + b * 2 powr (p + 1)) / real_of_int ((2::int) ^ nat (p - q + 1))\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1710
      using assms by (simp add: algebra_simps powr_realpow[symmetric])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1711
    also have "\<dots> = \<lfloor>(2 * ai - 1) / real_of_int ((2::int) ^ nat (p - q + 1))\<rfloor>"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1712
      using \<open>b < 0\<close> assms
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1713
      by (simp add: floor_divide_of_int_eq floor_eq floor_divide_real_eq_div
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1714
        del: of_int_mult of_int_power of_int_diff)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1715
    also have "\<dots> = \<lfloor>(2 * ai - 1) * 2 powr (q - p - 1)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1716
      using assms by (simp add: algebra_simps divide_powr_uminus powr_realpow[symmetric])
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1717
    finally show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1718
      using \<open>b < 0\<close> by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1719
  qed
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1720
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1721
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1722
lemma log2_abs_int_add_less_half_sgn_eq:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1723
  fixes ai :: int
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1724
    and b :: real
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1725
  assumes "\<bar>b\<bar> \<le> 1/2"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1726
    and "ai \<noteq> 0"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1727
  shows "\<lfloor>log 2 \<bar>real_of_int ai + b\<bar>\<rfloor> = \<lfloor>log 2 \<bar>ai + sgn b / 2\<bar>\<rfloor>"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1728
proof (cases "b = 0")
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1729
  case True
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1730
  then show ?thesis by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1731
next
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1732
  case False
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62421
diff changeset
  1733
  define k where "k = \<lfloor>log 2 \<bar>ai\<bar>\<rfloor>"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1734
  then have "\<lfloor>log 2 \<bar>ai\<bar>\<rfloor> = k"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1735
    by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1736
  then have k: "2 powr k \<le> \<bar>ai\<bar>" "\<bar>ai\<bar> < 2 powr (k + 1)"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1737
    by (simp_all add: floor_log_eq_powr_iff \<open>ai \<noteq> 0\<close>)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1738
  have "k \<ge> 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1739
    using assms by (auto simp: k_def)
63040
eb4ddd18d635 eliminated old 'def';
wenzelm
parents: 62421
diff changeset
  1740
  define r where "r = \<bar>ai\<bar> - 2 ^ nat k"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1741
  have r: "0 \<le> r" "r < 2 powr k"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1742
    using \<open>k \<ge> 0\<close> k
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1743
    by (auto simp: r_def k_def algebra_simps powr_add abs_if powr_int)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1744
  then have "r \<le> (2::int) ^ nat k - 1"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1745
    using \<open>k \<ge> 0\<close> by (auto simp: powr_int)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1746
  from this[simplified of_int_le_iff[symmetric]] \<open>0 \<le> k\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1747
  have r_le: "r \<le> 2 powr k - 1"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1748
    apply (auto simp: algebra_simps powr_int)
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1749
     by (metis of_int_1 of_int_add real_of_int_le_numeral_power_cancel_iff)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1750
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1751
  have "\<bar>ai\<bar> = 2 powr k + r"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1752
    using \<open>k \<ge> 0\<close> by (auto simp: k_def r_def powr_realpow[symmetric])
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1753
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1754
  have pos: "\<bar>b\<bar> < 1 \<Longrightarrow> 0 < 2 powr k + (r + b)" for b :: real
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1755
    using \<open>0 \<le> k\<close> \<open>ai \<noteq> 0\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1756
    by (auto simp add: r_def powr_realpow[symmetric] abs_if sgn_if algebra_simps
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  1757
      split: if_split_asm)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1758
  have less: "\<bar>sgn ai * b\<bar> < 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1759
    and less': "\<bar>sgn (sgn ai * b) / 2\<bar> < 1"
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  1760
    using \<open>\<bar>b\<bar> \<le> _\<close> by (auto simp: abs_if sgn_if split: if_split_asm)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1761
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1762
  have floor_eq: "\<And>b::real. \<bar>b\<bar> \<le> 1 / 2 \<Longrightarrow>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1763
      \<lfloor>log 2 (1 + (r + b) / 2 powr k)\<rfloor> = (if r = 0 \<and> b < 0 then -1 else 0)"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1764
    using \<open>k \<ge> 0\<close> r r_le
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1765
    by (auto simp: floor_log_eq_powr_iff powr_minus_divide field_simps sgn_if)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1766
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1767
  from \<open>real_of_int \<bar>ai\<bar> = _\<close> have "\<bar>ai + b\<bar> = 2 powr k + (r + sgn ai * b)"
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1768
    using \<open>\<bar>b\<bar> <= _\<close> \<open>0 \<le> k\<close> r
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1769
    by (auto simp add: sgn_if abs_if)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1770
  also have "\<lfloor>log 2 \<dots>\<rfloor> = \<lfloor>log 2 (2 powr k + r + sgn (sgn ai * b) / 2)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1771
  proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1772
    have "2 powr k + (r + (sgn ai) * b) = 2 powr k * (1 + (r + sgn ai * b) / 2 powr k)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1773
      by (simp add: field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1774
    also have "\<lfloor>log 2 \<dots>\<rfloor> = k + \<lfloor>log 2 (1 + (r + sgn ai * b) / 2 powr k)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1775
      using pos[OF less]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1776
      by (subst log_mult) (simp_all add: log_mult powr_mult field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1777
    also
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1778
    let ?if = "if r = 0 \<and> sgn ai * b < 0 then -1 else 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1779
    have "\<lfloor>log 2 (1 + (r + sgn ai * b) / 2 powr k)\<rfloor> = ?if"
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1780
      using \<open>\<bar>b\<bar> <= _\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1781
      by (intro floor_eq) (auto simp: abs_mult sgn_if)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1782
    also
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1783
    have "\<dots> = \<lfloor>log 2 (1 + (r + sgn (sgn ai * b) / 2) / 2 powr k)\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1784
      by (subst floor_eq) (auto simp: sgn_if)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1785
    also have "k + \<dots> = \<lfloor>log 2 (2 powr k * (1 + (r + sgn (sgn ai * b) / 2) / 2 powr k))\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1786
      unfolding floor_add2[symmetric]
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1787
      using pos[OF less'] \<open>\<bar>b\<bar> \<le> _\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1788
      by (simp add: field_simps add_log_eq_powr)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1789
    also have "2 powr k * (1 + (r + sgn (sgn ai * b) / 2) / 2 powr k) =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1790
        2 powr k + r + sgn (sgn ai * b) / 2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1791
      by (simp add: sgn_if field_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1792
    finally show ?thesis .
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1793
  qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1794
  also have "2 powr k + r + sgn (sgn ai * b) / 2 = \<bar>ai + sgn b / 2\<bar>"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1795
    unfolding \<open>real_of_int \<bar>ai\<bar> = _\<close>[symmetric] using \<open>ai \<noteq> 0\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1796
    by (auto simp: abs_if sgn_if algebra_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1797
  finally show ?thesis .
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1798
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1799
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1800
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1801
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1802
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1803
qualified lemma compute_far_float_plus_down:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1804
  fixes m1 e1 m2 e2 :: int
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1805
    and p :: nat
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1806
  defines "k1 \<equiv> Suc p - nat (bitlen \<bar>m1\<bar>)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1807
  assumes H: "bitlen \<bar>m2\<bar> \<le> e1 - e2 - k1 - 2" "m1 \<noteq> 0" "m2 \<noteq> 0" "e1 \<ge> e2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1808
  shows "float_plus_down p (Float m1 e1) (Float m2 e2) =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1809
    float_round_down p (Float (m1 * 2 ^ (Suc (Suc k1)) + sgn m2) (e1 - int k1 - 2))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1810
proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1811
  let ?a = "real_of_float (Float m1 e1)"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1812
  let ?b = "real_of_float (Float m2 e2)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1813
  let ?sum = "?a + ?b"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1814
  let ?shift = "real_of_int e2 - real_of_int e1 + real k1 + 1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1815
  let ?m1 = "m1 * 2 ^ Suc k1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1816
  let ?m2 = "m2 * 2 powr ?shift"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1817
  let ?m2' = "sgn m2 / 2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1818
  let ?e = "e1 - int k1 - 1"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1819
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1820
  have sum_eq: "?sum = (?m1 + ?m2) * 2 powr ?e"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1821
    by (auto simp: powr_add[symmetric] powr_mult[symmetric] algebra_simps
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1822
      powr_realpow[symmetric] powr_mult_base)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1823
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1824
  have "\<bar>?m2\<bar> * 2 < 2 powr (bitlen \<bar>m2\<bar> + ?shift + 1)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1825
    by (auto simp: field_simps powr_add powr_mult_base powr_numeral powr_divide2[symmetric] abs_mult)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1826
  also have "\<dots> \<le> 2 powr 0"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1827
    using H by (intro powr_mono) auto
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1828
  finally have abs_m2_less_half: "\<bar>?m2\<bar> < 1 / 2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1829
    by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1830
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1831
  then have "\<bar>real_of_int m2\<bar> < 2 powr -(?shift + 1)"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1832
    unfolding powr_minus_divide by (auto simp: bitlen_alt_def field_simps powr_mult_base abs_mult)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1833
  also have "\<dots> \<le> 2 powr real_of_int (e1 - e2 - 2)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1834
    by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1835
  finally have b_less_quarter: "\<bar>?b\<bar> < 1/4 * 2 powr real_of_int e1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1836
    by (simp add: powr_add field_simps powr_divide2[symmetric] powr_numeral abs_mult)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1837
  also have "1/4 < \<bar>real_of_int m1\<bar> / 2" using \<open>m1 \<noteq> 0\<close> by simp
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1838
  finally have b_less_half_a: "\<bar>?b\<bar> < 1/2 * \<bar>?a\<bar>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1839
    by (simp add: algebra_simps powr_mult_base abs_mult)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1840
  then have a_half_less_sum: "\<bar>?a\<bar> / 2 < \<bar>?sum\<bar>"
62390
842917225d56 more canonical names
nipkow
parents: 62348
diff changeset
  1841
    by (auto simp: field_simps abs_if split: if_split_asm)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1842
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1843
  from b_less_half_a have "\<bar>?b\<bar> < \<bar>?a\<bar>" "\<bar>?b\<bar> \<le> \<bar>?a\<bar>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1844
    by simp_all
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1845
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1846
  have "\<bar>real_of_float (Float m1 e1)\<bar> \<ge> 1/4 * 2 powr real_of_int e1"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1847
    using \<open>m1 \<noteq> 0\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1848
    by (auto simp: powr_add powr_int bitlen_nonneg divide_right_mono abs_mult)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1849
  then have "?sum \<noteq> 0" using b_less_quarter
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1850
    by (rule sum_neq_zeroI)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1851
  then have "?m1 + ?m2 \<noteq> 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1852
    unfolding sum_eq by (simp add: abs_mult zero_less_mult_iff)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1853
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1854
  have "\<bar>real_of_int ?m1\<bar> \<ge> 2 ^ Suc k1" "\<bar>?m2'\<bar> < 2 ^ Suc k1"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1855
    using \<open>m1 \<noteq> 0\<close> \<open>m2 \<noteq> 0\<close> by (auto simp: sgn_if less_1_mult abs_mult simp del: power.simps)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1856
  then have sum'_nz: "?m1 + ?m2' \<noteq> 0"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1857
    by (intro sum_neq_zeroI)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1858
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1859
  have "\<lfloor>log 2 \<bar>real_of_float (Float m1 e1) + real_of_float (Float m2 e2)\<bar>\<rfloor> = \<lfloor>log 2 \<bar>?m1 + ?m2\<bar>\<rfloor> + ?e"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1860
    using \<open>?m1 + ?m2 \<noteq> 0\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1861
    unfolding floor_add[symmetric] sum_eq
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1862
    by (simp add: abs_mult log_mult) linarith
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1863
  also have "\<lfloor>log 2 \<bar>?m1 + ?m2\<bar>\<rfloor> = \<lfloor>log 2 \<bar>?m1 + sgn (real_of_int m2 * 2 powr ?shift) / 2\<bar>\<rfloor>"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1864
    using abs_m2_less_half \<open>m1 \<noteq> 0\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1865
    by (intro log2_abs_int_add_less_half_sgn_eq) (auto simp: abs_mult)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1866
  also have "sgn (real_of_int m2 * 2 powr ?shift) = sgn m2"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1867
    by (auto simp: sgn_if zero_less_mult_iff less_not_sym)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1868
  also
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1869
  have "\<bar>?m1 + ?m2'\<bar> * 2 powr ?e = \<bar>?m1 * 2 + sgn m2\<bar> * 2 powr (?e - 1)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1870
    by (auto simp: field_simps powr_minus[symmetric] powr_divide2[symmetric] powr_mult_base)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1871
  then have "\<lfloor>log 2 \<bar>?m1 + ?m2'\<bar>\<rfloor> + ?e = \<lfloor>log 2 \<bar>real_of_float (Float (?m1 * 2 + sgn m2) (?e - 1))\<bar>\<rfloor>"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1872
    using \<open>?m1 + ?m2' \<noteq> 0\<close>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1873
    unfolding floor_add_of_int[symmetric]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1874
    by (simp add: log_add_eq_powr abs_mult_pos)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1875
  finally
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1876
  have "\<lfloor>log 2 \<bar>?sum\<bar>\<rfloor> = \<lfloor>log 2 \<bar>real_of_float (Float (?m1*2 + sgn m2) (?e - 1))\<bar>\<rfloor>" .
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1877
  then have "plus_down p (Float m1 e1) (Float m2 e2) =
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1878
      truncate_down p (Float (?m1*2 + sgn m2) (?e - 1))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1879
    unfolding plus_down_def
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1880
  proof (rule truncate_down_log2_eqI)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1881
    let ?f = "(int p - \<lfloor>log 2 \<bar>real_of_float (Float m1 e1) + real_of_float (Float m2 e2)\<bar>\<rfloor>)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1882
    let ?ai = "m1 * 2 ^ (Suc k1)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1883
    have "\<lfloor>(?a + ?b) * 2 powr real_of_int ?f\<rfloor> = \<lfloor>(real_of_int (2 * ?ai) + sgn ?b) * 2 powr real_of_int (?f - - ?e - 1)\<rfloor>"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1884
    proof (rule floor_sum_times_2_powr_sgn_eq)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1885
      show "?a * 2 powr real_of_int (-?e) = real_of_int ?ai"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1886
        by (simp add: powr_add powr_realpow[symmetric] powr_divide2[symmetric])
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1887
      show "\<bar>?b * 2 powr real_of_int (-?e + 1)\<bar> \<le> 1"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1888
        using abs_m2_less_half
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1889
        by (simp add: abs_mult powr_add[symmetric] algebra_simps powr_mult_base)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1890
    next
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1891
      have "e1 + \<lfloor>log 2 \<bar>real_of_int m1\<bar>\<rfloor> - 1 = \<lfloor>log 2 \<bar>?a\<bar>\<rfloor> - 1"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1892
        using \<open>m1 \<noteq> 0\<close>
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1893
        by (simp add: floor_add2[symmetric] algebra_simps log_mult abs_mult del: floor_add2)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1894
      also have "\<dots> \<le> \<lfloor>log 2 \<bar>?a + ?b\<bar>\<rfloor>"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1895
        using a_half_less_sum \<open>m1 \<noteq> 0\<close> \<open>?sum \<noteq> 0\<close>
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1896
        unfolding floor_diff_of_int[symmetric]
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1897
        by (auto simp add: log_minus_eq_powr powr_minus_divide intro!: floor_mono)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1898
      finally
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1899
      have "int p - \<lfloor>log 2 \<bar>?a + ?b\<bar>\<rfloor> \<le> p - (bitlen \<bar>m1\<bar>) - e1 + 2"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  1900
        by (auto simp: algebra_simps bitlen_alt_def \<open>m1 \<noteq> 0\<close>)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1901
      also have "\<dots> \<le> - ?e"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1902
        using bitlen_nonneg[of "\<bar>m1\<bar>"] by (simp add: k1_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1903
      finally show "?f \<le> - ?e" by simp
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1904
    qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1905
    also have "sgn ?b = sgn m2"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1906
      using powr_gt_zero[of 2 e2]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1907
      by (auto simp add: sgn_if zero_less_mult_iff simp del: powr_gt_zero)
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1908
    also have "\<lfloor>(real_of_int (2 * ?m1) + real_of_int (sgn m2)) * 2 powr real_of_int (?f - - ?e - 1)\<rfloor> =
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1909
        \<lfloor>Float (?m1 * 2 + sgn m2) (?e - 1) * 2 powr ?f\<rfloor>"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1910
      by (simp add: powr_add[symmetric] algebra_simps powr_realpow[symmetric])
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1911
    finally
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1912
    show "\<lfloor>(?a + ?b) * 2 powr ?f\<rfloor> = \<lfloor>real_of_float (Float (?m1 * 2 + sgn m2) (?e - 1)) * 2 powr ?f\<rfloor>" .
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1913
  qed
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1914
  then show ?thesis
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1915
    by transfer (simp add: plus_down_def ac_simps Let_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1916
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1917
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1918
lemma compute_float_plus_down_naive[code]: "float_plus_down p x y = float_round_down p (x + y)"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1919
  by transfer (auto simp: plus_down_def)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1920
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1921
qualified lemma compute_float_plus_down[code]:
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1922
  fixes p::nat and m1 e1 m2 e2::int
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1923
  shows "float_plus_down p (Float m1 e1) (Float m2 e2) =
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1924
    (if m1 = 0 then float_round_down p (Float m2 e2)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1925
    else if m2 = 0 then float_round_down p (Float m1 e1)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1926
    else (if e1 \<ge> e2 then
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1927
      (let
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1928
        k1 = Suc p - nat (bitlen \<bar>m1\<bar>)
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1929
      in
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1930
        if bitlen \<bar>m2\<bar> > e1 - e2 - k1 - 2 then float_round_down p ((Float m1 e1) + (Float m2 e2))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1931
        else float_round_down p (Float (m1 * 2 ^ (Suc (Suc k1)) + sgn m2) (e1 - int k1 - 2)))
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1932
    else float_plus_down p (Float m2 e2) (Float m1 e1)))"
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1933
proof -
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1934
  {
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1935
    assume "bitlen \<bar>m2\<bar> \<le> e1 - e2 - (Suc p - nat (bitlen \<bar>m1\<bar>)) - 2" "m1 \<noteq> 0" "m2 \<noteq> 0" "e1 \<ge> e2"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1936
    note compute_far_float_plus_down[OF this]
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1937
  }
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1938
  then show ?thesis
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1939
    by transfer (simp add: Let_def plus_down_def ac_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1940
qed
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1941
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1942
qualified lemma compute_float_plus_up[code]: "float_plus_up p x y = - float_plus_down p (-x) (-y)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1943
  using truncate_down_uminus_eq[of p "x + y"]
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1944
  by transfer (simp add: plus_down_def plus_up_def ac_simps)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1945
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1946
lemma mantissa_zero[simp]: "mantissa 0 = 0"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1947
  by (metis mantissa_0 zero_float.abs_eq)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1948
62421
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1949
qualified lemma compute_float_less[code]: "a < b \<longleftrightarrow> is_float_pos (float_plus_down 0 b (- a))"
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1950
  using truncate_down[of 0 "b - a"] truncate_down_pos[of "b - a" 0]
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1951
  by transfer (auto simp: plus_down_def)
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1952
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1953
qualified lemma compute_float_le[code]: "a \<le> b \<longleftrightarrow> is_float_nonneg (float_plus_down 0 b (- a))"
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1954
  using truncate_down[of 0 "b - a"] truncate_down_nonneg[of "b - a" 0]
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1955
  by transfer (auto simp: plus_down_def)
28d2c75dd180 finite precision computation to determine sign for comparison
immler
parents: 62420
diff changeset
  1956
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1957
end
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1958
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  1959
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  1960
subsection \<open>Lemmas needed by Approximate\<close>
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1961
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1962
lemma Float_num[simp]:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1963
   "real_of_float (Float 1 0) = 1"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1964
   "real_of_float (Float 1 1) = 2"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1965
   "real_of_float (Float 1 2) = 4"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1966
   "real_of_float (Float 1 (- 1)) = 1/2"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1967
   "real_of_float (Float 1 (- 2)) = 1/4"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1968
   "real_of_float (Float 1 (- 3)) = 1/8"
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1969
   "real_of_float (Float (- 1) 0) = -1"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1970
   "real_of_float (Float (numeral n) 0) = numeral n"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1971
   "real_of_float (Float (- numeral n) 0) = - numeral n"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1972
  using two_powr_int_float[of 2] two_powr_int_float[of "-1"] two_powr_int_float[of "-2"]
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1973
    two_powr_int_float[of "-3"]
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1974
  using powr_realpow[of 2 2] powr_realpow[of 2 3]
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1975
  using powr_minus[of 2 1] powr_minus[of 2 2] powr_minus[of 2 3]
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1976
  by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1977
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1978
lemma real_of_Float_int[simp]: "real_of_float (Float n 0) = real n"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1979
  by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1980
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1981
lemma float_zero[simp]: "real_of_float (Float 0 e) = 0"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1982
  by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1983
61945
1135b8de26c3 more symbols;
wenzelm
parents: 61942
diff changeset
  1984
lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> \<bar>(a::int) div 2\<bar> < \<bar>a\<bar>"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1985
  by arith
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  1986
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  1987
lemma lapprox_rat: "real_of_float (lapprox_rat prec x y) \<le> real_of_int x / real_of_int y"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  1988
  by (simp add: lapprox_rat.rep_eq truncate_down)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  1989
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1990
lemma mult_div_le:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1991
  fixes a b :: int
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1992
  assumes "b > 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1993
  shows "a \<ge> b * (a div b)"
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  1994
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1995
  from zmod_zdiv_equality'[of a b] have "a = b * (a div b) + a mod b"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1996
    by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1997
  also have "\<dots> \<ge> b * (a div b) + 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1998
    apply (rule add_left_mono)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  1999
    apply (rule pos_mod_sign)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2000
    using assms apply simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2001
    done
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2002
  finally show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2003
    by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2004
qed
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2005
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2006
lemma lapprox_rat_nonneg:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2007
  fixes n x y
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2008
  assumes "0 \<le> x" and "0 \<le> y"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2009
  shows "0 \<le> real_of_float (lapprox_rat n x y)"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2010
  using assms
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2011
  by transfer (simp add: truncate_down_nonneg)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2012
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2013
lemma rapprox_rat: "real_of_int x / real_of_int y \<le> real_of_float (rapprox_rat prec x y)"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2014
  by transfer (simp add: truncate_up)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2015
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2016
lemma rapprox_rat_le1:
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2017
  fixes n x y
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2018
  assumes xy: "0 \<le> x" "0 < y" "x \<le> y"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2019
  shows "real_of_float (rapprox_rat n x y) \<le> 1"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2020
  using assms
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2021
  by transfer (simp add: truncate_up_le1)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2022
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2023
lemma rapprox_rat_nonneg_nonpos: "0 \<le> x \<Longrightarrow> y \<le> 0 \<Longrightarrow> real_of_float (rapprox_rat n x y) \<le> 0"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2024
  by transfer (simp add: truncate_up_nonpos divide_nonneg_nonpos)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2025
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2026
lemma rapprox_rat_nonpos_nonneg: "x \<le> 0 \<Longrightarrow> 0 \<le> y \<Longrightarrow> real_of_float (rapprox_rat n x y) \<le> 0"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2027
  by transfer (simp add: truncate_up_nonpos divide_nonpos_nonneg)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2028
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2029
lemma real_divl: "real_divl prec x y \<le> x / y"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2030
  by (simp add: real_divl_def truncate_down)
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2031
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2032
lemma real_divr: "x / y \<le> real_divr prec x y"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2033
  by (simp add: real_divr_def truncate_up)
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2034
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2035
lemma float_divl: "real_of_float (float_divl prec x y) \<le> x / y"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2036
  by transfer (rule real_divl)
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2037
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2038
lemma real_divl_lower_bound:
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2039
  "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> real_divl prec x y"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2040
  by (simp add: real_divl_def truncate_down_nonneg)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2041
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2042
lemma float_divl_lower_bound:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2043
  "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> real_of_float (float_divl prec x y)"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2044
  by transfer (rule real_divl_lower_bound)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2045
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2046
lemma exponent_1: "exponent 1 = 0"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2047
  using exponent_float[of 1 0] by (simp add: one_float_def)
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2048
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2049
lemma mantissa_1: "mantissa 1 = 1"
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2050
  using mantissa_float[of 1 0] by (simp add: one_float_def)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2051
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2052
lemma bitlen_1: "bitlen 1 = 1"
63248
414e3550e9c0 generalized bitlen to floor of log
immler
parents: 63040
diff changeset
  2053
  by (simp add: bitlen_alt_def)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2054
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2055
lemma mantissa_eq_zero_iff: "mantissa x = 0 \<longleftrightarrow> x = 0"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2056
  (is "?lhs \<longleftrightarrow> ?rhs")
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2057
proof
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2058
  show ?rhs if ?lhs
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2059
  proof -
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2060
    from that have z: "0 = real_of_float x"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2061
      using mantissa_exponent by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2062
    show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2063
      by (simp add: zero_float_def z)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2064
  qed
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2065
  show ?lhs if ?rhs
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2066
    using that by (simp add: zero_float_def)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2067
qed
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2068
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2069
lemma float_upper_bound: "x \<le> 2 powr (bitlen \<bar>mantissa x\<bar> + exponent x)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2070
proof (cases "x = 0")
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2071
  case True
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2072
  then show ?thesis by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2073
next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2074
  case False
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2075
  then have "mantissa x \<noteq> 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2076
    using mantissa_eq_zero_iff by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2077
  have "x = mantissa x * 2 powr (exponent x)"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2078
    by (rule mantissa_exponent)
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2079
  also have "mantissa x \<le> \<bar>mantissa x\<bar>"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2080
    by simp
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2081
  also have "\<dots> \<le> 2 powr (bitlen \<bar>mantissa x\<bar>)"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2082
    using bitlen_bounds[of "\<bar>mantissa x\<bar>"] bitlen_nonneg \<open>mantissa x \<noteq> 0\<close>
61649
268d88ec9087 Tweaks for "real": Removal of [iff] status for some lemmas, adding [simp] for others. Plus fixes.
paulson <lp15@cam.ac.uk>
parents: 61639
diff changeset
  2083
    by (auto simp del: of_int_abs simp add: powr_int)
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2084
  finally show ?thesis by (simp add: powr_add)
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  2085
qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  2086
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2087
lemma real_divl_pos_less1_bound:
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2088
  assumes "0 < x" "x \<le> 1"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2089
  shows "1 \<le> real_divl prec 1 x"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2090
  using assms
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2091
  by (auto intro!: truncate_down_ge1 simp: real_divl_def)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2092
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2093
lemma float_divl_pos_less1_bound:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2094
  "0 < real_of_float x \<Longrightarrow> real_of_float x \<le> 1 \<Longrightarrow> prec \<ge> 1 \<Longrightarrow> 1 \<le> real_of_float (float_divl prec 1 x)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2095
  by transfer (rule real_divl_pos_less1_bound)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2096
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2097
lemma float_divr: "real_of_float x / real_of_float y \<le> real_of_float (float_divr prec x y)"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2098
  by transfer (rule real_divr)
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2099
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2100
lemma real_divr_pos_less1_lower_bound:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2101
  assumes "0 < x"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2102
    and "x \<le> 1"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2103
  shows "1 \<le> real_divr prec 1 x"
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  2104
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2105
  have "1 \<le> 1 / x"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2106
    using \<open>0 < x\<close> and \<open>x <= 1\<close> by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2107
  also have "\<dots> \<le> real_divr prec 1 x"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2108
    using real_divr[where x=1 and y=x] by auto
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2109
  finally show ?thesis by auto
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  2110
qed
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  2111
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2112
lemma float_divr_pos_less1_lower_bound: "0 < x \<Longrightarrow> x \<le> 1 \<Longrightarrow> 1 \<le> float_divr prec 1 x"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2113
  by transfer (rule real_divr_pos_less1_lower_bound)
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2114
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2115
lemma real_divr_nonpos_pos_upper_bound:
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2116
  "x \<le> 0 \<Longrightarrow> 0 \<le> y \<Longrightarrow> real_divr prec x y \<le> 0"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2117
  by (simp add: real_divr_def truncate_up_nonpos divide_le_0_iff)
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2118
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2119
lemma float_divr_nonpos_pos_upper_bound:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2120
  "real_of_float x \<le> 0 \<Longrightarrow> 0 \<le> real_of_float y \<Longrightarrow> real_of_float (float_divr prec x y) \<le> 0"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2121
  by transfer (rule real_divr_nonpos_pos_upper_bound)
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2122
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2123
lemma real_divr_nonneg_neg_upper_bound:
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2124
  "0 \<le> x \<Longrightarrow> y \<le> 0 \<Longrightarrow> real_divr prec x y \<le> 0"
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2125
  by (simp add: real_divr_def truncate_up_nonpos divide_le_0_iff)
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2126
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2127
lemma float_divr_nonneg_neg_upper_bound:
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2128
  "0 \<le> real_of_float x \<Longrightarrow> real_of_float y \<le> 0 \<Longrightarrow> real_of_float (float_divr prec x y) \<le> 0"
54782
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2129
  by transfer (rule real_divr_nonneg_neg_upper_bound)
cd8f55c358c5 additional definitions and lemmas for Float
immler
parents: 54489
diff changeset
  2130
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2131
lemma truncate_up_nonneg_mono:
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2132
  assumes "0 \<le> x" "x \<le> y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2133
  shows "truncate_up prec x \<le> truncate_up prec y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2134
proof -
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2135
  consider "\<lfloor>log 2 x\<rfloor> = \<lfloor>log 2 y\<rfloor>" | "\<lfloor>log 2 x\<rfloor> \<noteq> \<lfloor>log 2 y\<rfloor>" "0 < x" | "x \<le> 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2136
    by arith
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2137
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2138
  proof cases
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2139
    case 1
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2140
    then show ?thesis
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2141
      using assms
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2142
      by (auto simp: truncate_up_def round_up_def intro!: ceiling_mono)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2143
  next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2144
    case 2
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2145
    from assms \<open>0 < x\<close> have "log 2 x \<le> log 2 y"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2146
      by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2147
    with \<open>\<lfloor>log 2 x\<rfloor> \<noteq> \<lfloor>log 2 y\<rfloor>\<close>
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2148
    have logless: "log 2 x < log 2 y" and flogless: "\<lfloor>log 2 x\<rfloor> < \<lfloor>log 2 y\<rfloor>"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2149
      by (metis floor_less_cancel linorder_cases not_le)+
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2150
    have "truncate_up prec x =
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2151
      real_of_int \<lceil>x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor> )\<rceil> * 2 powr - real_of_int (int prec - \<lfloor>log 2 x\<rfloor>)"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2152
      using assms by (simp add: truncate_up_def round_up_def)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2153
    also have "\<lceil>x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>)\<rceil> \<le> (2 ^ (Suc prec))"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2154
    proof (unfold ceiling_le_iff)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2155
      have "x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le> x * (2 powr real (Suc prec) / (2 powr log 2 x))"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2156
        using real_of_int_floor_add_one_ge[of "log 2 x"] assms
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2157
        by (auto simp add: algebra_simps powr_divide2 intro!: mult_left_mono)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2158
      then show "x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le> real_of_int ((2::int) ^ (Suc prec))"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2159
        using \<open>0 < x\<close> by (simp add: powr_realpow powr_add)
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2160
    qed
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2161
    then have "real_of_int \<lceil>x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>)\<rceil> \<le> 2 powr int (Suc prec)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2162
      apply (auto simp: powr_realpow powr_add)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2163
      by (metis power_Suc real_of_int_le_numeral_power_cancel_iff)
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2164
    also
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2165
    have "2 powr - real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le> 2 powr - real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1)"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2166
      using logless flogless by (auto intro!: floor_mono)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2167
    also have "2 powr real_of_int (int (Suc prec)) \<le> 2 powr (log 2 y + real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1))"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2168
      using assms \<open>0 < x\<close>
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2169
      by (auto simp: algebra_simps)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2170
    finally have "truncate_up prec x \<le> 2 powr (log 2 y + real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1)) * 2 powr - real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1)"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2171
      by simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2172
    also have "\<dots> = 2 powr (log 2 y + real_of_int (int prec - \<lfloor>log 2 y\<rfloor>) - real_of_int (int prec - \<lfloor>log 2 y\<rfloor>))"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2173
      by (subst powr_add[symmetric]) simp
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2174
    also have "\<dots> = y"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2175
      using \<open>0 < x\<close> assms
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2176
      by (simp add: powr_add)
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2177
    also have "\<dots> \<le> truncate_up prec y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2178
      by (rule truncate_up)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2179
    finally show ?thesis .
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2180
  next
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2181
    case 3
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2182
    then show ?thesis
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2183
      using assms
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2184
      by (auto intro!: truncate_up_le)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2185
  qed
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2186
qed
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2187
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2188
lemma truncate_up_switch_sign_mono:
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2189
  assumes "x \<le> 0" "0 \<le> y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2190
  shows "truncate_up prec x \<le> truncate_up prec y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2191
proof -
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2192
  note truncate_up_nonpos[OF \<open>x \<le> 0\<close>]
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2193
  also note truncate_up_le[OF \<open>0 \<le> y\<close>]
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2194
  finally show ?thesis .
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2195
qed
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2196
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2197
lemma truncate_down_switch_sign_mono:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2198
  assumes "x \<le> 0"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2199
    and "0 \<le> y"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2200
    and "x \<le> y"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2201
  shows "truncate_down prec x \<le> truncate_down prec y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2202
proof -
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2203
  note truncate_down_le[OF \<open>x \<le> 0\<close>]
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2204
  also note truncate_down_nonneg[OF \<open>0 \<le> y\<close>]
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2205
  finally show ?thesis .
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2206
qed
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2207
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2208
lemma truncate_down_nonneg_mono:
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2209
  assumes "0 \<le> x" "x \<le> y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2210
  shows "truncate_down prec x \<le> truncate_down prec y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2211
proof -
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2212
  consider "x \<le> 0" | "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> = \<lfloor>log 2 \<bar>y\<bar>\<rfloor>" |
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2213
    "0 < x" "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<noteq> \<lfloor>log 2 \<bar>y\<bar>\<rfloor>"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2214
    by arith
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2215
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2216
  proof cases
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2217
    case 1
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2218
    with assms have "x = 0" "0 \<le> y" by simp_all
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2219
    then show ?thesis
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2220
      by (auto intro!: truncate_down_nonneg)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2221
  next
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2222
    case 2
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2223
    then show ?thesis
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2224
      using assms
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2225
      by (auto simp: truncate_down_def round_down_def intro!: floor_mono)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2226
  next
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2227
    case 3
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2228
    from \<open>0 < x\<close> have "log 2 x \<le> log 2 y" "0 < y" "0 \<le> y"
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2229
      using assms by auto
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2230
    with \<open>\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<noteq> \<lfloor>log 2 \<bar>y\<bar>\<rfloor>\<close>
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2231
    have logless: "log 2 x < log 2 y" and flogless: "\<lfloor>log 2 x\<rfloor> < \<lfloor>log 2 y\<rfloor>"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2232
      unfolding atomize_conj abs_of_pos[OF \<open>0 < x\<close>] abs_of_pos[OF \<open>0 < y\<close>]
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2233
      by (metis floor_less_cancel linorder_cases not_le)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2234
    have "2 powr prec \<le> y * 2 powr real prec / (2 powr log 2 y)"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2235
      using \<open>0 < y\<close> by simp
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2236
    also have "\<dots> \<le> y * 2 powr real (Suc prec) / (2 powr (real_of_int \<lfloor>log 2 y\<rfloor> + 1))"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2237
      using \<open>0 \<le> y\<close> \<open>0 \<le> x\<close> assms(2)
56544
b60d5d119489 made mult_pos_pos a simp rule
nipkow
parents: 56536
diff changeset
  2238
      by (auto intro!: powr_mono divide_left_mono
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2239
        simp: of_nat_diff powr_add
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2240
        powr_divide2[symmetric])
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2241
    also have "\<dots> = y * 2 powr real (Suc prec) / (2 powr real_of_int \<lfloor>log 2 y\<rfloor> * 2)"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2242
      by (auto simp: powr_add)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2243
    finally have "(2 ^ prec) \<le> \<lfloor>y * 2 powr real_of_int (int (Suc prec) - \<lfloor>log 2 \<bar>y\<bar>\<rfloor> - 1)\<rfloor>"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2244
      using \<open>0 \<le> y\<close>
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2245
      by (auto simp: powr_divide2[symmetric] le_floor_iff powr_realpow powr_add)
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2246
    then have "(2 ^ (prec)) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor>) \<le> truncate_down prec y"
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2247
      by (auto simp: truncate_down_def round_down_def)
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2248
    moreover
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2249
    {
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2250
      have "x = 2 powr (log 2 \<bar>x\<bar>)" using \<open>0 < x\<close> by simp
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2251
      also have "\<dots> \<le> (2 ^ (Suc prec )) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>)"
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2252
        using real_of_int_floor_add_one_ge[of "log 2 \<bar>x\<bar>"] \<open>0 < x\<close>
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2253
        by (auto simp: powr_realpow[symmetric] powr_add[symmetric] algebra_simps
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2254
          powr_mult_base le_powr_iff)
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2255
      also
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2256
      have "2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>) \<le> 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor> + 1)"
60500
903bb1495239 isabelle update_cartouches;
wenzelm
parents: 60376
diff changeset
  2257
        using logless flogless \<open>x > 0\<close> \<open>y > 0\<close>
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2258
        by (auto intro!: floor_mono)
62420
c7666166c24e positive precision for truncate; fixed precision for approximation of rationals; code for truncate
immler
parents: 62419
diff changeset
  2259
      finally have "x \<le> (2 ^ prec) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor>)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2260
        by (auto simp: powr_realpow[symmetric] powr_divide2[symmetric] assms of_nat_diff)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2261
    }
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2262
    ultimately show ?thesis
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2263
      by (metis dual_order.trans truncate_down)
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2264
  qed
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2265
qed
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2266
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2267
lemma truncate_down_eq_truncate_up: "truncate_down p x = - truncate_up p (-x)"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2268
  and truncate_up_eq_truncate_down: "truncate_up p x = - truncate_down p (-x)"
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2269
  by (auto simp: truncate_up_uminus_eq truncate_down_uminus_eq)
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2270
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2271
lemma truncate_down_mono: "x \<le> y \<Longrightarrow> truncate_down p x \<le> truncate_down p y"
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2272
  apply (cases "0 \<le> x")
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2273
  apply (rule truncate_down_nonneg_mono, assumption+)
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2274
  apply (simp add: truncate_down_eq_truncate_up)
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2275
  apply (cases "0 \<le> y")
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2276
  apply (auto intro: truncate_up_nonneg_mono truncate_up_switch_sign_mono)
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2277
  done
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2278
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2279
lemma truncate_up_mono: "x \<le> y \<Longrightarrow> truncate_up p x \<le> truncate_up p y"
58982
27e7e3f9e665 simplified computations based on round_up by reducing to round_down;
immler
parents: 58881
diff changeset
  2280
  by (simp add: truncate_up_eq_truncate_down truncate_down_mono)
54784
54f1ce13c140 monotonicity of rounding and truncating float
immler
parents: 54783
diff changeset
  2281
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2282
lemma Float_le_zero_iff: "Float a b \<le> 0 \<longleftrightarrow> a \<le> 0"
60017
b785d6d06430 Overloading of ln and powr, but "approximation" no longer works for powr. Code generation also fails due to type ambiguity in scala.
paulson <lp15@cam.ac.uk>
parents: 59984
diff changeset
  2283
 by (auto simp: zero_float_def mult_le_0_iff) (simp add: not_less [symmetric])
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2284
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2285
lemma real_of_float_pprt[simp]:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2286
  fixes a :: float
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2287
  shows "real_of_float (pprt a) = pprt (real_of_float a)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2288
  unfolding pprt_def sup_float_def max_def sup_real_def by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2289
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2290
lemma real_of_float_nprt[simp]:
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2291
  fixes a :: float
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2292
  shows "real_of_float (nprt a) = nprt (real_of_float a)"
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2293
  unfolding nprt_def inf_float_def min_def inf_real_def by auto
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2294
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2295
context
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2296
begin
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2297
55565
f663fc1e653b simplify proofs because of the stronger reflexivity prover
kuncar
parents: 54784
diff changeset
  2298
lift_definition int_floor_fl :: "float \<Rightarrow> int" is floor .
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2299
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2300
qualified lemma compute_int_floor_fl[code]:
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2301
  "int_floor_fl (Float m e) = (if 0 \<le> e then m * 2 ^ nat e else m div (2 ^ (nat (-e))))"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2302
  apply transfer
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2303
  apply (simp add: powr_int floor_divide_of_int_eq)
61942
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
  2304
  apply (metis (no_types, hide_lams)floor_divide_of_int_eq of_int_numeral of_int_power floor_of_int of_int_mult)
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
  2305
  done
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2306
61942
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
  2307
lift_definition floor_fl :: "float \<Rightarrow> float" is "\<lambda>x. real_of_int \<lfloor>x\<rfloor>"
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
  2308
  by simp
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2309
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2310
qualified lemma compute_floor_fl[code]:
47601
050718fe6eee use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
hoelzl
parents: 47600
diff changeset
  2311
  "floor_fl (Float m e) = (if 0 \<le> e then Float m e else Float (m div (2 ^ (nat (-e)))) 0)"
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2312
  apply transfer
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2313
  apply (simp add: powr_int floor_divide_of_int_eq)
61942
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
  2314
  apply (metis (no_types, hide_lams)floor_divide_of_int_eq of_int_numeral of_int_power of_int_mult)
f02b26f7d39d prefer symbols for "floor", "ceiling";
wenzelm
parents: 61799
diff changeset
  2315
  done
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2316
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2317
end
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2318
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2319
lemma floor_fl: "real_of_float (floor_fl x) \<le> real_of_float x"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2320
  by transfer simp
47600
e12289b5796b use lifting to introduce floating point numbers
hoelzl
parents: 47599
diff changeset
  2321
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2322
lemma int_floor_fl: "real_of_int (int_floor_fl x) \<le> real_of_float x"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2323
  by transfer simp
29804
e15b74577368 Added new Float theory and moved old Library/Float.thy to ComputeFloat
hoelzl
parents: 29667
diff changeset
  2324
47599
400b158f1589 replace the float datatype by a type with unique representation
hoelzl
parents: 47230
diff changeset
  2325
lemma floor_pos_exp: "exponent (floor_fl x) \<ge> 0"
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  2326
proof (cases "floor_fl x = float_of 0")
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  2327
  case True
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2328
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2329
    by (simp add: floor_fl_def)
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  2330
next
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  2331
  case False
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2332
  have eq: "floor_fl x = Float \<lfloor>real_of_float x\<rfloor> 0"
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2333
    by transfer simp
61609
77b453bd616f Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
paulson <lp15@cam.ac.uk>
parents: 60868
diff changeset
  2334
  obtain i where "\<lfloor>real_of_float x\<rfloor> = mantissa (floor_fl x) * 2 ^ i" "0 = exponent (floor_fl x) - int i"
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  2335
    by (rule denormalize_shift[OF eq[THEN eq_reflection] False])
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2336
  then show ?thesis
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2337
    by simp
53381
355a4cac5440 tuned proofs -- less guessing;
wenzelm
parents: 53215
diff changeset
  2338
qed
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2339
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2340
lemma compute_mantissa[code]:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2341
  "mantissa (Float m e) =
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2342
    (if m = 0 then 0 else if 2 dvd m then mantissa (normfloat (Float m e)) else m)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2343
  by (auto simp: mantissa_float Float.abs_eq)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2344
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2345
lemma compute_exponent[code]:
60698
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2346
  "exponent (Float m e) =
29e8bdc41f90 tuned proofs;
wenzelm
parents: 60679
diff changeset
  2347
    (if m = 0 then 0 else if 2 dvd m then exponent (normfloat (Float m e)) else e)"
58985
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2348
  by (auto simp: exponent_float Float.abs_eq)
bf498e0af9e3 truncate intermediate results in horner to improve performance of approximate;
immler
parents: 58982
diff changeset
  2349
16782
b214f21ae396 - use TableFun instead of homebrew binary tree in am_interpreter.ML
obua
parents:
diff changeset
  2350
end