Simon Peyton-Jones | 8 Feb 2010 17:18
Picon
Favicon
Gravatar

Negation

Folks

Which of these definitions are correct Haskell?

  x1 = 4 + -5
  x2 = -4 + 5
  x3 = 4 - -5
  x4 = -4 - 5
  x5 = 4 * -5
  x6 = -4 * 5

Ghc accepts x2, x4, x6 and rejects the others with a message like
Foo.hs:4:7:
    Precedence parsing error
        cannot mix `+' [infixl 6] and prefix `-' [infixl 6] in the same infix expression

Hugs accepts them all.

I believe that the language specifies that all should be rejected.  http://haskell.org/onlinereport/syntax-iso.html

I think that Hugs is right here.  After all, there is no ambiguity in any of these expressions.  And an
application-domain user found this behaviour very surprising.

I'm inclined to start a Haskell Prime ticket to fix this language definition bug.  But first, can anyone
think of a reason *not* to allow all the above?

Simon
Dan Doel | 8 Feb 2010 18:00
Picon
Gravatar

Re: Negation

On Monday 08 February 2010 11:18:07 am Simon Peyton-Jones wrote:
> I think that Hugs is right here.  After all, there is no ambiguity in any
>  of these expressions.  And an application-domain user found this behaviour
>  very surprising.

I think it's clear what one would expect the result of these expressions to 
be, but there is some ambiguity considering how GHC parses other similar 
expressions (I don't actually know if it's following the report in doing so or 
not). For instance:

  -4 `mod` 5

parses as:

  -(4 `mod` 5)

which I've seen confuse many a person (and it confused me the first time I saw 
it; it makes divMod and quotRem appear identical if one is testing them by 
hand as above, and doesn't know about the weird parsing). Knowing the above, I 
wasn't entirely sure what the results of x2 and x4 would be.

Of course, I think the `mod` parsing is the weird bit, and it'd be good if it 
could be amended such that

  -a `mod` -b = (-a) `mod` (-b)

like the rest of the proposal.

-- Dan
(Continue reading)

Ross Paterson | 8 Feb 2010 17:59
Picon
Favicon

Re: Negation

On Mon, Feb 08, 2010 at 04:18:07PM +0000, Simon Peyton-Jones wrote:
> Which of these definitions are correct Haskell?
> 
>   x1 = 4 + -5
>   x2 = -4 + 5
>   x3 = 4 - -5
>   x4 = -4 - 5
>   x5 = 4 * -5
>   x6 = -4 * 5
> 
> Ghc accepts x2, x4, x6 and rejects the others with a message like
> Foo.hs:4:7:
>     Precedence parsing error
>         cannot mix `+' [infixl 6] and prefix `-' [infixl 6] in the same infix expression
> 
> Hugs accepts them all.
> 
> I believe that the language specifies that all should be rejected.  http://haskell.org/onlinereport/syntax-iso.html

I think GHC conforms to the Report; here is the relevant part of the grammar:

exp6    ->      exp7
        |       lexp6
lexp6   ->      (lexp6 | exp7) + exp7
        |       (lexp6 | exp7) - exp7
        |       - exp7

exp7    ->      exp8
        |       lexp7
lexp7   ->      (lexp7 | exp8) * exp8
(Continue reading)

Ian Lynagh | 8 Feb 2010 18:20
Picon
Gravatar

Re: Negation

On Mon, Feb 08, 2010 at 04:59:59PM +0000, Ross Paterson wrote:
> 
> But I agree they should all be legal, i.e. that unary minus should bind
> more tightly than any infix operator (as in C).

See also
    http://hackage.haskell.org/trac/haskell-prime/wiki/NegativeSyntax

Thanks
Ian
Sjur Gjøstein Karevoll | 8 Feb 2010 18:54
Picon

Re: Negation

Måndag 8. februar 2010 17.59.59 skreiv Ross Paterson:
> But I agree they should all be legal, i.e. that unary minus should bind
> more tightly than any infix operator (as in C).

I second this, at least in general.

However, one issue is function application. Should unary minus bind tighter 
than it or not and are there special cases (spaces)? Consider:

1) foo-1
2) foo -1
3) foo - 1

If unary minus binds tighter than application then we get `foo (-1)` in all 
cases. The other way around we get `(foo) - (1)` in all cases. To me the most 
natural parsing would be

1) (foo) - (1)
2) foo (-1)
3) (foo) - (1)

Then there's also the issue of literals:

4) 1-1
5) 1 -1
6) 1 - 1

To me, all of these should parse as `(1) - (1)`. I'm a fan of treating 
literals and variables the same though (referential transparancy even in 
parsing), and that makes this problematic.
(Continue reading)

Lennart Augustsson | 8 Feb 2010 19:30
Favicon
Gravatar

Re: Negation

Of course unary minus should bind tighter than any infix operator.
I remember suggesting this when the language was designed, but the
Haskell committee was very set against it (mostly Joe Fasel I think).

I think it's too late to change that now, it could really introduce
some subtle bugs with no parse or type errors.

On Mon, Feb 8, 2010 at 5:59 PM, Ross Paterson <ross@...> wrote:
> On Mon, Feb 08, 2010 at 04:18:07PM +0000, Simon Peyton-Jones wrote:
>> Which of these definitions are correct Haskell?
>>
>>   x1 = 4 + -5
>>   x2 = -4 + 5
>>   x3 = 4 - -5
>>   x4 = -4 - 5
>>   x5 = 4 * -5
>>   x6 = -4 * 5
>>
>> Ghc accepts x2, x4, x6 and rejects the others with a message like
>> Foo.hs:4:7:
>>     Precedence parsing error
>>         cannot mix `+' [infixl 6] and prefix `-' [infixl 6] in the same infix expression
>>
>> Hugs accepts them all.
>>
>> I believe that the language specifies that all should be rejected.  http://haskell.org/onlinereport/syntax-iso.html
>
> I think GHC conforms to the Report; here is the relevant part of the grammar:
>
> exp6    ->      exp7
(Continue reading)

jur | 8 Feb 2010 20:04
Picon

Re: Negation


On Feb 8, 2010, at 5:18 PM, Simon Peyton-Jones wrote:

> Folks
>
> Which of these definitions are correct Haskell?
>
>  x1 = 4 + -5
>  x2 = -4 + 5
>  x3 = 4 - -5
>  x4 = -4 - 5
>  x5 = 4 * -5
>  x6 = -4 * 5
>
> Ghc accepts x2, x4, x6 and rejects the others with a message like
> Foo.hs:4:7:
>    Precedence parsing error
>        cannot mix `+' [infixl 6] and prefix `-' [infixl 6] in the  
> same infix expression
>
> Hugs accepts them all.
Helium accepts them all as well, and also delivers the, for me,  
expected results.

>
> I believe that the language specifies that all should be rejected.  http://haskell.org/onlinereport/syntax-iso.html
>
>
> I think that Hugs is right here.  After all, there is no ambiguity  
> in any of these expressions.  And an application-domain user found  
(Continue reading)

John Meacham | 8 Feb 2010 22:24
Favicon

Re: Negation

On Mon, Feb 08, 2010 at 04:18:07PM +0000, Simon Peyton-Jones wrote:
> Which of these definitions are correct Haskell?
> 
>   x1 = 4 + -5
>   x2 = -4 + 5
>   x3 = 4 - -5
>   x4 = -4 - 5
>   x5 = 4 * -5
>   x6 = -4 * 5
> 
> Ghc accepts x2, x4, x6 and rejects the others with a message like
> Foo.hs:4:7:
>     Precedence parsing error
>         cannot mix `+' [infixl 6] and prefix `-' [infixl 6] in the same infix expression
> 
> Hugs accepts them all.
> 
> I believe that the language specifies that all should be rejected.  http://haskell.org/onlinereport/syntax-iso.html
> 
> 
> I think that Hugs is right here.  After all, there is no ambiguity in any of these expressions.  And an
application-domain user found this behaviour very surprising.
> 
> I'm inclined to start a Haskell Prime ticket to fix this language definition bug.  But first, can anyone
think of a reason *not* to allow all the above?

What would be the actual change proposed? If it is something concrete
and not something like "negatives should be interpreted as unary minus
when otherwise it would lead to a parse error" then that wouldn't be
good. I have enough issues with the layout rule as is :)
(Continue reading)

Ross Paterson | 9 Feb 2010 00:04
Picon
Favicon

Re: Negation

On Mon, Feb 08, 2010 at 01:24:55PM -0800, John Meacham wrote:
> What would be the actual change proposed? If it is something concrete
> and not something like "negatives should be interpreted as unary minus
> when otherwise it would lead to a parse error" then that wouldn't be
> good. I have enough issues with the layout rule as is :)

I imagine it would be something like deleting the production

    lexp6    ->      - exp7

and adding the production

    exp10    ->      - fexp
Simon Peyton-Jones | 9 Feb 2010 08:30
Picon
Favicon
Gravatar

RE: Negation

| Of course unary minus should bind tighter than any infix operator.
| I remember suggesting this when the language was designed, but the
| Haskell committee was very set against it (mostly Joe Fasel I think).
| 
| I think it's too late to change that now, it could really introduce
| some subtle bugs with no parse or type errors.

I'm not sure it's too late to change.   That's what Haskell Prime is for.  The change would have a flag of course,
and could emit a warning if the old and new would give different results.

I think I'll create a ticket at least.

| I imagine it would be something like deleting the production
| 
|     lexp6    ->      - exp7
| 
| and adding the production
| 
|     exp10    ->      - fexp

Yes, exactly

Simon

Gmane