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

Fix handling of wildcard match cases #1243

Merged
merged 2 commits into from
Nov 10, 2023
Merged

Fix handling of wildcard match cases #1243

merged 2 commits into from
Nov 10, 2023

Conversation

shonfeder
Copy link
Contributor

This is a followup to #1242 and #1232.

While the preceding gave basic support for sum type constructors and
eliminators, we did not have support for properly typing "wildcard" or default
match cases.

This PR simplifies the representing and logic around such cases and extends the
constraint generator to give the correct typing for match expressions that
include a default case.

The gist:

A match expression of the form

match foo {
  | A(_:t) => bar
  | B(_:u) => baz
  | _      => qux
}

Is now typed to apply to a variant <A : t, B : u | v > where v is an free
row variable. The open row accounts for the fact that the wildcard handles the
rest of the labels, whatever they may be.

The constraint generator is also expanded with error handling to catch cases
where redundant wildcard's or match statements are included.

  • Tests added for any new code
  • Documentation added for any new functionality
  • Entries added to the respective CHANGELOG.md for any new functionality
  • Feature table on README.md updated for any listed functionality

Shon Feder added 2 commits November 10, 2023 11:24
We had been using thunks, lambdas that take no arguments, to encode the
eliminators for wildcard match cases. I.e., the match expression

```
match foo {
  | _ => bar
}
```

Was being converted into our builtin exotic thus:

```
matchVariant(foo, "_", () => bar)
```

This made sense because in the case of the wildcard match we are
discarding the variant, and there is no argument to be supplied to the
eliminator.

However:

1. We don't really support nullary lambdas (even tho we are making use
of them at several places in our code currently).
2. Since we are committed to using the exotic builtin operators, and there
is no way to specify the effect of a variadic builtin with unpredictable
arguments, we could not give this builtin the correct effect signature.

So I have changed the way wildcard cases are encoded, to also use unary
lambas that just ignore their argument. This simplifies a lot of the
match typing and evaluation, so it is the right call overall.
@shonfeder shonfeder requested review from bugarela and konnov November 10, 2023 17:56
@shonfeder
Copy link
Contributor Author

After this is in, I'll write a more extensive series of tests using the constructs, along with documentation and release notes announcing the feature.

@shonfeder shonfeder self-assigned this Nov 10, 2023
@shonfeder shonfeder added this to the Add support for sum types milestone Nov 10, 2023
Copy link
Collaborator

@bugarela bugarela left a comment

Choose a reason for hiding this comment

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

LGTM

@shonfeder shonfeder merged commit 467bdb7 into main Nov 10, 2023
15 checks passed
@shonfeder shonfeder deleted the wilcard-match-cases branch November 10, 2023 20:28
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