View patterns (but also as-pattern & pattern guards) #2813
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi,
Chatting with @theolaurent a while back, he had a nice idea: implementing view patterns in F*.
It's a pretty nice feature, that could especially be useful for meta-programming (so that we can pattern-match nested
term
s inspecting on the fly its nestedterm_view
s). But it also gives us for free:if
/when
clauses (since F*when
clauses are basically disabled),This PR is still quite draft, I just wrote it quickly during this week-end, some part are really ugly. The SMT encoding I wrote for those view patterns is fairly broken (in interactive mode it yields errors, but retrying a code block make it pass).
There's a lot of room for improvement, but I wanted to get some feedback anyway, before spending more time on it.
Examples
1. Matching on abstract data types
Example of pattern matching deeply in terms:
Other applications: matching over types defined in external OCaml libraries (e.g. yojson), data structures with multiple views (e.g. graphs stored as adjacency matrixes but consumed as adjacency lists)...
2. As-patterns
Example of as-patterns:
3. If clauses
Example of
if
clauses (this is a bit silly to have bothwhen
andif
, it's just for demo purposes):4. Disjunctive patterns
5. Fancy pattern matching on lists
6. Ranges
There are more examples in
examples/misc/ViewPatterns.fst
.What do you think about adding that to F*?