4

I'm beginning to learn Haskell and, while it's generally been great, some particularities of the type class system have led to a lot of frustration during numerically-focused projects. As a concrete example, if I open up ghci and check the type of addition, I get:

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

Addition is super-generic, Num is the most generic type class, etc, so everything makes sense. But if I declare some function to be addition, and then check the type of that function, the type class gets reduced to Integer!

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

So... what's going on?

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
kirnodel
  • 43
  • 3

1 Answers1

5

You have hit upon the dreaded Monomorphism restriction. You can disable it and get the generic 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

The concept here is that monomorphism restriction will make the types restricted to a single (mono) concrete piece. You can disable that using the NoMonomorphismRestriction extension.

Sibi
  • 47,472
  • 16
  • 95
  • 163