src/HOL/Matrix/Float.thy
 author obua Fri Sep 03 17:10:36 2004 +0200 (2004-09-03) changeset 15178 5f621aa35c25 child 15236 f289e8ba2bb3 permissions -rw-r--r--
Matrix theory, linear programming
1 theory Float = Hyperreal:
3 constdefs
4   pow2 :: "int \<Rightarrow> real"
5   "pow2 a == if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a))))"
6   float :: "int * int \<Rightarrow> real"
7   "float x == (real (fst x)) * (pow2 (snd x))"
9 lemma pow2_0[simp]: "pow2 0 = 1"
12 lemma pow2_1[simp]: "pow2 1 = 2"
15 lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
18 lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
19 proof -
20   have h: "! n. nat (2 + int n) - Suc 0 = nat (1 + int n)" by arith
21   have g: "! a b. a - -1 = a + (1::int)" by arith
22   have pos: "! n. pow2 (int n + 1) = 2 * pow2 (int n)"
23     apply (auto, induct_tac n)
25     apply (rule_tac m1="2" and n1="nat (2 + int na)" in ssubst[OF realpow_num_eq_if])
26     apply (auto simp add: h)
27     apply arith
28     done
29   show ?thesis
30   proof (induct a)
31     case (1 n)
32     from pos show ?case by (simp add: ring_eq_simps)
33   next
34     case (2 n)
35     show ?case
36       apply (auto)
37       apply (subst pow2_neg[of "- int n"])
38       apply (subst pow2_neg[of "-1 - int n"])
39       apply (auto simp add: g pos)
40       done
41   qed
42 qed
44 lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
45 proof (induct b)
46   case (1 n)
47   show ?case
48   proof (induct n)
49     case 0
50     show ?case by simp
51   next
52     case (Suc m)
54   qed
55 next
56   case (2 n)
57   show ?case
58   proof (induct n)
59     case 0
60     show ?case
61       apply (auto)
62       apply (subst pow2_neg[of "a + -1"])
63       apply (subst pow2_neg[of "-1"])
64       apply (simp)
67       apply (subst pow2_neg[of "-a"])
68       apply (simp)
69       done
70     case (Suc m)
71     have a: "int m - (a + -2) =  1 + (int m - a + 1)" by arith
72     have b: "int m - -2 = 1 + (int m + 1)" by arith
73     show ?case
74       apply (auto)
75       apply (subst pow2_neg[of "a + (-2 - int m)"])
76       apply (subst pow2_neg[of "-2 - int m"])
77       apply (auto simp add: ring_eq_simps)
78       apply (subst a)
79       apply (subst b)
81       apply (subst pow2_neg[of "int m - a + 1"])
82       apply (subst pow2_neg[of "int m + 1"])
83       apply auto
84       apply (insert prems)
85       apply (auto simp add: ring_eq_simps)
86       done
87   qed
88 qed
90 lemma "float (a, e) + float (b, e) = float (a + b, e)"
91 by (simp add: float_def ring_eq_simps)
93 constdefs
94   int_of_real :: "real \<Rightarrow> int"
95   "int_of_real x == SOME y. real y = x"
96   real_is_int :: "real \<Rightarrow> bool"
97   "real_is_int x == ? (u::int). x = real u"
99 lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
100 by (auto simp add: real_is_int_def int_of_real_def)
102 lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
105 lemma pow2_int: "pow2 (int c) = (2::real)^c"
108 lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)"
111 lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
112 by (auto simp add: real_is_int_def int_of_real_def)
114 lemma int_of_real_real[simp]: "int_of_real (real x) = x"
117 lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
118 by (auto simp add: int_of_real_def real_is_int_def)
120 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)"
121 by (auto simp add: int_of_real_def real_is_int_def)
123 lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
124 apply (subst real_is_int_def2)
126 done
128 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)"
129 by (auto simp add: int_of_real_def real_is_int_def)
131 lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
132 apply (subst real_is_int_def2)
133 apply (simp add: int_of_real_sub real_int_of_real)
134 done
136 lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
137 by (auto simp add: real_is_int_def)
139 lemma int_of_real_mult:
140   assumes "real_is_int a" "real_is_int b"
141   shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
142 proof -
143   from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
144   from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
145   from a obtain a'::int where a':"a = real a'" by auto
146   from b obtain b'::int where b':"b = real b'" by auto
147   have r: "real a' * real b' = real (a' * b')" by auto
148   show ?thesis
149     apply (simp add: a' b')
150     apply (subst r)
151     apply (simp only: int_of_real_real)
152     done
153 qed
155 lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
156 apply (subst real_is_int_def2)
158 done
160 lemma real_is_int_0[simp]: "real_is_int (0::real)"
161 by (simp add: real_is_int_def int_of_real_def)
163 lemma real_is_int_1[simp]: "real_is_int (1::real)"
164 proof -
165   have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
166   also have "\<dots> = True" by (simp only: real_is_int_real)
167   ultimately show ?thesis by auto
168 qed
170 lemma real_is_int_n1: "real_is_int (-1::real)"
171 proof -
172   have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
173   also have "\<dots> = True" by (simp only: real_is_int_real)
174   ultimately show ?thesis by auto
175 qed
177 lemma real_is_int_number_of[simp]: "real_is_int ((number_of::bin\<Rightarrow>real) x)"
178 proof -
179   have neg1: "real_is_int (-1::real)"
180   proof -
181     have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
182     also have "\<dots> = True" by (simp only: real_is_int_real)
183     ultimately show ?thesis by auto
184   qed
186   {
187     fix x::int
188     have "!! y. real_is_int ((number_of::bin\<Rightarrow>real) (Abs_Bin x))"
190       apply (subst Abs_Bin_inverse)
192       apply (induct x)
193       apply (induct_tac n)
194       apply (simp)
195       apply (simp)
196       apply (induct_tac n)
198     proof -
199       fix n :: nat
200       assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
201       have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
202       show "real_is_int (of_int (- (int (Suc (Suc n)))))"
203 	apply (simp only: s of_int_add)
206 	apply (simp only: rn)
207 	done
208     qed
209   }
210   note Abs_Bin = this
211   {
212     fix x :: bin
213     have "? u. x = Abs_Bin u"
214       apply (rule exI[where x = "Rep_Bin x"])
216       done
217   }
218   then obtain u::int where "x = Abs_Bin u" by auto
219   with Abs_Bin show ?thesis by auto
220 qed
222 lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
225 lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
226 proof -
227   have 1: "(1::real) = real (1::int)" by auto
228   show ?thesis by (simp only: 1 int_of_real_real)
229 qed
231 lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
232 proof -
233   have "real_is_int (number_of b)" by simp
234   then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
235   then obtain u::int where u:"number_of b = real u" by auto
236   have "number_of b = real ((number_of b)::int)"
237     by (simp add: number_of_eq real_of_int_def)
238   have ub: "number_of b = real ((number_of b)::int)"
239     by (simp add: number_of_eq real_of_int_def)
240   from uu u ub have unb: "u = number_of b"
241     by blast
242   have "int_of_real (number_of b) = u" by (simp add: u)
243   with unb show ?thesis by simp
244 qed
246 lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
247   apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
248   apply (simp_all add: pow2_def even_def real_is_int_def ring_eq_simps)
249   apply (auto)
250 proof -
251   fix q::int
252   have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
253   show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))"
255 qed
257 consts
258   norm_float :: "int*int \<Rightarrow> int*int"
260 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
261 apply (subst split_div, auto)
262 apply (subst split_zdiv, auto)
263 apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)
264 apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
265 done
267 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
268 apply (subst split_mod, auto)
269 apply (subst split_zmod, auto)
270 apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia in IntDiv.unique_remainder)
271 apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
272 done
274 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
275 by arith
277 lemma terminating_norm_float: "\<forall>a. (a::int) \<noteq> 0 \<and> even a \<longrightarrow> a \<noteq> 0 \<and> \<bar>a div 2\<bar> < \<bar>a\<bar>"
278 apply (auto)
279 apply (rule abs_div_2_less)
280 apply (auto)
281 done
283 ML {* simp_depth_limit := 2 *}
284 recdef norm_float "measure (% (a,b). nat (abs a))"
285   "norm_float (a,b) = (if (a \<noteq> 0) & (even a) then norm_float (a div 2, b+1) else (if a=0 then (0,0) else (a,b)))"
286 (hints simp: terminating_norm_float)
287 ML {* simp_depth_limit := 1000 *}
290 lemma norm_float: "float x = float (norm_float x)"
291 proof -
292   {
293     fix a b :: int
294     have norm_float_pair: "float (a,b) = float (norm_float (a,b))"
295     proof (induct a b rule: norm_float.induct)
296       case (1 u v)
297       show ?case
298       proof cases
299 	assume u: "u \<noteq> 0 \<and> even u"
300 	with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2, v + 1))" by auto
301 	with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even)
302 	then show ?thesis
303 	  apply (subst norm_float.simps)
305 	  done
306       next
307 	assume "~(u \<noteq> 0 \<and> even u)"
308 	then show ?thesis
309 	  by (simp add: prems float_def)
310       qed
311     qed
312   }
313   note helper = this
314   have "? a b. x = (a,b)" by auto
315   then obtain a b where "x = (a, b)" by blast
316   then show ?thesis by (simp only: helper)
317 qed
319 lemma pow2_int: "pow2 (int n) = 2^n"
323   "float (a1, e1) + float (a2, e2) =
324   (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1)
325   else float (a1*2^(nat (e1-e2))+a2, e2))"
326   apply (simp add: float_def ring_eq_simps)
328   done
330 lemma float_mult:
331   "float (a1, e1) * float (a2, e2) =
332   (float (a1 * a2, e1 + e2))"
335 lemma float_minus:
336   "- (float (a,b)) = float (-a, b)"
339 lemma zero_less_pow2:
340   "0 < pow2 x"
341 proof -
342   {
343     fix y
344     have "0 <= y \<Longrightarrow> 0 < pow2 y"
346   }
347   note helper=this
348   show ?thesis
349     apply (case_tac "0 <= x")
351     apply (subst pow2_neg)
353     done
354 qed
356 lemma zero_le_float:
357   "(0 <= float (a,b)) = (0 <= a)"
358   apply (auto simp add: float_def)
359   apply (auto simp add: zero_le_mult_iff zero_less_pow2)
360   apply (insert zero_less_pow2[of b])
361   apply (simp_all)
362   done
364 lemma float_abs:
365   "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
366   apply (auto simp add: abs_if)
367   apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
368   done
370 lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
371   by auto
374   by simp
377   by simp
379 lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
380   by simp
382 lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
383   by simp
385 lemma int_pow_0: "(a::int)^(Numeral0) = 1"
386   by simp
388 lemma int_pow_1: "(a::int)^(Numeral1) = a"
389   by simp
391 lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
392   by simp
394 lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
395   by simp
397 lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
398   by simp
400 lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
401   by simp
403 lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
404   by simp
406 lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
407 proof -
408   have 1:"((-1)::nat) = 0"
409     by simp
410   show ?thesis by (simp add: 1)
411 qed
413 lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
414   by simp
416 lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
417   by simp
419 lemma lift_bool: "x \<Longrightarrow> x=True"
420   by simp
422 lemma nlift_bool: "~x \<Longrightarrow> x=False"
423   by simp
425 lemma not_false_eq_true: "(~ False) = True" by simp
427 lemma not_true_eq_false: "(~ True) = False" by simp
430 lemmas binarith =
431   Pls_0_eq Min_1_eq
432   bin_pred_Pls bin_pred_Min bin_pred_1 bin_pred_0
433   bin_succ_Pls bin_succ_Min bin_succ_1 bin_succ_0
436   bin_minus_0 bin_mult_Pls bin_mult_Min bin_mult_1 bin_mult_0
439 thm eq_number_of_eq
441 lemma int_eq_number_of_eq: "(((number_of v)::int)=(number_of w)) = iszero ((number_of (bin_add v (bin_minus w)))::int)"
442   by simp
444 lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
445   by (simp only: iszero_number_of_Pls)
447 lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
448   by simp
449 thm iszero_number_of_1
451 lemma int_iszero_number_of_0: "iszero ((number_of (w BIT False))::int) = iszero ((number_of w)::int)"
452   by simp
454 lemma int_iszero_number_of_1: "\<not> iszero ((number_of (w BIT True))::int)"
455   by simp
457 lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (bin_add x (bin_minus y)))::int)"
458   by simp
460 lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
461   by simp
463 lemma int_neg_number_of_Min: "neg (-1::int)"
464   by simp
466 lemma int_neg_number_of_BIT: "neg ((number_of (w BIT x))::int) = neg ((number_of w)::int)"
467   by simp
469 lemma int_le_number_of_eq: "(((number_of x)::int) \<le> number_of y) = (\<not> neg ((number_of (bin_add y (bin_minus x)))::int))"
470   by simp
472 lemmas intarithrel =
473   (*abs_zero abs_one*)
474   int_eq_number_of_eq
475   lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_0
476   lift_bool[OF int_iszero_number_of_1] int_less_number_of_eq_neg nlift_bool[OF int_not_neg_number_of_Pls] lift_bool[OF int_neg_number_of_Min]
477   int_neg_number_of_BIT int_le_number_of_eq
479 lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (bin_add v w)"
480   by simp
482 lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (bin_add v (bin_minus w))"
483   by simp
485 lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (bin_mult v w)"
486   by simp
488 lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (bin_minus v)"
489   by simp
491 lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
493 lemmas natarith = (*zero_eq_Numeral0_nat one_eq_Numeral1_nat*) add_nat_number_of
494   diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
496 lemmas powerarith = nat_number_of zpower_number_of_even
497   zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
498   zpower_Pls zpower_Min
500 lemmas floatarith[simplified norm_0_1] = float_add float_mult float_minus float_abs zero_le_float
502 lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
504 (* needed for the verifying code generator *)
505 lemmas arith_no_let = arith[simplified Let_def]
507 end