Skip to content

pattern-matching refactoring: move pattern types to a new module typing/Patterns#9563

Merged
gasche merged 11 commits intoocaml:trunkfrom
trefis:rematch-standalone-patterns
May 25, 2020
Merged

pattern-matching refactoring: move pattern types to a new module typing/Patterns#9563
gasche merged 11 commits intoocaml:trunkfrom
trefis:rematch-standalone-patterns

Conversation

@gasche
Copy link
Copy Markdown
Member

@gasche gasche commented May 14, 2020

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.

gasche and others added 9 commits May 14, 2020 10:11
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.
@gasche
Copy link
Copy Markdown
Member Author

gasche commented May 15, 2020

(I just fixed a couple check-typo issues.)

@gasche gasche force-pushed the rematch-standalone-patterns branch from 27bd55b to 9ca2a35 Compare May 16, 2020 10:33

val strip_vars : pattern -> Half_simple.pattern

val assert_simple : pattern -> Simple.pattern
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

@gasche gasche May 21, 2020

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

(I propose to send it for review after the fate of the current PR is decided.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The patch looks good, but it can indeed wait.

Copy link
Copy Markdown
Member Author

@gasche gasche May 25, 2020

Choose a reason for hiding this comment

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

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).

@gasche gasche force-pushed the rematch-standalone-patterns branch from 7d3e203 to 1ee6ee4 Compare May 21, 2020 07:51
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.

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.

@gasche
Copy link
Copy Markdown
Member Author

gasche commented May 25, 2020

Thanks for the glowing review! I will rebase the history to hide the review fixups, wait for the CI to return, and then merge.

@gasche
Copy link
Copy Markdown
Member Author

gasche commented May 25, 2020

@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.)

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.

3 participants