6.9.1. Negative literals¶
Enable negative numeric literals.
-123 is, according to Haskell98 and Haskell 2010,
two tokens, a unary minus (
-) and the number 123, and is
negate (fromInteger 123). The language extension
NegativeLiterals causes it to be treated as a single
token and desugared as
This can be useful when the positive and negative range of a numeric
data type don’t match up. For example, in 8-bit arithmetic -128
is representable, but +128 is not. So
negate (fromInteger 128)
will elicit an unexpected integer-literal-overflow message.
Whitespace can be inserted, as in
- 123, to force interpretation
as two tokens.
In 8.12, the behavior of this extension changed, and now we require that a negative literal must not be preceded by a closing token (see
GHC Proposal #229
for the definition of a closing token). In other words, we parse
f -123 as
f (-123), but
123. Before this amendment,
x-123 to be parsed as