1 (* Title: HOL/Real/Float.thy
6 header {* Floating Point Representation of the Reals *}
10 uses "~~/src/Tools/float.ML" ("float_arith.ML")
14 pow2 :: "int \<Rightarrow> real" where
15 "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
18 float :: "int * int \<Rightarrow> real" where
19 "float x = real (fst x) * pow2 (snd x)"
21 lemma pow2_0[simp]: "pow2 0 = 1"
22 by (simp add: pow2_def)
24 lemma pow2_1[simp]: "pow2 1 = 2"
25 by (simp add: pow2_def)
27 lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
28 by (simp add: pow2_def)
30 lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
32 have h: "! n. nat (2 + int n) - Suc 0 = nat (1 + int n)" by arith
33 have g: "! a b. a - -1 = a + (1::int)" by arith
34 have pos: "! n. pow2 (int n + 1) = 2 * pow2 (int n)"
35 apply (auto, induct_tac n)
36 apply (simp_all add: pow2_def)
37 apply (rule_tac m1="2" and n1="nat (2 + int na)" in ssubst[OF realpow_num_eq_if])
42 from pos show ?case by (simp add: ring_simps)
47 apply (subst pow2_neg[of "- int n"])
48 apply (subst pow2_neg[of "-1 - int n"])
49 apply (auto simp add: g pos)
54 lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
63 show ?case by (auto simp add: ring_simps pow2_add1 prems)
72 apply (subst pow2_neg[of "a + -1"])
73 apply (subst pow2_neg[of "-1"])
75 apply (insert pow2_add1[of "-a"])
76 apply (simp add: ring_simps)
77 apply (subst pow2_neg[of "-a"])
81 have a: "int m - (a + -2) = 1 + (int m - a + 1)" by arith
82 have b: "int m - -2 = 1 + (int m + 1)" by arith
85 apply (subst pow2_neg[of "a + (-2 - int m)"])
86 apply (subst pow2_neg[of "-2 - int m"])
87 apply (auto simp add: ring_simps)
90 apply (simp only: pow2_add1)
91 apply (subst pow2_neg[of "int m - a + 1"])
92 apply (subst pow2_neg[of "int m + 1"])
95 apply (auto simp add: ring_simps)
100 lemma "float (a, e) + float (b, e) = float (a + b, e)"
101 by (simp add: float_def ring_simps)
104 int_of_real :: "real \<Rightarrow> int" where
105 "int_of_real x = (SOME y. real y = x)"
108 real_is_int :: "real \<Rightarrow> bool" where
109 "real_is_int x = (EX (u::int). x = real u)"
111 lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
112 by (auto simp add: real_is_int_def int_of_real_def)
114 lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
115 by (simp add: float_def real_is_int_def2 pow2_add[symmetric])
117 lemma pow2_int: "pow2 (int c) = 2^c"
118 by (simp add: pow2_def)
120 lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)"
121 by (simp add: float_def pow2_int[symmetric] pow2_add[symmetric])
123 lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
124 by (auto simp add: real_is_int_def int_of_real_def)
126 lemma int_of_real_real[simp]: "int_of_real (real x) = x"
127 by (simp add: int_of_real_def)
129 lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
130 by (auto simp add: int_of_real_def real_is_int_def)
132 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)"
133 by (auto simp add: int_of_real_def real_is_int_def)
135 lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
136 apply (subst real_is_int_def2)
137 apply (simp add: real_is_int_add_int_of_real real_int_of_real)
140 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)"
141 by (auto simp add: int_of_real_def real_is_int_def)
143 lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
144 apply (subst real_is_int_def2)
145 apply (simp add: int_of_real_sub real_int_of_real)
148 lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
149 by (auto simp add: real_is_int_def)
151 lemma int_of_real_mult:
152 assumes "real_is_int a" "real_is_int b"
153 shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
155 from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
156 from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
157 from a obtain a'::int where a':"a = real a'" by auto
158 from b obtain b'::int where b':"b = real b'" by auto
159 have r: "real a' * real b' = real (a' * b')" by auto
161 apply (simp add: a' b')
163 apply (simp only: int_of_real_real)
167 lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
168 apply (subst real_is_int_def2)
169 apply (simp add: int_of_real_mult)
172 lemma real_is_int_0[simp]: "real_is_int (0::real)"
173 by (simp add: real_is_int_def int_of_real_def)
175 lemma real_is_int_1[simp]: "real_is_int (1::real)"
177 have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
178 also have "\<dots> = True" by (simp only: real_is_int_real)
179 ultimately show ?thesis by auto
182 lemma real_is_int_n1: "real_is_int (-1::real)"
184 have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
185 also have "\<dots> = True" by (simp only: real_is_int_real)
186 ultimately show ?thesis by auto
189 lemma real_is_int_number_of[simp]: "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
191 have neg1: "real_is_int (-1::real)"
193 have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
194 also have "\<dots> = True" by (simp only: real_is_int_real)
195 ultimately show ?thesis by auto
200 have "real_is_int ((number_of \<Colon> int \<Rightarrow> real) x)"
201 unfolding number_of_eq
207 apply (simp add: neg1)
210 assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
211 have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
212 show "real_is_int (of_int (- (int (Suc (Suc n)))))"
213 apply (simp only: s of_int_add)
214 apply (rule real_is_int_add)
215 apply (simp add: neg1)
216 apply (simp only: rn)
224 apply (rule exI[where x = "x"])
228 then obtain u::int where "x = u" by auto
229 with Abs_Bin show ?thesis by auto
232 lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
233 by (simp add: int_of_real_def)
235 lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
237 have 1: "(1::real) = real (1::int)" by auto
238 show ?thesis by (simp only: 1 int_of_real_real)
241 lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
243 have "real_is_int (number_of b)" by simp
244 then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
245 then obtain u::int where u:"number_of b = real u" by auto
246 have "number_of b = real ((number_of b)::int)"
247 by (simp add: number_of_eq real_of_int_def)
248 have ub: "number_of b = real ((number_of b)::int)"
249 by (simp add: number_of_eq real_of_int_def)
250 from uu u ub have unb: "u = number_of b"
252 have "int_of_real (number_of b) = u" by (simp add: u)
253 with unb show ?thesis by simp
256 lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
257 apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
258 apply (simp_all add: pow2_def even_def real_is_int_def ring_simps)
262 have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
263 show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))"
267 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
270 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
273 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
276 function norm_float :: "int \<Rightarrow> int \<Rightarrow> int \<times> int" where
277 "norm_float a b = (if a \<noteq> 0 \<and> even a then norm_float (a div 2) (b + 1)
278 else if a = 0 then (0, 0) else (a, b))"
281 termination by (relation "measure (nat o abs o fst)")
282 (auto intro: abs_div_2_less)
284 lemma norm_float: "float x = float (split norm_float x)"
288 have norm_float_pair: "float (a, b) = float (norm_float a b)"
289 proof (induct a b rule: norm_float.induct)
293 assume u: "u \<noteq> 0 \<and> even u"
294 with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2) (v + 1))" by auto
295 with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even)
297 apply (subst norm_float.simps)
298 apply (simp add: ind)
301 assume "~(u \<noteq> 0 \<and> even u)"
303 by (simp add: prems float_def)
308 have "? a b. x = (a,b)" by auto
309 then obtain a b where "x = (a, b)" by blast
310 then show ?thesis by (simp add: helper)
313 lemma float_add_l0: "float (0, e) + x = x"
314 by (simp add: float_def)
316 lemma float_add_r0: "x + float (0, e) = x"
317 by (simp add: float_def)
320 "float (a1, e1) + float (a2, e2) =
321 (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1)
322 else float (a1*2^(nat (e1-e2))+a2, e2))"
323 apply (simp add: float_def ring_simps)
324 apply (auto simp add: pow2_int[symmetric] pow2_add[symmetric])
327 lemma float_add_assoc1:
328 "(x + float (y1, e1)) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
331 lemma float_add_assoc2:
332 "(float (y1, e1) + x) + float (y2, e2) = (float (y1, e1) + float (y2, e2)) + x"
335 lemma float_add_assoc3:
336 "float (y1, e1) + (x + float (y2, e2)) = (float (y1, e1) + float (y2, e2)) + x"
339 lemma float_add_assoc4:
340 "float (y1, e1) + (float (y2, e2) + x) = (float (y1, e1) + float (y2, e2)) + x"
343 lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
344 by (simp add: float_def)
346 lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
347 by (simp add: float_def)
350 lbound :: "real \<Rightarrow> real"
355 ubound :: "real \<Rightarrow> real"
359 lemma lbound: "lbound x \<le> x"
360 by (simp add: lbound_def)
362 lemma ubound: "x \<le> ubound x"
363 by (simp add: ubound_def)
366 "float (a1, e1) * float (a2, e2) =
367 (float (a1 * a2, e1 + e2))"
368 by (simp add: float_def pow2_add)
371 "- (float (a,b)) = float (-a, b)"
372 by (simp add: float_def)
374 lemma zero_less_pow2:
379 have "0 <= y \<Longrightarrow> 0 < pow2 y"
380 by (induct y, induct_tac n, simp_all add: pow2_add)
384 apply (case_tac "0 <= x")
385 apply (simp add: helper)
386 apply (subst pow2_neg)
387 apply (simp add: helper)
392 "(0 <= float (a,b)) = (0 <= a)"
393 apply (auto simp add: float_def)
394 apply (auto simp add: zero_le_mult_iff zero_less_pow2)
395 apply (insert zero_less_pow2[of b])
400 "(float (a,b) <= 0) = (a <= 0)"
401 apply (auto simp add: float_def)
402 apply (auto simp add: mult_le_0_iff)
403 apply (insert zero_less_pow2[of b])
408 "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
409 apply (auto simp add: abs_if)
410 apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
415 by (simp add: float_def)
418 "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
419 by (auto simp add: zero_le_float float_le_zero float_zero)
421 lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
422 apply (simp add: float_def)
423 apply (rule pprt_eq_0)
424 apply (simp add: lbound_def)
427 lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
428 apply (simp add: float_def)
429 apply (rule nprt_eq_0)
430 apply (simp add: ubound_def)
434 "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
435 by (auto simp add: zero_le_float float_le_zero float_zero)
437 lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
440 lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
443 lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
446 lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
449 lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
452 lemma int_pow_0: "(a::int)^(Numeral0) = 1"
455 lemma int_pow_1: "(a::int)^(Numeral1) = a"
458 lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
461 lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
464 lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
467 lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
470 lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
473 lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
475 have 1:"((-1)::nat) = 0"
477 show ?thesis by (simp add: 1)
480 lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
483 lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
486 lemma lift_bool: "x \<Longrightarrow> x=True"
489 lemma nlift_bool: "~x \<Longrightarrow> x=False"
492 lemma not_false_eq_true: "(~ False) = True" by simp
494 lemma not_true_eq_false: "(~ True) = False" by simp
498 pred_bin_simps succ_bin_simps
499 add_bin_simps minus_bin_simps mult_bin_simps
501 lemma int_eq_number_of_eq:
502 "(((number_of v)::int)=(number_of w)) = iszero ((number_of (v + uminus w))::int)"
505 lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
506 by (simp only: iszero_number_of_Pls)
508 lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
511 lemma int_iszero_number_of_Bit0: "iszero ((number_of (Int.Bit0 w))::int) = iszero ((number_of w)::int)"
514 lemma int_iszero_number_of_Bit1: "\<not> iszero ((number_of (Int.Bit1 w))::int)"
517 lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (x + (uminus y)))::int)"
520 lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
523 lemma int_neg_number_of_Min: "neg (-1::int)"
526 lemma int_neg_number_of_Bit0: "neg ((number_of (Int.Bit0 w))::int) = neg ((number_of w)::int)"
529 lemma int_neg_number_of_Bit1: "neg ((number_of (Int.Bit1 w))::int) = neg ((number_of w)::int)"
532 lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (y + (uminus x)))::int))"
537 lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_Bit0
538 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]
539 int_neg_number_of_Bit0 int_neg_number_of_Bit1 int_le_number_of_eq
541 lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (v + w)"
544 lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (v + (uminus w))"
547 lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (v * w)"
550 lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (uminus v)"
553 lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
555 lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
557 lemmas powerarith = nat_number_of zpower_number_of_even
558 zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
559 zpower_Pls zpower_Min
561 lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0
562 float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
564 (* for use with the compute oracle *)
565 lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
567 use "float_arith.ML";