summaryrefslogtreecommitdiff
path: root/docs/users_guide/exts/negative_literals.rst
blob: 237fabf0444291c70cfc493588d0aaf8d39fb0ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.. _negative-literals:

Negative literals
-----------------

.. extension:: NegativeLiterals
    :shortdesc: Enable support for negative literals.

    :since: 7.8.1

    Enable negative numeric literals.

The literal ``-123`` is, according to Haskell98 and Haskell 2010,
two tokens, a unary minus (``-``) and the number 123, and is
desugared as ``negate (fromInteger 123)``. The language extension
:extension:`NegativeLiterals` causes it to be treated as a single
token and desugared as ``fromInteger (-123)``.

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.

One pitfall is that with :extension:`NegativeLiterals`, ``x-1`` will
be parsed as ``x`` applied to the argument ``-1``, which is usually
not what you want.  ``x - 1`` or even ``x- 1`` can be used instead
for subtraction. To avoid this, consider using :extension:`LexicalNegation`
instead.