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

Implement pattern matching with literals and guards #385

Merged
merged 16 commits into from
Feb 2, 2024

Conversation

b-studios
Copy link
Collaborator

@b-studios b-studios commented Jan 29, 2024

This PR implements proposal #383. A working example can be found here:

Notes about the implementation can be found at the top of

The implementation of the compiler was rather modest; exhaustivity checking was surprisingly tricky since it is difficult to maintain the necessary information (like types to check exhaustivity of nested scrutinees).

Both implementations (compiler and checker) are structure to

  1. preprocess clauses into an internal representation (transformation requires Context)
  2. perform the actual work (transformation does NOT require Context and can thus be tested more easily)

Note: for now, I decided that Or-Patterns are not worth it.


Implemented in frontend

  • pattern guards
  • boolean guards
    - [x] or patterns (properly, which requires significant changes to always bind the SAME symbol)
  • literal patterns
  • guards on if and while
  • exhaustivity check

pattern compiler

  • guards
    - [ ] or patterns
  • literal patterns
  • heuristic
  • compilation of if and while
    - [ ] fuse subsequent predicates using && but also make && by-name in its arguments.

translation

  • default clause in matches
  • non trivial guards in while and if

@b-studios b-studios changed the title Implement proposal #383 in frontend to ease discussion Implement pattern matching with guards Feb 2, 2024
@b-studios
Copy link
Collaborator Author

b-studios commented Feb 2, 2024

Reminder to future me: Hey Jonathan, nice that you have some time for programming again; if you really want to add Or-Patterns again, start by reverting d4452a3.

@b-studios
Copy link
Collaborator Author

To fix in the future: Using Map[ValueVar, Pattern] as a semantic domain for patterns in the compiler is wrong. It should be List[(ValueVar, Pattern)] since we might have multiple matches on the same scrutinee.

@b-studios b-studios marked this pull request as ready for review February 2, 2024 15:30
@b-studios b-studios changed the title Implement pattern matching with guards Implement pattern matching with literals and guards Feb 2, 2024
@b-studios b-studios merged commit 6088190 into master Feb 2, 2024
1 check passed
@b-studios b-studios deleted the feature/matching branch February 2, 2024 17:01
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.

None yet

1 participant