I have defined the following type in myfile.hs:
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Typeable
import Data.Fixed
data E18 = E18 deriving (Typeable)
instance HasResolution E18 where resolution _ = 10^18
type MyDouble = Fixed E18
--myRound :: MyDouble > MyDouble
--myRound x = round x
and whereas the round function works well in ghci :
Prelude Data.Fixed> :load myfile.hs
Prelude Data.Fixed> round (3.1::MyDouble)
3
Prelude Data.Fixed>
if I append the following code to the file :
myRound :: MyDouble -> MyDouble
myRound x = round x
I get the following compilation error :
Prelude Data.Fixed> :load myfile.hs
No instance for (Integral MyDouble)
arising from a use of round
Could someone please tell me how to call round inside a function ?
PS: As you may guess, I'm beginner in Haskell
round takes one RealFrac and produces a Integral:
round :: (Integral b, RealFrac a) => a -> b
Your myRound should have about the same type as round: take one MyDouble and produce an Int. So instead of MyDouble -> Int -> MyDouble, it should be MyDouble -> Int, since (I guess) what you need in the end is an Int.
Also note instead of writing
myRound x = round x
you can just say
myRound = round
If you want myRound produce you a MyDouble, then
myRound :: MyDouble -> MyDouble
myRound = fromIntegral . round
should work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With