Unary operator: Difference between revisions
(explanation) |
(Expand; including relevant extensions and postfix operators) |
||
Line 1: | Line 1: | ||
A unary operator is an operator with one parameter. | A unary operator is an operator with one parameter. | ||
In mathematics, they can be written in prefix notation like in <math>-2</math> or in postfix notation like in <math>2!</math>. | |||
Haskell only has one built-in unary operator, namely the negation operator <hask>-</hask>. | |||
It is syntactic sugar for the <hask>Prelude.negate</hask> function and allows writing expressions like <hask>-a</hask>. | |||
Unary operators may also be defined and used by the programmer with similar rules to [[Infix operator|infix operators]]. | |||
== Negation operator == | |||
The standard parsing rules concerning the negation operator are somewhat controversial because they can lead to unexpected results. | |||
In particular, they conflict with [[Section of an infix operator|operator sections]] and disallow subtraction sectioning. | |||
For example, expressions like <hask>map (- 2)</hask> are rejected by the compiler because <hask>(- 2)</hask> is interpreted as the negated digit <hask>-2</hask> rather than the partial application of <hask>(-)</hask>. | |||
There is a workaround to this: the <hask>subtract</hask> function from <code>base</code> can be used to write the previous example as <hask>map (subtract 2)</hask>. | |||
[[GHC]] supports two language extensions that modify the behaviour of the unary minus, [https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/negative_literals.html#extension-NegativeLiterals NegativeLiterals] and [https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/lexical_negation.html LexicalNegation]. | |||
The latter effectively solves the aforementioned problem by parsing expressions like <hask>map (- 2)</hask> as expected. | |||
== Postfix operators == | |||
Standard Haskell does not support postfix operators. | |||
This is because expressions like <hask>(n !)</hask> are parsed as the partial application of an [[Infix operator|infix operator]] rather than the application of a unary operator <hask>(!)</hask>. | |||
A typical use-case would be the [https://en.wikipedia.org/wiki/Factorial factorial], which is written postfix in mathematical notation (i.e. <math>n!</math>) | |||
[[GHC]] also supports a language extension to mitigate this deficiency, [https://downloads.haskell.org/ghc/latest/docs/users_guide/exts/rebindable_syntax.html#postfix-operators PostfixOperators]. | |||
With it enabled, expressions like <hask>(n !)</hask> are parsed as <hask>((!) n)</hask>. | |||
== See also == | == See also == | ||
* GHC docs: [https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/syntax.html Syntax extensions] | |||
* Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2006-September/017941.html Discussion on unary minus] with title <hask>map (-2) [1..5]</hask> | * Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2006-September/017941.html Discussion on unary minus] with title <hask>map (-2) [1..5]</hask> | ||
* Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2007-September/031544.html Custom unary operator extension?] | * Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2007-September/031544.html Custom unary operator extension?] | ||
[[Category:Syntax]] | [[Category:Syntax]] |
Latest revision as of 20:54, 24 February 2024
A unary operator is an operator with one parameter. In mathematics, they can be written in prefix notation like in or in postfix notation like in .
Haskell only has one built-in unary operator, namely the negation operator -
.
It is syntactic sugar for the Prelude.negate
function and allows writing expressions like -a
.
Unary operators may also be defined and used by the programmer with similar rules to infix operators.
Negation operator
The standard parsing rules concerning the negation operator are somewhat controversial because they can lead to unexpected results.
In particular, they conflict with operator sections and disallow subtraction sectioning.
For example, expressions like map (- 2)
are rejected by the compiler because (- 2)
is interpreted as the negated digit -2
rather than the partial application of (-)
.
There is a workaround to this: the subtract
function from base
can be used to write the previous example as map (subtract 2)
.
GHC supports two language extensions that modify the behaviour of the unary minus, NegativeLiterals and LexicalNegation.
The latter effectively solves the aforementioned problem by parsing expressions like map (- 2)
as expected.
Postfix operators
Standard Haskell does not support postfix operators.
This is because expressions like (n !)
are parsed as the partial application of an infix operator rather than the application of a unary operator (!)
.
A typical use-case would be the factorial, which is written postfix in mathematical notation (i.e. )
GHC also supports a language extension to mitigate this deficiency, PostfixOperators.
With it enabled, expressions like (n !)
are parsed as ((!) n)
.
See also
- GHC docs: Syntax extensions
- Haskell-Cafe: Discussion on unary minus with title
map (-2) [1..5]
- Haskell-Cafe: Custom unary operator extension?