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

ADT destructuring #1812

Merged
merged 10 commits into from
Feb 9, 2024
Merged

ADT destructuring #1812

merged 10 commits into from
Feb 9, 2024

Conversation

yannham
Copy link
Member

@yannham yannham commented Feb 8, 2024

Depends on #1799

This PR adds support for ADTs (enum variants) in patterns. Patterns can currently only be used in destructuring, which isn't very useful for ADTs: you have to know the tag in advance to be able to actually destructure them. However, it's a first way to inspect enum variants, and a stepping stone toward full pattern matching as we will use the same notion (and the same Rust type in practice) for patterns in a match expression, and common implementations for type elaboration as well as the extraction code.

Content

  • introduces three new primops to be able to desugar the new enum variant pattern (%enum_unwrap_variant%, %enum_is_variant%, %enum_get_tag%) as well as a new internal contract $enum_variant
  • adds a pattern type for enum variants
  • implements type elaboration for this new pattern
  • implements desugaring of this new pattern to normal (non-destructuring) let bindings
  • add tests

Follow-ups

  • Add a pattern for enum tags (enum variants without argument). The reason it's not been done is that currently enum tags are probably better described as constant of the language, and that we can add constants all at once, to be able to match on numers, boolean and strings as well (it might become false if we introduce the Unit type and the () value)

@github-actions github-actions bot temporarily deployed to pull request February 8, 2024 18:45 Inactive
@github-actions github-actions bot temporarily deployed to pull request February 9, 2024 10:55 Inactive

Pattern { data: PatternData::Any(<>), alias: None, span }
},
#[inline]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason we need the inline pragma to get LALRPOP to accept the grammar as non-ambiguous (it was suggested by LALRPOP error message itself).

@github-actions github-actions bot temporarily deployed to pull request February 9, 2024 11:13 Inactive
@github-actions github-actions bot temporarily deployed to pull request February 9, 2024 11:52 Inactive
@yannham yannham marked this pull request as ready for review February 9, 2024 12:11
@yannham yannham requested review from jneem and vkleen February 9, 2024 12:11
Copy link
Contributor

@vkleen vkleen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're introducing a lot of differences between enum tags and aenum variants, and it makes me wonder if we shouldn't either allow something like [| 'Foo,, 'Foo Number |] or really go with the unit type route were discussing. But I think that's better addressed in another iteration on the idea.

Base automatically changed from refactor/pattern-matching to master February 9, 2024 14:52
Copy link

dpulls bot commented Feb 9, 2024

🎉 All dependencies have been resolved !

@yannham
Copy link
Member Author

yannham commented Feb 9, 2024

and it makes me wonder if we shouldn't either allow something like [| 'Foo,, 'Foo Number |] or really go with the unit type route were discussing. But I think that's better addressed in another iteration on the idea.

This is actually a very good point. We can go both routes (make them different, or make 'Foo x a supercase of 'Foo), but we should be consistent. If both constructs are really distinct, then they shouldn't conflict in a row type - they're almost like different dimensions, because you could match on 'Foo and 'Foo x without ambiguity.

Add a primitive operation to extract the argument from an enum variant,
which will prove useful to enum matching and destructuring.
This commit adds support for enum variant patterns in destructuring
forms (let bindings and function declarations). This paves the way for
matching on ADTs once we allow patterns not only appear in a
destructuring binding, but also in a match expression.
A test revelead that the typechecking errors were off sometimes for
destructuring lets: if the destructured value didn't correspond to the
pattern, the type elaborated from the pattern appeared as the inferred
type of the expression, and the inferred type of the expression as the
expected type (i.e. the two were reversed).

This causes incorrect error messages like "extra row" instead of
"missing row". This commits fix the issue by unifying the type deduced
from the pattern and the type inferred from the bound expression in the
right order (unification is symmetric, as far as typechecking is
concerned, but for error reporting we do distinguish between expected
and inferred type).
@github-actions github-actions bot temporarily deployed to pull request February 9, 2024 16:34 Inactive
@yannham yannham added this pull request to the merge queue Feb 9, 2024
Merged via the queue into master with commit b01f9b8 Feb 9, 2024
5 checks passed
@yannham yannham deleted the feat/adt-destructuring branch February 9, 2024 17:47
@yannham yannham mentioned this pull request Feb 9, 2024
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