[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Prefix Minus



Original-Via: uk.ac.nsf; Mon, 11 Mar 91 18:15:54 GMT
Date: Mon, 11 Mar 91 10:34:57 EST
From: Paul Hudak <hudak-paul@edu.yale.cs>
To: haskell@edu.yale.cs
Cc: guzman-maria@edu.yale.cs, simonpj@cs.glasgow.ac.uk
Subject: Prefix Minus
Original-Sender: hudak-paul@cs.yale.edu
Sender: haskell-request@cs.glasgow.ac.uk

Although there appeared to be a consensus that -2^2 should parse as
-(2^2), after thinking about it for awhile I'd now like to argue
against this for two reasons:

First off, it seems odd to me that things like -2+2, -2/2, etc. should
parse one way, and -2^2 parse in another.  Perhaps this is consistent
with mathematics, but quite frankly programming is not the same as
mathematics, so we should not get carried away with this principle.
It seems far more consistent to me that -2^2 should parse the same as
the others, thus evaluating to 4.  This rule is extremely simple to
understand -- prefix minus binds as strongly as prefix application.

Second, even if you don't buy the above argument, fixing the syntax to
do otherwise runs into problems.  The main point is this:  The
committee decided quite awhile ago that Haskell would have no general
prefix operators (there are good readability arguments for this which
I do not wish to get into here), and the only one we couldn't do away
with entirely was prefix minus.  Thus it was "hacked" into the syntax
in its current form.  The simple fix that Maria suggested, to change
the production "- aexp" to "- exp" has a serious problem:  We must
state a precedence for prefix minus.  The most natural way to do this
is to tie it to the precedence of infix minus -- but it seems quite
odd to tie a FIXED part of the syntax to a part that is defined in the
Standard Prelude!  (Consider, for example, that infix minus may be
redefined in a module.)  The other alternative, to give prefix minus
either highest or lowest precedence, is unacceptable because things
don't get parsed the way we want.  Another problem with this approach
is that, as Brian points out, the simple rule that says "substitute
negate for -" gets more complicated.

So, if we want to fix this "problem" it seems that the right thing to
do is add a general class of prefix operators that we can assign
precedences to in the Standard Prelude -- exactly what the committee
decided against many moons ago!

Proposal:  Leave things as they are for now, and debate the inclusion
of general prefix operators at a later time, for possible inclusion in
Haskell 2.

  -Paul