usethesource / rascal

The implementation of the Rascal meta-programming language (including interpreter, type checker, parser generator, compiler and JVM based run-time system)
http://www.rascal-mpl.org
Other
405 stars 78 forks source link

Parser rejects dot notation in nested patterns in comprehensions #1731

Open sungshik opened 1 year ago

sungshik commented 1 year ago

Describe the bug

Parser rejects dot notation in nested patterns in comprehensions.

To Reproduce

Setup:

rascal>alias Pair = tuple[int x, int y];
ok

rascal>pairs = [<1,1>, <2,2>, <3,3>];
lrel[int,int]: [
  <1,1>,
  <2,2>,
  <3,3>
]

Ok:

rascal>[p.x * p.y | Pair p <- pairs];
list[int]: [1,4,9]

rascal>[p.x * p.y | Pair p <- pairs, [*_, p, *_] := pairs];
list[int]: [1,4,9]

rascal>[p.x * p.y | Pair p <- pairs, x := p.x, y := p.y, [*_, <x, y>, *_] := pairs];
list[int]: [1,4,9]

Not ok:

rascal>[p.x * p.y | Pair p <- pairs, [*_, <p.x, p.y>, *_] := pairs];
                                                           ^ Parse error here
jurgenvinju commented 1 year ago

Yes; the pattern notation does not have the . or any other expression notation other than variable names and constructor notations. So no projections, closures, etc.

The proposal is to add a new syntax for equality checks in patterns that is not the implicit variable name anymore, so:

jurgenvinju commented 1 year ago

@PaulKlint would be cool to work on this sometime soon; if not as a welcome diversion.

sungshik commented 1 year ago

Ah, ok, I see, thanks for clarifying!

Aside: My actual use case was to use patterns in the LHS of in. This doesn't work:

rascal><3, _> in toSet(pairs);
|prompt:///|(4,1,<1,4>,<1,5>): Undeclared variable: _

This does work:

rascal>{<3, _>, *_} := toSet(pairs);
bool: true

But I suppose that's a different enhancement ;)

jurgenvinju commented 1 year ago

Yes that's another different enhancement, but also cool :) Thanks Sung. Keep em coming!

DavyLandman commented 1 year ago

Aside: My actual use case was to use patterns in the LHS of in. This doesn't work:

rascal><3, _> in toSet(pairs);
|prompt:///|(4,1,<1,4>,<1,5>): Undeclared variable: _

btw, since any generator just generates a stream of booleans, you can write:

rascal><3, _> <- toSet(pairs)
bool: true

I sometimes write like this:

if (<3, _> <- pairs) {
 // do x
}
sungshik commented 1 year ago

Ah, <- even looks a bit like with some imagination ;) Thanks!

DavyLandman commented 1 year ago

True, just a warning, if you use this in a comprehension (or reducer/for) it will repeat for all matching entries. So be careful how you use this pattern ;)

I think, i would write it as: 3 in pairs<0> if I was in a comprehension/loop.

On Tue, 6 Dec 2022, 16:08 sungshik, @.***> wrote:

Ah, <- even looks a bit like ∈ with some imagination ;) Thanks!

— Reply to this email directly, view it on GitHub https://github.com/usethesource/rascal/issues/1731#issuecomment-1339524994, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABL3E3RWV5TLKKGS7ZNRKTWL5JGHANCNFSM6AAAAAASVOGM6M . You are receiving this because you commented.Message ID: @.***>