Haskell Type Class Deficiency

I am starting to learn Haskell, and although it was great, some features of a class-like system led to great disappointment during projects with an entire number of focused tasks. As a concrete example, if I open ghci and check the type of addition, I get:

Prelude> :t (+)
(+) :: Num a => a -> a -> a

A complement is super-generic, Numis the most general type of class, etc., so everything makes sense. But if I declare some function as a complement, and then check the type of this function, the type class reduces to Integer!

Prelude> let add = (+)
Prelude> :t add
add :: Integer -> Integer -> Integer

So ... what's going on?

+4
source share
1 answer

You have fallen into a terrible restriction of monomorphism. You can disable it and get a general function.

Prelude> let x = (+)
Prelude> :t x
x :: Integer -> Integer -> Integer
Prelude> :set -XNoMonomorphismRestriction
Prelude> let y = (+)
Prelude> :t y
y :: Num a => a -> a -> a

, () . NoMonomorphismRestriction.

+5

All Articles