header{*Code Generation for rational numbers in Haskell*}
theory Code_Rational
imports
Rat
"~~/src/HOL/Library/Code_Target_Int"
begin
subsection{*Serializations*}
text{*The following @{text "code_include"} module is the usual way to import libraries in Haskell. In this case, we
rebind some functions from Data.Ratio. See \url{https://lists.cam.ac.uk/pipermail/cl-isabelle-users/2013-June/msg00007.html}*}
code_include Haskell Rational {*
import qualified Data.Ratio;
fract a b = a Data.Ratio.% b;
numerator a = Data.Ratio.numerator a;
denominator a = Data.Ratio.denominator a;
*}
text{*@{term "Frct"} has been serialized to a function in Haskell that makes use of @{term "integer_of_int"}.
The problem is that @{text "integer_of_int"} is an Isabelle's function that it is not always automatically generated to Haskell.
Then, sometimes we will have to add it when we will export code. See the example at the end of this file.*}
code_printing
type_constructor rat \<rightharpoonup> (Haskell) "Prelude.Rational"
| class_instance rat :: "HOL.equal" => (Haskell) -
| constant "0 :: rat" \<rightharpoonup> (Haskell) "(Prelude.toRational (0::Integer))"
| constant "1 :: rat" \<rightharpoonup> (Haskell) "(Prelude.toRational (1::Integer))"
| constant "Frct" \<rightharpoonup> (Haskell) "(let (x,y) = _ in (Rational.fract (integer'_of'_int x) (integer'_of'_int y)))"
| constant "quotient_of" \<rightharpoonup> (Haskell) "(let x = _ in (Int'_of'_integer (Rational.numerator x), Int'_of'_integer (Rational.denominator x)))"
| constant "HOL.equal :: rat => rat => bool" \<rightharpoonup>
(Haskell) "(_) == (_)"
| constant "op < :: rat => rat => bool" \<rightharpoonup>
(Haskell) "_ < _"
| constant "op ≤ :: rat => rat => bool" \<rightharpoonup>
(Haskell) "_ <= _"
| constant "op + :: rat => rat => rat" \<rightharpoonup>
(Haskell) "(_) + (_)"
| constant "op - :: rat => rat => rat" \<rightharpoonup>
(Haskell) "(_) - (_)"
| constant "op * :: rat => rat => rat" \<rightharpoonup>
(Haskell) "(_) * (_)"
| constant "op / :: rat => rat => rat" \<rightharpoonup>
(Haskell) " (_) '/ (_)"
| constant "uminus :: rat => rat" \<rightharpoonup>
(Haskell) "Prelude.negate"
end