[Haskell-cafe] powering of new types

Jochem Berndsen jochem at functor.nl
Tue Dec 22 13:13:33 EST 2009

slemi wrote:
> oh well thats pretty straight-forward:)
> the next thing i don't understand is how ghci turns 1 into (Scalar 1).
> 1 == (Scalar 1) returns True, which is logical in a way (NOT), but if i
> change the type definition to
> data Matrix a = Matr {unMatr :: [[a]]} | Lol a | Scalar a
> then
> 1 == (Scalar 1) still returns True, but
> 1 == (Lol 1) returns False, no matter in what order I put them in the
> definition... o.O

Numeric literals in Haskell are overloaded. 1 really means
fromInteger 1. The function fromInteger is defined by instances of Num.
This is why you can use a numeric literal wherever your program expects
a Double, Int, Integer, and so on. This includes your own type. You have
probably defined fromInteger = Scalar for your type.

Cheers, Jochem

Jochem Berndsen | jochem at functor.nl | jochem@牛在田里.com

More information about the Haskell-Cafe mailing list