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

Combine clauses of record-selector function declarations #181

Closed
wants to merge 3 commits into from
Closed

Combine clauses of record-selector function declarations #181

wants to merge 3 commits into from

Conversation

RyanGlScott
Copy link
Collaborator

Previously, in promoteDataDecs we were simply calling getRecordSelectors on each constructor of a data type and building a LetDecEnv map of names to function declarations clauses. But if a record selector is contained in multiple constructors, then the clauses of the record selector function will be spread out across multiple DLetDs, so when the LetDecEnv is built, it only keeps the most recent DLetD for a particular record selector name! This causes the incorrect promoted definition that appeared in #180.

This adds mergeLetDecs to post-process the results of getRecordSelectors so that all of the clauses for a particular record selector are grouped together before being turned into a LetDecEnv. I've added a lengthy comment above mergeLetDecs explaining what it does and why it's needed.

Fixes #180.

Copy link
Owner

@goldfirere goldfirere left a comment

Choose a reason for hiding this comment

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

I'm happy to accept this patch. Many thanks for writing it!

But, it strikes me that any use of getRecordSelectors will be wrong in exactly this way. So I think fixing the problem at the source would be an improvement. It's also conceivable that we should refactor getRecordSelectors out of th-desugar and into singletons. It's unclear to me at the moment as to why that function is there. The new getRecordSelectors would have to work on a list of Cons.

Other designs around this are welcome.

concatMapM (getRecordSelectors arg_ty) cons
mergeLetDecs <$> concatMapM (getRecordSelectors arg_ty) cons

-- After retrieving the record selectors from a data type's constructors, it
Copy link
Owner

Choose a reason for hiding this comment

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

Great comment.

-- their clauses.
| DFunD n clauses <- x
= let (eq_n, neq_n) = partition (\case DFunD n2 _ -> n == n2; _ -> False) xs
merged_clauses = concat $ clauses:map (\(DFunD _ cls) -> cls) eq_n
Copy link
Owner

Choose a reason for hiding this comment

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

partitionWith (stolen from GHC and put in Data.Singletons.Util) should simplify this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch, I'll refactor it to use partitionWith.

@RyanGlScott
Copy link
Collaborator Author

I agree that making getRecordSelectors work on one Con at a time is fraught with peril. I'd be fine with removing getRecordSelectors from th-desugar as well—where in singletons would be the best home for it?

@goldfirere
Copy link
Owner

Considering getRecordSelectors is used only in Promote, that looks like the best spot for the new functionality. Thanks!

@RyanGlScott
Copy link
Collaborator Author

Oh, it turns out there'd be one awkward aspect of removing getRecordSelectors from th-desugar entirely—it's used in the test suite. I'm not sure what the best way to deal with this is... should we:

  • Find a way to alter that test?
  • Keep getRecordSelectors in th-desugar, but port it over to singletons with the necessary fixes?
  • Keep getRecordSelectors in th-desugar, and put the necessary fixes in place there?

@goldfirere
Copy link
Owner

I suppose option 3 is the most widely helpful.... just in case some user of th-desugar (other than singletons) uses getRecordSelectors.

@RyanGlScott
Copy link
Collaborator Author

Closing in favor of goldfirere/th-desugar#55.

@RyanGlScott RyanGlScott closed this Apr 9, 2017
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