Exponentiation operator precedence

2015-08-24 Thread Jason Orendorff
In math, -x² is -(x²), not (-x)². But as proposed for JS, -x**2 is (-x)**2.

PHP, Python, Haskell, and D side with the traditional algebraic
notation, against JS. Here's PHP:

$ php -r 'print(-2 ** 2);'
-4

Python:

 -2 ** 2
-4

Haskell:

Prelude -2 ^ 2
-4

The D grammar: http://dlang.org/grammar.html#UnaryExpression

Let's switch.

Another case to think about is `-2 ** -2`. In Haskell, that's a syntax
error, but the other three languages all treat it the same way, so
maybe JS should follow suit.

-j
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Exponentiation operator precedence

2015-08-24 Thread Waldemar Horwat

On 08/24/2015 17:24, Jason Orendorff wrote:

On Mon, Aug 24, 2015 at 5:45 PM, Waldemar Horwat walde...@google.com wrote:

Let's not.  As I said at the last meeting, making ** bind tighter than unary
operators would break x**-2.  And making it sometimes tighter and sometimes
looser would be too confusing and lead to other opportunities for precedence
inversion.


Don't you think having `-x**2` mean the same thing as `x**2` is more
confusing? It seems like it will cause problems for the exact
programmers we are trying to help with this feature.

What you're describing as sometimes tighter and sometimes looser I
would call the same precedence. It's even easier to specify than the
current proposal:

 UnaryExpression : PostfixExpression ** UnaryExpression

An expression using both `**` and unary `-` is then parsed right-associatively:

 -a ** -b ** -c ** -d
 means -(a ** (-(b ** (-(c ** (-d))


That has different right and left precedence and is probably the closest to the 
mathematical intent.  However, it does carry other surprises.  What does each 
of the following do?

++x ** y;
x++ ** y;
x ** ++y;
x ** y++;

Waldemar

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Exponentiation operator precedence

2015-08-24 Thread Jason Orendorff
On Mon, Aug 24, 2015 at 7:24 PM, Jason Orendorff
jason.orendo...@gmail.com wrote:
 What you're describing as sometimes tighter and sometimes looser I
 would call the same precedence. It's even easier to specify than the
 current proposal:

 UnaryExpression : PostfixExpression ** UnaryExpression

P.S. Admittedly it might be a good idea to rename UnaryExpression if
we put a binary operator in there.

-j
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Exponentiation operator precedence

2015-08-24 Thread Jason Orendorff
On Mon, Aug 24, 2015 at 8:10 PM, Waldemar Horwat walde...@google.com wrote:
 That has different right and left precedence and is probably the closest to
 the mathematical intent.

Not to quibble, but I do want to understand:

UnaryExpression : PostfixExpression ** UnaryExpression

AdditiveExpression : AdditiveExpression + MultiplicativeExpression

We don't say binary `+` has different left and right precedence,
right? What's different with `**`, apart from being right-associative?

  However, it does carry other surprises.  What does
 each of the following do?

Interesting:

++x ** y;  // early error: invalid operand for ++
(because it's parsed as `++(x ** y)`)
x++ ** y;  // parses as expected: (x++) ** y
x ** ++y;  // parses as expected: x ** (++y)
x ** y++;  // parses as expected: x ** (y++)

I'm OK with this. It should be rarer in practice than `-x**2`, and at
least the language won't silently assign a surprising meaning to user
code.

It could be made to parse as `(++x) ** y` by changing prefix ++ and --
to be higher precedence than the other prefix operators. I don't think
this would break any existing programs, because combinations like
`++typeof 3` are already early errors. PHP apparently does something
like this:

$ php -r '$x = 3; print(++$x**2 . \n); print($x . \n);'
16
4

D doesn't. Python and Haskell don't have ++/--.

-j
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Exponentiation operator precedence

2015-08-24 Thread Daniel Ehrenberg
On Mon, Aug 24, 2015 at 3:45 PM, Waldemar Horwat walde...@google.com wrote:
 On 08/24/2015 10:08, Jason Orendorff wrote:

 In math, -x² is -(x²), not (-x)². But as proposed for JS, -x**2 is
 (-x)**2.

 PHP, Python, Haskell, and D side with the traditional algebraic
 notation, against JS. Here's PHP:

  $ php -r 'print(-2 ** 2);'
  -4

 Python:

   -2 ** 2
  -4

 Haskell:

  Prelude -2 ^ 2
  -4

 The D grammar: http://dlang.org/grammar.html#UnaryExpression

 Let's switch.


 Let's not.  As I said at the last meeting, making ** bind tighter than unary
 operators would break x**-2.  And making it sometimes tighter and sometimes
 looser would be too confusing and lead to other opportunities for precedence
 inversion.

 Waldemar

Agreed that the precedence should not bind sometimes tighter and
sometimes looser is problematic.

I think following the way other languages solve the same problem is
more important for minimizing user surprise than any particular
expression looking especially good. Looking kinda similar to other
languages in surface syntax has been a major advantage of JS all
along.

Dan
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Exponentiation operator precedence

2015-08-24 Thread Jason Orendorff
On Mon, Aug 24, 2015 at 5:45 PM, Waldemar Horwat walde...@google.com wrote:
 Let's not.  As I said at the last meeting, making ** bind tighter than unary
 operators would break x**-2.  And making it sometimes tighter and sometimes
 looser would be too confusing and lead to other opportunities for precedence
 inversion.

Don't you think having `-x**2` mean the same thing as `x**2` is more
confusing? It seems like it will cause problems for the exact
programmers we are trying to help with this feature.

What you're describing as sometimes tighter and sometimes looser I
would call the same precedence. It's even easier to specify than the
current proposal:

UnaryExpression : PostfixExpression ** UnaryExpression

An expression using both `**` and unary `-` is then parsed right-associatively:

-a ** -b ** -c ** -d
means -(a ** (-(b ** (-(c ** (-d))

-j
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss