Type signature num in double?

I am just starting Learn You a Haskell for Great Good, and I have problems with class types. I would like to create a function that accepts any type of number and makes it be double.

My first thought was to identify

numToDouble :: Num -> Double

But I don’t think it worked because it’s Numnot a type, it is a typeclass (which, it seems to me, is a set of types). So, looking at read, shows (Read a) => String -> a. I read this as "read takes a string and returns a thing of the type athat the user has specified." So I wrote the following

numToDouble :: (Num n) => n -> Double
numToDouble i = ((i) :: Double)

Which looks at me like “take a thing like n (should be in class Numand convert it to Double.” This seems reasonable, because I can do20::Double

This produces the following output

Could not deduce (n ~ Double)                                                                                                                                                                                                                                              
from the context (Num n)                                                                                                                                                                                                                                                   
  bound by the type signature for numToDouble :: Num n => n -> Double

I have no idea what I'm reading. Based on what I can find, it looks like this has something to do with polymorphism?

Edit:

To be clear, my question is: why does this not work?

+3
source share
4 answers

The reason you can say “20 :: Double” is because in Haskell the integer literal is of type “Num a => a”, that is, it can be any number type that you like.

You are right that typeclass is a collection of types. To be precise, this is a set of types that implement functions in the "where" clause of the typeclass class. Your type signature for your numToDouble correctly expresses what you want to do.

, "n" , , Num. +, -, *, negate, abs, signum fromInteger. , , , .

, Num. numToDouble ? , , .

Real, , , , , , float, doubleles . "toRational", , Double, "fromRational", "Fractional".

, :

toDouble :: (Real n) => n -> Double
toDouble = fromRational . toRational

, , . GHCI :

Prelude> :type fromRational . toRational
fromRational . toRational :: (Fractional c, Real a) => a -> c

, ( , , , Real, Complex). , , .

:, leftaroundabout ,

realToFrac = fromRational . toRational
+9

"" - Haskell. - .

, , . Num - 1 , , ( , , fromInteger).

, - , Double. Complex.

, , suprise, Real. , , , toRational, ... , . , :

realToDouble :: Real n => n -> Double
realToDouble i = fromRational $ toRational i

, fromRational . toRational : realToFrac, .


" " , - , . , , - : , .

+3

Haskell , . i :: Double, , "cast i to Double"; , i Double. , , , i - Num n => n, n ( ), Num; , n Integer. , .

, 1 :: Double. , Haskell , 1, , fromInteger one, one :: Integer - Integer, .

. , . , " , "; Haskell, , fromIntegral fromRational. , Haskell .

+3

100% , ,

(i) :: Double

i Double, , i Double. , .

. (, , , , .) .

, - .

, Num Integer Num. -, Integer, . , , , fromRational ...

+3

All Articles