Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Or patterns #1916

Merged
merged 1 commit into from
May 25, 2024
Merged

Or patterns #1916

merged 1 commit into from
May 25, 2024

Conversation

yannham
Copy link
Member

@yannham yannham commented May 13, 2024

Depends on #1912.

This PR introduces or-patterns, which allows to express alternatives within patterns, including in a deep subpattern.

The compilation of or-patterns is rather simple: we simply try each alternative until one matches, and use the corresponding bindings.

Typechecking of or-patterns can be done following the same process as for typechecking a whole match expression (which is also a disjunction of patterns), although the treatment of bound variables is a bit different.

Most of the complexity of this PR comes from the fact that we don't want to make or a reserved language keyword, which would break backward compatibility. This is possible, because or in pattern can't be confused with an identifier, but it requires some tweaking to make our LALR(1) parser accept this.

Syntax

Beside the technicalities around making the grammar unambiguous, this PR also imposes parentheses around enum variant patterns which are part of an or-pattern. That is, it forces to write ('Foo x) or ('Bar x) instead of 'Foo x or 'Bar x, which is arguably less readable. Even less so when the identifier is or: 'Foo or or 'Bar or 😰

@github-actions github-actions bot temporarily deployed to pull request May 13, 2024 16:37 Inactive
@github-actions github-actions bot temporarily deployed to pull request May 14, 2024 09:49 Inactive
@github-actions github-actions bot temporarily deployed to pull request May 14, 2024 13:45 Inactive
@github-actions github-actions bot temporarily deployed to pull request May 15, 2024 18:14 Inactive
@github-actions github-actions bot temporarily deployed to pull request May 16, 2024 14:11 Inactive
@github-actions github-actions bot temporarily deployed to pull request May 16, 2024 15:11 Inactive
@yannham yannham marked this pull request as ready for review May 16, 2024 15:25
@yannham yannham requested review from jneem and vkleen May 16, 2024 15:25
@github-actions github-actions bot temporarily deployed to pull request May 16, 2024 15:27 Inactive
Base automatically changed from feat/array-patterns to master May 16, 2024 15:36
Copy link

dpulls bot commented May 16, 2024

🎉 All dependencies have been resolved !

@github-actions github-actions bot temporarily deployed to pull request May 16, 2024 16:24 Inactive
core/src/parser/lexer.rs Outdated Show resolved Hide resolved
core/src/term/pattern/mod.rs Outdated Show resolved Hide resolved
@github-actions github-actions bot temporarily deployed to pull request May 24, 2024 22:29 Inactive
@yannham yannham enabled auto-merge May 24, 2024 22:32
@github-actions github-actions bot temporarily deployed to pull request May 24, 2024 22:35 Inactive
@yannham yannham added this pull request to the merge queue May 24, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 24, 2024
This commit introduces or-patterns, which allows to express alternatives
within patterns, including in a deep subpattern.

The compilation of or-patterns is rather simple: we simply try each
alternative until one matches, and use the corresponding bindings.

Typechecking of or-patterns can be done following the same process as
for typechecking a whole match expression (which is also a disjunction
of patterns), although the treatment of bound variables is a bit
different.

Most of the complexity of this commit comes from the fact that we don't
want to make `or` a reserved language keyword, which would break
backward compatibility. This is possible, because `or` in pattern can't
be confused with an identifier, but it requires some tweaking to make
our LALR(1) parser accept this.
@yannham yannham enabled auto-merge May 25, 2024 12:07
@github-actions github-actions bot temporarily deployed to pull request May 25, 2024 12:09 Inactive
@yannham yannham added this pull request to the merge queue May 25, 2024
Merged via the queue into master with commit ca0f78f May 25, 2024
5 checks passed
@yannham yannham deleted the feat/or-patterns branch May 25, 2024 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants