| author | wenzelm | 
| Thu, 10 Nov 2022 14:55:20 +0100 | |
| changeset 76506 | ac5833ebe6d1 | 
| parent 73932 | fd21b4a93043 | 
| child 76796 | 454984e807db | 
| 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" | |
| 68406 | 85 | by (simp add: powr_diff field_simps flip: powr_realpow) | 
| 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) | 
| 68406 | 135 | apply (simp flip: powr_realpow powr_add add: field_simps) | 
| 47599 
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) | 
| 68406 | 144 | apply (simp flip: powr_realpow powr_add add: field_simps) | 
| 47599 
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 | |
| 67399 | 202 | lift_definition plus_float :: "float \<Rightarrow> float \<Rightarrow> float" is "(+)" 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 | |
| 69064 
5840724b1d71
Prefix form of infix with * on either side no longer needs special treatment
 nipkow parents: 
68406diff
changeset | 205 | lift_definition times_float :: "float \<Rightarrow> float \<Rightarrow> float" is "(*)" 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 | |
| 67399 | 208 | lift_definition minus_float :: "float \<Rightarrow> float \<Rightarrow> float" is "(-)" 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 | |
| 67399 | 220 | lift_definition equal_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "(=) :: real \<Rightarrow> real \<Rightarrow> bool" . | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 221 | |
| 67399 | 222 | lift_definition less_eq_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "(\<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 | |
| 67399 | 225 | lift_definition less_float :: "float \<Rightarrow> float \<Rightarrow> bool" is "(<)" . | 
| 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]: | 
| 67399 | 293 | "rel_fun (=) 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]: | 
| 67399 | 300 | "rel_fun (=) 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 | |
| 67573 | 303 | lemma float_of_numeral: "numeral k = float_of (numeral k)" | 
| 304 | and float_of_neg_numeral: "- 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 | |
| 72607 | 321 | context | 
| 322 | includes term_syntax | |
| 323 | begin | |
| 324 | ||
| 325 | definition [code_unfold]: | |
| 58987 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 326 |   "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 | 327 | |
| 72607 | 328 | end | 
| 329 | ||
| 58987 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 330 | instantiation float :: full_exhaustive | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 331 | begin | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 332 | |
| 63356 | 333 | definition | 
| 58987 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 334 | "full_exhaustive_float f d = | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 335 | Quickcheck_Exhaustive.full_exhaustive | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 336 | (\<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 | 337 | |
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 338 | instance .. | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 339 | |
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 340 | end | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 341 | |
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 342 | instantiation float :: random | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 343 | begin | 
| 
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 | definition "Quickcheck_Random.random i = | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 346 | 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 | 347 | (\<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 | 348 | |
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 349 | instance .. | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 350 | |
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 351 | end | 
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 352 | |
| 
119680ebf37c
quickcheck setup for float, inspired by rat::{exhaustive,full_exhaustive,random}
 immler parents: 
58985diff
changeset | 353 | |
| 60500 | 354 | subsection \<open>Represent floats as unique mantissa and exponent\<close> | 
| 47108 
2a1953f0d20d
merged fork with new numeral representation (see NEWS)
 huffman parents: 
46670diff
changeset | 355 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 356 | lemma int_induct_abs[case_names less]: | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 357 | fixes j :: int | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 358 | 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 | 359 | shows "P j" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 360 | proof (induct "nat \<bar>j\<bar>" arbitrary: j rule: less_induct) | 
| 60698 | 361 | case less | 
| 362 | show ?case by (rule H[OF less]) simp | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 363 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 364 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 365 | lemma int_cancel_factors: | 
| 60698 | 366 | fixes n :: int | 
| 367 | assumes "1 < r" | |
| 368 | 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 | 369 | proof (induct n rule: int_induct_abs) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 370 | case (less n) | 
| 60698 | 371 | have "\<exists>k i. n = k * r ^ Suc i \<and> \<not> r dvd k" if "n \<noteq> 0" "n = m * r" for m | 
| 372 | proof - | |
| 373 | from that have "\<bar>m \<bar> < \<bar>n\<bar>" | |
| 60500 | 374 | using \<open>1 < r\<close> by (simp add: abs_mult) | 
| 60698 | 375 | from less[OF this] that show ?thesis by auto | 
| 376 | qed | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 377 | then show ?case | 
| 59554 
4044f53326c9
inlined rules to free user-space from technical names
 haftmann parents: 
59487diff
changeset | 378 | 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 | 379 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 380 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 381 | lemma mult_powr_eq_mult_powr_iff_asym: | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 382 | fixes m1 m2 e1 e2 :: int | 
| 60698 | 383 | assumes m1: "\<not> 2 dvd m1" | 
| 384 | and "e1 \<le> e2" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 385 | shows "m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2" | 
| 60698 | 386 | (is "?lhs \<longleftrightarrow> ?rhs") | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 387 | proof | 
| 60698 | 388 | show ?rhs if eq: ?lhs | 
| 389 | proof - | |
| 390 | have "m1 \<noteq> 0" | |
| 391 | using m1 unfolding dvd_def by auto | |
| 392 | from \<open>e1 \<le> e2\<close> eq have "m1 = m2 * 2 powr nat (e2 - e1)" | |
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 393 | by (simp add: powr_diff field_simps) | 
| 60698 | 394 | also have "\<dots> = m2 * 2^nat (e2 - e1)" | 
| 395 | by (simp add: powr_realpow) | |
| 396 | 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 | 397 | by linarith | 
| 60698 | 398 | with m1 have "m1 = m2" | 
| 399 | by (cases "nat (e2 - e1)") (auto simp add: dvd_def) | |
| 400 | then show ?thesis | |
| 401 | using eq \<open>m1 \<noteq> 0\<close> by (simp add: powr_inj) | |
| 402 | qed | |
| 403 | show ?lhs if ?rhs | |
| 404 | using that by simp | |
| 405 | qed | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 406 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 407 | lemma mult_powr_eq_mult_powr_iff: | 
| 63356 | 408 | "\<not> 2 dvd m1 \<Longrightarrow> \<not> 2 dvd m2 \<Longrightarrow> m1 * 2 powr e1 = m2 * 2 powr e2 \<longleftrightarrow> m1 = m2 \<and> e1 = e2" | 
| 409 | for m1 m2 e1 e2 :: int | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 410 | 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 | 411 | 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 | 412 | by (cases e1 e2 rule: linorder_le_cases) auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 413 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 414 | lemma floatE_normed: | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 415 | assumes x: "x \<in> float" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 416 | obtains (zero) "x = 0" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 417 | | (powr) m e :: int where "x = m * 2 powr e" "\<not> 2 dvd m" "x \<noteq> 0" | 
| 60698 | 418 | proof - | 
| 63356 | 419 | have "\<exists>(m::int) (e::int). x = m * 2 powr e \<and> \<not> (2::int) dvd m" if "x \<noteq> 0" | 
| 420 | proof - | |
| 60698 | 421 | from x obtain m e :: int where x: "x = m * 2 powr e" | 
| 422 | by (auto simp: float_def) | |
| 60500 | 423 | 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 | 424 | by auto | 
| 63356 | 425 | with \<open>\<not> 2 dvd k\<close> x show ?thesis | 
| 426 | apply (rule_tac exI[of _ "k"]) | |
| 427 | apply (rule_tac exI[of _ "e + int i"]) | |
| 428 | apply (simp add: powr_add powr_realpow) | |
| 429 | done | |
| 430 | qed | |
| 60698 | 431 | with that show thesis by blast | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 432 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 433 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 434 | lemma float_normed_cases: | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 435 | fixes f :: float | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 436 | 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 | 437 | | (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 | 438 | proof (atomize_elim, induct f) | 
| 60698 | 439 | case (float_of y) | 
| 440 | then show ?case | |
| 47600 | 441 | 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 | 442 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 443 | |
| 63356 | 444 | definition mantissa :: "float \<Rightarrow> int" | 
| 445 | where "mantissa f = | |
| 446 | fst (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0) \<or> | |
| 447 | (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 | 448 | |
| 63356 | 449 | definition exponent :: "float \<Rightarrow> int" | 
| 450 | where "exponent f = | |
| 451 | snd (SOME p::int \<times> int. (f = 0 \<and> fst p = 0 \<and> snd p = 0) \<or> | |
| 452 | (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 | 453 | |
| 67573 | 454 | lemma exponent_0[simp]: "exponent 0 = 0" (is ?E) | 
| 455 | and mantissa_0[simp]: "mantissa 0 = 0" (is ?M) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 456 | proof - | 
| 60698 | 457 | have "\<And>p::int \<times> int. fst p = 0 \<and> snd p = 0 \<longleftrightarrow> p = (0, 0)" | 
| 458 | by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 459 | then show ?E ?M | 
| 47600 | 460 | 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 | 461 | qed | 
| 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 462 | |
| 63356 | 463 | lemma mantissa_exponent: "real_of_float f = mantissa f * 2 powr exponent f" (is ?E) | 
| 67573 | 464 | and mantissa_not_dvd: "f \<noteq> 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 | 465 | proof cases | 
| 67573 | 466 | assume [simp]: "f \<noteq> 0" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 467 | 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 | 468 | proof (cases f rule: float_normed_cases) | 
| 60698 | 469 | case zero | 
| 67573 | 470 | then show ?thesis by simp | 
| 60698 | 471 | next | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 472 | case (powr m e) | 
| 60698 | 473 | 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 | 474 | (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 | 475 | by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 476 | then show ?thesis | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 477 | unfolding exponent_def mantissa_def | 
| 67573 | 478 | by (rule someI2_ex) simp | 
| 60698 | 479 | qed | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 480 | then show ?E ?D by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 481 | qed simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 482 | |
| 67573 | 483 | lemma mantissa_noteq_0: "f \<noteq> 0 \<Longrightarrow> mantissa f \<noteq> 0" | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 484 | using mantissa_not_dvd[of f] by auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 485 | |
| 67573 | 486 | lemma mantissa_eq_zero_iff: "mantissa x = 0 \<longleftrightarrow> x = 0" | 
| 487 | (is "?lhs \<longleftrightarrow> ?rhs") | |
| 488 | proof | |
| 489 | show ?rhs if ?lhs | |
| 490 | proof - | |
| 491 | from that have z: "0 = real_of_float x" | |
| 492 | using mantissa_exponent by simp | |
| 493 | show ?thesis | |
| 494 | by (simp add: zero_float_def z) | |
| 495 | qed | |
| 496 | show ?lhs if ?rhs | |
| 497 | using that by simp | |
| 498 | qed | |
| 499 | ||
| 500 | lemma mantissa_pos_iff: "0 < mantissa x \<longleftrightarrow> 0 < x" | |
| 70817 
dd675800469d
dedicated fact collections for algebraic simplification rules potentially splitting goals
 haftmann parents: 
70355diff
changeset | 501 | by (auto simp: mantissa_exponent algebra_split_simps) | 
| 67573 | 502 | |
| 503 | lemma mantissa_nonneg_iff: "0 \<le> mantissa x \<longleftrightarrow> 0 \<le> x" | |
| 70817 
dd675800469d
dedicated fact collections for algebraic simplification rules potentially splitting goals
 haftmann parents: 
70355diff
changeset | 504 | by (auto simp: mantissa_exponent algebra_split_simps) | 
| 67573 | 505 | |
| 506 | lemma mantissa_neg_iff: "0 > mantissa x \<longleftrightarrow> 0 > x" | |
| 70817 
dd675800469d
dedicated fact collections for algebraic simplification rules potentially splitting goals
 haftmann parents: 
70355diff
changeset | 507 | by (auto simp: mantissa_exponent algebra_split_simps) | 
| 67573 | 508 | |
| 53381 | 509 | lemma | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 510 | fixes m e :: int | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 511 | defines "f \<equiv> float_of (m * 2 powr e)" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 512 | assumes dvd: "\<not> 2 dvd m" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 513 | shows mantissa_float: "mantissa f = m" (is "?M") | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 514 | 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 | 515 | proof cases | 
| 60698 | 516 | assume "m = 0" | 
| 517 | with dvd show "mantissa f = m" by auto | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 518 | next | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 519 | assume "m \<noteq> 0" | 
| 67573 | 520 | then have f_not_0: "f \<noteq> 0" by (simp add: f_def zero_float_def) | 
| 60698 | 521 | 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 | 522 | by (auto simp add: f_def) | 
| 63356 | 523 | then show ?M ?E | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 524 | using mantissa_not_dvd[OF f_not_0] dvd | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 525 | by (auto simp: mult_powr_eq_mult_powr_iff) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 526 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 527 | |
| 60698 | 528 | |
| 60500 | 529 | subsection \<open>Compute arithmetic operations\<close> | 
| 47600 | 530 | |
| 531 | lemma Float_mantissa_exponent: "Float (mantissa f) (exponent f) = f" | |
| 532 | unfolding real_of_float_eq mantissa_exponent[of f] by simp | |
| 533 | ||
| 60698 | 534 | lemma Float_cases [cases type: float]: | 
| 47600 | 535 | fixes f :: float | 
| 536 | obtains (Float) m e :: int where "f = Float m e" | |
| 537 | using Float_mantissa_exponent[symmetric] | |
| 538 | by (atomize_elim) auto | |
| 539 | ||
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 540 | lemma denormalize_shift: | 
| 67573 | 541 | assumes f_def: "f = Float m e" | 
| 542 | and not_0: "f \<noteq> 0" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 543 | 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 | 544 | proof | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 545 | from mantissa_exponent[of f] f_def | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 546 | 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 | 547 | by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 548 | then have eq: "m = mantissa f * 2 powr (exponent f - e)" | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 549 | by (simp add: powr_diff field_simps) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 550 | moreover | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 551 | have "e \<le> exponent f" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 552 | proof (rule ccontr) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 553 | assume "\<not> e \<le> exponent f" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 554 | 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 | 555 | 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 | 556 | by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 557 | also have "\<dots> = 1 / 2^nat (e - exponent f)" | 
| 68406 | 558 | using pos by (simp flip: powr_realpow add: powr_diff) | 
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 559 | 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 | 560 | using eq by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 561 | 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 | 562 | by linarith | 
| 60500 | 563 | 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 | 564 | 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 | 565 | apply (cases "nat (e - exponent f)") | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 566 | apply auto | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 567 | done | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 568 | 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 | 569 | 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 | 570 | ultimately have "real_of_int m = mantissa f * 2^nat (exponent f - e)" | 
| 68406 | 571 | by (simp flip: powr_realpow) | 
| 60500 | 572 | with \<open>e \<le> exponent f\<close> | 
| 63356 | 573 | 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 | 574 | 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 | 575 | show "e = exponent f - nat (exponent f - e)" | 
| 61799 | 576 | 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 | 577 | qed | 
| 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 578 | |
| 60698 | 579 | context | 
| 580 | begin | |
| 47600 | 581 | |
| 60698 | 582 | qualified lemma compute_float_zero[code_unfold, code]: "0 = Float 0 0" | 
| 47600 | 583 | by transfer simp | 
| 60698 | 584 | |
| 585 | qualified lemma compute_float_one[code_unfold, code]: "1 = Float 1 0" | |
| 586 | by transfer simp | |
| 47600 | 587 | |
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 588 | 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 | 589 | lemma normloat_id[simp]: "normfloat x = x" by transfer rule | 
| 47600 | 590 | |
| 63356 | 591 | qualified lemma compute_normfloat[code]: | 
| 592 | "normfloat (Float m e) = | |
| 593 | (if m mod 2 = 0 \<and> m \<noteq> 0 then normfloat (Float (m div 2) (e + 1)) | |
| 594 | else if m = 0 then 0 else Float m e)" | |
| 47600 | 595 | 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 | 596 | |
| 60698 | 597 | qualified lemma compute_float_numeral[code_abbrev]: "Float (numeral k) 0 = numeral k" | 
| 47600 | 598 | by transfer simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 599 | |
| 60698 | 600 | qualified lemma compute_float_neg_numeral[code_abbrev]: "Float (- numeral k) 0 = - numeral k" | 
| 47600 | 601 | by transfer simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 602 | |
| 60698 | 603 | qualified lemma compute_float_uminus[code]: "- Float m1 e1 = Float (- m1) e1" | 
| 47600 | 604 | by transfer simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 605 | |
| 60698 | 606 | qualified lemma compute_float_times[code]: "Float m1 e1 * Float m2 e2 = Float (m1 * m2) (e1 + e2)" | 
| 47600 | 607 | by transfer (simp add: field_simps powr_add) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 608 | |
| 63356 | 609 | qualified lemma compute_float_plus[code]: | 
| 610 | "Float m1 e1 + Float m2 e2 = | |
| 611 | (if m1 = 0 then Float m2 e2 | |
| 612 | else if m2 = 0 then Float m1 e1 | |
| 613 | else if e1 \<le> e2 then Float (m1 + m2 * 2^nat (e2 - e1)) e1 | |
| 614 | else Float (m2 + m1 * 2^nat (e1 - e2)) e2)" | |
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 615 | by transfer (simp add: field_simps powr_realpow[symmetric] powr_diff) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 616 | |
| 63356 | 617 | qualified lemma compute_float_minus[code]: "f - g = f + (-g)" for f g :: float | 
| 47600 | 618 | by simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 619 | |
| 63356 | 620 | qualified lemma compute_float_sgn[code]: | 
| 621 | "sgn (Float m1 e1) = (if 0 < m1 then 1 else if m1 < 0 then -1 else 0)" | |
| 64240 | 622 | by transfer (simp add: sgn_mult) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 623 | |
| 67399 | 624 | lift_definition is_float_pos :: "float \<Rightarrow> bool" is "(<) 0 :: real \<Rightarrow> bool" . | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 625 | |
| 60698 | 626 | qualified lemma compute_is_float_pos[code]: "is_float_pos (Float m e) \<longleftrightarrow> 0 < m" | 
| 47600 | 627 | 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 | 628 | |
| 67399 | 629 | lift_definition is_float_nonneg :: "float \<Rightarrow> bool" is "(\<le>) 0 :: real \<Rightarrow> bool" . | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 630 | |
| 60698 | 631 | qualified lemma compute_is_float_nonneg[code]: "is_float_nonneg (Float m e) \<longleftrightarrow> 0 \<le> m" | 
| 47600 | 632 | 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 | 633 | |
| 67399 | 634 | lift_definition is_float_zero :: "float \<Rightarrow> bool" is "(=) 0 :: real \<Rightarrow> bool" . | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 635 | |
| 60698 | 636 | qualified lemma compute_is_float_zero[code]: "is_float_zero (Float m e) \<longleftrightarrow> 0 = m" | 
| 47600 | 637 | 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 | 638 | |
| 61945 | 639 | qualified lemma compute_float_abs[code]: "\<bar>Float m e\<bar> = Float \<bar>m\<bar> e" | 
| 47600 | 640 | by transfer (simp add: abs_mult) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 641 | |
| 60698 | 642 | qualified lemma compute_float_eq[code]: "equal_class.equal f g = is_float_zero (f - g)" | 
| 47600 | 643 | by transfer simp | 
| 60698 | 644 | |
| 645 | end | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 646 | |
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 647 | |
| 69593 | 648 | subsection \<open>Lemmas for types \<^typ>\<open>real\<close>, \<^typ>\<open>nat\<close>, \<^typ>\<open>int\<close>\<close> | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 649 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 650 | 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 | 651 | 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 | 652 | 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 | 653 | 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 | 654 | 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 | 655 | 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 | 656 | of_int_numeral of_int_neg_numeral | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 657 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 658 | lemmas int_of_reals = real_of_ints[symmetric] | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 659 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 660 | |
| 60500 | 661 | subsection \<open>Rounding Real Numbers\<close> | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 662 | |
| 60698 | 663 | definition round_down :: "int \<Rightarrow> real \<Rightarrow> real" | 
| 61942 | 664 | 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 | 665 | |
| 60698 | 666 | definition round_up :: "int \<Rightarrow> real \<Rightarrow> real" | 
| 61942 | 667 | 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 | 668 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 669 | 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 | 670 | unfolding round_down_def | 
| 68406 | 671 | by (auto intro!: times_float simp flip: of_int_minus) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 672 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 673 | 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 | 674 | unfolding round_up_def | 
| 68406 | 675 | by (auto intro!: times_float simp flip: of_int_minus) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 676 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 677 | 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 | 678 | 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 | 679 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 680 | lemma round_down: "round_down prec x \<le> x" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 681 | 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 | 682 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 683 | 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 | 684 | unfolding round_up_def by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 685 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 686 | 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 | 687 | unfolding round_down_def by simp | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 688 | |
| 63356 | 689 | 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 | 690 | proof - | 
| 63356 | 691 | 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 | 692 | 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 | 693 | also have "\<dots> \<le> 1 * 2 powr -prec" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 694 | by (rule mult_mono) | 
| 68406 | 695 | (auto simp flip: of_int_diff simp: ceiling_diff_floor_le_1) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 696 | finally show ?thesis by simp | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 697 | qed | 
| 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 698 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 699 | 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 | 700 | unfolding round_down_def | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 701 | by (simp add: powr_add powr_mult field_simps powr_diff) | 
| 68406 | 702 | (simp flip: powr_add) | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 703 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 704 | 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 | 705 | unfolding round_up_def | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 706 | by (simp add: powr_add powr_mult field_simps powr_diff) | 
| 68406 | 707 | (simp flip: powr_add) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 708 | |
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 709 | 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 | 710 | 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 | 711 | 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 | 712 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 713 | 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 | 714 | 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 | 715 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 716 | lemma round_up_le1: | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 717 | assumes "x \<le> 1" "prec \<ge> 0" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 718 | shows "round_up prec x \<le> 1" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 719 | 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 | 720 | 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 | 721 | using assms by (auto intro!: ceiling_mono) | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 722 | 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 | 723 | finally show ?thesis | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 724 | 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 | 725 | qed | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 726 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 727 | lemma round_up_less1: | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 728 | assumes "x < 1 / 2" "p > 0" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 729 | shows "round_up p x < 1" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 730 | proof - | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 731 | 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 | 732 | using assms by simp | 
| 60500 | 733 | also have "\<dots> \<le> 2 powr p - 1" using \<open>p > 0\<close> | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 734 | by (auto simp: powr_diff powr_int field_simps self_le_power) | 
| 60500 | 735 | 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 | 736 | 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 | 737 | qed | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 738 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 739 | lemma round_down_ge1: | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 740 | assumes x: "x \<ge> 1" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 741 | assumes prec: "p \<ge> - log 2 x" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 742 | shows "1 \<le> round_down p x" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 743 | proof cases | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 744 | 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 | 745 | 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 | 746 | 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 | 747 | 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 | 748 | using assms by (auto intro!: floor_mono) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 749 | finally show ?thesis | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 750 | 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 | 751 | next | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 752 | assume neg: "\<not> 0 \<le> p" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 753 | have "x = 2 powr (log 2 x)" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 754 | using x by simp | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 755 | 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 | 756 | using prec by auto | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 757 | 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 | 758 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 759 | 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 | 760 | 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 | 761 | 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 | 762 | 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 | 763 | unfolding of_int_le_iff | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 764 | 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 | 765 | finally show ?thesis | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 766 | using prec x | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 767 | 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 | 768 | qed | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 769 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 770 | 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 | 771 | unfolding round_up_def | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 772 | 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 | 773 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 774 | |
| 60500 | 775 | subsection \<open>Rounding Floats\<close> | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 776 | |
| 60698 | 777 | definition div_twopow :: "int \<Rightarrow> nat \<Rightarrow> int" | 
| 778 | 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 | 779 | |
| 60698 | 780 | definition mod_twopow :: "int \<Rightarrow> nat \<Rightarrow> int" | 
| 781 | 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 | 782 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 783 | lemma compute_div_twopow[code]: | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 784 | "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 | 785 | 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 | 786 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 787 | lemma compute_mod_twopow[code]: | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 788 | "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 | 789 | by (cases n) (auto simp: zmod_zmult2_eq) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 790 | |
| 47600 | 791 | 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 | 792 | declare float_up.rep_eq[simp] | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 793 | |
| 60698 | 794 | lemma round_up_correct: "round_up e f - f \<in> {0..2 powr -e}"
 | 
| 795 | unfolding atLeastAtMost_iff | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 796 | proof | 
| 60698 | 797 | have "round_up e f - f \<le> round_up e f - round_down e f" | 
| 798 | using round_down by simp | |
| 799 | also have "\<dots> \<le> 2 powr -e" | |
| 800 | 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 | 801 | finally show "round_up e f - f \<le> 2 powr - (real_of_int e)" | 
| 47600 | 802 | by simp | 
| 803 | 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 | 804 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 805 | lemma float_up_correct: "real_of_float (float_up e f) - real_of_float f \<in> {0..2 powr -e}"
 | 
| 54782 | 806 | by transfer (rule round_up_correct) | 
| 807 | ||
| 47600 | 808 | 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 | 809 | declare float_down.rep_eq[simp] | 
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 810 | |
| 60698 | 811 | lemma round_down_correct: "f - (round_down e f) \<in> {0..2 powr -e}"
 | 
| 812 | unfolding atLeastAtMost_iff | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 813 | proof | 
| 60698 | 814 | have "f - round_down e f \<le> round_up e f - round_down e f" | 
| 815 | using round_up by simp | |
| 816 | also have "\<dots> \<le> 2 powr -e" | |
| 817 | 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 | 818 | finally show "f - round_down e f \<le> 2 powr - (real_of_int e)" | 
| 47600 | 819 | by simp | 
| 820 | qed (simp add: algebra_simps round_down) | |
| 24301 | 821 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 822 | lemma float_down_correct: "real_of_float f - real_of_float (float_down e f) \<in> {0..2 powr -e}"
 | 
| 54782 | 823 | by transfer (rule round_down_correct) | 
| 824 | ||
| 60698 | 825 | context | 
| 826 | begin | |
| 827 | ||
| 828 | qualified lemma compute_float_down[code]: | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 829 | "float_down p (Float m e) = | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 830 | (if p + e < 0 then Float (div_twopow m (nat (-(p + e)))) (-p) else Float m e)" | 
| 60698 | 831 | proof (cases "p + e < 0") | 
| 832 | 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 | 833 | 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 | 834 | using powr_realpow[of 2 "nat (-(p + e))"] by simp | 
| 60698 | 835 | 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 | 836 | 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 | 837 | finally show ?thesis | 
| 60500 | 838 | 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 | 839 | apply transfer | 
| 70355 | 840 | apply (simp add: round_down_def field_simps flip: floor_divide_of_int_eq) | 
| 73932 
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
 desharna parents: 
73655diff
changeset | 841 | apply (metis (no_types, opaque_lifting) Float.rep_eq | 
| 70355 | 842 | add.inverse_inverse compute_real_of_float diff_minus_eq_add | 
| 843 | floor_divide_of_int_eq int_of_reals(1) linorder_not_le | |
| 844 | minus_add_distrib of_int_eq_numeral_power_cancel_iff powr_add) | |
| 845 | done | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 846 | next | 
| 60698 | 847 | case False | 
| 63356 | 848 | then have r: "real_of_int e + real_of_int p = real (nat (e + p))" | 
| 849 | 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 | 850 | 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 | 851 | by (auto intro: exI[where x="m*2^nat (e+p)"] | 
| 63356 | 852 | simp add: ac_simps powr_add[symmetric] r powr_realpow) | 
| 60500 | 853 | with \<open>\<not> p + e < 0\<close> show ?thesis | 
| 57862 | 854 | 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 | 855 | qed | 
| 24301 | 856 | |
| 54782 | 857 | lemma abs_round_down_le: "\<bar>f - (round_down e f)\<bar> \<le> 2 powr -e" | 
| 858 | using round_down_correct[of f e] by simp | |
| 859 | ||
| 860 | lemma abs_round_up_le: "\<bar>f - (round_up e f)\<bar> \<le> 2 powr -e" | |
| 861 | using round_up_correct[of e f] by simp | |
| 862 | ||
| 863 | lemma round_down_nonneg: "0 \<le> s \<Longrightarrow> 0 \<le> round_down p s" | |
| 56536 | 864 | by (auto simp: round_down_def) | 
| 54782 | 865 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 866 | lemma ceil_divide_floor_conv: | 
| 60698 | 867 | assumes "b \<noteq> 0" | 
| 63356 | 868 | shows "\<lceil>real_of_int a / real_of_int b\<rceil> = | 
| 869 | (if b dvd a then a div b else \<lfloor>real_of_int a / real_of_int b\<rfloor> + 1)" | |
| 60698 | 870 | proof (cases "b dvd a") | 
| 871 | case True | |
| 872 | then show ?thesis | |
| 68406 | 873 | by (simp add: ceiling_def floor_divide_of_int_eq dvd_neg_div | 
| 874 | flip: of_int_minus divide_minus_left) | |
| 60698 | 875 | next | 
| 876 | case False | |
| 877 | then have "a mod b \<noteq> 0" | |
| 878 | 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 | 879 | then have ne: "real_of_int (a mod b) / real_of_int b \<noteq> 0" | 
| 60698 | 880 | 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 | 881 | have "\<lceil>real_of_int a / real_of_int b\<rceil> = \<lfloor>real_of_int a / real_of_int b\<rfloor> + 1" | 
| 60698 | 882 | apply (rule ceiling_eq) | 
| 68406 | 883 | apply (auto simp flip: floor_divide_of_int_eq) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 884 | 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 | 885 | have "real_of_int \<lfloor>real_of_int a / real_of_int b\<rfloor> \<le> real_of_int a / real_of_int b" | 
| 60698 | 886 | 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 | 887 | 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 | 888 | 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 | 889 | unfolding floor_divide_of_int_eq | 
| 60698 | 890 | using ne \<open>b \<noteq> 0\<close> apply auto | 
| 891 | 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 | 892 | 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 | 893 | qed | 
| 60698 | 894 | then show ?thesis | 
| 895 | using \<open>\<not> b dvd a\<close> by simp | |
| 896 | qed | |
| 19765 | 897 | |
| 60698 | 898 | 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 | 899 | by transfer (simp add: round_down_uminus_eq) | 
| 60698 | 900 | |
| 901 | end | |
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 902 | |
| 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 903 | |
| 63664 | 904 | lemma bitlen_Float: | 
| 905 | fixes m e | |
| 67573 | 906 | defines [THEN meta_eq_to_obj_eq]: "f \<equiv> Float m e" | 
| 63664 | 907 | shows "bitlen \<bar>mantissa f\<bar> + exponent f = (if m = 0 then 0 else bitlen \<bar>m\<bar> + e)" | 
| 908 | proof (cases "m = 0") | |
| 909 | case True | |
| 67573 | 910 | then show ?thesis by (simp add: f_def bitlen_alt_def) | 
| 63664 | 911 | next | 
| 912 | case False | |
| 67573 | 913 | then have "f \<noteq> 0" | 
| 63664 | 914 | unfolding real_of_float_eq by (simp add: f_def) | 
| 915 | then have "mantissa f \<noteq> 0" | |
| 67573 | 916 | by (simp add: mantissa_eq_zero_iff) | 
| 63664 | 917 | moreover | 
| 918 | obtain i where "m = mantissa f * 2 ^ i" "e = exponent f - int i" | |
| 67573 | 919 | by (rule f_def[THEN denormalize_shift, OF \<open>f \<noteq> 0\<close>]) | 
| 63664 | 920 | ultimately show ?thesis by (simp add: abs_mult) | 
| 921 | qed | |
| 922 | ||
| 63356 | 923 | lemma float_gt1_scale: | 
| 924 | assumes "1 \<le> Float m e" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 925 | shows "0 \<le> e + (bitlen m - 1)" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 926 | proof - | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 927 | have "0 < Float m e" using assms by auto | 
| 60698 | 928 | then have "0 < m" using powr_gt_zero[of 2 e] | 
| 67573 | 929 | by (auto simp: zero_less_mult_iff) | 
| 60698 | 930 | then have "m \<noteq> 0" by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 931 | show ?thesis | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 932 | proof (cases "0 \<le> e") | 
| 60698 | 933 | case True | 
| 934 | then show ?thesis | |
| 63248 | 935 | 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 | 936 | next | 
| 60698 | 937 | case False | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 938 | have "(1::int) < 2" by simp | 
| 60698 | 939 | let ?S = "2^(nat (-e))" | 
| 940 | have "inverse (2 ^ nat (- e)) = 2 powr e" | |
| 941 | using assms False powr_realpow[of 2 "nat (-e)"] | |
| 57862 | 942 | 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 | 943 | then have "1 \<le> real_of_int m * inverse ?S" | 
| 60698 | 944 | 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 | 945 | 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 | 946 | then have "1 * ?S \<le> real_of_int m * inverse ?S * ?S" | 
| 60698 | 947 | 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 | 948 | then have "?S \<le> real_of_int m" | 
| 60698 | 949 | unfolding mult.assoc by auto | 
| 950 | 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 | 951 | unfolding of_int_le_iff[symmetric] by auto | 
| 60500 | 952 | from this bitlen_bounds[OF \<open>0 < m\<close>, THEN conjunct2] | 
| 60698 | 953 | have "nat (-e) < (nat (bitlen m))" | 
| 954 | 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 | 955 | by (rule order_le_less_trans) | 
| 60698 | 956 | then have "-e < bitlen m" | 
| 957 | using False by auto | |
| 958 | then show ?thesis | |
| 959 | by auto | |
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 960 | qed | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 961 | qed | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 962 | |
| 60698 | 963 | |
| 60500 | 964 | subsection \<open>Truncating Real Numbers\<close> | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 965 | |
| 60698 | 966 | 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 | 967 | 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 | 968 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 969 | lemma truncate_down: "truncate_down prec x \<le> x" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 970 | using round_down by (simp add: truncate_down_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 971 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 972 | 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 | 973 | by (rule order_trans[OF truncate_down]) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 974 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 975 | 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 | 976 | by (simp add: truncate_down_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_down_float[simp]: "truncate_down p x \<in> float" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 979 | by (auto simp: truncate_down_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 980 | |
| 60698 | 981 | 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 | 982 | 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 | 983 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 984 | lemma truncate_up: "x \<le> truncate_up prec x" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 985 | using round_up by (simp add: truncate_up_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 986 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 987 | 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 | 988 | by (rule order_trans[OF _ truncate_up]) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 989 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 990 | 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 | 991 | by (simp add: truncate_up_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 992 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 993 | 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 | 994 | 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 | 995 | 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 | 996 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 997 | 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 | 998 | by (auto simp: truncate_up_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 999 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1000 | 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 | 1001 | by (simp_all add: powr_add) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1002 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1003 | lemma truncate_down_pos: | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1004 | assumes "x > 0" | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1005 | shows "truncate_down p x > 0" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1006 | 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 | 1007 | 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 | 1008 | 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 | 1009 | with assms | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1010 | show ?thesis | 
| 63356 | 1011 | 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 | 1012 | 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 | 1013 | by linarith | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1014 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1015 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1016 | 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 | 1017 | by (auto simp: truncate_down_def round_down_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1018 | |
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1019 | 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 | 1020 | 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 | 1021 | apply linarith | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1022 | done | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1023 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1024 | 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 | 1025 | 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 | 1026 | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1027 | lemma truncate_up_le1: | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1028 | assumes "x \<le> 1" | 
| 60698 | 1029 | shows "truncate_up p x \<le> 1" | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1030 | proof - | 
| 60698 | 1031 | consider "x \<le> 0" | "x > 0" | 
| 1032 | by arith | |
| 1033 | then show ?thesis | |
| 1034 | proof cases | |
| 1035 | case 1 | |
| 1036 | with truncate_up_nonpos[OF this, of p] show ?thesis | |
| 1037 | by simp | |
| 1038 | next | |
| 1039 | case 2 | |
| 1040 | 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 | 1041 | 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 | 1042 | 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 | 1043 | from add_mono[OF this le] | 
| 60698 | 1044 | show ?thesis | 
| 1045 | using assms by (simp add: truncate_up_def round_up_le1 add_mono) | |
| 1046 | qed | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1047 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1048 | |
| 63356 | 1049 | lemma truncate_down_shift_int: | 
| 1050 | "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 | 1051 | by (cases "x = 0") | 
| 63356 | 1052 | (simp_all add: algebra_simps abs_mult log_mult truncate_down_def | 
| 1053 | 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 | 1054 | |
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1055 | 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 | 1056 | 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 | 1057 | |
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1058 | 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 | 1059 | by (cases "x = 0") | 
| 63356 | 1060 | (simp_all add: algebra_simps abs_mult log_mult truncate_up_def | 
| 1061 | 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 | 1062 | |
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1063 | 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 | 1064 | 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 | 1065 | |
| 60698 | 1066 | |
| 60500 | 1067 | subsection \<open>Truncating Floats\<close> | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1068 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1069 | 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 | 1070 | by (simp add: truncate_up_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1071 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1072 | 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 | 1073 | using truncate_up by transfer simp | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1074 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1075 | 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 | 1076 | by transfer simp | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1077 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1078 | 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 | 1079 | by (simp add: truncate_down_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1080 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1081 | 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 | 1082 | using truncate_down by transfer simp | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1083 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1084 | 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 | 1085 | by transfer simp | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1086 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1087 | 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 | 1088 | 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 | 1089 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1090 | 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 | 1091 | and minus_float_round_down_eq: "- float_round_down prec x = float_round_up prec (- x)" | 
| 63356 | 1092 | 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 | 1093 | |
| 60698 | 1094 | context | 
| 1095 | begin | |
| 1096 | ||
| 1097 | qualified lemma compute_float_round_down[code]: | |
| 63356 | 1098 | "float_round_down prec (Float m e) = | 
| 1099 | (let d = bitlen \<bar>m\<bar> - int prec - 1 in | |
| 1100 | if 0 < d then Float (div_twopow m (nat d)) (e + d) | |
| 1101 | else Float m e)" | |
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1102 | 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 | 1103 | by transfer | 
| 63248 | 1104 | (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 | 1105 | cong del: if_weak_cong) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1106 | |
| 60698 | 1107 | qualified lemma compute_float_round_up[code]: | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1108 | "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 | 1109 | by transfer (simp add: truncate_down_uminus_eq) | 
| 60698 | 1110 | |
| 1111 | end | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1112 | |
| 71036 | 1113 | lemma truncate_up_nonneg_mono: | 
| 1114 | assumes "0 \<le> x" "x \<le> y" | |
| 1115 | shows "truncate_up prec x \<le> truncate_up prec y" | |
| 1116 | proof - | |
| 1117 | 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" | |
| 1118 | by arith | |
| 1119 | then show ?thesis | |
| 1120 | proof cases | |
| 1121 | case 1 | |
| 1122 | then show ?thesis | |
| 1123 | using assms | |
| 1124 | by (auto simp: truncate_up_def round_up_def intro!: ceiling_mono) | |
| 1125 | next | |
| 1126 | case 2 | |
| 1127 | from assms \<open>0 < x\<close> have "log 2 x \<le> log 2 y" | |
| 1128 | by auto | |
| 1129 | with \<open>\<lfloor>log 2 x\<rfloor> \<noteq> \<lfloor>log 2 y\<rfloor>\<close> | |
| 1130 | have logless: "log 2 x < log 2 y" | |
| 1131 | by linarith | |
| 1132 | have flogless: "\<lfloor>log 2 x\<rfloor> < \<lfloor>log 2 y\<rfloor>" | |
| 1133 | using \<open>\<lfloor>log 2 x\<rfloor> \<noteq> \<lfloor>log 2 y\<rfloor>\<close> \<open>log 2 x \<le> log 2 y\<close> by linarith | |
| 1134 | have "truncate_up prec x = | |
| 1135 | 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>)" | |
| 1136 | using assms by (simp add: truncate_up_def round_up_def) | |
| 1137 | also have "\<lceil>x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>)\<rceil> \<le> (2 ^ (Suc prec))" | |
| 1138 | proof (simp only: ceiling_le_iff) | |
| 1139 | have "x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le> | |
| 1140 | x * (2 powr real (Suc prec) / (2 powr log 2 x))" | |
| 1141 | using real_of_int_floor_add_one_ge[of "log 2 x"] assms | |
| 1142 | by (auto simp: algebra_simps simp flip: powr_diff intro!: mult_left_mono) | |
| 1143 | then show "x * 2 powr real_of_int (int prec - \<lfloor>log 2 x\<rfloor>) \<le> real_of_int ((2::int) ^ (Suc prec))" | |
| 1144 | using \<open>0 < x\<close> by (simp add: powr_realpow powr_add) | |
| 1145 | qed | |
| 1146 | 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)" | |
| 1147 | by (auto simp: powr_realpow powr_add) | |
| 1148 | (metis power_Suc of_int_le_numeral_power_cancel_iff) | |
| 1149 | also | |
| 1150 | 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)" | |
| 1151 | using logless flogless by (auto intro!: floor_mono) | |
| 1152 | also have "2 powr real_of_int (int (Suc prec)) \<le> | |
| 1153 | 2 powr (log 2 y + real_of_int (int prec - \<lfloor>log 2 y\<rfloor> + 1))" | |
| 1154 | using assms \<open>0 < x\<close> | |
| 1155 | by (auto simp: algebra_simps) | |
| 1156 | finally have "truncate_up prec x \<le> | |
| 1157 | 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)" | |
| 1158 | by simp | |
| 1159 | 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>))" | |
| 1160 | by (subst powr_add[symmetric]) simp | |
| 1161 | also have "\<dots> = y" | |
| 1162 | using \<open>0 < x\<close> assms | |
| 1163 | by (simp add: powr_add) | |
| 1164 | also have "\<dots> \<le> truncate_up prec y" | |
| 1165 | by (rule truncate_up) | |
| 1166 | finally show ?thesis . | |
| 1167 | next | |
| 1168 | case 3 | |
| 1169 | then show ?thesis | |
| 1170 | using assms | |
| 1171 | by (auto intro!: truncate_up_le) | |
| 1172 | qed | |
| 1173 | qed | |
| 1174 | ||
| 1175 | lemma truncate_up_switch_sign_mono: | |
| 1176 | assumes "x \<le> 0" "0 \<le> y" | |
| 1177 | shows "truncate_up prec x \<le> truncate_up prec y" | |
| 1178 | proof - | |
| 1179 | note truncate_up_nonpos[OF \<open>x \<le> 0\<close>] | |
| 1180 | also note truncate_up_le[OF \<open>0 \<le> y\<close>] | |
| 1181 | finally show ?thesis . | |
| 1182 | qed | |
| 1183 | ||
| 1184 | lemma truncate_down_switch_sign_mono: | |
| 1185 | assumes "x \<le> 0" | |
| 1186 | and "0 \<le> y" | |
| 1187 | and "x \<le> y" | |
| 1188 | shows "truncate_down prec x \<le> truncate_down prec y" | |
| 1189 | proof - | |
| 1190 | note truncate_down_le[OF \<open>x \<le> 0\<close>] | |
| 1191 | also note truncate_down_nonneg[OF \<open>0 \<le> y\<close>] | |
| 1192 | finally show ?thesis . | |
| 1193 | qed | |
| 1194 | ||
| 1195 | lemma truncate_down_nonneg_mono: | |
| 1196 | assumes "0 \<le> x" "x \<le> y" | |
| 1197 | shows "truncate_down prec x \<le> truncate_down prec y" | |
| 1198 | proof - | |
| 1199 | consider "x \<le> 0" | "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> = \<lfloor>log 2 \<bar>y\<bar>\<rfloor>" | | |
| 1200 | "0 < x" "\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<noteq> \<lfloor>log 2 \<bar>y\<bar>\<rfloor>" | |
| 1201 | by arith | |
| 1202 | then show ?thesis | |
| 1203 | proof cases | |
| 1204 | case 1 | |
| 1205 | with assms have "x = 0" "0 \<le> y" by simp_all | |
| 1206 | then show ?thesis | |
| 1207 | by (auto intro!: truncate_down_nonneg) | |
| 1208 | next | |
| 1209 | case 2 | |
| 1210 | then show ?thesis | |
| 1211 | using assms | |
| 1212 | by (auto simp: truncate_down_def round_down_def intro!: floor_mono) | |
| 1213 | next | |
| 1214 | case 3 | |
| 1215 | from \<open>0 < x\<close> have "log 2 x \<le> log 2 y" "0 < y" "0 \<le> y" | |
| 1216 | using assms by auto | |
| 1217 | with \<open>\<lfloor>log 2 \<bar>x\<bar>\<rfloor> \<noteq> \<lfloor>log 2 \<bar>y\<bar>\<rfloor>\<close> | |
| 1218 | have logless: "log 2 x < log 2 y" and flogless: "\<lfloor>log 2 x\<rfloor> < \<lfloor>log 2 y\<rfloor>" | |
| 1219 | unfolding atomize_conj abs_of_pos[OF \<open>0 < x\<close>] abs_of_pos[OF \<open>0 < y\<close>] | |
| 1220 | by (metis floor_less_cancel linorder_cases not_le) | |
| 1221 | have "2 powr prec \<le> y * 2 powr real prec / (2 powr log 2 y)" | |
| 1222 | using \<open>0 < y\<close> by simp | |
| 1223 | also have "\<dots> \<le> y * 2 powr real (Suc prec) / (2 powr (real_of_int \<lfloor>log 2 y\<rfloor> + 1))" | |
| 1224 | using \<open>0 \<le> y\<close> \<open>0 \<le> x\<close> assms(2) | |
| 1225 | by (auto intro!: powr_mono divide_left_mono | |
| 1226 | simp: of_nat_diff powr_add powr_diff) | |
| 1227 | also have "\<dots> = y * 2 powr real (Suc prec) / (2 powr real_of_int \<lfloor>log 2 y\<rfloor> * 2)" | |
| 1228 | by (auto simp: powr_add) | |
| 1229 | finally have "(2 ^ prec) \<le> \<lfloor>y * 2 powr real_of_int (int (Suc prec) - \<lfloor>log 2 \<bar>y\<bar>\<rfloor> - 1)\<rfloor>" | |
| 1230 | using \<open>0 \<le> y\<close> | |
| 1231 | by (auto simp: powr_diff le_floor_iff powr_realpow powr_add) | |
| 1232 | then have "(2 ^ (prec)) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor>) \<le> truncate_down prec y" | |
| 1233 | by (auto simp: truncate_down_def round_down_def) | |
| 1234 | moreover have "x \<le> (2 ^ prec) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>y\<bar>\<rfloor>)" | |
| 1235 | proof - | |
| 1236 | have "x = 2 powr (log 2 \<bar>x\<bar>)" using \<open>0 < x\<close> by simp | |
| 1237 | also have "\<dots> \<le> (2 ^ (Suc prec )) * 2 powr - real_of_int (int prec - \<lfloor>log 2 \<bar>x\<bar>\<rfloor>)" | |
| 1238 | using real_of_int_floor_add_one_ge[of "log 2 \<bar>x\<bar>"] \<open>0 < x\<close> | |
| 1239 | by (auto simp flip: powr_realpow powr_add simp: algebra_simps powr_mult_base le_powr_iff) | |
| 1240 | also | |
| 1241 | 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)" | |
| 1242 | using logless flogless \<open>x > 0\<close> \<open>y > 0\<close> | |
| 1243 | by (auto intro!: floor_mono) | |
| 1244 | finally show ?thesis | |
| 1245 | by (auto simp flip: powr_realpow simp: powr_diff assms of_nat_diff) | |
| 1246 | qed | |
| 1247 | ultimately show ?thesis | |
| 1248 | by (metis dual_order.trans truncate_down) | |
| 1249 | qed | |
| 1250 | qed | |
| 1251 | ||
| 1252 | lemma truncate_down_eq_truncate_up: "truncate_down p x = - truncate_up p (-x)" | |
| 1253 | and truncate_up_eq_truncate_down: "truncate_up p x = - truncate_down p (-x)" | |
| 1254 | by (auto simp: truncate_up_uminus_eq truncate_down_uminus_eq) | |
| 1255 | ||
| 1256 | lemma truncate_down_mono: "x \<le> y \<Longrightarrow> truncate_down p x \<le> truncate_down p y" | |
| 1257 | apply (cases "0 \<le> x") | |
| 1258 | apply (rule truncate_down_nonneg_mono, assumption+) | |
| 1259 | apply (simp add: truncate_down_eq_truncate_up) | |
| 1260 | apply (cases "0 \<le> y") | |
| 1261 | apply (auto intro: truncate_up_nonneg_mono truncate_up_switch_sign_mono) | |
| 1262 | done | |
| 1263 | ||
| 1264 | lemma truncate_up_mono: "x \<le> y \<Longrightarrow> truncate_up p x \<le> truncate_up p y" | |
| 1265 | by (simp add: truncate_up_eq_truncate_down truncate_down_mono) | |
| 1266 | ||
| 1267 | lemma truncate_up_nonneg: "0 \<le> truncate_up p x" if "0 \<le> x" | |
| 1268 | by (simp add: that truncate_up_le) | |
| 1269 | ||
| 1270 | lemma truncate_up_pos: "0 < truncate_up p x" if "0 < x" | |
| 1271 | by (meson less_le_trans that truncate_up) | |
| 1272 | ||
| 1273 | lemma truncate_up_less_zero_iff[simp]: "truncate_up p x < 0 \<longleftrightarrow> x < 0" | |
| 1274 | proof - | |
| 1275 | have f1: "\<forall>n r. truncate_up n r + truncate_down n (- 1 * r) = 0" | |
| 1276 | by (simp add: truncate_down_uminus_eq) | |
| 1277 | have f2: "(\<forall>v0 v1. truncate_up v0 v1 + truncate_down v0 (- 1 * v1) = 0) = (\<forall>v0 v1. truncate_up v0 v1 = - 1 * truncate_down v0 (- 1 * v1))" | |
| 1278 | by (auto simp: truncate_up_eq_truncate_down) | |
| 1279 | have f3: "\<forall>x1. ((0::real) < x1) = (\<not> x1 \<le> 0)" | |
| 1280 | by fastforce | |
| 1281 | have "(- 1 * x \<le> 0) = (0 \<le> x)" | |
| 1282 | by force | |
| 1283 | then have "0 \<le> x \<or> \<not> truncate_down p (- 1 * x) \<le> 0" | |
| 1284 | using f3 by (meson truncate_down_pos) | |
| 1285 | then have "(0 \<le> truncate_up p x) \<noteq> (\<not> 0 \<le> x)" | |
| 1286 | using f2 f1 truncate_up_nonneg by force | |
| 1287 | then show ?thesis | |
| 1288 | by linarith | |
| 1289 | qed | |
| 1290 | ||
| 1291 | lemma truncate_up_nonneg_iff[simp]: "truncate_up p x \<ge> 0 \<longleftrightarrow> x \<ge> 0" | |
| 1292 | using truncate_up_less_zero_iff[of p x] truncate_up_nonneg[of x] | |
| 1293 | by linarith | |
| 1294 | ||
| 1295 | lemma truncate_down_less_zero_iff[simp]: "truncate_down p x < 0 \<longleftrightarrow> x < 0" | |
| 1296 | by (metis le_less_trans not_less_iff_gr_or_eq truncate_down truncate_down_pos truncate_down_zero) | |
| 1297 | ||
| 1298 | lemma truncate_down_nonneg_iff[simp]: "truncate_down p x \<ge> 0 \<longleftrightarrow> x \<ge> 0" | |
| 1299 | using truncate_down_less_zero_iff[of p x] truncate_down_nonneg[of x p] | |
| 1300 | by linarith | |
| 1301 | ||
| 1302 | lemma truncate_down_eq_zero_iff[simp]: "truncate_down prec x = 0 \<longleftrightarrow> x = 0" | |
| 1303 | by (metis not_less_iff_gr_or_eq truncate_down_less_zero_iff truncate_down_pos truncate_down_zero) | |
| 1304 | ||
| 1305 | lemma truncate_up_eq_zero_iff[simp]: "truncate_up prec x = 0 \<longleftrightarrow> x = 0" | |
| 1306 | by (metis not_less_iff_gr_or_eq truncate_up_less_zero_iff truncate_up_pos truncate_up_zero) | |
| 1307 | ||
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1308 | |
| 60500 | 1309 | 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 | 1310 | |
| 63356 | 1311 | lemma div_mult_twopow_eq: "a div ((2::nat) ^ n) div b = a div (b * 2 ^ n)" for a b :: nat | 
| 60698 | 1312 | 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 | 1313 | |
| 63356 | 1314 | 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 | 1315 | 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 | 1316 | |
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1317 | definition "rat_precision prec x y = | 
| 63356 | 1318 | (let d = bitlen x - bitlen y | 
| 1319 | 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 | 1320 | |
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1321 | lemma floor_log_divide_eq: | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1322 | 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 | 1323 | shows "\<lfloor>log p (i / j)\<rfloor> = floor (log p i) - floor (log p j) - | 
| 63356 | 1324 | (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 | 1325 | proof - | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1326 | let ?l = "log p" | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1327 | 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 | 1328 | 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 | 1329 | by (auto simp: log_divide) | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1330 | 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 | 1331 | (is "_ = floor (_ + ?r)") | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1332 | by (simp add: algebra_simps) | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1333 | also note floor_add2 | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1334 | also note \<open>p > 1\<close> | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1335 | 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 | 1336 | 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 | 1337 | 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 | 1338 | using assms | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1339 | by (linarith | | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1340 | auto | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1341 | intro!: floor_eq2 | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1342 | intro: powr_strict powr | 
| 70817 
dd675800469d
dedicated fact collections for algebraic simplification rules potentially splitting goals
 haftmann parents: 
70355diff
changeset | 1343 | simp: powr_diff powr_add field_split_simps algebra_simps)+ | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1344 | finally | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1345 | show ?thesis by simp | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1346 | qed | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1347 | |
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1348 | lemma truncate_down_rat_precision: | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1349 | "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 | 1350 | and truncate_up_rat_precision: | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1351 | "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 | 1352 | unfolding truncate_down_def truncate_up_def rat_precision_def | 
| 63248 | 1353 | 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 | 1354 | |
| 47600 | 1355 | 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 | 1356 | is "\<lambda>prec (x::nat) (y::nat). truncate_down prec (x / y)" | 
| 60698 | 1357 | by simp | 
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 1358 | |
| 60698 | 1359 | context | 
| 1360 | begin | |
| 1361 | ||
| 1362 | qualified lemma compute_lapprox_posrat[code]: | |
| 63356 | 1363 | "lapprox_posrat prec x y = | 
| 53381 | 1364 | (let | 
| 60698 | 1365 | l = rat_precision prec x y; | 
| 1366 | 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 | 1367 | in normfloat (Float d (- l)))" | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1368 | unfolding div_mult_twopow_eq | 
| 47600 | 1369 | by transfer | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1370 | (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 | 1371 | truncate_down_rat_precision del: two_powr_minus_int_float) | 
| 60698 | 1372 | |
| 1373 | end | |
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 1374 | |
| 47600 | 1375 | 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 | 1376 | 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 | 1377 | by simp | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 1378 | |
| 60376 | 1379 | context | 
| 1380 | begin | |
| 1381 | ||
| 1382 | qualified lemma compute_rapprox_posrat[code]: | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1383 | fixes prec x y | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1384 | defines "l \<equiv> rat_precision prec x y" | 
| 63356 | 1385 | shows "rapprox_posrat prec x y = | 
| 1386 | (let | |
| 1387 | l = l; | |
| 1388 | (r, s) = if 0 \<le> l then (x * 2^nat l, y) else (x, y * 2^nat(-l)); | |
| 1389 | d = r div s; | |
| 1390 | m = r mod s | |
| 1391 | 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 | 1392 | proof (cases "y = 0") | 
| 60698 | 1393 | assume "y = 0" | 
| 1394 | then show ?thesis by transfer simp | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1395 | next | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1396 | assume "y \<noteq> 0" | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 1397 | show ?thesis | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1398 | proof (cases "0 \<le> l") | 
| 60698 | 1399 | case True | 
| 63040 | 1400 | define x' where "x' = x * 2 ^ nat l" | 
| 60698 | 1401 | have "int x * 2 ^ nat l = x'" | 
| 63356 | 1402 | 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 | 1403 | moreover have "real x * 2 powr l = real x'" | 
| 68406 | 1404 | by (simp flip: powr_realpow add: \<open>0 \<le> l\<close> x'_def) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1405 | ultimately show ?thesis | 
| 60500 | 1406 | 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 | 1407 | 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 | 1408 | apply transfer | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1409 | apply (auto simp add: round_up_def truncate_up_rat_precision) | 
| 67118 | 1410 | apply (metis dvd_triv_left of_nat_dvd_iff) | 
| 63356 | 1411 | apply (metis floor_divide_of_int_eq of_int_of_nat_eq) | 
| 1412 | done | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1413 | next | 
| 60698 | 1414 | case False | 
| 63040 | 1415 | define y' where "y' = y * 2 ^ nat (- l)" | 
| 60500 | 1416 | from \<open>y \<noteq> 0\<close> have "y' \<noteq> 0" by (simp add: y'_def) | 
| 63356 | 1417 | have "int y * 2 ^ nat (- l) = y'" | 
| 1418 | 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 | 1419 | moreover have "real x * real_of_int (2::int) powr real_of_int l / real y = x / real y'" | 
| 68406 | 1420 | using \<open>\<not> 0 \<le> l\<close> by (simp flip: powr_realpow add: powr_minus y'_def field_simps) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1421 | ultimately show ?thesis | 
| 60500 | 1422 | 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 | 1423 | 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 | 1424 | apply transfer | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1425 | apply (auto simp add: round_up_def ceil_divide_floor_conv truncate_up_rat_precision) | 
| 67118 | 1426 | apply (metis dvd_triv_left of_nat_dvd_iff) | 
| 63356 | 1427 | apply (metis floor_divide_of_int_eq of_int_of_nat_eq) | 
| 1428 | done | |
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 1429 | qed | 
| 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 1430 | qed | 
| 60376 | 1431 | |
| 1432 | end | |
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 1433 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1434 | lemma rat_precision_pos: | 
| 60698 | 1435 | assumes "0 \<le> x" | 
| 1436 | and "0 < y" | |
| 1437 | and "2 * x < y" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1438 | 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 | 1439 | proof - | 
| 60698 | 1440 | have "0 < x \<Longrightarrow> log 2 x + 1 = log 2 (2 * x)" | 
| 1441 | by (simp add: log_mult) | |
| 1442 | then have "bitlen (int x) < bitlen (int y)" | |
| 1443 | using assms | |
| 63599 | 1444 | by (simp add: bitlen_alt_def) | 
| 1445 | (auto intro!: floor_mono simp add: one_add_floor) | |
| 60698 | 1446 | then show ?thesis | 
| 1447 | using assms | |
| 1448 | 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 | 1449 | qed | 
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 1450 | |
| 47601 
050718fe6eee
use real :: float => real as lifting-morphism so we can directlry use the rep_eq theorems
 hoelzl parents: 
47600diff
changeset | 1451 | lemma rapprox_posrat_less1: | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1452 | "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 | 1453 | 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 | 1454 | |
| 47600 | 1455 | 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 | 1456 | "\<lambda>prec (x::int) (y::int). truncate_down prec (x / y)" | 
| 60698 | 1457 | by simp | 
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 1458 | |
| 60698 | 1459 | context | 
| 1460 | begin | |
| 1461 | ||
| 1462 | qualified lemma compute_lapprox_rat[code]: | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1463 | "lapprox_rat prec x y = | 
| 60698 | 1464 | (if y = 0 then 0 | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1465 | else if 0 \<le> x then | 
| 60698 | 1466 | (if 0 < y then lapprox_posrat prec (nat x) (nat y) | 
| 53381 | 1467 | else - (rapprox_posrat prec (nat x) (nat (-y)))) | 
| 63356 | 1468 | else | 
| 1469 | (if 0 < y | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1470 | then - (rapprox_posrat prec (nat (-x)) (nat y)) | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1471 | 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 | 1472 | by transfer (simp add: truncate_up_uminus_eq) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1473 | |
| 47600 | 1474 | 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 | 1475 | "\<lambda>prec (x::int) (y::int). truncate_up prec (x / y)" | 
| 60698 | 1476 | by simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1477 | |
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1478 | lemma "rapprox_rat = rapprox_posrat" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1479 | by transfer auto | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1480 | |
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1481 | lemma "lapprox_rat = lapprox_posrat" | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1482 | by transfer auto | 
| 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1483 | |
| 60698 | 1484 | qualified lemma compute_rapprox_rat[code]: | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1485 | "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 | 1486 | 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 | 1487 | |
| 63356 | 1488 | qualified lemma compute_truncate_down[code]: | 
| 1489 | "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 | 1490 | 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 | 1491 | |
| 63356 | 1492 | qualified lemma compute_truncate_up[code]: | 
| 1493 | "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 | 1494 | by transfer (auto split: prod.split simp: of_rat_divide dest!: quotient_of_div) | 
| 60698 | 1495 | |
| 1496 | end | |
| 1497 | ||
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1498 | |
| 60500 | 1499 | subsection \<open>Division\<close> | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1500 | |
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1501 | definition "real_divl prec a b = truncate_down prec (a / b)" | 
| 54782 | 1502 | |
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1503 | definition "real_divr prec a b = truncate_up prec (a / b)" | 
| 54782 | 1504 | |
| 1505 | lift_definition float_divl :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is real_divl | |
| 1506 | by (simp add: real_divl_def) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1507 | |
| 60698 | 1508 | context | 
| 1509 | begin | |
| 1510 | ||
| 1511 | qualified lemma compute_float_divl[code]: | |
| 47600 | 1512 | "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 | 1513 | apply transfer | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1514 | unfolding real_divl_def of_int_1 mult_1 truncate_down_shift_int[symmetric] | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 1515 | apply (simp add: powr_diff powr_minus) | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1516 | done | 
| 47600 | 1517 | |
| 54782 | 1518 | lift_definition float_divr :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" is real_divr | 
| 1519 | by (simp add: real_divr_def) | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 1520 | |
| 60698 | 1521 | qualified lemma compute_float_divr[code]: | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 1522 | "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 | 1523 | by transfer (simp add: real_divr_def real_divl_def truncate_down_uminus_eq) | 
| 60698 | 1524 | |
| 1525 | end | |
| 47600 | 1526 | |
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 1527 | |
| 60500 | 1528 | subsection \<open>Approximate Addition\<close> | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1529 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1530 | 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 | 1531 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1532 | 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 | 1533 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1534 | 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 | 1535 | by (simp add: plus_down_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1536 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1537 | 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 | 1538 | by (simp add: plus_up_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1539 | |
| 63356 | 1540 | 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 | 1541 | |
| 63356 | 1542 | 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 | 1543 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1544 | 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 | 1545 | 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 | 1546 | 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 | 1547 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1548 | 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 | 1549 | and float_plus_up: "x + y \<le> real_of_float (float_plus_up prec x y)" | 
| 63356 | 1550 | by (transfer; rule plus_down plus_up)+ | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1551 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1552 | lemmas plus_down_le = order_trans[OF plus_down] | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1553 | and plus_up_le = order_trans[OF _ plus_up] | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1554 | 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 | 1555 | 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 | 1556 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1557 | 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 | 1558 | using truncate_down_uminus_eq[of p "x + y"] | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1559 | by (auto simp: plus_down_def plus_up_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1560 | |
| 60698 | 1561 | lemma truncate_down_log2_eqI: | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1562 | 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 | 1563 | 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 | 1564 | shows "truncate_down p x = truncate_down p y" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1565 | 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 | 1566 | |
| 60698 | 1567 | lemma sum_neq_zeroI: | 
| 63356 | 1568 | "\<bar>a\<bar> \<ge> k \<Longrightarrow> \<bar>b\<bar> < k \<Longrightarrow> a + b \<noteq> 0" | 
| 1569 | "\<bar>a\<bar> > k \<Longrightarrow> \<bar>b\<bar> \<le> k \<Longrightarrow> a + b \<noteq> 0" | |
| 1570 | for a k :: real | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1571 | by auto | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1572 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1573 | lemma abs_real_le_2_powr_bitlen[simp]: "\<bar>real_of_int m2\<bar> < 2 powr real_of_int (bitlen \<bar>m2\<bar>)" | 
| 60698 | 1574 | proof (cases "m2 = 0") | 
| 1575 | case True | |
| 1576 | then show ?thesis by simp | |
| 1577 | next | |
| 1578 | case False | |
| 1579 | 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 | 1580 | using bitlen_bounds[of "\<bar>m2\<bar>"] | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1581 | by (auto simp: powr_add bitlen_nonneg) | 
| 60698 | 1582 | then show ?thesis | 
| 66912 
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
 immler parents: 
65583diff
changeset | 1583 | by (metis bitlen_nonneg powr_int of_int_abs of_int_less_numeral_power_cancel_iff | 
| 
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
 immler parents: 
65583diff
changeset | 1584 | zero_less_numeral) | 
| 60698 | 1585 | qed | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1586 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1587 | lemma floor_sum_times_2_powr_sgn_eq: | 
| 60698 | 1588 | fixes ai p q :: int | 
| 1589 | and a b :: real | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1590 | assumes "a * 2 powr p = ai" | 
| 61945 | 1591 | and b_le_1: "\<bar>b * 2 powr (p + 1)\<bar> \<le> 1" | 
| 60698 | 1592 | and leqp: "q \<le> p" | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1593 | 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 | 1594 | proof - | 
| 60698 | 1595 | consider "b = 0" | "b > 0" | "b < 0" by arith | 
| 1596 | then show ?thesis | |
| 1597 | proof cases | |
| 1598 | case 1 | |
| 1599 | then show ?thesis | |
| 68406 | 1600 | by (simp flip: assms(1) powr_add add: algebra_simps powr_mult_base) | 
| 60698 | 1601 | next | 
| 1602 | case 2 | |
| 61945 | 1603 | then have "b * 2 powr p < \<bar>b * 2 powr (p + 1)\<bar>" | 
| 60698 | 1604 | by simp | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1605 | 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 | 1606 | 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 | 1607 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1608 | 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 | 1609 | by (simp_all add: floor_eq_iff) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1610 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1611 | have "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(a + b) * 2 powr p * 2 powr (q - p)\<rfloor>" | 
| 68406 | 1612 | by (simp add: algebra_simps flip: powr_realpow powr_add) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1613 | 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 | 1614 | 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 | 1615 | 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 | 1616 | using assms | 
| 68406 | 1617 | by (simp add: algebra_simps divide_powr_uminus flip: powr_realpow 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 | 1618 | 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 | 1619 | 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 | 1620 | 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 | 1621 | moreover | 
| 63356 | 1622 | have "\<lfloor>(2 * ai + (sgn b)) * 2 powr (real_of_int (q - p) - 1)\<rfloor> = | 
| 1623 | \<lfloor>real_of_int ai / real_of_int ((2::int) ^ nat (p - q))\<rfloor>" | |
| 1624 | 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 | 1625 | 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>" | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 1626 | by (subst powr_diff) (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 | 1627 | also have "\<dots> = \<lfloor>(ai + sgn b / 2) / real_of_int ((2::int) ^ nat (p - q))\<rfloor>" | 
| 68406 | 1628 | using leqp by (simp flip: powr_realpow add: powr_diff) | 
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1629 | 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 | 1630 | by (simp del: of_int_power add: floor_divide_real_eq_div floor_eq) | 
| 63356 | 1631 | finally show ?thesis . | 
| 1632 | qed | |
| 60698 | 1633 | ultimately show ?thesis by simp | 
| 1634 | next | |
| 1635 | 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 | 1636 | 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 | 1637 | using b_le_1 | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1638 | by (auto simp: floor_eq_iff algebra_simps pos_divide_le_eq[symmetric] abs_if divide_powr_uminus | 
| 62390 | 1639 | intro!: mult_neg_pos split: if_split_asm) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1640 | have "\<lfloor>(a + b) * 2 powr q\<rfloor> = \<lfloor>(2*a + 2*b) * 2 powr p * 2 powr (q - p - 1)\<rfloor>" | 
| 68406 | 1641 | by (simp add: algebra_simps powr_mult_base flip: powr_realpow powr_add) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1642 | 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 | 1643 | by (simp add: algebra_simps) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1644 | 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 | 1645 | 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 | 1646 | also have "\<dots> = \<lfloor>(2 * ai + b * 2 powr (p + 1)) / real_of_int ((2::int) ^ nat (p - q + 1))\<rfloor>" | 
| 68406 | 1647 | using assms by (simp add: algebra_simps flip: 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 | 1648 | also have "\<dots> = \<lfloor>(2 * ai - 1) / real_of_int ((2::int) ^ nat (p - q + 1))\<rfloor>" | 
| 60500 | 1649 | 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 | 1650 | 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 | 1651 | 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 | 1652 | also have "\<dots> = \<lfloor>(2 * ai - 1) * 2 powr (q - p - 1)\<rfloor>" | 
| 68406 | 1653 | using assms by (simp add: algebra_simps divide_powr_uminus flip: powr_realpow) | 
| 60698 | 1654 | finally show ?thesis | 
| 1655 | using \<open>b < 0\<close> by simp | |
| 1656 | qed | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1657 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1658 | |
| 60698 | 1659 | lemma log2_abs_int_add_less_half_sgn_eq: | 
| 1660 | fixes ai :: int | |
| 1661 | and b :: real | |
| 61945 | 1662 | assumes "\<bar>b\<bar> \<le> 1/2" | 
| 60698 | 1663 | 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 | 1664 | shows "\<lfloor>log 2 \<bar>real_of_int ai + b\<bar>\<rfloor> = \<lfloor>log 2 \<bar>ai + sgn b / 2\<bar>\<rfloor>" | 
| 60698 | 1665 | proof (cases "b = 0") | 
| 1666 | case True | |
| 1667 | then show ?thesis by simp | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1668 | next | 
| 60698 | 1669 | case False | 
| 63040 | 1670 | define k where "k = \<lfloor>log 2 \<bar>ai\<bar>\<rfloor>" | 
| 60698 | 1671 | then have "\<lfloor>log 2 \<bar>ai\<bar>\<rfloor> = k" | 
| 1672 | by simp | |
| 1673 | then have k: "2 powr k \<le> \<bar>ai\<bar>" "\<bar>ai\<bar> < 2 powr (k + 1)" | |
| 60500 | 1674 | 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 | 1675 | have "k \<ge> 0" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1676 | using assms by (auto simp: k_def) | 
| 63040 | 1677 | 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 | 1678 | have r: "0 \<le> r" "r < 2 powr k" | 
| 60500 | 1679 | using \<open>k \<ge> 0\<close> k | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1680 | by (auto simp: r_def k_def algebra_simps powr_add abs_if powr_int) | 
| 60698 | 1681 | then have "r \<le> (2::int) ^ nat k - 1" | 
| 60500 | 1682 | 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 | 1683 | 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 | 1684 | have r_le: "r \<le> 2 powr k - 1" | 
| 63356 | 1685 | by (auto simp: algebra_simps powr_int) | 
| 66912 
a99a7cbf0fb5
generalized lemmas cancelling real_of_int/real in (in)equalities with power; completed set of related simp rules; lemmas about floorlog/bitlen
 immler parents: 
65583diff
changeset | 1686 | (metis of_int_1 of_int_add of_int_le_numeral_power_cancel_iff) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1687 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1688 | have "\<bar>ai\<bar> = 2 powr k + r" | 
| 68406 | 1689 | using \<open>k \<ge> 0\<close> by (auto simp: k_def r_def simp flip: powr_realpow) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1690 | |
| 61945 | 1691 | have pos: "\<bar>b\<bar> < 1 \<Longrightarrow> 0 < 2 powr k + (r + b)" for b :: real | 
| 60500 | 1692 | 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 | 1693 | by (auto simp add: r_def powr_realpow[symmetric] abs_if sgn_if algebra_simps | 
| 62390 | 1694 | split: if_split_asm) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1695 | have less: "\<bar>sgn ai * b\<bar> < 1" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1696 | and less': "\<bar>sgn (sgn ai * b) / 2\<bar> < 1" | 
| 62390 | 1697 | 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 | 1698 | |
| 61945 | 1699 | 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 | 1700 | \<lfloor>log 2 (1 + (r + b) / 2 powr k)\<rfloor> = (if r = 0 \<and> b < 0 then -1 else 0)" | 
| 60500 | 1701 | 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 | 1702 | 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 | 1703 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1704 | from \<open>real_of_int \<bar>ai\<bar> = _\<close> have "\<bar>ai + b\<bar> = 2 powr k + (r + sgn ai * b)" | 
| 63356 | 1705 | 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 | 1706 | by (auto simp add: sgn_if abs_if) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1707 | 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 | 1708 | proof - | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1709 | 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 | 1710 | by (simp add: field_simps) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1711 | 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 | 1712 | using pos[OF less] | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1713 | 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 | 1714 | also | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1715 | 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 | 1716 | have "\<lfloor>log 2 (1 + (r + sgn ai * b) / 2 powr k)\<rfloor> = ?if" | 
| 63356 | 1717 | using \<open>\<bar>b\<bar> \<le> _\<close> | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1718 | 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 | 1719 | also | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1720 | 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 | 1721 | by (subst floor_eq) (auto simp: sgn_if) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1722 | also have "k + \<dots> = \<lfloor>log 2 (2 powr k * (1 + (r + sgn (sgn ai * b) / 2) / 2 powr k))\<rfloor>" | 
| 63599 | 1723 | unfolding int_add_floor | 
| 61945 | 1724 | using pos[OF less'] \<open>\<bar>b\<bar> \<le> _\<close> | 
| 63599 | 1725 | 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 | 1726 | 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 | 1727 | 2 powr k + r + sgn (sgn ai * b) / 2" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1728 | by (simp add: sgn_if field_simps) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1729 | finally show ?thesis . | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1730 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1731 | 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 | 1732 | 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 | 1733 | by (auto simp: abs_if sgn_if algebra_simps) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1734 | finally show ?thesis . | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1735 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1736 | |
| 60698 | 1737 | context | 
| 1738 | begin | |
| 1739 | ||
| 1740 | qualified lemma compute_far_float_plus_down: | |
| 1741 | fixes m1 e1 m2 e2 :: int | |
| 1742 | and p :: nat | |
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1743 | 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 | 1744 | 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 | 1745 | 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 | 1746 | 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 | 1747 | 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 | 1748 | 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 | 1749 | let ?b = "real_of_float (Float m2 e2)" | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1750 | 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 | 1751 | 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 | 1752 | let ?m1 = "m1 * 2 ^ Suc k1" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1753 | let ?m2 = "m2 * 2 powr ?shift" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1754 | let ?m2' = "sgn m2 / 2" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1755 | let ?e = "e1 - int k1 - 1" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1756 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1757 | have sum_eq: "?sum = (?m1 + ?m2) * 2 powr ?e" | 
| 68406 | 1758 | by (auto simp flip: powr_add powr_mult powr_realpow simp: powr_mult_base algebra_simps) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1759 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1760 | have "\<bar>?m2\<bar> * 2 < 2 powr (bitlen \<bar>m2\<bar> + ?shift + 1)" | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 1761 | by (auto simp: field_simps powr_add powr_mult_base powr_diff abs_mult) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1762 | also have "\<dots> \<le> 2 powr 0" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1763 | using H by (intro powr_mono) auto | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1764 | 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 | 1765 | by simp | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1766 | |
| 61609 
77b453bd616f
Coercion "real" now has type nat => real only and is no longer overloaded. Type class "real_of" is gone. Many duplicate theorems removed.
 paulson <lp15@cam.ac.uk> parents: 
60868diff
changeset | 1767 | then have "\<bar>real_of_int m2\<bar> < 2 powr -(?shift + 1)" | 
| 63248 | 1768 | 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 | 1769 | 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 | 1770 | 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 | 1771 | finally have b_less_quarter: "\<bar>?b\<bar> < 1/4 * 2 powr real_of_int e1" | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 1772 | by (simp add: powr_add field_simps powr_diff 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 | 1773 | 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 | 1774 | 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 | 1775 | by (simp add: algebra_simps powr_mult_base abs_mult) | 
| 60698 | 1776 | then have a_half_less_sum: "\<bar>?a\<bar> / 2 < \<bar>?sum\<bar>" | 
| 62390 | 1777 | 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 | 1778 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1779 | 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 | 1780 | by simp_all | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1781 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1782 | have "\<bar>real_of_float (Float m1 e1)\<bar> \<ge> 1/4 * 2 powr real_of_int e1" | 
| 60500 | 1783 | using \<open>m1 \<noteq> 0\<close> | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1784 | by (auto simp: powr_add powr_int bitlen_nonneg divide_right_mono abs_mult) | 
| 60698 | 1785 | 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 | 1786 | by (rule sum_neq_zeroI) | 
| 60698 | 1787 | then have "?m1 + ?m2 \<noteq> 0" | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1788 | 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 | 1789 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | have "\<bar>real_of_int ?m1\<bar> \<ge> 2 ^ Suc k1" "\<bar>?m2'\<bar> < 2 ^ Suc k1" | 
| 60500 | 1791 | 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 | 1792 | then have sum'_nz: "?m1 + ?m2' \<noteq> 0" | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1793 | by (intro sum_neq_zeroI) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1794 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1795 | 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 | 1796 | using \<open>?m1 + ?m2 \<noteq> 0\<close> | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1797 | 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 | 1798 | 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 | 1799 | 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 | 1800 | 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 | 1801 | 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 | 1802 | 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 | 1803 | 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 | 1804 | also | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1805 | have "\<bar>?m1 + ?m2'\<bar> * 2 powr ?e = \<bar>?m1 * 2 + sgn m2\<bar> * 2 powr (?e - 1)" | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 1806 | by (auto simp: field_simps powr_minus[symmetric] powr_diff 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 | 1807 | 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 | 1808 | using \<open>?m1 + ?m2' \<noteq> 0\<close> | 
| 63599 | 1809 | unfolding floor_add_int | 
| 1810 | 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 | 1811 | 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 | 1812 | 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 | 1813 | 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 | 1814 | 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 | 1815 | unfolding plus_down_def | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1816 | 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 | 1817 | 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 | 1818 | 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 | 1819 | 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 | 1820 | 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 | 1821 | show "?a * 2 powr real_of_int (-?e) = real_of_int ?ai" | 
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 1822 | by (simp add: powr_add powr_realpow[symmetric] powr_diff) | 
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 1823 | 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 | 1824 | using abs_m2_less_half | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1825 | 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 | 1826 | 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 | 1827 | have "e1 + \<lfloor>log 2 \<bar>real_of_int m1\<bar>\<rfloor> - 1 = \<lfloor>log 2 \<bar>?a\<bar>\<rfloor> - 1" | 
| 60500 | 1828 | using \<open>m1 \<noteq> 0\<close> | 
| 63599 | 1829 | 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 | 1830 | also have "\<dots> \<le> \<lfloor>log 2 \<bar>?a + ?b\<bar>\<rfloor>" | 
| 60500 | 1831 | 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 | 1832 | 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 | 1833 | 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 | 1834 | finally | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1835 | have "int p - \<lfloor>log 2 \<bar>?a + ?b\<bar>\<rfloor> \<le> p - (bitlen \<bar>m1\<bar>) - e1 + 2" | 
| 63248 | 1836 | 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 | 1837 | also have "\<dots> \<le> - ?e" | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1838 | 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 | 1839 | finally show "?f \<le> - ?e" by simp | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1840 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1841 | also have "sgn ?b = sgn m2" | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1842 | using powr_gt_zero[of 2 e2] | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1843 | 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 | 1844 | 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 | 1845 | \<lfloor>Float (?m1 * 2 + sgn m2) (?e - 1) * 2 powr ?f\<rfloor>" | 
| 68406 | 1846 | by (simp flip: powr_add powr_realpow add: algebra_simps) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1847 | 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 | 1848 | 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 | 1849 | qed | 
| 60698 | 1850 | then show ?thesis | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1851 | 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 | 1852 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1853 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1854 | 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 | 1855 | by transfer (auto simp: plus_down_def) | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1856 | |
| 60698 | 1857 | qualified lemma compute_float_plus_down[code]: | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1858 | fixes p::nat and m1 e1 m2 e2::int | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1859 | 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 | 1860 | (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 | 1861 | else if m2 = 0 then float_round_down p (Float m1 e1) | 
| 63356 | 1862 | else | 
| 1863 | (if e1 \<ge> e2 then | |
| 1864 | (let k1 = Suc p - nat (bitlen \<bar>m1\<bar>) in | |
| 1865 | if bitlen \<bar>m2\<bar> > e1 - e2 - k1 - 2 | |
| 1866 | then float_round_down p ((Float m1 e1) + (Float m2 e2)) | |
| 1867 | 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 | 1868 | 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 | 1869 | proof - | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1870 |   {
 | 
| 62420 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 1871 | 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 | 1872 | note compute_far_float_plus_down[OF this] | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1873 | } | 
| 60698 | 1874 | then show ?thesis | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1875 | 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 | 1876 | qed | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1877 | |
| 60698 | 1878 | 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 | 1879 | using truncate_down_uminus_eq[of p "x + y"] | 
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1880 | 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 | 1881 | |
| 70347 | 1882 | lemma mantissa_zero: "mantissa 0 = 0" | 
| 1883 | by (fact mantissa_0) | |
| 60698 | 1884 | |
| 62421 
28d2c75dd180
finite precision computation to determine sign for comparison
 immler parents: 
62420diff
changeset | 1885 | 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 | 1886 | 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 | 1887 | by transfer (auto simp: plus_down_def) | 
| 
28d2c75dd180
finite precision computation to determine sign for comparison
 immler parents: 
62420diff
changeset | 1888 | |
| 
28d2c75dd180
finite precision computation to determine sign for comparison
 immler parents: 
62420diff
changeset | 1889 | 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 | 1890 | 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 | 1891 | by transfer (auto simp: plus_down_def) | 
| 
28d2c75dd180
finite precision computation to determine sign for comparison
 immler parents: 
62420diff
changeset | 1892 | |
| 60698 | 1893 | end | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 1894 | |
| 71036 | 1895 | lemma plus_down_mono: "plus_down p a b \<le> plus_down p c d" if "a + b \<le> c + d" | 
| 1896 | by (auto simp: plus_down_def intro!: truncate_down_mono that) | |
| 1897 | ||
| 1898 | lemma plus_up_mono: "plus_up p a b \<le> plus_up p c d" if "a + b \<le> c + d" | |
| 1899 | by (auto simp: plus_up_def intro!: truncate_up_mono that) | |
| 1900 | ||
| 1901 | subsection \<open>Approximate Multiplication\<close> | |
| 1902 | ||
| 1903 | lemma mult_mono_nonpos_nonneg: "a * b \<le> c * d" | |
| 1904 | if "a \<le> c" "a \<le> 0" "0 \<le> d" "d \<le> b" for a b c d::"'a::ordered_ring" | |
| 1905 | by (meson dual_order.trans mult_left_mono_neg mult_right_mono that) | |
| 1906 | ||
| 1907 | lemma mult_mono_nonneg_nonpos: "b * a \<le> d * c" | |
| 1908 | if "a \<le> c" "c \<le> 0" "0 \<le> d" "d \<le> b" for a b c d::"'a::ordered_ring" | |
| 1909 | by (meson dual_order.trans mult_right_mono_neg mult_left_mono that) | |
| 1910 | ||
| 1911 | lemma mult_mono_nonpos_nonpos: "a * b \<le> c * d" | |
| 1912 | if "a \<ge> c" "a \<le> 0" "b \<ge> d" "d \<le> 0" for a b c d::real | |
| 1913 | by (meson dual_order.trans mult_left_mono_neg mult_right_mono_neg that) | |
| 1914 | ||
| 1915 | lemma mult_float_mono1: | |
| 1916 | notes mono_rules = plus_down_mono add_mono nprt_mono nprt_le_zero zero_le_pprt pprt_mono | |
| 1917 | shows "a \<le> b \<Longrightarrow> ab \<le> bb \<Longrightarrow> | |
| 1918 | aa \<le> a \<Longrightarrow> | |
| 1919 | b \<le> ba \<Longrightarrow> | |
| 1920 | ac \<le> ab \<Longrightarrow> | |
| 1921 | bb \<le> bc \<Longrightarrow> | |
| 1922 | plus_down prec (nprt aa * pprt bc) | |
| 1923 | (plus_down prec (nprt ba * nprt bc) | |
| 1924 | (plus_down prec (pprt aa * pprt ac) | |
| 1925 | (pprt ba * nprt ac))) | |
| 1926 | \<le> plus_down prec (nprt a * pprt bb) | |
| 1927 | (plus_down prec (nprt b * nprt bb) | |
| 1928 | (plus_down prec (pprt a * pprt ab) | |
| 1929 | (pprt b * nprt ab)))" | |
| 1930 | apply (rule order_trans) | |
| 1931 | apply (rule mono_rules | assumption)+ | |
| 1932 | apply (rule mult_mono_nonpos_nonneg) | |
| 1933 | apply (rule mono_rules | assumption)+ | |
| 1934 | apply (rule mult_mono_nonpos_nonpos) | |
| 1935 | apply (rule mono_rules | assumption)+ | |
| 1936 | apply (rule mult_mono) | |
| 1937 | apply (rule mono_rules | assumption)+ | |
| 1938 | apply (rule mult_mono_nonneg_nonpos) | |
| 1939 | apply (rule mono_rules | assumption)+ | |
| 1940 | by (rule order_refl)+ | |
| 1941 | ||
| 1942 | lemma mult_float_mono2: | |
| 1943 | notes mono_rules = plus_up_mono add_mono nprt_mono nprt_le_zero zero_le_pprt pprt_mono | |
| 1944 | shows "a \<le> b \<Longrightarrow> | |
| 1945 | ab \<le> bb \<Longrightarrow> | |
| 1946 | aa \<le> a \<Longrightarrow> | |
| 1947 | b \<le> ba \<Longrightarrow> | |
| 1948 | ac \<le> ab \<Longrightarrow> | |
| 1949 | bb \<le> bc \<Longrightarrow> | |
| 1950 | plus_up prec (pprt b * pprt bb) | |
| 1951 | (plus_up prec (pprt a * nprt bb) | |
| 1952 | (plus_up prec (nprt b * pprt ab) | |
| 1953 | (nprt a * nprt ab))) | |
| 1954 | \<le> plus_up prec (pprt ba * pprt bc) | |
| 1955 | (plus_up prec (pprt aa * nprt bc) | |
| 1956 | (plus_up prec (nprt ba * pprt ac) | |
| 1957 | (nprt aa * nprt ac)))" | |
| 1958 | apply (rule order_trans) | |
| 1959 | apply (rule mono_rules | assumption)+ | |
| 1960 | apply (rule mult_mono) | |
| 1961 | apply (rule mono_rules | assumption)+ | |
| 1962 | apply (rule mult_mono_nonneg_nonpos) | |
| 1963 | apply (rule mono_rules | assumption)+ | |
| 1964 | apply (rule mult_mono_nonpos_nonneg) | |
| 1965 | apply (rule mono_rules | assumption)+ | |
| 1966 | apply (rule mult_mono_nonpos_nonpos) | |
| 1967 | apply (rule mono_rules | assumption)+ | |
| 1968 | by (rule order_refl)+ | |
| 1969 | ||
| 1970 | ||
| 1971 | subsection \<open>Approximate Power\<close> | |
| 1972 | ||
| 1973 | lemma div2_less_self[termination_simp]: "odd n \<Longrightarrow> n div 2 < n" for n :: nat | |
| 1974 | by (simp add: odd_pos) | |
| 1975 | ||
| 1976 | fun power_down :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real" | |
| 1977 | where | |
| 1978 | "power_down p x 0 = 1" | |
| 1979 | | "power_down p x (Suc n) = | |
| 1980 | (if odd n then truncate_down (Suc p) ((power_down p x (Suc n div 2))\<^sup>2) | |
| 1981 | else truncate_down (Suc p) (x * power_down p x n))" | |
| 1982 | ||
| 1983 | fun power_up :: "nat \<Rightarrow> real \<Rightarrow> nat \<Rightarrow> real" | |
| 1984 | where | |
| 1985 | "power_up p x 0 = 1" | |
| 1986 | | "power_up p x (Suc n) = | |
| 1987 | (if odd n then truncate_up p ((power_up p x (Suc n div 2))\<^sup>2) | |
| 1988 | else truncate_up p (x * power_up p x n))" | |
| 1989 | ||
| 1990 | lift_definition power_up_fl :: "nat \<Rightarrow> float \<Rightarrow> nat \<Rightarrow> float" is power_up | |
| 1991 | by (induct_tac rule: power_up.induct) simp_all | |
| 1992 | ||
| 1993 | lift_definition power_down_fl :: "nat \<Rightarrow> float \<Rightarrow> nat \<Rightarrow> float" is power_down | |
| 1994 | by (induct_tac rule: power_down.induct) simp_all | |
| 1995 | ||
| 1996 | lemma power_float_transfer[transfer_rule]: | |
| 1997 | "(rel_fun pcr_float (rel_fun (=) pcr_float)) (^) (^)" | |
| 1998 | unfolding power_def | |
| 1999 | by transfer_prover | |
| 2000 | ||
| 2001 | lemma compute_power_up_fl[code]: | |
| 2002 | "power_up_fl p x 0 = 1" | |
| 2003 | "power_up_fl p x (Suc n) = | |
| 2004 | (if odd n then float_round_up p ((power_up_fl p x (Suc n div 2))\<^sup>2) | |
| 2005 | else float_round_up p (x * power_up_fl p x n))" | |
| 2006 | and compute_power_down_fl[code]: | |
| 2007 | "power_down_fl p x 0 = 1" | |
| 2008 | "power_down_fl p x (Suc n) = | |
| 2009 | (if odd n then float_round_down (Suc p) ((power_down_fl p x (Suc n div 2))\<^sup>2) | |
| 2010 | else float_round_down (Suc p) (x * power_down_fl p x n))" | |
| 2011 | unfolding atomize_conj by transfer simp | |
| 2012 | ||
| 2013 | lemma power_down_pos: "0 < x \<Longrightarrow> 0 < power_down p x n" | |
| 2014 | by (induct p x n rule: power_down.induct) | |
| 2015 | (auto simp del: odd_Suc_div_two intro!: truncate_down_pos) | |
| 2016 | ||
| 2017 | lemma power_down_nonneg: "0 \<le> x \<Longrightarrow> 0 \<le> power_down p x n" | |
| 2018 | by (induct p x n rule: power_down.induct) | |
| 2019 | (auto simp del: odd_Suc_div_two intro!: truncate_down_nonneg mult_nonneg_nonneg) | |
| 2020 | ||
| 2021 | lemma power_down: "0 \<le> x \<Longrightarrow> power_down p x n \<le> x ^ n" | |
| 2022 | proof (induct p x n rule: power_down.induct) | |
| 2023 | case (2 p x n) | |
| 2024 | have ?case if "odd n" | |
| 2025 | proof - | |
| 2026 | from that 2 have "(power_down p x (Suc n div 2)) ^ 2 \<le> (x ^ (Suc n div 2)) ^ 2" | |
| 2027 | by (auto intro: power_mono power_down_nonneg simp del: odd_Suc_div_two) | |
| 2028 | also have "\<dots> = x ^ (Suc n div 2 * 2)" | |
| 2029 | by (simp flip: power_mult) | |
| 2030 | also have "Suc n div 2 * 2 = Suc n" | |
| 2031 | using \<open>odd n\<close> by presburger | |
| 2032 | finally show ?thesis | |
| 2033 | using that by (auto intro!: truncate_down_le simp del: odd_Suc_div_two) | |
| 2034 | qed | |
| 2035 | then show ?case | |
| 2036 | by (auto intro!: truncate_down_le mult_left_mono 2 mult_nonneg_nonneg power_down_nonneg) | |
| 2037 | qed simp | |
| 2038 | ||
| 2039 | lemma power_up: "0 \<le> x \<Longrightarrow> x ^ n \<le> power_up p x n" | |
| 2040 | proof (induct p x n rule: power_up.induct) | |
| 2041 | case (2 p x n) | |
| 2042 | have ?case if "odd n" | |
| 2043 | proof - | |
| 2044 | from that even_Suc have "Suc n = Suc n div 2 * 2" | |
| 2045 | by presburger | |
| 2046 | then have "x ^ Suc n \<le> (x ^ (Suc n div 2))\<^sup>2" | |
| 2047 | by (simp flip: power_mult) | |
| 2048 | also from that 2 have "\<dots> \<le> (power_up p x (Suc n div 2))\<^sup>2" | |
| 2049 | by (auto intro: power_mono simp del: odd_Suc_div_two) | |
| 2050 | finally show ?thesis | |
| 2051 | using that by (auto intro!: truncate_up_le simp del: odd_Suc_div_two) | |
| 2052 | qed | |
| 2053 | then show ?case | |
| 2054 | by (auto intro!: truncate_up_le mult_left_mono 2) | |
| 2055 | qed simp | |
| 2056 | ||
| 2057 | lemmas power_up_le = order_trans[OF _ power_up] | |
| 2058 | and power_up_less = less_le_trans[OF _ power_up] | |
| 2059 | and power_down_le = order_trans[OF power_down] | |
| 2060 | ||
| 2061 | lemma power_down_fl: "0 \<le> x \<Longrightarrow> power_down_fl p x n \<le> x ^ n" | |
| 2062 | by transfer (rule power_down) | |
| 2063 | ||
| 2064 | lemma power_up_fl: "0 \<le> x \<Longrightarrow> x ^ n \<le> power_up_fl p x n" | |
| 2065 | by transfer (rule power_up) | |
| 2066 | ||
| 2067 | lemma real_power_up_fl: "real_of_float (power_up_fl p x n) = power_up p x n" | |
| 2068 | by transfer simp | |
| 2069 | ||
| 2070 | lemma real_power_down_fl: "real_of_float (power_down_fl p x n) = power_down p x n" | |
| 2071 | by transfer simp | |
| 2072 | ||
| 2073 | lemmas [simp del] = power_down.simps(2) power_up.simps(2) | |
| 2074 | ||
| 2075 | lemmas power_down_simp = power_down.simps(2) | |
| 2076 | lemmas power_up_simp = power_up.simps(2) | |
| 2077 | ||
| 2078 | lemma power_down_even_nonneg: "even n \<Longrightarrow> 0 \<le> power_down p x n" | |
| 2079 | by (induct p x n rule: power_down.induct) | |
| 2080 | (auto simp: power_down_simp simp del: odd_Suc_div_two intro!: truncate_down_nonneg ) | |
| 2081 | ||
| 2082 | lemma power_down_eq_zero_iff[simp]: "power_down prec b n = 0 \<longleftrightarrow> b = 0 \<and> n \<noteq> 0" | |
| 2083 | proof (induction n arbitrary: b rule: less_induct) | |
| 2084 | case (less x) | |
| 2085 | then show ?case | |
| 2086 | using power_down_simp[of _ _ "x - 1"] | |
| 2087 | by (cases x) (auto simp add: div2_less_self) | |
| 2088 | qed | |
| 2089 | ||
| 2090 | lemma power_down_nonneg_iff[simp]: | |
| 2091 | "power_down prec b n \<ge> 0 \<longleftrightarrow> even n \<or> b \<ge> 0" | |
| 2092 | proof (induction n arbitrary: b rule: less_induct) | |
| 2093 | case (less x) | |
| 2094 | show ?case | |
| 2095 | using less(1)[of "x - 1" b] power_down_simp[of _ _ "x - 1"] | |
| 2096 | by (cases x) (auto simp: algebra_split_simps zero_le_mult_iff) | |
| 2097 | qed | |
| 2098 | ||
| 2099 | lemma power_down_neg_iff[simp]: | |
| 2100 | "power_down prec b n < 0 \<longleftrightarrow> | |
| 2101 | b < 0 \<and> odd n" | |
| 2102 | using power_down_nonneg_iff[of prec b n] by (auto simp del: power_down_nonneg_iff) | |
| 2103 | ||
| 2104 | lemma power_down_nonpos_iff[simp]: | |
| 2105 | notes [simp del] = power_down_neg_iff power_down_eq_zero_iff | |
| 2106 | shows "power_down prec b n \<le> 0 \<longleftrightarrow> b < 0 \<and> odd n \<or> b = 0 \<and> n \<noteq> 0" | |
| 2107 | using power_down_neg_iff[of prec b n] power_down_eq_zero_iff[of prec b n] | |
| 2108 | by auto | |
| 2109 | ||
| 2110 | lemma power_down_mono: | |
| 2111 | "power_down prec a n \<le> power_down prec b n" | |
| 2112 | if "((0 \<le> a \<and> a \<le> b)\<or>(odd n \<and> a \<le> b) \<or> (even n \<and> a \<le> 0 \<and> b \<le> a))" | |
| 2113 | using that | |
| 2114 | proof (induction n arbitrary: a b rule: less_induct) | |
| 2115 | case (less i) | |
| 2116 | show ?case | |
| 2117 | proof (cases i) | |
| 2118 | case j: (Suc j) | |
| 2119 | note IH = less[unfolded j even_Suc not_not] | |
| 2120 | note [simp del] = power_down.simps | |
| 2121 | show ?thesis | |
| 2122 | proof cases | |
| 2123 | assume [simp]: "even j" | |
| 2124 | have "a * power_down prec a j \<le> b * power_down prec b j" | |
| 73655 
26a1d66b9077
tuned proofs --- avoid z3, which is absent on arm64-linux;
 wenzelm parents: 
72607diff
changeset | 2125 | by (metis IH(1) IH(2) \<open>even j\<close> lessI linear mult_mono mult_mono' mult_mono_nonpos_nonneg power_down_even_nonneg) | 
| 71036 | 2126 | then have "truncate_down (Suc prec) (a * power_down prec a j) \<le> truncate_down (Suc prec) (b * power_down prec b j)" | 
| 2127 | by (auto intro!: truncate_down_mono simp: abs_le_square_iff[symmetric] abs_real_def) | |
| 2128 | then show ?thesis | |
| 2129 | unfolding j | |
| 2130 | by (simp add: power_down_simp) | |
| 2131 | next | |
| 2132 | assume [simp]: "odd j" | |
| 2133 | have "power_down prec 0 (Suc (j div 2)) \<le> - power_down prec b (Suc (j div 2))" | |
| 2134 | if "b < 0" "even (j div 2)" | |
| 2135 | apply (rule order_trans[where y=0]) | |
| 2136 | using IH that by (auto simp: div2_less_self) | |
| 2137 | then have "truncate_down (Suc prec) ((power_down prec a (Suc (j div 2)))\<^sup>2) | |
| 2138 | \<le> truncate_down (Suc prec) ((power_down prec b (Suc (j div 2)))\<^sup>2)" | |
| 2139 | using IH | |
| 2140 | by (auto intro!: truncate_down_mono intro: order_trans[where y=0] | |
| 2141 | simp: abs_le_square_iff[symmetric] abs_real_def | |
| 2142 | div2_less_self) | |
| 2143 | then show ?thesis | |
| 2144 | unfolding j | |
| 2145 | by (simp add: power_down_simp) | |
| 2146 | qed | |
| 2147 | qed simp | |
| 2148 | qed | |
| 2149 | ||
| 2150 | lemma power_up_even_nonneg: "even n \<Longrightarrow> 0 \<le> power_up p x n" | |
| 2151 | by (induct p x n rule: power_up.induct) | |
| 2152 | (auto simp: power_up.simps simp del: odd_Suc_div_two intro!: ) | |
| 2153 | ||
| 2154 | lemma power_up_eq_zero_iff[simp]: "power_up prec b n = 0 \<longleftrightarrow> b = 0 \<and> n \<noteq> 0" | |
| 2155 | proof (induction n arbitrary: b rule: less_induct) | |
| 2156 | case (less x) | |
| 2157 | then show ?case | |
| 2158 | using power_up_simp[of _ _ "x - 1"] | |
| 2159 | by (cases x) (auto simp: algebra_split_simps zero_le_mult_iff div2_less_self) | |
| 2160 | qed | |
| 2161 | ||
| 2162 | lemma power_up_nonneg_iff[simp]: | |
| 2163 | "power_up prec b n \<ge> 0 \<longleftrightarrow> even n \<or> b \<ge> 0" | |
| 2164 | proof (induction n arbitrary: b rule: less_induct) | |
| 2165 | case (less x) | |
| 2166 | show ?case | |
| 2167 | using less(1)[of "x - 1" b] power_up_simp[of _ _ "x - 1"] | |
| 2168 | by (cases x) (auto simp: algebra_split_simps zero_le_mult_iff) | |
| 2169 | qed | |
| 2170 | ||
| 2171 | lemma power_up_neg_iff[simp]: | |
| 2172 | "power_up prec b n < 0 \<longleftrightarrow> b < 0 \<and> odd n" | |
| 2173 | using power_up_nonneg_iff[of prec b n] by (auto simp del: power_up_nonneg_iff) | |
| 2174 | ||
| 2175 | lemma power_up_nonpos_iff[simp]: | |
| 2176 | notes [simp del] = power_up_neg_iff power_up_eq_zero_iff | |
| 2177 | shows "power_up prec b n \<le> 0 \<longleftrightarrow> b < 0 \<and> odd n \<or> b = 0 \<and> n \<noteq> 0" | |
| 2178 | using power_up_neg_iff[of prec b n] power_up_eq_zero_iff[of prec b n] | |
| 2179 | by auto | |
| 2180 | ||
| 2181 | lemma power_up_mono: | |
| 2182 | "power_up prec a n \<le> power_up prec b n" | |
| 2183 | if "((0 \<le> a \<and> a \<le> b)\<or>(odd n \<and> a \<le> b) \<or> (even n \<and> a \<le> 0 \<and> b \<le> a))" | |
| 2184 | using that | |
| 2185 | proof (induction n arbitrary: a b rule: less_induct) | |
| 2186 | case (less i) | |
| 2187 | show ?case | |
| 2188 | proof (cases i) | |
| 2189 | case j: (Suc j) | |
| 2190 | note IH = less[unfolded j even_Suc not_not] | |
| 2191 | note [simp del] = power_up.simps | |
| 2192 | show ?thesis | |
| 2193 | proof cases | |
| 2194 | assume [simp]: "even j" | |
| 2195 | have "a * power_up prec a j \<le> b * power_up prec b j" | |
| 73655 
26a1d66b9077
tuned proofs --- avoid z3, which is absent on arm64-linux;
 wenzelm parents: 
72607diff
changeset | 2196 | by (metis IH(1) IH(2) \<open>even j\<close> lessI linear mult_mono mult_mono' mult_mono_nonpos_nonneg power_up_even_nonneg) | 
| 71036 | 2197 | then have "truncate_up prec (a * power_up prec a j) \<le> truncate_up prec (b * power_up prec b j)" | 
| 2198 | by (auto intro!: truncate_up_mono simp: abs_le_square_iff[symmetric] abs_real_def) | |
| 2199 | then show ?thesis | |
| 2200 | unfolding j | |
| 2201 | by (simp add: power_up_simp) | |
| 2202 | next | |
| 2203 | assume [simp]: "odd j" | |
| 2204 | have "power_up prec 0 (Suc (j div 2)) \<le> - power_up prec b (Suc (j div 2))" | |
| 2205 | if "b < 0" "even (j div 2)" | |
| 2206 | apply (rule order_trans[where y=0]) | |
| 2207 | using IH that by (auto simp: div2_less_self) | |
| 2208 | then have "truncate_up prec ((power_up prec a (Suc (j div 2)))\<^sup>2) | |
| 2209 | \<le> truncate_up prec ((power_up prec b (Suc (j div 2)))\<^sup>2)" | |
| 2210 | using IH | |
| 2211 | by (auto intro!: truncate_up_mono intro: order_trans[where y=0] | |
| 2212 | simp: abs_le_square_iff[symmetric] abs_real_def | |
| 2213 | div2_less_self) | |
| 2214 | then show ?thesis | |
| 2215 | unfolding j | |
| 2216 | by (simp add: power_up_simp) | |
| 2217 | qed | |
| 2218 | qed simp | |
| 2219 | qed | |
| 2220 | ||
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 2221 | |
| 60500 | 2222 | subsection \<open>Lemmas needed by Approximate\<close> | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2223 | |
| 60698 | 2224 | 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 | 2225 | "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 | 2226 | "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 | 2227 | "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 | 2228 | "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 | 2229 | "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 | 2230 | "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 | 2231 | "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 | 2232 | "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 | 2233 | "real_of_float (Float (- numeral n) 0) = - numeral n" | 
| 60698 | 2234 | using two_powr_int_float[of 2] two_powr_int_float[of "-1"] two_powr_int_float[of "-2"] | 
| 2235 | two_powr_int_float[of "-3"] | |
| 2236 | using powr_realpow[of 2 2] powr_realpow[of 2 3] | |
| 65583 
8d53b3bebab4
Further new material. The simprule status of some exp and ln identities was reverted.
 paulson <lp15@cam.ac.uk> parents: 
65109diff
changeset | 2237 | using powr_minus[of "2::real" 1] powr_minus[of "2::real" 2] powr_minus[of "2::real" 3] | 
| 60698 | 2238 | by auto | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2239 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2240 | lemma real_of_Float_int[simp]: "real_of_float (Float n 0) = real n" | 
| 60698 | 2241 | by simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2242 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2243 | lemma float_zero[simp]: "real_of_float (Float 0 e) = 0" | 
| 60698 | 2244 | by simp | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2245 | |
| 61945 | 2246 | lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> \<bar>(a::int) div 2\<bar> < \<bar>a\<bar>" | 
| 60698 | 2247 | by arith | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 2248 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2249 | 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 | 2250 | 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 | 2251 | |
| 60698 | 2252 | lemma mult_div_le: | 
| 2253 | fixes a b :: int | |
| 2254 | assumes "b > 0" | |
| 2255 | shows "a \<ge> b * (a div b)" | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2256 | proof - | 
| 64246 | 2257 | from minus_div_mult_eq_mod [symmetric, of a b] have "a = b * (a div b) + a mod b" | 
| 60698 | 2258 | by simp | 
| 2259 | also have "\<dots> \<ge> b * (a div b) + 0" | |
| 2260 | apply (rule add_left_mono) | |
| 2261 | apply (rule pos_mod_sign) | |
| 63356 | 2262 | using assms | 
| 2263 | apply simp | |
| 60698 | 2264 | done | 
| 2265 | finally show ?thesis | |
| 2266 | by simp | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2267 | qed | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2268 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2269 | lemma lapprox_rat_nonneg: | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 2270 | 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 | 2271 | 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 | 2272 | using assms | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 2273 | by transfer (simp add: truncate_down_nonneg) | 
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 2274 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2275 | 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 | 2276 | by transfer (simp add: truncate_up) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2277 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2278 | lemma rapprox_rat_le1: | 
| 63356 | 2279 | 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 | 2280 | 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 | 2281 | using assms | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 2282 | by transfer (simp add: truncate_up_le1) | 
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 2283 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2284 | 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 | 2285 | 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 | 2286 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2287 | 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 | 2288 | 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 | 2289 | |
| 54782 | 2290 | 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 | 2291 | by (simp add: real_divl_def truncate_down) | 
| 54782 | 2292 | |
| 2293 | 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 | 2294 | by (simp add: real_divr_def truncate_up) | 
| 54782 | 2295 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2296 | lemma float_divl: "real_of_float (float_divl prec x y) \<le> x / y" | 
| 54782 | 2297 | by transfer (rule real_divl) | 
| 2298 | ||
| 63356 | 2299 | 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 | 2300 | 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 | 2301 | |
| 63356 | 2302 | lemma float_divl_lower_bound: "0 \<le> x \<Longrightarrow> 0 \<le> y \<Longrightarrow> 0 \<le> real_of_float (float_divl prec x y)" | 
| 54782 | 2303 | by transfer (rule real_divl_lower_bound) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2304 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2305 | lemma exponent_1: "exponent 1 = 0" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2306 | 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 | 2307 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2308 | lemma mantissa_1: "mantissa 1 = 1" | 
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2309 | 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 | 2310 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2311 | lemma bitlen_1: "bitlen 1 = 1" | 
| 63248 | 2312 | by (simp add: bitlen_alt_def) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2313 | |
| 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2314 | lemma float_upper_bound: "x \<le> 2 powr (bitlen \<bar>mantissa x\<bar> + exponent x)" | 
| 60698 | 2315 | proof (cases "x = 0") | 
| 2316 | case True | |
| 2317 | then show ?thesis by simp | |
| 2318 | next | |
| 2319 | case False | |
| 2320 | then have "mantissa x \<noteq> 0" | |
| 2321 | using mantissa_eq_zero_iff by auto | |
| 2322 | have "x = mantissa x * 2 powr (exponent x)" | |
| 2323 | by (rule mantissa_exponent) | |
| 2324 | also have "mantissa x \<le> \<bar>mantissa x\<bar>" | |
| 2325 | by simp | |
| 2326 | also have "\<dots> \<le> 2 powr (bitlen \<bar>mantissa x\<bar>)" | |
| 60500 | 2327 | 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 | 2328 | 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 | 2329 | 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 | 2330 | qed | 
| 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 2331 | |
| 54782 | 2332 | 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 | 2333 | assumes "0 < x" "x \<le> 1" | 
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 2334 | 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 | 2335 | using assms | 
| 
c7666166c24e
positive precision for truncate; fixed precision for approximation of rationals; code for truncate
 immler parents: 
62419diff
changeset | 2336 | 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 | 2337 | |
| 54782 | 2338 | lemma float_divl_pos_less1_bound: | 
| 63356 | 2339 | "0 < real_of_float x \<Longrightarrow> real_of_float x \<le> 1 \<Longrightarrow> prec \<ge> 1 \<Longrightarrow> | 
| 2340 | 1 \<le> real_of_float (float_divl prec 1 x)" | |
| 60698 | 2341 | 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 | 2342 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2343 | lemma float_divr: "real_of_float x / real_of_float y \<le> real_of_float (float_divr prec x y)" | 
| 54782 | 2344 | by transfer (rule real_divr) | 
| 2345 | ||
| 60698 | 2346 | lemma real_divr_pos_less1_lower_bound: | 
| 2347 | assumes "0 < x" | |
| 2348 | and "x \<le> 1" | |
| 2349 | 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 | 2350 | proof - | 
| 60698 | 2351 | have "1 \<le> 1 / x" | 
| 63356 | 2352 | using \<open>0 < x\<close> and \<open>x \<le> 1\<close> by auto | 
| 60698 | 2353 | also have "\<dots> \<le> real_divr prec 1 x" | 
| 63356 | 2354 | using real_divr[where x = 1 and y = x] by auto | 
| 47600 | 2355 | finally show ?thesis by auto | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 2356 | qed | 
| 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 2357 | |
| 58982 
27e7e3f9e665
simplified computations based on round_up by reducing to round_down;
 immler parents: 
58881diff
changeset | 2358 | lemma float_divr_pos_less1_lower_bound: "0 < x \<Longrightarrow> x \<le> 1 \<Longrightarrow> 1 \<le> float_divr prec 1 x" | 
| 54782 | 2359 | by transfer (rule real_divr_pos_less1_lower_bound) | 
| 2360 | ||
| 63356 | 2361 | 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 | 2362 | by (simp add: real_divr_def truncate_up_nonpos divide_le_0_iff) | 
| 54782 | 2363 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2364 | 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 | 2365 | "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 | 2366 | by transfer (rule real_divr_nonpos_pos_upper_bound) | 
| 2367 | ||
| 63356 | 2368 | 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 | 2369 | 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 | 2370 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2371 | 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 | 2372 | "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 | 2373 | by transfer (rule real_divr_nonneg_neg_upper_bound) | 
| 2374 | ||
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2375 | lemma Float_le_zero_iff: "Float a b \<le> 0 \<longleftrightarrow> a \<le> 0" | 
| 67573 | 2376 | by (auto simp: zero_float_def mult_le_0_iff) | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2377 | |
| 60698 | 2378 | lemma real_of_float_pprt[simp]: | 
| 2379 | 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 | 2380 | shows "real_of_float (pprt a) = pprt (real_of_float a)" | 
| 47600 | 2381 | 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 | 2382 | |
| 60698 | 2383 | lemma real_of_float_nprt[simp]: | 
| 2384 | 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 | 2385 | shows "real_of_float (nprt a) = nprt (real_of_float a)" | 
| 47600 | 2386 | 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 | 2387 | |
| 60698 | 2388 | context | 
| 2389 | begin | |
| 2390 | ||
| 55565 
f663fc1e653b
simplify proofs because of the stronger reflexivity prover
 kuncar parents: 
54784diff
changeset | 2391 | 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 | 2392 | |
| 60698 | 2393 | 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 | 2394 | "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 | 2395 | 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 | 2396 | apply (simp add: powr_int floor_divide_of_int_eq) | 
| 73932 
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
 desharna parents: 
73655diff
changeset | 2397 | apply (metis (no_types, opaque_lifting)floor_divide_of_int_eq of_int_numeral of_int_power floor_of_int of_int_mult) | 
| 61942 | 2398 | done | 
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2399 | |
| 61942 | 2400 | lift_definition floor_fl :: "float \<Rightarrow> float" is "\<lambda>x. real_of_int \<lfloor>x\<rfloor>" | 
| 2401 | by simp | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2402 | |
| 60698 | 2403 | 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 | 2404 | "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 | 2405 | 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 | 2406 | apply (simp add: powr_int floor_divide_of_int_eq) | 
| 73932 
fd21b4a93043
added opaque_combs and renamed hide_lams to opaque_lifting
 desharna parents: 
73655diff
changeset | 2407 | apply (metis (no_types, opaque_lifting)floor_divide_of_int_eq of_int_numeral of_int_power of_int_mult) | 
| 61942 | 2408 | done | 
| 60698 | 2409 | |
| 2410 | end | |
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 2411 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2412 | lemma floor_fl: "real_of_float (floor_fl x) \<le> real_of_float x" | 
| 60698 | 2413 | by transfer simp | 
| 47600 | 2414 | |
| 61609 
77b453bd616f
Coercion "real" now has type 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 | 2415 | lemma int_floor_fl: "real_of_int (int_floor_fl x) \<le> real_of_float x" | 
| 60698 | 2416 | by transfer simp | 
| 29804 
e15b74577368
Added new Float theory and moved old Library/Float.thy to ComputeFloat
 hoelzl parents: 
29667diff
changeset | 2417 | |
| 47599 
400b158f1589
replace the float datatype by a type with unique representation
 hoelzl parents: 
47230diff
changeset | 2418 | lemma floor_pos_exp: "exponent (floor_fl x) \<ge> 0" | 
| 67573 | 2419 | proof (cases "floor_fl x = 0") | 
| 53381 | 2420 | case True | 
| 60698 | 2421 | then show ?thesis | 
| 2422 | by (simp add: floor_fl_def) | |
| 53381 | 2423 | next | 
| 2424 | 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 | 2425 | have eq: "floor_fl x = Float \<lfloor>real_of_float x\<rfloor> 0" | 
| 60698 | 2426 | 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 | 2427 | obtain i where "\<lfloor>real_of_float x\<rfloor> = mantissa (floor_fl x) * 2 ^ i" "0 = exponent (floor_fl x) - int i" | 
| 67573 | 2428 | by (rule denormalize_shift[OF eq False]) | 
| 60698 | 2429 | then show ?thesis | 
| 2430 | by simp | |
| 53381 | 2431 | qed | 
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 2432 | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 2433 | lemma compute_mantissa[code]: | 
| 60698 | 2434 | "mantissa (Float m e) = | 
| 2435 | (if m = 0 then 0 else if 2 dvd m then mantissa (normfloat (Float m e)) else m)" | |
| 68406 | 2436 | by (auto simp: mantissa_float Float.abs_eq simp flip: zero_float_def) | 
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 2437 | |
| 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 2438 | lemma compute_exponent[code]: | 
| 60698 | 2439 | "exponent (Float m e) = | 
| 2440 | (if m = 0 then 0 else if 2 dvd m then exponent (normfloat (Float m e)) else e)" | |
| 68406 | 2441 | by (auto simp: exponent_float Float.abs_eq simp flip: zero_float_def) | 
| 67573 | 2442 | |
| 2443 | lifting_update Float.float.lifting | |
| 2444 | lifting_forget Float.float.lifting | |
| 58985 
bf498e0af9e3
truncate intermediate results in horner to improve performance of approximate;
 immler parents: 
58982diff
changeset | 2445 | |
| 16782 
b214f21ae396
- use TableFun instead of homebrew binary tree in am_interpreter.ML
 obua parents: diff
changeset | 2446 | end |