src/HOL/Matrix_LP/ComputeFloat.thy
 author wenzelm Sun Nov 02 18:21:45 2014 +0100 (2014-11-02) changeset 58889 5b7a9633cfa8 parent 56255 968667bbb8d2 child 60017 b785d6d06430 permissions -rw-r--r--
1 (*  Title:      HOL/Matrix_LP/ComputeFloat.thy
2     Author:     Steven Obua
3 *)
5 section {* Floating Point Representation of the Reals *}
7 theory ComputeFloat
8 imports Complex_Main "~~/src/HOL/Library/Lattice_Algebras"
9 begin
11 ML_file "~~/src/Tools/float.ML"
13 definition int_of_real :: "real \<Rightarrow> int"
14   where "int_of_real x = (SOME y. real y = x)"
16 definition real_is_int :: "real \<Rightarrow> bool"
17   where "real_is_int x = (EX (u::int). x = real u)"
19 lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
20   by (auto simp add: real_is_int_def int_of_real_def)
22 lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
23 by (auto simp add: real_is_int_def int_of_real_def)
25 lemma int_of_real_real[simp]: "int_of_real (real x) = x"
28 lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
29 by (auto simp add: int_of_real_def real_is_int_def)
31 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)"
32 by (auto simp add: int_of_real_def real_is_int_def)
34 lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
35 apply (subst real_is_int_def2)
37 done
39 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)"
40 by (auto simp add: int_of_real_def real_is_int_def)
42 lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
43 apply (subst real_is_int_def2)
44 apply (simp add: int_of_real_sub real_int_of_real)
45 done
47 lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
48 by (auto simp add: real_is_int_def)
50 lemma int_of_real_mult:
51   assumes "real_is_int a" "real_is_int b"
52   shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
53   using assms
54   by (auto simp add: real_is_int_def real_of_int_mult[symmetric]
55            simp del: real_of_int_mult)
57 lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
58 apply (subst real_is_int_def2)
60 done
62 lemma real_is_int_0[simp]: "real_is_int (0::real)"
63 by (simp add: real_is_int_def int_of_real_def)
65 lemma real_is_int_1[simp]: "real_is_int (1::real)"
66 proof -
67   have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
68   also have "\<dots> = True" by (simp only: real_is_int_real)
69   ultimately show ?thesis by auto
70 qed
72 lemma real_is_int_n1: "real_is_int (-1::real)"
73 proof -
74   have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
75   also have "\<dots> = True" by (simp only: real_is_int_real)
76   ultimately show ?thesis by auto
77 qed
79 lemma real_is_int_numeral[simp]: "real_is_int (numeral x)"
80   by (auto simp: real_is_int_def intro!: exI[of _ "numeral x"])
82 lemma real_is_int_neg_numeral[simp]: "real_is_int (- numeral x)"
83   by (auto simp: real_is_int_def intro!: exI[of _ "- numeral x"])
85 lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
88 lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
89 proof -
90   have 1: "(1::real) = real (1::int)" by auto
91   show ?thesis by (simp only: 1 int_of_real_real)
92 qed
94 lemma int_of_real_numeral[simp]: "int_of_real (numeral b) = numeral b"
95   unfolding int_of_real_def
96   by (intro some_equality)
97      (auto simp add: real_of_int_inject[symmetric] simp del: real_of_int_inject)
99 lemma int_of_real_neg_numeral[simp]: "int_of_real (- numeral b) = - numeral b"
100   unfolding int_of_real_def
101   by (intro some_equality)
102      (auto simp add: real_of_int_inject[symmetric] simp del: real_of_int_inject)
104 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
105 by (rule zdiv_int)
107 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
108 by (rule zmod_int)
110 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
111 by arith
113 lemma norm_0_1: "(1::_::numeral) = Numeral1"
114   by auto
117   by simp
120   by simp
122 lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
123   by simp
125 lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
126   by simp
128 lemma int_pow_0: "(a::int)^0 = 1"
129   by simp
131 lemma int_pow_1: "(a::int)^(Numeral1) = a"
132   by simp
134 lemma one_eq_Numeral1_nring: "(1::'a::numeral) = Numeral1"
135   by simp
137 lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
138   by simp
140 lemma zpower_Pls: "(z::int)^0 = Numeral1"
141   by simp
143 lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
144   by simp
146 lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
147   by simp
149 lemma lift_bool: "x \<Longrightarrow> x=True"
150   by simp
152 lemma nlift_bool: "~x \<Longrightarrow> x=False"
153   by simp
155 lemma not_false_eq_true: "(~ False) = True" by simp
157 lemma not_true_eq_false: "(~ True) = False" by simp
159 lemmas powerarith = nat_numeral zpower_numeral_even
160   zpower_numeral_odd zpower_Pls
162 definition float :: "(int \<times> int) \<Rightarrow> real" where
163   "float = (\<lambda>(a, b). real a * 2 powr real b)"
165 lemma float_add_l0: "float (0, e) + x = x"
168 lemma float_add_r0: "x + float (0, e) = x"
172   "float (a1, e1) + float (a2, e2) =
173   (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1) else float (a1*2^(nat (e1-e2))+a2, e2))"
174   by (simp add: float_def algebra_simps powr_realpow[symmetric] powr_divide2[symmetric])
176 lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
179 lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
182 lemma float_mult:
183   "float (a1, e1) * float (a2, e2) = (float (a1 * a2, e1 + e2))"
186 lemma float_minus:
187   "- (float (a,b)) = float (-a, b)"
190 lemma zero_le_float:
191   "(0 <= float (a,b)) = (0 <= a)"
192   using powr_gt_zero[of 2 "real b", arith]
193   by (simp add: float_def zero_le_mult_iff)
195 lemma float_le_zero:
196   "(float (a,b) <= 0) = (a <= 0)"
197   using powr_gt_zero[of 2 "real b", arith]
198   by (simp add: float_def mult_le_0_iff)
200 lemma float_abs:
201   "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
202   using powr_gt_zero[of 2 "real b", arith]
203   by (simp add: float_def abs_if mult_less_0_iff)
205 lemma float_zero:
206   "float (0, b) = 0"
209 lemma float_pprt:
210   "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
211   by (auto simp add: zero_le_float float_le_zero float_zero)
213 lemma float_nprt:
214   "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
215   by (auto simp add: zero_le_float float_le_zero float_zero)
217 definition lbound :: "real \<Rightarrow> real"
218   where "lbound x = min 0 x"
220 definition ubound :: "real \<Rightarrow> real"
221   where "ubound x = max 0 x"
223 lemma lbound: "lbound x \<le> x"