Skip to content

Check that instance declarations don't overlap#3129

Merged
LiamGoodacre merged 1 commit intopurescript:masterfrom
LiamGoodacre:feature/overlapping-inst-decl
Apr 13, 2018
Merged

Check that instance declarations don't overlap#3129
LiamGoodacre merged 1 commit intopurescript:masterfrom
LiamGoodacre:feature/overlapping-inst-decl

Conversation

@LiamGoodacre
Copy link
Copy Markdown
Member

Fixes #2957

I plan to add a failing example which demonstrates the covering sets aspect of this.

:: Qualified (ProperName 'ClassName)
-> TypeClassData
-> [Type]
-> m (S.Set ModuleName)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No need for m here, right?

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.

Oh right, I was thinking that internalError required it.

typesApart :: Int -> Bool
typesApart i = typeHeadsApart (lhs !! i) (rhs !! i)

typeHeadsApart :: Type -> Type -> Bool
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe this should live alongside the equality test we have elsewhere?

-> S.Set ModuleName
-> m ()
checkOverlappingInstance ch dictName className typeClass tys' nonOrphanModules = do
for_ nonOrphanModules $ \m -> do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there is a possible issue with MPTCs here, where we can miss an instance in an unrelated module, but that's fine. We should probably just document it.

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.

Unless I've misunderstood you, I'm not sure that's correct. The modules in nonOrphanModules are the only modules that the instance is allowed to appear based on the type class's covering sets (we'd get an orphan instance error if one were in a different module). So we are definitely seeing all the possible instances that could overlap. Right?

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.

Wait, I understand, we're only looking for modules that the instance talks about.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Right, I can always define an instance for an unrelated data type in another module. This is only an issue with FlexibleInstances or MPTCs though.

nonOrphanModules' = foldl1 S.intersection (foldMap lookupModule `S.map` typeClassCoveringSets typeClass)
findNonOrphanModules _ _ _ = internalError "Unqualified class name in findNonOrphanModules"

checkOverlappingInstance
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please add a comment here?

Copy link
Copy Markdown
Contributor

@paf31 paf31 left a comment

Choose a reason for hiding this comment

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

Looks good at first glance, thanks for working on this! I'll review again once there is a failing example though.

@LiamGoodacre
Copy link
Copy Markdown
Member Author

The build will fail due to the lack of purescript/purescript-psci-support#7

@LiamGoodacre LiamGoodacre changed the base branch from master to 0.12.0-dev November 12, 2017 18:19
@LiamGoodacre LiamGoodacre changed the base branch from 0.12.0-dev to master December 30, 2017 23:00
@LiamGoodacre
Copy link
Copy Markdown
Member Author

Did you want to take another look at this? @paf31 (or anyone else interested).
I've now updated the tests to point at purescript/purescript-psci-support#compiler/0.12.

@paf31
Copy link
Copy Markdown
Contributor

paf31 commented Apr 12, 2018

I feel like I've reviewed this in quite a lot of detail already, and it's been tested quite a lot, so I think it's more than good enough for a first release. :shipit:

@LiamGoodacre
Copy link
Copy Markdown
Member Author

(btw, I can't approve my own PRs) 😸

@LiamGoodacre LiamGoodacre merged commit dd6925d into purescript:master Apr 13, 2018
@LiamGoodacre
Copy link
Copy Markdown
Member Author

Thanks @hdgarrood

@LiamGoodacre LiamGoodacre deleted the feature/overlapping-inst-decl branch May 9, 2021 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants