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

Add front-end support for case expressions boolean side conditions #2852

Merged
merged 16 commits into from
Jul 3, 2024

Conversation

janmasrovira
Copy link
Collaborator

@janmasrovira janmasrovira commented Jun 21, 2024

This pr introduces:

  1. front-end support (parsing, printing, typechecking) for boolean side conditions for branches of case expressions.
  2. Now if is a reserved keyword.
  3. Multiway if is allowed to have only the else branch. I've also refactored the parser to be simpler.

Example:

 multiCaseBr : Nat :=
    case 1 of
      | zero
        | if 0 < 0 := 3
        | else := 4
      | suc (suc n)
        | if 0 < 0 := 3
        | else := n
      | suc n if 0 < 0 := 3;

The side if branches must satisfy the following.

  1. There must be at least one if branch.
  2. The else branch is optional. If present, it must be the last.

Future work:

  1. Translate side if conditions to Core and extend the exhaustiveness algorithm.
  2. Add side if conditions to function clauses.

@janmasrovira janmasrovira added enhancement New feature or request syntax labels Jun 21, 2024
@janmasrovira janmasrovira added this to the 0.6.3 milestone Jun 21, 2024
@janmasrovira janmasrovira self-assigned this Jun 21, 2024
@janmasrovira janmasrovira force-pushed the syntax-case-side-conditions branch 3 times, most recently from ed4e7db to d1c2e98 Compare June 22, 2024 11:33
@janmasrovira janmasrovira changed the title Add syntax for case expressions boolean side conditions Add front-end support for case expressions boolean side conditions Jun 25, 2024
@janmasrovira janmasrovira force-pushed the syntax-case-side-conditions branch 2 times, most recently from d7706e8 to 8f90a07 Compare June 25, 2024 15:36
@janmasrovira janmasrovira requested a review from lukaszcz June 25, 2024 15:44
@janmasrovira janmasrovira marked this pull request as ready for review June 25, 2024 15:53
@janmasrovira janmasrovira marked this pull request as draft June 25, 2024 16:31
@janmasrovira janmasrovira marked this pull request as draft June 25, 2024 16:31
@janmasrovira janmasrovira marked this pull request as draft June 25, 2024 16:31
@janmasrovira janmasrovira force-pushed the syntax-case-side-conditions branch from 7792bc1 to 2d79121 Compare June 26, 2024 13:15
janmasrovira added a commit to anoma/juvix-stdlib that referenced this pull request Jun 28, 2024
- We rename `if` to `ite` since `if` will become a keyword in
anoma/juvix#2852
@janmasrovira janmasrovira force-pushed the syntax-case-side-conditions branch 3 times, most recently from 604d7c4 to 4740d22 Compare July 2, 2024 15:22
@janmasrovira janmasrovira marked this pull request as ready for review July 2, 2024 15:22
@paulcadman paulcadman modified the milestones: 0.6.3, 0.6.4 Jul 2, 2024
| else := n
| suc n if 0 < 0 := 3;
end;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we have a test with multiple if conditions for one pattern? I understand this is allowed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

lukaszcz
lukaszcz previously approved these changes Jul 3, 2024
@janmasrovira janmasrovira force-pushed the syntax-case-side-conditions branch from 4740d22 to 031e8e1 Compare July 3, 2024 17:46
@janmasrovira janmasrovira merged commit d08bf94 into main Jul 3, 2024
4 checks passed
@janmasrovira janmasrovira deleted the syntax-case-side-conditions branch July 3, 2024 23:16
lukaszcz added a commit that referenced this pull request Sep 9, 2024
* Closes #2804 
* Requires #3003
* Front-end syntax for side conditions was implemented in #2852. This PR
implements compilation of side conditions.
* Adds side-conditions to `Match` nodes in Core. Updates Core parsing,
printing and the evaluator.
* Only side-conditions without an `else` branch are allowed in Core. If
there is an `else` branch, the side conditions are translated in
`fromInternal` into nested ifs. Because with `else` the conditions are
exhaustive, there are no implications for pattern exhaustiveness
checking.
* Adjusts the "wildcard row" case in the pattern matching compilation
algorithm to take into account the side conditions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request syntax
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants