Difference between revisions of "Unary operator"

From HaskellWiki
Jump to navigation Jump to search
(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.
It can be written in [[prefix notation]] like in <math>-2</math> or in [[postfix notation]] <math>2!</math> ([[factorial]]).
+
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>.
   
In Haskell there is only one unary operator, namely the [[unary minus]].
 
It has been discussed in length, whether the unary minus shall be part of numeric literals or whether it shall be an independent operator.
 
In fact in Haskell it is the latter one, in order to allow expressions like <hask>-a</hask>.
 
The unary minus is [[syntactic sugar]] for the Prelude function <hask>negate</hask>.
 
The special syntax disallows the use of [[section of an infix operator|sectioning]].
 
That is, you cannot write <hask>map (-2)</hask> if you want to substract 2 from all elements of a list.
 
Instead you have to write <hask>map (subtract 2)</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?]

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