Skip to content

pattern-matching refactoring: merge the two matchers#9493

Merged
gasche merged 10 commits intoocaml:trunkfrom
trefis:rematch-matcher-unification
May 2, 2020
Merged

pattern-matching refactoring: merge the two matchers#9493
gasche merged 10 commits intoocaml:trunkfrom
trefis:rematch-matcher-unification

Conversation

@gasche
Copy link
Copy Markdown
Member

@gasche gasche commented Apr 23, 2020

This is the next bit of the big pattern-matching refactoring change (#9321) after #9464 has been merged.

(cc @trefis @Octachron)

It is a series of refactoring with two highlights:

  • In the compilation part, we move from a series of matcher_* functions to a single matcher on a head. This is similar in spirit to the move from the group_* functions to a single can_group function on a head in acd44f9 ( matching: simplify can_group #9417 ).
  • We merge the resulting matcher function (for compilation) with the ctx_matcher function from Context (for optimization), which deduplicates the matching logic.

As previously, this is best reviewed commit-by-commit.

@gasche
Copy link
Copy Markdown
Member Author

gasche commented Apr 29, 2020

@Octachron remarks that, for pattern heads, in many cases we only use the pat_desc field and not the other metadata -- we only need the metadata when we want to reconstruct a full pattern from the head. He suggests refactoring the code to only pass the pat_desc (head_discr ?) in the places that don't need the metadata. I proposed to push this independent refactoring to the end of the PR stack.

@gasche
Copy link
Copy Markdown
Member Author

gasche commented Apr 29, 2020

Note: reconstructing a pattern from a head is a more natural implementation in Parmatch (rebuilding counter-examples, etc.) and probably done less often in Matching.

@gasche gasche force-pushed the rematch-matcher-unification branch from c0625a2 to 39042a8 Compare April 29, 2020 15:58
@gasche
Copy link
Copy Markdown
Member Author

gasche commented Apr 29, 2020

I just rebased on top of trunk. There were conflicts due to the sprinkling of ~scopes parameter coming from 2986bea.

@gasche
Copy link
Copy Markdown
Member Author

gasche commented Apr 29, 2020

I just pushed a last commit that fine-tunes some aspects of ~scopes handling, but does not otherwise touch the pattern-matching logic.

@gasche gasche force-pushed the rematch-matcher-unification branch from a47015a to 8b8fb62 Compare April 29, 2020 16:38
Copy link
Copy Markdown
Member

@Octachron Octachron left a comment

Choose a reason for hiding this comment

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

This PR merges together the two matcher functions by making them more regular, without any behavior change. The invariant that records need to be expanded is a bit inelegant, but no more than the pre-existing code and it could be fixed later.

Overall, this is a clear and nice code simplification.

gasche added 8 commits May 1, 2020 21:56
This commit is delicate and needs a careful review.

The `matcher_of_pattern` function is a temporary measure to reduce the
invasiveness of the patch, and make it easier to review.

(Note for reviewers: in the previous version the Record case had
a funny handling of Any, but it is in fact equivalent to just adding
omegas as we now do in all cases.)

There are two obvious directions for improvement:

- Get rid of matcher_of_pattern and pass a head directly to the
  various make_matching_* functions.

- Try to factorize this code with ctx_matcher which, it is now
  obvious, does essentially the same thing.

Another, less immediate area of attack would be to consider
a presentation of Pattern_head.t where the Any case can be statically
ruled out -- maybe the description could have two levels, one
isomorphic to option (Any or not?) and one for non-any heads.
@gasche gasche force-pushed the rematch-matcher-unification branch from f1f47a9 to 1c14957 Compare May 1, 2020 19:56
@gasche gasche force-pushed the rematch-matcher-unification branch from 1c14957 to 3903f9f Compare May 1, 2020 19:58
@gasche
Copy link
Copy Markdown
Member Author

gasche commented May 1, 2020

I addressed the remaining comments of @Octachron, this should be good to merge if/when the CI returns.

@gasche gasche merged commit 876af25 into ocaml:trunk May 2, 2020
@gasche
Copy link
Copy Markdown
Member Author

gasche commented May 2, 2020

Merged! Thanks a lot @Octachron for the review, which is also produced nice suggestions for things we have to work on in this refactoring.

gasche added a commit to trefis/ocaml that referenced this pull request May 2, 2020
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