pattern-matching refactoring: move pattern types to a new module typing/Patterns#9563
pattern-matching refactoring: move pattern types to a new module typing/Patterns#9563gasche merged 11 commits intoocaml:trunkfrom
Conversation
The aim is to also move the Simple/Half_simple/General stuff from matching, but we need to split in those modules the part that are purely structural (they go in Patterns) and the parts that are actually compilation logic (Half_simple.of_clause), those stay in Matching.
"rows" are common abstraction of both pattern analysis and compilation, but clauses carrying a Lambda.t term are compilation-specific. Separating rows will thus enable moving more logic to typing/patterns for use in both settings.
|
(I just fixed a couple |
27bd55b to
9ca2a35
Compare
typing/patterns.mli
Outdated
|
|
||
| val strip_vars : pattern -> Half_simple.pattern | ||
|
|
||
| val assert_simple : pattern -> Simple.pattern |
There was a problem hiding this comment.
This function seems too ad hoc: it is used in a single case (in Matching.complete_pat_constrs), where it is nearly immediately refined into checking than rather than a Simple.pattern we have a [ `Constr _ ] pattern_data.
There was a problem hiding this comment.
I disagree with the general argument:
assert_<static-knowledge>functions are not ad-hoc, there are general ways to add a dynamic check at the boundary of a statically-checked region of code. In particular they are useful to gradually introduce static knowledge in a codebase that relied on unspoken invariants and dynamic checks.- The fact that a function is used only once is not in itself a bad thing. (It is a mistake to believe that functions should only be defined to factorize multiple occurrences of a pattern.) If a function makes sense as a unit of abstraction, it has value even if used only once.
(Of course the reason why we have assert_simple is that we used it for other things as well in previous iterations of the patchset, and I kept it around as a general solution to the problem of coercing patterns into their simple form.)
This being said, I agree that, in this specific case, there is an independent improvement that we could make, which is to carry around a finer-grained argument of type constructor_description pattern_data. This would be very nice, and/but it is a non-local change, unrelated to the present PR, that would make more sense as a separate commit.
I changed the code according to your comment: I inlined the definition of assert_simple and got rid of it (the mega-patchset does not use it more for now, and we can always re-introduce it later if we want). I first inlined it in Parmatch.complete_constrs, to be closed to where the existing comment was, but this entailed weakening the Parmatch interface (to take a general pattern for this function). So I moved the inlining and the comment to the Matching side.
There was a problem hiding this comment.
Note: I'm trying to push a constructor_description pattern_data in this part of the code, but it is proving quite difficult because this interacts with the compile_test machinery which is a bunch of unstructured polymorphic higher-order functions.
There was a problem hiding this comment.
Here is a patchset that pushes a constructor_description pattern_data all the way to complete_constrs: trefis/ocaml@rematch-standalone-patterns...gasche:rematch-complete-constrs
There was a problem hiding this comment.
(I propose to send it for review after the fate of the current PR is decided.)
There was a problem hiding this comment.
The patch looks good, but it can indeed wait.
There was a problem hiding this comment.
Of course my preference would send it sooner rather than later. But I don't see the point of piling up more open review work for you before this one is closed, and I don't think it would make sense to merge the two PRs (they are mostly orthogonal).
7d3e203 to
1ee6ee4
Compare
Octachron
left a comment
There was a problem hiding this comment.
As a short description, this PR unifies the head-oriented pattern types used in typing/parmatch and lambda/matching .
It works.
More verbosely, everything falls in place smoothly and there are no unpleasant circonvolutions.
|
Thanks for the glowing review! I will rebase the history to hide the review fixups, wait for the CI to return, and then merge. |
|
@Octachron may be amused to know that this PR is the last that contains refactoring work that we did in August 2019, with a bit more in September 2019. (We made several iterations, the first by @trefis using abstract types instead of polymorphic variants.) The next PR from the stack (not #9599 which was not initially part of the PR stack) corresponds to the first part done in 2020 (in February 2020 by @trefis). And it is also the last PR of the stack! After that one, the only things left in store are the TODO items from previous reviews by @Octachron. (Before we decide to refactor more, that is. Of course we have ideas for further simplifications / straightening of the static types.) |
This is the next bit of the big pattern-matching refactoring change (#9321) after #9520 has been merged.
(cc @trefis @Octachron)
By now we have grown interesting static types to describe patterns and use in patterns-decomposition algoriths: "heads" in Parmatch, and "(half-)simple" polymorphic variants in Matching. This PR moves these two categories into a shared compilation unit typing/Patterns, for use in both modules.
As usual, it is best reviewed commit-by-commit.