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--
modernized header uniformly as section;
     1 (*  Title:      HOL/Matrix_LP/ComputeFloat.thy
     2     Author:     Steven Obua
     3 *)
     4 
     5 section {* Floating Point Representation of the Reals *}
     6 
     7 theory ComputeFloat
     8 imports Complex_Main "~~/src/HOL/Library/Lattice_Algebras"
     9 begin
    10 
    11 ML_file "~~/src/Tools/float.ML"
    12 
    13 definition int_of_real :: "real \<Rightarrow> int"
    14   where "int_of_real x = (SOME y. real y = x)"
    15 
    16 definition real_is_int :: "real \<Rightarrow> bool"
    17   where "real_is_int x = (EX (u::int). x = real u)"
    18 
    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)
    21 
    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)
    24 
    25 lemma int_of_real_real[simp]: "int_of_real (real x) = x"
    26 by (simp add: int_of_real_def)
    27 
    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)
    30 
    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)
    33 
    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)
    36 apply (simp add: real_is_int_add_int_of_real real_int_of_real)
    37 done
    38 
    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)
    41 
    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
    46 
    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)
    49 
    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)
    56 
    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)
    59 apply (simp add: int_of_real_mult)
    60 done
    61 
    62 lemma real_is_int_0[simp]: "real_is_int (0::real)"
    63 by (simp add: real_is_int_def int_of_real_def)
    64 
    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
    71 
    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
    78 
    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"])
    81 
    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"])
    84 
    85 lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
    86 by (simp add: int_of_real_def)
    87 
    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
    93 
    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)
    98 
    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)
   103 
   104 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
   105 by (rule zdiv_int)
   106 
   107 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
   108 by (rule zmod_int)
   109 
   110 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> abs((a::int) div 2) < abs a"
   111 by arith
   112 
   113 lemma norm_0_1: "(1::_::numeral) = Numeral1"
   114   by auto
   115 
   116 lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
   117   by simp
   118 
   119 lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
   120   by simp
   121 
   122 lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
   123   by simp
   124 
   125 lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
   126   by simp
   127 
   128 lemma int_pow_0: "(a::int)^0 = 1"
   129   by simp
   130 
   131 lemma int_pow_1: "(a::int)^(Numeral1) = a"
   132   by simp
   133 
   134 lemma one_eq_Numeral1_nring: "(1::'a::numeral) = Numeral1"
   135   by simp
   136 
   137 lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
   138   by simp
   139 
   140 lemma zpower_Pls: "(z::int)^0 = Numeral1"
   141   by simp
   142 
   143 lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
   144   by simp
   145 
   146 lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
   147   by simp
   148 
   149 lemma lift_bool: "x \<Longrightarrow> x=True"
   150   by simp
   151 
   152 lemma nlift_bool: "~x \<Longrightarrow> x=False"
   153   by simp
   154 
   155 lemma not_false_eq_true: "(~ False) = True" by simp
   156 
   157 lemma not_true_eq_false: "(~ True) = False" by simp
   158 
   159 lemmas powerarith = nat_numeral zpower_numeral_even
   160   zpower_numeral_odd zpower_Pls
   161 
   162 definition float :: "(int \<times> int) \<Rightarrow> real" where
   163   "float = (\<lambda>(a, b). real a * 2 powr real b)"
   164 
   165 lemma float_add_l0: "float (0, e) + x = x"
   166   by (simp add: float_def)
   167 
   168 lemma float_add_r0: "x + float (0, e) = x"
   169   by (simp add: float_def)
   170 
   171 lemma float_add:
   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])
   175 
   176 lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
   177   by (simp add: float_def)
   178 
   179 lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
   180   by (simp add: float_def)
   181 
   182 lemma float_mult:
   183   "float (a1, e1) * float (a2, e2) = (float (a1 * a2, e1 + e2))"
   184   by (simp add: float_def powr_add)
   185 
   186 lemma float_minus:
   187   "- (float (a,b)) = float (-a, b)"
   188   by (simp add: float_def)
   189 
   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)
   194 
   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)
   199 
   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)
   204 
   205 lemma float_zero:
   206   "float (0, b) = 0"
   207   by (simp add: float_def)
   208 
   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)
   212 
   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)
   216 
   217 definition lbound :: "real \<Rightarrow> real"
   218   where "lbound x = min 0 x"
   219 
   220 definition ubound :: "real \<Rightarrow> real"
   221   where "ubound x = max 0 x"
   222 
   223 lemma lbound: "lbound x \<le> x"   
   224   by (simp add: lbound_def)
   225 
   226 lemma ubound: "x \<le> ubound x"
   227   by (simp add: ubound_def)
   228 
   229 lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
   230   by (auto simp: float_def lbound_def)
   231 
   232 lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
   233   by (auto simp: float_def ubound_def)
   234 
   235 lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0 
   236           float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
   237 
   238 (* for use with the compute oracle *)
   239 lemmas arith = arith_simps rel_simps diff_nat_numeral nat_0
   240   nat_neg_numeral powerarith floatarith not_false_eq_true not_true_eq_false
   241 
   242 ML_file "float_arith.ML"
   243 
   244 end