src/HOL/Matrix_LP/ComputeFloat.thy
author wenzelm
Tue Oct 10 19:23:03 2017 +0200 (2017-10-10)
changeset 66831 29ea2b900a05
parent 66453 cc19f7ca2ed6
child 67573 ed0a7090167d
permissions -rw-r--r--
tuned: each session has at most one defining entry;
     1 (*  Title:      HOL/Matrix_LP/ComputeFloat.thy
     2     Author:     Steven Obua
     3 *)
     4 
     5 section \<open>Floating Point Representation of the Reals\<close>
     6 
     7 theory ComputeFloat
     8 imports Complex_Main "HOL-Library.Lattice_Algebras"
     9 begin
    10 
    11 ML_file "~~/src/Tools/float.ML"
    12 
    13 (*FIXME surely floor should be used? This file is full of redundant material.*)
    14 
    15 definition int_of_real :: "real \<Rightarrow> int"
    16   where "int_of_real x = (SOME y. real_of_int y = x)"
    17 
    18 definition real_is_int :: "real \<Rightarrow> bool"
    19   where "real_is_int x = (EX (u::int). x = real_of_int u)"
    20 
    21 lemma real_is_int_def2: "real_is_int x = (x = real_of_int (int_of_real x))"
    22   by (auto simp add: real_is_int_def int_of_real_def)
    23 
    24 lemma real_is_int_real[simp]: "real_is_int (real_of_int (x::int))"
    25 by (auto simp add: real_is_int_def int_of_real_def)
    26 
    27 lemma int_of_real_real[simp]: "int_of_real (real_of_int x) = x"
    28 by (simp add: int_of_real_def)
    29 
    30 lemma real_int_of_real[simp]: "real_is_int x \<Longrightarrow> real_of_int (int_of_real x) = x"
    31 by (auto simp add: int_of_real_def real_is_int_def)
    32 
    33 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)"
    34 by (auto simp add: int_of_real_def real_is_int_def)
    35 
    36 lemma real_is_int_add[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a+b)"
    37 apply (subst real_is_int_def2)
    38 apply (simp add: real_is_int_add_int_of_real real_int_of_real)
    39 done
    40 
    41 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)"
    42 by (auto simp add: int_of_real_def real_is_int_def)
    43 
    44 lemma real_is_int_sub[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a-b)"
    45 apply (subst real_is_int_def2)
    46 apply (simp add: int_of_real_sub real_int_of_real)
    47 done
    48 
    49 lemma real_is_int_rep: "real_is_int x \<Longrightarrow> \<exists>!(a::int). real_of_int a = x"
    50 by (auto simp add: real_is_int_def)
    51 
    52 lemma int_of_real_mult:
    53   assumes "real_is_int a" "real_is_int b"
    54   shows "(int_of_real (a*b)) = (int_of_real a) * (int_of_real b)"
    55   using assms
    56   by (auto simp add: real_is_int_def of_int_mult[symmetric]
    57            simp del: of_int_mult)
    58 
    59 lemma real_is_int_mult[simp]: "real_is_int a \<Longrightarrow> real_is_int b \<Longrightarrow> real_is_int (a*b)"
    60 apply (subst real_is_int_def2)
    61 apply (simp add: int_of_real_mult)
    62 done
    63 
    64 lemma real_is_int_0[simp]: "real_is_int (0::real)"
    65 by (simp add: real_is_int_def int_of_real_def)
    66 
    67 lemma real_is_int_1[simp]: "real_is_int (1::real)"
    68 proof -
    69   have "real_is_int (1::real) = real_is_int(real_of_int (1::int))" by auto
    70   also have "\<dots> = True" by (simp only: real_is_int_real)
    71   ultimately show ?thesis by auto
    72 qed
    73 
    74 lemma real_is_int_n1: "real_is_int (-1::real)"
    75 proof -
    76   have "real_is_int (-1::real) = real_is_int(real_of_int (-1::int))" by auto
    77   also have "\<dots> = True" by (simp only: real_is_int_real)
    78   ultimately show ?thesis by auto
    79 qed
    80 
    81 lemma real_is_int_numeral[simp]: "real_is_int (numeral x)"
    82   by (auto simp: real_is_int_def intro!: exI[of _ "numeral x"])
    83 
    84 lemma real_is_int_neg_numeral[simp]: "real_is_int (- numeral x)"
    85   by (auto simp: real_is_int_def intro!: exI[of _ "- numeral x"])
    86 
    87 lemma int_of_real_0[simp]: "int_of_real (0::real) = (0::int)"
    88 by (simp add: int_of_real_def)
    89 
    90 lemma int_of_real_1[simp]: "int_of_real (1::real) = (1::int)"
    91 proof -
    92   have 1: "(1::real) = real_of_int (1::int)" by auto
    93   show ?thesis by (simp only: 1 int_of_real_real)
    94 qed
    95 
    96 lemma int_of_real_numeral[simp]: "int_of_real (numeral b) = numeral b"
    97   unfolding int_of_real_def by simp
    98 
    99 lemma int_of_real_neg_numeral[simp]: "int_of_real (- numeral b) = - numeral b"
   100   unfolding int_of_real_def
   101   by (metis int_of_real_def int_of_real_real of_int_minus of_int_of_nat_eq of_nat_numeral) 
   102 
   103 lemma int_div_zdiv: "int (a div b) = (int a) div (int b)"
   104 by (rule zdiv_int)
   105 
   106 lemma int_mod_zmod: "int (a mod b) = (int a) mod (int b)"
   107 by (rule zmod_int)
   108 
   109 lemma abs_div_2_less: "a \<noteq> 0 \<Longrightarrow> a \<noteq> -1 \<Longrightarrow> \<bar>(a::int) div 2\<bar> < \<bar>a\<bar>"
   110 by arith
   111 
   112 lemma norm_0_1: "(1::_::numeral) = Numeral1"
   113   by auto
   114 
   115 lemma add_left_zero: "0 + a = (a::'a::comm_monoid_add)"
   116   by simp
   117 
   118 lemma add_right_zero: "a + 0 = (a::'a::comm_monoid_add)"
   119   by simp
   120 
   121 lemma mult_left_one: "1 * a = (a::'a::semiring_1)"
   122   by simp
   123 
   124 lemma mult_right_one: "a * 1 = (a::'a::semiring_1)"
   125   by simp
   126 
   127 lemma int_pow_0: "(a::int)^0 = 1"
   128   by simp
   129 
   130 lemma int_pow_1: "(a::int)^(Numeral1) = a"
   131   by simp
   132 
   133 lemma one_eq_Numeral1_nring: "(1::'a::numeral) = Numeral1"
   134   by simp
   135 
   136 lemma one_eq_Numeral1_nat: "(1::nat) = Numeral1"
   137   by simp
   138 
   139 lemma zpower_Pls: "(z::int)^0 = Numeral1"
   140   by simp
   141 
   142 lemma fst_cong: "a=a' \<Longrightarrow> fst (a,b) = fst (a',b)"
   143   by simp
   144 
   145 lemma snd_cong: "b=b' \<Longrightarrow> snd (a,b) = snd (a,b')"
   146   by simp
   147 
   148 lemma lift_bool: "x \<Longrightarrow> x=True"
   149   by simp
   150 
   151 lemma nlift_bool: "~x \<Longrightarrow> x=False"
   152   by simp
   153 
   154 lemma not_false_eq_true: "(~ False) = True" by simp
   155 
   156 lemma not_true_eq_false: "(~ True) = False" by simp
   157 
   158 lemmas powerarith = nat_numeral power_numeral_even
   159   power_numeral_odd zpower_Pls
   160 
   161 definition float :: "(int \<times> int) \<Rightarrow> real" where
   162   "float = (\<lambda>(a, b). real_of_int a * 2 powr real_of_int b)"
   163 
   164 lemma float_add_l0: "float (0, e) + x = x"
   165   by (simp add: float_def)
   166 
   167 lemma float_add_r0: "x + float (0, e) = x"
   168   by (simp add: float_def)
   169 
   170 lemma float_add:
   171   "float (a1, e1) + float (a2, e2) =
   172   (if e1<=e2 then float (a1+a2*2^(nat(e2-e1)), e1) else float (a1*2^(nat (e1-e2))+a2, e2))"
   173   by (simp add: float_def algebra_simps powr_realpow[symmetric] powr_diff)
   174 
   175 lemma float_mult_l0: "float (0, e) * x = float (0, 0)"
   176   by (simp add: float_def)
   177 
   178 lemma float_mult_r0: "x * float (0, e) = float (0, 0)"
   179   by (simp add: float_def)
   180 
   181 lemma float_mult:
   182   "float (a1, e1) * float (a2, e2) = (float (a1 * a2, e1 + e2))"
   183   by (simp add: float_def powr_add)
   184 
   185 lemma float_minus:
   186   "- (float (a,b)) = float (-a, b)"
   187   by (simp add: float_def)
   188 
   189 lemma zero_le_float:
   190   "(0 <= float (a,b)) = (0 <= a)"
   191   by (simp add: float_def zero_le_mult_iff) (simp add: not_less [symmetric])
   192 
   193 lemma float_le_zero:
   194   "(float (a,b) <= 0) = (a <= 0)"
   195   by (simp add: float_def mult_le_0_iff) (simp add: not_less [symmetric])
   196 
   197 lemma float_abs:
   198   "\<bar>float (a,b)\<bar> = (if 0 <= a then (float (a,b)) else (float (-a,b)))"
   199   by (simp add: float_def abs_if mult_less_0_iff not_less)
   200 
   201 lemma float_zero:
   202   "float (0, b) = 0"
   203   by (simp add: float_def)
   204 
   205 lemma float_pprt:
   206   "pprt (float (a, b)) = (if 0 <= a then (float (a,b)) else (float (0, b)))"
   207   by (auto simp add: zero_le_float float_le_zero float_zero)
   208 
   209 lemma float_nprt:
   210   "nprt (float (a, b)) = (if 0 <= a then (float (0,b)) else (float (a, b)))"
   211   by (auto simp add: zero_le_float float_le_zero float_zero)
   212 
   213 definition lbound :: "real \<Rightarrow> real"
   214   where "lbound x = min 0 x"
   215 
   216 definition ubound :: "real \<Rightarrow> real"
   217   where "ubound x = max 0 x"
   218 
   219 lemma lbound: "lbound x \<le> x"   
   220   by (simp add: lbound_def)
   221 
   222 lemma ubound: "x \<le> ubound x"
   223   by (simp add: ubound_def)
   224 
   225 lemma pprt_lbound: "pprt (lbound x) = float (0, 0)"
   226   by (auto simp: float_def lbound_def)
   227 
   228 lemma nprt_ubound: "nprt (ubound x) = float (0, 0)"
   229   by (auto simp: float_def ubound_def)
   230 
   231 lemmas floatarith[simplified norm_0_1] = float_add float_add_l0 float_add_r0 float_mult float_mult_l0 float_mult_r0 
   232           float_minus float_abs zero_le_float float_pprt float_nprt pprt_lbound nprt_ubound
   233 
   234 (* for use with the compute oracle *)
   235 lemmas arith = arith_simps rel_simps diff_nat_numeral nat_0
   236   nat_neg_numeral powerarith floatarith not_false_eq_true not_true_eq_false
   237 
   238 ML_file "float_arith.ML"
   239 
   240 end