Thu, 30 Aug 2001 20:21:02 +1200
Marcin 'Qrczak' Kowalczyk wrote:
> Wed, 29 Aug 2001 16:34:52 +0200, Rijk-Jan van Haaften <email@example.com> pisze:
> > -3 ^ 2 = -9
> > In most programming languages as well as in mathematics
> > it is quite unusual to have infix operators with a higher
> > priority than prefix operators.
> In mathematics the prefix negation is handled like in Haskell:
> -3 = -9
> I agree that many languages got it wrong :-)
This is not a simple matter.
Mathematics (and some programming languages) also have postfix
operators. Suppose ! is defined as usual as postfix factorial, and * as
prefix square, then it is not obvious what the value of *5! should be.
Mathematics has some conventions about operator precedence and
associativity, but unfortunately not enough to serve as a model for
programming languages which allow the definition of new operators.
There seem to be two main classes of languages which allow prefix
operators. Languages like C have them all at higher precedence than any
infix operator, and right associative (in the sense that *$x is *($x),
not (*$)x). This makes good sense for languages with many prefix
operators. Suppose prefix * is bound to square, then *-5 is 25, and -*5
is -25. If instead you use the same precedence as the infix version of
the operator, *-5 is illegal, and -*5 is -25.
Haskell has only the one prefix operator. I remember there being a
discussion about its precedence, but not the reasons for the final
decision. If anyone remembers, it will be Joe Fasel. To my mind, it is
not a big deal. Neither solution is ideal, but neither is a bad as not
having unary -. Sure, that would simplify the syntax, but that is not
necessarily the goal.