src/HOL/Library/Code_Real_Approx_By_Float.thy
changeset 63355 7b23053be254
parent 61424 c3658c18b7bc
child 66147 9225370db5f0
equal deleted inserted replaced
63354:6038ba2687cf 63355:7b23053be254
     1 (* Authors: Florian Haftmann, Johannes Hölzl, Tobias Nipkow *)
     1 (*  Title:      HOL/Library/Code_Real_Approx_By_Float.thy
       
     2     Author:     Florian Haftmann
       
     3     Author:     Johannes Hölzl
       
     4     Author:     Tobias Nipkow
       
     5 *)
     2 
     6 
     3 theory Code_Real_Approx_By_Float
     7 theory Code_Real_Approx_By_Float
     4 imports Complex_Main Code_Target_Int
     8 imports Complex_Main Code_Target_Int
     5 begin
     9 begin
     6 
    10 
     7 text\<open>\textbf{WARNING} This theory implements mathematical reals by machine
    11 text\<open>
     8 reals (floats). This is inconsistent. See the proof of False at the end of
    12   \<^bold>\<open>WARNING!\<close> This theory implements mathematical reals by machine reals
     9 the theory, where an equality on mathematical reals is (incorrectly)
    13   (floats). This is inconsistent. See the proof of False at the end of the
    10 disproved by mapping it to machine reals.
    14   theory, where an equality on mathematical reals is (incorrectly) disproved
    11 
    15   by mapping it to machine reals.
    12 The value command cannot display real results yet.
    16 
    13 
    17   The \<^theory_text>\<open>value\<close> command cannot display real results yet.
    14 The only legitimate use of this theory is as a tool for code generation
    18 
    15 purposes.\<close>
    19   The only legitimate use of this theory is as a tool for code generation
       
    20   purposes.
       
    21 \<close>
    16 
    22 
    17 code_printing
    23 code_printing
    18   type_constructor real \<rightharpoonup>
    24   type_constructor real \<rightharpoonup>
    19     (SML) "real"
    25     (SML) "real"
    20     and (OCaml) "float"
    26     and (OCaml) "float"
    86 declare sqrt_def[code del]
    92 declare sqrt_def[code del]
    87 
    93 
    88 context
    94 context
    89 begin
    95 begin
    90 
    96 
    91 qualified definition real_exp :: "real \<Rightarrow> real" where "real_exp = exp"
    97 qualified definition real_exp :: "real \<Rightarrow> real"
    92 
    98   where "real_exp = exp"
    93 lemma exp_eq_real_exp[code_unfold]: "exp = real_exp"
    99 
       
   100 lemma exp_eq_real_exp [code_unfold]: "exp = real_exp"
    94   unfolding real_exp_def ..
   101   unfolding real_exp_def ..
    95 
   102 
    96 end
   103 end
    97 
   104 
    98 code_printing
   105 code_printing
   142   constant arcsin \<rightharpoonup>
   149   constant arcsin \<rightharpoonup>
   143     (SML) "Math.asin"
   150     (SML) "Math.asin"
   144     and (OCaml) "Pervasives.asin"
   151     and (OCaml) "Pervasives.asin"
   145 declare arcsin_def[code del]
   152 declare arcsin_def[code del]
   146 
   153 
   147 definition real_of_integer :: "integer \<Rightarrow> real" where
   154 definition real_of_integer :: "integer \<Rightarrow> real"
   148   "real_of_integer = of_int \<circ> int_of_integer"
   155   where "real_of_integer = of_int \<circ> int_of_integer"
   149 
   156 
   150 code_printing
   157 code_printing
   151   constant real_of_integer \<rightharpoonup>
   158   constant real_of_integer \<rightharpoonup>
   152     (SML) "Real.fromInt"
   159     (SML) "Real.fromInt"
   153     and (OCaml) "Pervasives.float (Big'_int.int'_of'_big'_int (_))"
   160     and (OCaml) "Pervasives.float (Big'_int.int'_of'_big'_int (_))"
   154 
   161 
   155 context
   162 context
   156 begin
   163 begin
   157 
   164 
   158 qualified definition real_of_int :: "int \<Rightarrow> real" where
   165 qualified definition real_of_int :: "int \<Rightarrow> real"
   159   [code_abbrev]: "real_of_int = of_int"
   166   where [code_abbrev]: "real_of_int = of_int"
   160 
   167 
   161 lemma [code]:
   168 lemma [code]: "real_of_int = real_of_integer \<circ> integer_of_int"
   162   "real_of_int = real_of_integer \<circ> integer_of_int"
       
   163   by (simp add: fun_eq_iff real_of_integer_def real_of_int_def)
   169   by (simp add: fun_eq_iff real_of_integer_def real_of_int_def)
   164 
   170 
   165 lemma [code_unfold del]:
   171 lemma [code_unfold del]: "0 \<equiv> (of_rat 0 :: real)"
   166   "0 \<equiv> (of_rat 0 :: real)"
   172   by simp
   167   by simp
   173 
   168 
   174 lemma [code_unfold del]: "1 \<equiv> (of_rat 1 :: real)"
   169 lemma [code_unfold del]:
   175   by simp
   170   "1 \<equiv> (of_rat 1 :: real)"
   176 
   171   by simp
   177 lemma [code_unfold del]: "numeral k \<equiv> (of_rat (numeral k) :: real)"
   172 
   178   by simp
   173 lemma [code_unfold del]:
   179 
   174   "numeral k \<equiv> (of_rat (numeral k) :: real)"
   180 lemma [code_unfold del]: "- numeral k \<equiv> (of_rat (- numeral k) :: real)"
   175   by simp
       
   176 
       
   177 lemma [code_unfold del]:
       
   178   "- numeral k \<equiv> (of_rat (- numeral k) :: real)"
       
   179   by simp
   181   by simp
   180 
   182 
   181 end
   183 end
   182 
   184 
   183 code_printing
   185 code_printing
   184   constant Ratreal \<rightharpoonup> (SML)
   186   constant Ratreal \<rightharpoonup> (SML)
   185 
   187 
   186 definition Realfract :: "int => int => real"
   188 definition Realfract :: "int \<Rightarrow> int \<Rightarrow> real"
   187 where
   189   where "Realfract p q = of_int p / of_int q"
   188   "Realfract p q = of_int p / of_int q"
       
   189 
   190 
   190 code_datatype Realfract
   191 code_datatype Realfract
   191 
   192 
   192 code_printing
   193 code_printing
   193   constant Realfract \<rightharpoonup> (SML) "Real.fromInt _/ '// Real.fromInt _"
   194   constant Realfract \<rightharpoonup> (SML) "Real.fromInt _/ '// Real.fromInt _"
   194 
   195 
   195 lemma [code]:
   196 lemma [code]: "Ratreal r = case_prod Realfract (quotient_of r)"
   196   "Ratreal r = case_prod Realfract (quotient_of r)"
       
   197   by (cases r) (simp add: Realfract_def quotient_of_Fract of_rat_rat)
   197   by (cases r) (simp add: Realfract_def quotient_of_Fract of_rat_rat)
   198 
   198 
   199 lemma [code, code del]:
   199 lemma [code, code del]: "(HOL.equal :: real \<Rightarrow> real \<Rightarrow> bool) = (HOL.equal :: real \<Rightarrow> real \<Rightarrow> bool)"
   200   "(HOL.equal :: real=>real=>bool) = (HOL.equal :: real => real => bool) "
   200   ..
   201   ..
   201 
   202 
   202 lemma [code, code del]: "(plus :: real \<Rightarrow> real \<Rightarrow> real) = plus"
   203 lemma [code, code del]:
   203   ..
   204   "(plus :: real => real => real) = plus"
   204 
   205   ..
   205 lemma [code, code del]: "(uminus :: real \<Rightarrow> real) = uminus"
   206 
   206   ..
   207 lemma [code, code del]:
   207 
   208   "(uminus :: real => real) = uminus"
   208 lemma [code, code del]: "(minus :: real \<Rightarrow> real \<Rightarrow> real) = minus"
   209   ..
   209   ..
   210 
   210 
   211 lemma [code, code del]:
   211 lemma [code, code del]: "(times :: real \<Rightarrow> real \<Rightarrow> real) = times"
   212   "(minus :: real => real => real) = minus"
   212   ..
   213   ..
   213 
   214 
   214 lemma [code, code del]: "(divide :: real \<Rightarrow> real \<Rightarrow> real) = divide"
   215 lemma [code, code del]:
   215   ..
   216   "(times :: real => real => real) = times"
   216 
   217   ..
   217 lemma [code]: "inverse r = 1 / r" for r :: real
   218 
       
   219 lemma [code, code del]:
       
   220   "(divide :: real => real => real) = divide"
       
   221   ..
       
   222 
       
   223 lemma [code]:
       
   224   fixes r :: real
       
   225   shows "inverse r = 1 / r"
       
   226   by (fact inverse_eq_divide)
   218   by (fact inverse_eq_divide)
   227 
   219 
   228 notepad
   220 notepad
   229 begin
   221 begin
   230   have "cos (pi/2) = 0" by (rule cos_pi_half)
   222   have "cos (pi/2) = 0" by (rule cos_pi_half)
   231   moreover have "cos (pi/2) \<noteq> 0" by eval
   223   moreover have "cos (pi/2) \<noteq> 0" by eval
   232   ultimately have "False" by blast
   224   ultimately have False by blast
   233 end
   225 end
   234 
   226 
   235 end
   227 end
   236