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