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