News for hackers. Stuff that matters.
Re:Parse, don’t type-check by WalterBright on Thursday November 26, 2020 @08:07PM (#25224742)
> The essential message is to make illegal states unrepresentable.

That's why, in C we have:

    if (a = b)
and:

    if (a < b < c)
that cause (in some compilers) warnings. I wanted to make this an error in D. One option is to check for and disallow them after the parse, but that left:

    if ((a < b) < c)
as still an error. I wanted to allow these constructs to remain if they were parenthesized, as that would imply they were deliberate. (Yes, parenthesized expressions could be a separate grammar, but I thought that was an awful idea.)

The solution is to fix the grammar, so they wouldn't even parse. The beauty of this is one doesn't have to enable warnings, or have a separate check for them, it just won't compile. Even better, D's expression grammar is the same as C's, and it did not upset anything else in the grammar as a side effect.

Based on my experience at Boeing designing hardware, defining the problem out of existence is a great way to design robust systems.

Re:Parse, don’t type-check by andreareina on Friday November 27, 2020 @05:48AM (#25227495)
What's your take on desugaring it to

    (a < b) && (b < c)
ala Python?

Speaking hypothetically of course, since it would be a breaking change.

Re:Parse, don’t type-check by WalterBright on Friday November 27, 2020 @04:33PM (#25232768)
That was considered, but the problem is what does the user think when he sees `a < b < c` ?

1. is it intended to be the C way?

2. is it the Python way?

3. did the user assume it was (1) or did he assume it was (2) and guess wrong?

This means that such expressions are a never-ending red flag for a code review. Hence it is made an error. The user can still get the C behavior by using parentheses.

Note that if D was a Python-derived language, then selecting (2) semantics would be a no-brainer. But as a C-derived language, it has to be an error.

Re:Parse, don’t type-check by st1ck on Friday November 27, 2020 @06:40AM (#25227822)
Note that Python applies same logic to other operators, which can be pretty unintuitive:

    1 in [1,2,3] is True
evaluates to False.
Re:Parse, don’t type-check by vanni on Friday November 27, 2020 @10:02AM (#25229202)
"pretty unintuitive" is an understatement...

  In [1]: import dis
  
  In [2]: def a():
     ...:     return (1 in [1,2,3] is True)
  
  In [3]: def b():
      ..:     return ((1 in [1,2,3]) is True)
  
  In [4]: a()
  Out[4]: False
  
  In [5]: b()
  Out[5]: True
  
  In [6]: dis.dis(a)
    2           0 LOAD_CONST               1 (1)
                2 LOAD_CONST               1 (1)
                4 LOAD_CONST               2 (2)
                6 LOAD_CONST               3 (3)
                8 BUILD_LIST               3
               10 DUP_TOP
               12 ROT_THREE
               14 COMPARE_OP               6 (in)
               16 JUMP_IF_FALSE_OR_POP    24
               18 LOAD_CONST               4 (True)
               20 COMPARE_OP               8 (is)
               22 RETURN_VALUE
          >>   24 ROT_TWO
               26 POP_TOP
               28 RETURN_VALUE
  
  In [7]: dis.dis(b)
    2           0 LOAD_CONST               1 (1)
                2 LOAD_CONST               5 ((1, 2, 3))
                4 COMPARE_OP               6 (in)
                6 LOAD_CONST               4 (True)
                8 COMPARE_OP               8 (is)
               10 RETURN_VALUE
Why?!
Re:Parse, don’t type-check by vanni on Friday November 27, 2020 @11:29AM (#25230020)
The disassembly in parent comment is made with CPython 3.6.9, but nothing substantial changes on CPython 3.8.x/3.9.x or PyPy.

Maybe it is related to grammar, and not to compiler...

AST dump in CPython 3.6.9 (manually formatted):

  In [1]: import ast
  
  In [2]: print(ast.dump(ast.parse("""\
     ...: def a():
     ...:     return (1 in [1,2,3] is True)""")))
  
  Module(
      body=[
          FunctionDef(
              name='a',
              args=arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
              body=[
                  Return(
                      value=Compare(
                          left=Num(n=1),
                          ops=[In(), Is()],
                          comparators=[
                              List(elts=[Num(n=1), Num(n=2), Num(n=3)], ctx=Load()),
                              NameConstant(value=True)
                          ]
                      )
                  )
              ],
              decorator_list=[],
              returns=None
          )
      ]
  )
  
  In [3]: print(ast.dump(ast.parse("""\
     ...: def b():
     ...:     return ((1 in [1,2,3]) is True)""")))
  
  Module(
      body=[
          FunctionDef(
              name='b',
              args=arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]),
              body=[
                  Return(
                      value=Compare(
                          left=Compare(
                              left=Num(n=1),
                              ops=[In()],
                              comparators=[List(elts=[Num(n=1), Num(n=2), Num(n=3)], ctx=Load())]
                          ),
                          ops=[Is()],
                          comparators=[NameConstant(value=True)]
                      )
                  )
              ],
              decorator_list=[],
              returns=None
          )
      ]
  )
Re:Parse, don’t type-check by vanni on Friday November 27, 2020 @11:43AM (#25230174)
OK, mystery solved. In Python:

  1 in [1,2,3] is True
is evaluated as

  (1 in [1, 2, 3]) and ([1, 2, 3] is True)
similarly to

  1 < 2 < 3
:facepalm:
Re:Parse, don’t type-check by zelphirkalt on Friday November 27, 2020 @10:33AM (#25229435)
I don't see anything surprising. "is" has higher prevalenz than "in" and that's it. Or am I misreading something?
Re:Parse, don’t type-check by vanni on Friday November 27, 2020 @10:58AM (#25229687)
It's not a matter of operator precedence for two reasons:

1) "in" and "is" have same precedence, and group left to right (see https://docs.python.org/3/reference/expressions.html#operato...)

2) you'll have runtime error:

  In [1]: def c():
     ...:     return (1 in ([1,2,3] is True))
  
  In [2]: c()
  ...  
  TypeError: argument of type 'bool' is not iterable
It seems related to CPython bytecode compiler implementation, the two functions are parsed in a different way, parentheses make the compiler go on a different path... but I'd like to understand why, without diving into CPython source code :) Anyone?
Re:Parse, don’t type-check by morelisp on Friday November 27, 2020 @02:34PM (#25231884)
If this was the reason you'd get a TypeError, since there's no `bool.__contains__`.
Re:Parse, don’t type-check by lispweeniejr on Saturday November 28, 2020 @01:27PM (#25239385)
this is one place where prefix notation really shines:

    (< a b c)