src/HOL/Library/Float.thy
changeset 46573 8c4c5c8dcf7a
parent 46028 9f113cdf3d66
child 46670 e9aa6d151329
     1.1 --- a/src/HOL/Library/Float.thy	Tue Feb 21 16:04:58 2012 +0100
     1.2 +++ b/src/HOL/Library/Float.thy	Tue Feb 21 16:28:18 2012 +0100
     1.3 @@ -9,8 +9,7 @@
     1.4  imports Complex_Main Lattice_Algebras
     1.5  begin
     1.6  
     1.7 -definition
     1.8 -  pow2 :: "int \<Rightarrow> real" where
     1.9 +definition pow2 :: "int \<Rightarrow> real" where
    1.10    [simp]: "pow2 a = (if (0 <= a) then (2^(nat a)) else (inverse (2^(nat (-a)))))"
    1.11  
    1.12  datatype float = Float int int
    1.13 @@ -30,17 +29,20 @@
    1.14  primrec scale :: "float \<Rightarrow> int" where
    1.15    "scale (Float a b) = b"
    1.16  
    1.17 -instantiation float :: zero begin
    1.18 +instantiation float :: zero
    1.19 +begin
    1.20  definition zero_float where "0 = Float 0 0"
    1.21  instance ..
    1.22  end
    1.23  
    1.24 -instantiation float :: one begin
    1.25 +instantiation float :: one
    1.26 +begin
    1.27  definition one_float where "1 = Float 1 0"
    1.28  instance ..
    1.29  end
    1.30  
    1.31 -instantiation float :: number begin
    1.32 +instantiation float :: number
    1.33 +begin
    1.34  definition number_of_float where "number_of n = Float n 0"
    1.35  instance ..
    1.36  end
    1.37 @@ -124,13 +126,13 @@
    1.38    by (auto simp add: pow2_def)
    1.39  
    1.40  lemma pow2_int: "pow2 (int c) = 2^c"
    1.41 -by (simp add: pow2_def)
    1.42 +  by (simp add: pow2_def)
    1.43  
    1.44 -lemma zero_less_pow2[simp]:
    1.45 -  "0 < pow2 x"
    1.46 +lemma zero_less_pow2[simp]: "0 < pow2 x"
    1.47    by (simp add: pow2_powr)
    1.48  
    1.49 -lemma normfloat_imp_odd_or_zero: "normfloat f = Float a b \<Longrightarrow> odd a \<or> (a = 0 \<and> b = 0)"
    1.50 +lemma normfloat_imp_odd_or_zero:
    1.51 +  "normfloat f = Float a b \<Longrightarrow> odd a \<or> (a = 0 \<and> b = 0)"
    1.52  proof (induct f rule: normfloat.induct)
    1.53    case (1 u v)
    1.54    from 1 have ab: "normfloat (Float u v) = Float a b" by auto
    1.55 @@ -169,7 +171,7 @@
    1.56  
    1.57  lemma float_eq_odd_helper: 
    1.58    assumes odd: "odd a'"
    1.59 -  and floateq: "real (Float a b) = real (Float a' b')"
    1.60 +    and floateq: "real (Float a b) = real (Float a' b')"
    1.61    shows "b \<le> b'"
    1.62  proof - 
    1.63    from odd have "a' \<noteq> 0" by auto
    1.64 @@ -191,8 +193,8 @@
    1.65  
    1.66  lemma float_eq_odd: 
    1.67    assumes odd1: "odd a"
    1.68 -  and odd2: "odd a'"
    1.69 -  and floateq: "real (Float a b) = real (Float a' b')"
    1.70 +    and odd2: "odd a'"
    1.71 +    and floateq: "real (Float a b) = real (Float a' b')"
    1.72    shows "a = a' \<and> b = b'"
    1.73  proof -
    1.74    from 
    1.75 @@ -216,7 +218,7 @@
    1.76    have ab': "odd a' \<or> (a' = 0 \<and> b' = 0)" by (rule normfloat_imp_odd_or_zero[OF normg])
    1.77    {
    1.78      assume odd: "odd a"
    1.79 -    then have "a \<noteq> 0" by (simp add: even_def, arith)
    1.80 +    then have "a \<noteq> 0" by (simp add: even_def) arith
    1.81      with float_eq have "a' \<noteq> 0" by auto
    1.82      with ab' have "odd a'" by simp
    1.83      from odd this float_eq have "a = a' \<and> b = b'" by (rule float_eq_odd)
    1.84 @@ -236,7 +238,8 @@
    1.85      done
    1.86  qed
    1.87  
    1.88 -instantiation float :: plus begin
    1.89 +instantiation float :: plus
    1.90 +begin
    1.91  fun plus_float where
    1.92  [simp del]: "(Float a_m a_e) + (Float b_m b_e) = 
    1.93       (if a_e \<le> b_e then Float (a_m + b_m * 2^(nat(b_e - a_e))) a_e 
    1.94 @@ -244,17 +247,20 @@
    1.95  instance ..
    1.96  end
    1.97  
    1.98 -instantiation float :: uminus begin
    1.99 +instantiation float :: uminus
   1.100 +begin
   1.101  primrec uminus_float where [simp del]: "uminus_float (Float m e) = Float (-m) e"
   1.102  instance ..
   1.103  end
   1.104  
   1.105 -instantiation float :: minus begin
   1.106 -definition minus_float where [simp del]: "(z::float) - w = z + (- w)"
   1.107 +instantiation float :: minus
   1.108 +begin
   1.109 +definition minus_float where "(z::float) - w = z + (- w)"
   1.110  instance ..
   1.111  end
   1.112  
   1.113 -instantiation float :: times begin
   1.114 +instantiation float :: times
   1.115 +begin
   1.116  fun times_float where [simp del]: "(Float a_m a_e) * (Float b_m b_e) = Float (a_m * b_m) (a_e + b_e)"
   1.117  instance ..
   1.118  end
   1.119 @@ -265,7 +271,8 @@
   1.120  primrec float_nprt :: "float \<Rightarrow> float" where
   1.121    "float_nprt (Float a e) = (if 0 <= a then 0 else (Float a e))" 
   1.122  
   1.123 -instantiation float :: ord begin
   1.124 +instantiation float :: ord
   1.125 +begin
   1.126  definition le_float_def: "z \<le> (w :: float) \<equiv> real z \<le> real w"
   1.127  definition less_float_def: "z < (w :: float) \<equiv> real z < real w"
   1.128  instance ..
   1.129 @@ -276,7 +283,7 @@
   1.130        auto simp add: pow2_int[symmetric] pow2_add[symmetric])
   1.131  
   1.132  lemma real_of_float_minus[simp]: "real (- a) = - real (a :: float)"
   1.133 -  by (cases a) (simp add: uminus_float.simps)
   1.134 +  by (cases a) simp
   1.135  
   1.136  lemma real_of_float_sub[simp]: "real (a - b) = real a - real (b :: float)"
   1.137    by (cases a, cases b) (simp add: minus_float_def)
   1.138 @@ -285,7 +292,7 @@
   1.139    by (cases a, cases b) (simp add: times_float.simps pow2_add)
   1.140  
   1.141  lemma real_of_float_0[simp]: "real (0 :: float) = 0"
   1.142 -  by (auto simp add: zero_float_def float_zero)
   1.143 +  by (auto simp add: zero_float_def)
   1.144  
   1.145  lemma real_of_float_1[simp]: "real (1 :: float) = 1"
   1.146    by (auto simp add: one_float_def)
   1.147 @@ -1161,16 +1168,20 @@
   1.148  qed
   1.149  
   1.150  definition lb_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
   1.151 -"lb_mult prec x y = (case normfloat (x * y) of Float m e \<Rightarrow> let
   1.152 -    l = bitlen m - int prec
   1.153 -  in if l > 0 then Float (m div (2^nat l)) (e + l)
   1.154 -              else Float m e)"
   1.155 +  "lb_mult prec x y =
   1.156 +    (case normfloat (x * y) of Float m e \<Rightarrow>
   1.157 +      let
   1.158 +        l = bitlen m - int prec
   1.159 +      in if l > 0 then Float (m div (2^nat l)) (e + l)
   1.160 +                  else Float m e)"
   1.161  
   1.162  definition ub_mult :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
   1.163 -"ub_mult prec x y = (case normfloat (x * y) of Float m e \<Rightarrow> let
   1.164 -    l = bitlen m - int prec
   1.165 -  in if l > 0 then Float (m div (2^nat l) + 1) (e + l)
   1.166 -              else Float m e)"
   1.167 +  "ub_mult prec x y =
   1.168 +    (case normfloat (x * y) of Float m e \<Rightarrow>
   1.169 +      let
   1.170 +        l = bitlen m - int prec
   1.171 +      in if l > 0 then Float (m div (2^nat l) + 1) (e + l)
   1.172 +                  else Float m e)"
   1.173  
   1.174  lemma lb_mult: "real (lb_mult prec x y) \<le> real (x * y)"
   1.175  proof (cases "normfloat (x * y)")
   1.176 @@ -1225,7 +1236,8 @@
   1.177  primrec float_abs :: "float \<Rightarrow> float" where
   1.178    "float_abs (Float m e) = Float \<bar>m\<bar> e"
   1.179  
   1.180 -instantiation float :: abs begin
   1.181 +instantiation float :: abs
   1.182 +begin
   1.183  definition abs_float_def: "\<bar>x\<bar> = float_abs x"
   1.184  instance ..
   1.185  end
   1.186 @@ -1290,10 +1302,10 @@
   1.187  declare ceiling_fl.simps[simp del]
   1.188  
   1.189  definition lb_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
   1.190 -"lb_mod prec x ub lb = x - ceiling_fl (float_divr prec x lb) * ub"
   1.191 +  "lb_mod prec x ub lb = x - ceiling_fl (float_divr prec x lb) * ub"
   1.192  
   1.193  definition ub_mod :: "nat \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float \<Rightarrow> float" where
   1.194 -"ub_mod prec x ub lb = x - floor_fl (float_divl prec x ub) * lb"
   1.195 +  "ub_mod prec x ub lb = x - floor_fl (float_divl prec x ub) * lb"
   1.196  
   1.197  lemma lb_mod: fixes k :: int assumes "0 \<le> real x" and "real k * y \<le> real x" (is "?k * y \<le> ?x")
   1.198    assumes "0 < real lb" "real lb \<le> y" (is "?lb \<le> y") "y \<le> real ub" (is "y \<le> ?ub")
   1.199 @@ -1307,7 +1319,9 @@
   1.200    finally show ?thesis unfolding lb_mod_def real_of_float_sub real_of_float_mult by auto
   1.201  qed
   1.202  
   1.203 -lemma ub_mod: fixes k :: int and x :: float assumes "0 \<le> real x" and "real x \<le> real k * y" (is "?x \<le> ?k * y")
   1.204 +lemma ub_mod:
   1.205 +  fixes k :: int and x :: float
   1.206 +  assumes "0 \<le> real x" and "real x \<le> real k * y" (is "?x \<le> ?k * y")
   1.207    assumes "0 < real lb" "real lb \<le> y" (is "?lb \<le> y") "y \<le> real ub" (is "y \<le> ?ub")
   1.208    shows "?x - ?k * y \<le> real (ub_mod prec x ub lb)"
   1.209  proof -