Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning "bare" numbers to newtypes

Tags:

haskell

pragma

Note the second line in this GHCi session. What is it about the Latitude type that allows me to use a "bare" number as a value, instead of having to invoke a constructor? I would like to do something similar with some of my own types.

λ> :m + Data.Geo.GPX.Type.Latitude                                                                                                   
λ> let t = 45 :: Latitude                                                                                                            
λ> t                                                                                                                                 
45.0

I've examined the source code for the Latitude type, but I had trouble figuring it out at first. Eventually I found the answer, so I thought I'd document it here. See my answer below.

like image 443
mhwombat Avatar asked Apr 17 '12 13:04

mhwombat


2 Answers

What makes this work is that the type is a Num. The easiest way to do that is to use "deriving Num", in which case I need the language pragma GeneralizedNewtypeDeriving. So I can create a type like the following,

newtype Seconds = Seconds Double deriving (Eq, Ord, Enum, Num, Fractional, Floating, Real, RealFrac, RealFloat, Show)

And then in GHCi,

λ> let s = 5 :: Seconds                                                                                                              
λ> s                                                                                                                                 
Seconds 5.0

Alternatively, I could explicitly implement Num.

like image 55
mhwombat Avatar answered Oct 25 '22 14:10

mhwombat


According to the Haskell98 standard, numeric literals are actually calls to fromInteger and fromRational. This allows them to be converted to any type that implements those functions (fromInteger is in the Prelude.Num typeclass and fromRational is in the Prelude.Fractional typeclass).

The syntax of numeric literals is given in Section 2.5. An integer literal represents the application of the function fromInteger to the appropriate value of type Integer. Similarly, a floating literal stands for an application of fromRational to a value of type Rational (that is, Ratio Integer). Given the typings:

fromInteger :: (Num a) => Integer -> a

fromRational :: (Fractional a) => Rational -> a

integer and floating literals have the typings (Num a) => a and (Fractional a) => a, respectively. Numeric literals are defined in this indirect way so that they may be interpreted as values of any appropriate numeric type. See Section 4.3.4 for a discussion of overloading ambiguity.

http://www.haskell.org/onlinereport/basic.html#numeric-literals

like image 34
hugomg Avatar answered Oct 25 '22 15:10

hugomg