Why does this Haskell expression compile?

Here are some definitions I wrote, to avoid mixing currencies

{-# LANGUAGE GeneralizedNewtypeDeriving #-}

data EUR
data USD 

newtype Amount a = Amount Double deriving (Fractional, Num, Show)

eur :: Double -> Amount EUR
eur = Amount

usd :: Double -> Amount USD
usd = Amount

Can you explain why usd 33 + 3 compiles and if it is possible to make the type-checker reject this expression.

Upvotes: 3

Views: 174

Answers (2)

sabauma
sabauma

Reputation: 4253

When GHC derives the Num class, it provides an implementation for the fromInteger function. Integer literals like 3 actually have the type Num a => a.

ghci> :t (34)
(34) :: Num a => a

When the type checker sees that you are trying to add a value of type Amount USD to 3, it determines 3 :: Amount USD, which is valid since it is a member of the Num typeclass.

Upvotes: 2

J. Abrahamson
J. Abrahamson

Reputation: 74374

Numbers in Haskell have lots of implicitness. Mentally, you ought to replace every number literal like 3 with fromInteger 3. Since Amount uses GeneralizedNewtypeDeriving to be part of the Num typeclass, it inherits a fromInteger instance. So the compiler is doing this

usd 33 + 3
===                                      [implicit fromInteger & expand usd]
(Amount 33 :: Amount USD) + 
  fromInteger 3
===                                      [fromInteger :: Num a => a -> Amount a]
(Amount 33 :: Amount USD) + 
  (Amount 3 :: Amount a)
===                                      [unify a]
(Amount 33 :: Amount USD) + 
  (Amount 3 :: Amount USD)

Upvotes: 12

Related Questions