In ghci, when I type
:t (-)
to figure out the type of (-)
, it returns
(-) :: Num a => a -> a -> a
However, when I write -1
haskell returns a number, which seems to imply that (-)
is of type Num a => a -> a
. How can (-)
seemingly have two different types?
This was a design decision in the language. -1
is a number, but its usage in this context is not related to the function (-)
. (As Ackdari mentions in their answer, this usage is related to the function negate
.) There are a couple compromises that allow this to work:
You cannot take a right slice of the (-)
operator. As a workaround, Haskell provides the subtract
function.
You cannot write a negative integer without parentheses unless it is at the beginning of an assignment (e.g. directly after =
or ->
). This produces a parse error:
let x = 8 * -1
Instead, it should be written as
let x = 8 * (-1)
However, this is fine:
let x = -1 * 8
These were considered to be reasonable tradeoffs to the designers of the language.
The answer is already descirbed in the haskell-wiki, it states
The unary minus is syntactic sugar for the Prelude function
negate
so the function (-)
is always the a - b
function and if you write code like
let x = -y
the compiler will translate it to let x = negate y
.
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