src/HOL/Real/Float.thy
author wenzelm
Fri Jun 02 23:22:29 2006 +0200 (2006-06-02)
changeset 19765 dfe940911617
parent 16890 c4e5afaba440
child 20217 25b068a99d2b
permissions -rw-r--r--
misc cleanup;
     1 (*  Title: HOL/Real/Float.thy
     2     ID:    $Id$
     3     Author: Steven Obua
     4 *)
     5 
     6 theory Float imports Real begin
     7 
     8 definition
     9   pow2 :: "int \<Rightarrow> real"
    10   "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
    11   float :: "int * int \<Rightarrow> real"
    12   "float x = real (fst x) * pow2 (snd x)"
    13 
    14 lemma pow2_0[simp]: "pow2 0 = 1"
    15 by (simp add: pow2_def)
    16 
    17 lemma pow2_1[simp]: "pow2 1 = 2"
    18 by (simp add: pow2_def)
    19 
    20 lemma pow2_neg: "pow2 x = inverse (pow2 (-x))"
    21 by (simp add: pow2_def)
    22 
    23 lemma pow2_add1: "pow2 (1 + a) = 2 * (pow2 a)"
    24 proof -
    25   have h: "! n. nat (2 + int n) - Suc 0 = nat (1 + int n)" by arith
    26   have g: "! a b. a - -1 = a + (1::int)" by arith
    27   have pos: "! n. pow2 (int n + 1) = 2 * pow2 (int n)"
    28     apply (auto, induct_tac n)
    29     apply (simp_all add: pow2_def)
    30     apply (rule_tac m1="2" and n1="nat (2 + int na)" in ssubst[OF realpow_num_eq_if])
    31     apply (auto simp add: h)
    32     apply arith
    33     done
    34   show ?thesis
    35   proof (induct a)
    36     case (1 n)
    37     from pos show ?case by (simp add: ring_eq_simps)
    38   next
    39     case (2 n)
    40     show ?case
    41       apply (auto)
    42       apply (subst pow2_neg[of "- int n"])
    43       apply (subst pow2_neg[of "-1 - int n"])
    44       apply (auto simp add: g pos)
    45       done
    46   qed
    47 qed
    48 
    49 lemma pow2_add: "pow2 (a+b) = (pow2 a) * (pow2 b)"
    50 proof (induct b)
    51   case (1 n)
    52   show ?case
    53   proof (induct n)
    54     case 0
    55     show ?case by simp
    56   next
    57     case (Suc m)
    58     show ?case by (auto simp add: ring_eq_simps pow2_add1 prems)
    59   qed
    60 next
    61   case (2 n)
    62   show ?case
    63   proof (induct n)
    64     case 0
    65     show ?case
    66       apply (auto)
    67       apply (subst pow2_neg[of "a + -1"])
    68       apply (subst pow2_neg[of "-1"])
    69       apply (simp)
    70       apply (insert pow2_add1[of "-a"])
    71       apply (simp add: ring_eq_simps)
    72       apply (subst pow2_neg[of "-a"])
    73       apply (simp)
    74       done
    75     case (Suc m)
    76     have a: "int m - (a + -2) =  1 + (int m - a + 1)" by arith
    77     have b: "int m - -2 = 1 + (int m + 1)" by arith
    78     show ?case
    79       apply (auto)
    80       apply (subst pow2_neg[of "a + (-2 - int m)"])
    81       apply (subst pow2_neg[of "-2 - int m"])
    82       apply (auto simp add: ring_eq_simps)
    83       apply (subst a)
    84       apply (subst b)
    85       apply (simp only: pow2_add1)
    86       apply (subst pow2_neg[of "int m - a + 1"])
    87       apply (subst pow2_neg[of "int m + 1"])
    88       apply auto
    89       apply (insert prems)
    90       apply (auto simp add: ring_eq_simps)
    91       done
    92   qed
    93 qed
    94 
    95 lemma "float (a, e) + float (b, e) = float (a + b, e)"
    96 by (simp add: float_def ring_eq_simps)
    97 
    98 definition
    99   int_of_real :: "real \<Rightarrow> int"
   100   "int_of_real x = (SOME y. real y = x)"
   101   real_is_int :: "real \<Rightarrow> bool"
   102   "real_is_int x = (EX (u::int). x = real u)"
   103 
   104 lemma real_is_int_def2: "real_is_int x = (x = real (int_of_real x))"
   105 by (auto simp add: real_is_int_def int_of_real_def)
   106 
   107 lemma float_transfer: "real_is_int ((real a)*(pow2 c)) \<Longrightarrow> float (a, b) = float (int_of_real ((real a)*(pow2 c)), b - c)"
   108 by (simp add: float_def real_is_int_def2 pow2_add[symmetric])
   109 
   110 lemma pow2_int: "pow2 (int c) = (2::real)^c"
   111 by (simp add: pow2_def)
   112 
   113 lemma float_transfer_nat: "float (a, b) = float (a * 2^c, b - int c)"
   114 by (simp add: float_def pow2_int[symmetric] pow2_add[symmetric])
   115 
   116 lemma real_is_int_real[simp]: "real_is_int (real (x::int))"
   117 by (auto simp add: real_is_int_def int_of_real_def)
   118 
   119 lemma int_of_real_real[simp]: "int_of_real (real x) = x"
   120 by (simp add: int_of_real_def)
   121 
   122 lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real (int_of_real x) = x"
   123 by (auto simp add: int_of_real_def real_is_int_def)
   124 
   125 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)"
   126 by (auto simp add: int_of_real_def real_is_int_def)
   127 
   128 lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
   129 apply (subst real_is_int_def2)
   130 apply (simp add: real_is_int_add_int_of_real real_int_of_real)
   131 done
   132 
   133 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)"
   134 by (auto simp add: int_of_real_def real_is_int_def)
   135 
   136 lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
   137 apply (subst real_is_int_def2)
   138 apply (simp add: int_of_real_sub real_int_of_real)
   139 done
   140 
   141 lemma real_is_int_rep: "real_is_int x \<Longrightarrow> ?! (a::int). real a = x"
   142 by (auto simp add: real_is_int_def)
   143 
   144 lemma int_of_real_mult:
   145   assumes "real_is_int a" "real_is_int b"
   146   shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
   147 proof -
   148   from prems have a: "?! (a'::int). real a' = a" by (rule_tac real_is_int_rep, auto)
   149   from prems have b: "?! (b'::int). real b' = b" by (rule_tac real_is_int_rep, auto)
   150   from a obtain a'::int where a':"a = real a'" by auto
   151   from b obtain b'::int where b':"b = real b'" by auto
   152   have r: "real a' * real b' = real (a' * b')" by auto
   153   show ?thesis
   154     apply (simp add: a' b')
   155     apply (subst r)
   156     apply (simp only: int_of_real_real)
   157     done
   158 qed
   159 
   160 lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
   161 apply (subst real_is_int_def2)
   162 apply (simp add: int_of_real_mult)
   163 done
   164 
   165 lemma real_is_int_0[simp]: "real_is_int (0::real)"
   166 by (simp add: real_is_int_def int_of_real_def)
   167 
   168 lemma real_is_int_1[simp]: "real_is_int (1::real)"
   169 proof -
   170   have "real_is_int (1::real) = real_is_int(real (1::int))" by auto
   171   also have "\<dots> = True" by (simp only: real_is_int_real)
   172   ultimately show ?thesis by auto
   173 qed
   174 
   175 lemma real_is_int_n1: "real_is_int (-1::real)"
   176 proof -
   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
   180 qed
   181 
   182 lemma real_is_int_number_of[simp]: "real_is_int ((number_of::bin\<Rightarrow>real) x)"
   183 proof -
   184   have neg1: "real_is_int (-1::real)"
   185   proof -
   186     have "real_is_int (-1::real) = real_is_int(real (-1::int))" by auto
   187     also have "\<dots> = True" by (simp only: real_is_int_real)
   188     ultimately show ?thesis by auto
   189   qed
   190 
   191   {
   192     fix x::int
   193     have "!! y. real_is_int ((number_of::bin\<Rightarrow>real) (Abs_Bin x))"
   194       apply (simp add: number_of_eq)
   195       apply (subst Abs_Bin_inverse)
   196       apply (simp add: Bin_def)
   197       apply (induct x)
   198       apply (induct_tac n)
   199       apply (simp)
   200       apply (simp)
   201       apply (induct_tac n)
   202       apply (simp add: neg1)
   203     proof -
   204       fix n :: nat
   205       assume rn: "(real_is_int (of_int (- (int (Suc n)))))"
   206       have s: "-(int (Suc (Suc n))) = -1 + - (int (Suc n))" by simp
   207       show "real_is_int (of_int (- (int (Suc (Suc n)))))"
   208         apply (simp only: s of_int_add)
   209         apply (rule real_is_int_add)
   210         apply (simp add: neg1)
   211         apply (simp only: rn)
   212         done
   213     qed
   214   }
   215   note Abs_Bin = this
   216   {
   217     fix x :: bin
   218     have "? u. x = Abs_Bin u"
   219       apply (rule exI[where x = "Rep_Bin x"])
   220       apply (simp add: Rep_Bin_inverse)
   221       done
   222   }
   223   then obtain u::int where "x = Abs_Bin u" by auto
   224   with Abs_Bin show ?thesis by auto
   225 qed
   226 
   227 lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
   228 by (simp add: int_of_real_def)
   229 
   230 lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
   231 proof -
   232   have 1: "(1::real) = real (1::int)" by auto
   233   show ?thesis by (simp only: 1 int_of_real_real)
   234 qed
   235 
   236 lemma int_of_real_number_of[simp]: "int_of_real (number_of b) = number_of b"
   237 proof -
   238   have "real_is_int (number_of b)" by simp
   239   then have uu: "?! u::int. number_of b = real u" by (auto simp add: real_is_int_rep)
   240   then obtain u::int where u:"number_of b = real u" by auto
   241   have "number_of b = real ((number_of b)::int)"
   242     by (simp add: number_of_eq real_of_int_def)
   243   have ub: "number_of b = real ((number_of b)::int)"
   244     by (simp add: number_of_eq real_of_int_def)
   245   from uu u ub have unb: "u = number_of b"
   246     by blast
   247   have "int_of_real (number_of b) = u" by (simp add: u)
   248   with unb show ?thesis by simp
   249 qed
   250 
   251 lemma float_transfer_even: "even a \<Longrightarrow> float (a, b) = float (a div 2, b+1)"
   252   apply (subst float_transfer[where a="a" and b="b" and c="-1", simplified])
   253   apply (simp_all add: pow2_def even_def real_is_int_def ring_eq_simps)
   254   apply (auto)
   255 proof -
   256   fix q::int
   257   have a:"b - (-1\<Colon>int) = (1\<Colon>int) + b" by arith
   258   show "(float (q, (b - (-1\<Colon>int)))) = (float (q, ((1\<Colon>int) + b)))"
   259     by (simp add: a)
   260 qed
   261 
   262 consts
   263   norm_float :: "int*int \<Rightarrow> int*int"
   264 
   265 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
   266 apply (subst split_div, auto)
   267 apply (subst split_zdiv, auto)
   268 apply (rule_tac a="int (b * i) + int j" and b="int b" and r="int j" and r'=ja in IntDiv.unique_quotient)
   269 apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
   270 done
   271 
   272 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
   273 apply (subst split_mod, auto)
   274 apply (subst split_zmod, auto)
   275 apply (rule_tac a="int (b * i) + int j" and b="int b" and q="int i" and q'=ia in IntDiv.unique_remainder)
   276 apply (auto simp add: IntDiv.quorem_def int_eq_of_nat)
   277 done
   278 
   279 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
   280 by arith
   281 
   282 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>"
   283 apply (auto)
   284 apply (rule abs_div_2_less)
   285 apply (auto)
   286 done
   287 
   288 ML {* simp_depth_limit := 2 *}
   289 recdef norm_float "measure (% (a,b). nat (abs a))"
   290   "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)))"
   291 (hints simp: terminating_norm_float)
   292 ML {* simp_depth_limit := 1000 *}
   293 
   294 lemma norm_float: "float x = float (norm_float x)"
   295 proof -
   296   {
   297     fix a b :: int
   298     have norm_float_pair: "float (a,b) = float (norm_float (a,b))"
   299     proof (induct a b rule: norm_float.induct)
   300       case (1 u v)
   301       show ?case
   302       proof cases
   303         assume u: "u \<noteq> 0 \<and> even u"
   304         with prems have ind: "float (u div 2, v + 1) = float (norm_float (u div 2, v + 1))" by auto
   305         with u have "float (u,v) = float (u div 2, v+1)" by (simp add: float_transfer_even)
   306         then show ?thesis
   307           apply (subst norm_float.simps)
   308           apply (simp add: ind)
   309           done
   310       next
   311         assume "~(u \<noteq> 0 \<and> even u)"
   312         then show ?thesis
   313           by (simp add: prems float_def)
   314       qed
   315     qed
   316   }
   317   note helper = this
   318   have "? a b. x = (a,b)" by auto
   319   then obtain a b where "x = (a, b)" by blast
   320   then show ?thesis by (simp only: helper)
   321 qed
   322 
   323 lemma pow2_int: "pow2 (int n) = 2^n"
   324   by (simp add: pow2_def)
   325 
   326 lemma float_add:
   327   "float (a1, e1) + float (a2, e2) =
   328   (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1)
   329   else float (a1*2^(nat (e1-e2))+a2, e2))"
   330   apply (simp add: float_def ring_eq_simps)
   331   apply (auto simp add: pow2_int[symmetric] pow2_add[symmetric])
   332   done
   333 
   334 lemma float_mult:
   335   "float (a1, e1) * float (a2, e2) =
   336   (float (a1 * a2, e1 + e2))"
   337   by (simp add: float_def pow2_add)
   338 
   339 lemma float_minus:
   340   "- (float (a,b)) = float (-a, b)"
   341   by (simp add: float_def)
   342 
   343 lemma zero_less_pow2:
   344   "0 < pow2 x"
   345 proof -
   346   {
   347     fix y
   348     have "0 <= y \<Longrightarrow> 0 < pow2 y"
   349       by (induct y, induct_tac n, simp_all add: pow2_add)
   350   }
   351   note helper=this
   352   show ?thesis
   353     apply (case_tac "0 <= x")
   354     apply (simp add: helper)
   355     apply (subst pow2_neg)
   356     apply (simp add: helper)
   357     done
   358 qed
   359 
   360 lemma zero_le_float:
   361   "(0 <= float (a,b)) = (0 <= a)"
   362   apply (auto simp add: float_def)
   363   apply (auto simp add: zero_le_mult_iff zero_less_pow2)
   364   apply (insert zero_less_pow2[of b])
   365   apply (simp_all)
   366   done
   367 
   368 lemma float_le_zero:
   369   "(float (a,b) <= 0) = (a <= 0)"
   370   apply (auto simp add: float_def)
   371   apply (auto simp add: mult_le_0_iff)
   372   apply (insert zero_less_pow2[of b])
   373   apply auto
   374   done
   375 
   376 lemma float_abs:
   377   "abs (float (a,b)) = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
   378   apply (auto simp add: abs_if)
   379   apply (simp_all add: zero_le_float[symmetric, of a b] float_minus)
   380   done
   381 
   382 lemma float_zero:
   383   "float (0, b) = 0"
   384   by (simp add: float_def)
   385 
   386 lemma float_pprt:
   387   "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
   388   by (auto simp add: zero_le_float float_le_zero float_zero)
   389 
   390 lemma float_nprt:
   391   "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
   392   by (auto simp add: zero_le_float float_le_zero float_zero)
   393 
   394 lemma norm_0_1: "(0::_::number_ring) = Numeral0 & (1::_::number_ring) = Numeral1"
   395   by auto
   396 
   397 lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
   398   by simp
   399 
   400 lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
   401   by simp
   402 
   403 lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
   404   by simp
   405 
   406 lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
   407   by simp
   408 
   409 lemma int_pow_0: "(a::int)^(Numeral0) = 1"
   410   by simp
   411 
   412 lemma int_pow_1: "(a::int)^(Numeral1) = a"
   413   by simp
   414 
   415 lemma zero_eq_Numeral0_nring: "(0::'a::number_ring) = Numeral0"
   416   by simp
   417 
   418 lemma one_eq_Numeral1_nring: "(1::'a::number_ring) = Numeral1"
   419   by simp
   420 
   421 lemma zero_eq_Numeral0_nat: "(0::nat) = Numeral0"
   422   by simp
   423 
   424 lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
   425   by simp
   426 
   427 lemma zpower_Pls: "(z::int)^Numeral0 = Numeral1"
   428   by simp
   429 
   430 lemma zpower_Min: "(z::int)^((-1)::nat) = Numeral1"
   431 proof -
   432   have 1:"((-1)::nat) = 0"
   433     by simp
   434   show ?thesis by (simp add: 1)
   435 qed
   436 
   437 lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
   438   by simp
   439 
   440 lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
   441   by simp
   442 
   443 lemma lift_bool: "x \<Longrightarrow> x=True"
   444   by simp
   445 
   446 lemma nlift_bool: "~x \<Longrightarrow> x=False"
   447   by simp
   448 
   449 lemma not_false_eq_true: "(~ False) = True" by simp
   450 
   451 lemma not_true_eq_false: "(~ True) = False" by simp
   452 
   453 
   454 lemmas binarith =
   455   Pls_0_eq Min_1_eq
   456   bin_pred_Pls bin_pred_Min bin_pred_1 bin_pred_0
   457   bin_succ_Pls bin_succ_Min bin_succ_1 bin_succ_0
   458   bin_add_Pls bin_add_Min bin_add_BIT_0 bin_add_BIT_10
   459   bin_add_BIT_11 bin_minus_Pls bin_minus_Min bin_minus_1
   460   bin_minus_0 bin_mult_Pls bin_mult_Min bin_mult_1 bin_mult_0
   461   bin_add_Pls_right bin_add_Min_right
   462 
   463 lemma int_eq_number_of_eq: "(((number_of v)::int)=(number_of w)) = iszero ((number_of (bin_add v (bin_minus w)))::int)"
   464   by simp
   465 
   466 lemma int_iszero_number_of_Pls: "iszero (Numeral0::int)"
   467   by (simp only: iszero_number_of_Pls)
   468 
   469 lemma int_nonzero_number_of_Min: "~(iszero ((-1)::int))"
   470   by simp
   471 
   472 lemma int_iszero_number_of_0: "iszero ((number_of (w BIT bit.B0))::int) = iszero ((number_of w)::int)"
   473   by simp
   474 
   475 lemma int_iszero_number_of_1: "\<not> iszero ((number_of (w BIT bit.B1))::int)"
   476   by simp
   477 
   478 lemma int_less_number_of_eq_neg: "(((number_of x)::int) < number_of y) = neg ((number_of (bin_add x (bin_minus y)))::int)"
   479   by simp
   480 
   481 lemma int_not_neg_number_of_Pls: "\<not> (neg (Numeral0::int))"
   482   by simp
   483 
   484 lemma int_neg_number_of_Min: "neg (-1::int)"
   485   by simp
   486 
   487 lemma int_neg_number_of_BIT: "neg ((number_of (w BIT x))::int) = neg ((number_of w)::int)"
   488   by simp
   489 
   490 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))"
   491   by simp
   492 
   493 lemmas intarithrel =
   494   int_eq_number_of_eq
   495   lift_bool[OF int_iszero_number_of_Pls] nlift_bool[OF int_nonzero_number_of_Min] int_iszero_number_of_0
   496   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]
   497   int_neg_number_of_BIT int_le_number_of_eq
   498 
   499 lemma int_number_of_add_sym: "((number_of v)::int) + number_of w = number_of (bin_add v w)"
   500   by simp
   501 
   502 lemma int_number_of_diff_sym: "((number_of v)::int) - number_of w = number_of (bin_add v (bin_minus w))"
   503   by simp
   504 
   505 lemma int_number_of_mult_sym: "((number_of v)::int) * number_of w = number_of (bin_mult v w)"
   506   by simp
   507 
   508 lemma int_number_of_minus_sym: "- ((number_of v)::int) = number_of (bin_minus v)"
   509   by simp
   510 
   511 lemmas intarith = int_number_of_add_sym int_number_of_minus_sym int_number_of_diff_sym int_number_of_mult_sym
   512 
   513 lemmas natarith = add_nat_number_of diff_nat_number_of mult_nat_number_of eq_nat_number_of less_nat_number_of
   514 
   515 lemmas powerarith = nat_number_of zpower_number_of_even
   516   zpower_number_of_odd[simplified zero_eq_Numeral0_nring one_eq_Numeral1_nring]
   517   zpower_Pls zpower_Min
   518 
   519 lemmas floatarith[simplified norm_0_1] = float_add float_mult float_minus float_abs zero_le_float float_pprt float_nprt
   520 
   521 (* for use with the compute oracle *)
   522 lemmas arith = binarith intarith intarithrel natarith powerarith floatarith not_false_eq_true not_true_eq_false
   523 
   524 end