1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/HOL/Matrix_LP/ComputeFloat.thy Sat Mar 17 12:52:40 2012 +0100
1.3 @@ -0,0 +1,309 @@
1.4 +(* Title: HOL/Matrix/ComputeFloat.thy
1.5 + Author: Steven Obua
1.6 +*)
1.7 +
1.8 +header {* Floating Point Representation of the Reals *}
1.9 +
1.10 +theory ComputeFloat
1.11 +imports Complex_Main "~~/src/HOL/Library/Lattice_Algebras"
1.12 +uses "~~/src/Tools/float.ML" ("~~/src/HOL/Tools/float_arith.ML")
1.13 +begin
1.14 +
1.15 +definition int_of_real :: "real \<Rightarrow> int"
1.16 + where "int_of_real x = (SOME y. real y = x)"
1.17 +
1.18 +definition real_is_int :: "real \<Rightarrow> bool"
1.19 + where "real_is_int x = (EX (u::int). x = real u)"
1.20 +
1.21 +lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
1.22 + by (auto simp add: real_is_int_def int_of_real_def)
1.23 +
1.24 +lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
1.25 +by (auto simp add: real_is_int_def int_of_real_def)
1.26 +
1.27 +lemma int_of_real_real[simp]: "int_of_real (real x) = x"
1.28 +by (simp add: int_of_real_def)
1.29 +
1.30 +lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
1.31 +by (auto simp add: int_of_real_def real_is_int_def)
1.32 +
1.33 +lemma real_is_int_add_int_of_real: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a+b)) = (int_of_real a) + (int_of_real b)"
1.34 +by (auto simp add: int_of_real_def real_is_int_def)
1.35 +
1.36 +lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
1.37 +apply (subst real_is_int_def2)
1.38 +apply (simp add: real_is_int_add_int_of_real real_int_of_real)
1.39 +done
1.40 +
1.41 +lemma int_of_real_sub: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> (int_of_real (a-b)) = (int_of_real a) - (int_of_real b)"
1.42 +by (auto simp add: int_of_real_def real_is_int_def)
1.43 +
1.44 +lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
1.45 +apply (subst real_is_int_def2)
1.46 +apply (simp add: int_of_real_sub real_int_of_real)
1.47 +done
1.48 +
1.49 +lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
1.50 +by (auto simp add: real_is_int_def)
1.51 +
1.52 +lemma int_of_real_mult:
1.53 + assumes "real_is_int a" "real_is_int b"
1.54 + shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
1.55 + using assms
1.56 + by (auto simp add: real_is_int_def real_of_int_mult[symmetric]
1.57 + simp del: real_of_int_mult)
1.58 +
1.59 +lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
1.60 +apply (subst real_is_int_def2)
1.61 +apply (simp add: int_of_real_mult)
1.62 +done
1.63 +
1.64 +lemma real_is_int_0[simp]: "real_is_int (0::real)"
1.65 +by (simp add: real_is_int_def int_of_real_def)
1.66 +
1.67 +lemma real_is_int_1[simp]: "real_is_int (1::real)"
1.68 +proof -
1.69 + have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
1.70 + also have "\<dots> = True" by (simp only: real_is_int_real)
1.71 + ultimately show ?thesis by auto
1.72 +qed
1.73 +
1.74 +lemma real_is_int_n1: "real_is_int (-1::real)"
1.75 +proof -
1.76 + have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
1.77 + also have "\<dots> = True" by (simp only: real_is_int_real)
1.78 + ultimately show ?thesis by auto
1.79 +qed
1.80 +
1.81 +lemma real_is_int_number_of[simp]: "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
1.82 + by (auto simp: real_is_int_def intro!: exI[of _ "number_of x"])
1.83 +
1.84 +lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
1.85 +by (simp add: int_of_real_def)
1.86 +
1.87 +lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
1.88 +proof -
1.89 + have 1: "(1::real) = real (1::int)" by auto
1.90 + show ?thesis by (simp only: 1 int_of_real_real)
1.91 +qed
1.92 +
1.93 +lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
1.94 + unfolding int_of_real_def
1.95 + by (intro some_equality)
1.96 + (auto simp add: real_of_int_inject[symmetric] simp del: real_of_int_inject)
1.97 +
1.98 +lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
1.99 +by (rule zdiv_int)
1.100 +
1.101 +lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
1.102 +by (rule zmod_int)
1.103 +
1.104 +lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
1.105 +by arith
1.106 +
1.107 +lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
1.108 + by auto
1.109 +
1.110 +lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
1.111 + by simp
1.112 +
1.113 +lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
1.114 + by simp
1.115 +
1.116 +lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
1.117 + by simp
1.118 +
1.119 +lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
1.120 + by simp
1.121 +
1.122 +lemma int_pow_0: "(a::int)^(Numeral0) = 1"
1.123 + by simp
1.124 +
1.125 +lemma int_pow_1: "(a::int)^(Numeral1) = a"
1.126 + by simp
1.127 +
1.128 +lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
1.129 + by simp
1.130 +
1.131 +lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
1.132 + by simp
1.133 +
1.134 +lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
1.135 + by simp
1.136 +
1.137 +lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
1.138 + by simp
1.139 +
1.140 +lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
1.141 + by simp
1.142 +
1.143 +lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
1.144 +proof -
1.145 + have 1:"((-1)::nat) = 0"
1.146 + by simp
1.147 + show ?thesis by (simp add: 1)
1.148 +qed
1.149 +
1.150 +lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
1.151 + by simp
1.152 +
1.153 +lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
1.154 + by simp
1.155 +
1.156 +lemma lift_bool: "x \<Longrightarrow> x=True"
1.157 + by simp
1.158 +
1.159 +lemma nlift_bool: "~x \<Longrightarrow> x=False"
1.160 + by simp
1.161 +
1.162 +lemma not_false_eq_true: "(~ False) = True" by simp
1.163 +
1.164 +lemma not_true_eq_false: "(~ True) = False" by simp
1.165 +
1.166 +lemmas binarith =
1.167 + normalize_bin_simps
1.168 + pred_bin_simps succ_bin_simps
1.169 + add_bin_simps minus_bin_simps mult_bin_simps
1.170 +
1.171 +lemma int_eq_number_of_eq:
1.172 + "(((number_of v)::int)=(number_of w)) = iszero ((number_of (v + uminus w))::int)"
1.173 + by (rule eq_number_of_eq)
1.174 +
1.175 +lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
1.176 + by (simp only: iszero_number_of_Pls)
1.177 +
1.178 +lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
1.179 + by simp
1.180 +
1.181 +lemma int_iszero_number_of_Bit0: "iszero ((number_of (Int.Bit0 w))::int) = iszero ((number_of w)::int)"
1.182 + by simp
1.183 +
1.184 +lemma int_iszero_number_of_Bit1: "\<not> iszero ((number_of (Int.Bit1 w))::int)"
1.185 + by simp
1.186 +
1.187 +lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (x + (uminus y)))::int)"
1.188 + unfolding neg_def number_of_is_id by simp
1.189 +
1.190 +lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
1.191 + by simp
1.192 +
1.193 +lemma int_neg_number_of_Min: "neg (-1::int)"
1.194 + by simp
1.195 +
1.196 +lemma int_neg_number_of_Bit0: "neg ((number_of (Int.Bit0 w))::int) = neg ((number_of w)::int)"
1.197 + by simp
1.198 +
1.199 +lemma int_neg_number_of_Bit1: "neg ((number_of (Int.Bit1 w))::int) = neg ((number_of w)::int)"
1.200 + by simp
1.201 +
1.202 +lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (y + (uminus x)))::int))"
1.203 + unfolding neg_def number_of_is_id by (simp add: not_less)
1.204 +
1.205 +lemmas intarithrel =
1.206 + int_eq_number_of_eq
1.207 + lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_Bit0
1.208 + lift_bool[OF int_iszero_number_of_Bit1] int_less_number_of_eq_neg nlift_bool[OF int_not_neg_number_of_Pls] lift_bool[OF int_neg_number_of_Min]
1.209 + int_neg_number_of_Bit0 int_neg_number_of_Bit1 int_le_number_of_eq
1.210 +
1.211 +lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (v + w)"
1.212 + by simp
1.213 +
1.214 +lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (v + (uminus w))"
1.215 + by simp
1.216 +
1.217 +lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (v * w)"
1.218 + by simp
1.219 +
1.220 +lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (uminus v)"
1.221 + by simp
1.222 +
1.223 +lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
1.224 +
1.225 +lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
1.226 +
1.227 +lemmas powerarith = nat_number_of zpower_number_of_even
1.228 + zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
1.229 + zpower_Pls zpower_Min
1.230 +
1.231 +definition float :: "(int \<times> int) \<Rightarrow> real" where
1.232 + "float = (\<lambda>(a, b). real a * 2 powr real b)"
1.233 +
1.234 +lemma float_add_l0: "float (0, e) + x = x"
1.235 + by (simp add: float_def)
1.236 +
1.237 +lemma float_add_r0: "x + float (0, e) = x"
1.238 + by (simp add: float_def)
1.239 +
1.240 +lemma float_add:
1.241 + "float (a1, e1) + float (a2, e2) =
1.242 + (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1) else float (a1*2^(nat (e1-e2))+a2, e2))"
1.243 + by (simp add: float_def algebra_simps powr_realpow[symmetric] powr_divide2[symmetric])
1.244 +
1.245 +lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
1.246 + by (simp add: float_def)
1.247 +
1.248 +lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
1.249 + by (simp add: float_def)
1.250 +
1.251 +lemma float_mult:
1.252 + "float (a1, e1) * float (a2, e2) = (float (a1 * a2, e1 + e2))"
1.253 + by (simp add: float_def powr_add)
1.254 +
1.255 +lemma float_minus:
1.256 + "- (float (a,b)) = float (-a, b)"
1.257 + by (simp add: float_def)
1.258 +
1.259 +lemma zero_le_float:
1.260 + "(0 <= float (a,b)) = (0 <= a)"
1.261 + using powr_gt_zero[of 2 "real b", arith]
1.262 + by (simp add: float_def zero_le_mult_iff)
1.263 +
1.264 +lemma float_le_zero:
1.265 + "(float (a,b) <= 0) = (a <= 0)"
1.266 + using powr_gt_zero[of 2 "real b", arith]
1.267 + by (simp add: float_def mult_le_0_iff)
1.268 +
1.269 +lemma float_abs:
1.270 + "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
1.271 + using powr_gt_zero[of 2 "real b", arith]
1.272 + by (simp add: float_def abs_if mult_less_0_iff)
1.273 +
1.274 +lemma float_zero:
1.275 + "float (0, b) = 0"
1.276 + by (simp add: float_def)
1.277 +
1.278 +lemma float_pprt:
1.279 + "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
1.280 + by (auto simp add: zero_le_float float_le_zero float_zero)
1.281 +
1.282 +lemma float_nprt:
1.283 + "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
1.284 + by (auto simp add: zero_le_float float_le_zero float_zero)
1.285 +
1.286 +definition lbound :: "real \<Rightarrow> real"
1.287 + where "lbound x = min 0 x"
1.288 +
1.289 +definition ubound :: "real \<Rightarrow> real"
1.290 + where "ubound x = max 0 x"
1.291 +
1.292 +lemma lbound: "lbound x \<le> x"
1.293 + by (simp add: lbound_def)
1.294 +
1.295 +lemma ubound: "x \<le> ubound x"
1.296 + by (simp add: ubound_def)
1.297 +
1.298 +lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
1.299 + by (auto simp: float_def lbound_def)
1.300 +
1.301 +lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
1.302 + by (auto simp: float_def ubound_def)
1.303 +
1.304 +lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0
1.305 + float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
1.306 +
1.307 +(* for use with the compute oracle *)
1.308 +lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
1.309 +
1.310 +use "~~/src/HOL/Tools/float_arith.ML"
1.311 +
1.312 +end