unary minus

Christian Maeder maeder@tzi.de
Fri, 05 Sep 2003 13:59:12 +0200


I wrote:
 > I wonder why Haskell only allows the unary minus on the left side of 
an expression ("lexp" in the grammar).


The unary minus may also occur on the right hand side (rule: "exp -> lexp").

So "-1 == -1" is correct, because "==" has lower precedence than "-".

Also "-1*2" is correct although it is parsed as "-(1*2)" because "*" has 
higher precedence.

 > There should be no problem to uniquely recognize an unary minus right 
beside an operation symbol ("qop"). Does the grammar allow two 
consecutive qops in other cases?
 >
 > This would allow "1 * - 1" to be correct (which I think is sort of 
"standard"), while "1 - 1" clearly is the infix minus.


Hugs accepts "1 * - 1", "1+ -1" "1- -1", whereas ghc rejects these as 
"precedence parsing error"!

I think, ghc's behaviour corresponds to the grammar of the report, but 
hugs behaves more like I would expect.

Christian