When I ask the type of the + operator it is as you would expect
Prelude> :t (+)
(+) :: Num a => a -> a -> a
When I assign the operator to a variable then the type signatures changes
Prelude> let x = (+)
Prelude> :t x
x :: Integer -> Integer -> Integer
Why would the type of an operator change when it is assigned?
This is the "dreaded monomorphism restriction". Essentially, when you define a
then, by default, Haskell tries to be smart and pick a less-than-entirely general type for it. The reasons were originally to make Haskell easier to use (without it it's perhaps easier to write programs with ambiguous types), but more recently it seems to just trip everyone up since it's very unexpected behavior.
The resolution?
Provide a type annotation like
let { x :: Num a => a -> a -> a; x = (+) }
In normal Haskell code, method (3) is most highly recommended. When using GHCi, (1) and (2) are more convenient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With