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

Dont suggest const when NamedFieldPuns are used #1521

Merged
merged 5 commits into from
Jun 12, 2023

Conversation

batkot
Copy link
Contributor

@batkot batkot commented Jun 11, 2023

Closes #1453

@@ -201,7 +201,7 @@ checkSide x bind = maybe True bool x
isType "Compare" x = True -- Just a hint for proof stuff
isType "Atom" x = isAtom x
isType "WHNF" x = isWHNF x
isType "Wildcard" x = any isFieldPun (universeBi x) || any hasFieldsDotDot (universeBi x)
isType "Wildcard" x = any isFieldPun (universeBi x) || any hasFieldsDotDot (universeBi x) || any isFieldPunUpdate (universeBi x)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice attempt! But I don't think either any isFieldPun (universeBi x) or any isFieldPunUpdate (universeBi x) is the right thing to do.

The goal is to avoid suggesting "Use const" for either \a -> Foo{a} or \a -> x{a}. The first one is field pun, and the second one is field pun update. But there's no need to exclude all field puns or field pun updates. We should indeed suggest "Use const" for \a -> Foo{b} and \a -> x{b}.

I think the correct approach is to reject \a -> Foo{a} and \a -> x{a} on the basis of introducing free variables: if we turn \a -> Foo{a} into const Foo{a}, then we'd introduce a new free variable a, and that's why we shouldn't do it. The check is here.

This check does the right thing for field puns, but not for field pun updates, because the freeVar computation for field pun updates is incorrect. When hfbPun==True, instead of returning freeVars x, it should return the OccName contained in the LHS (the argument before x) as the free variable. Hope this helps!

Copy link
Contributor Author

@batkot batkot Jun 12, 2023

Choose a reason for hiding this comment

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

Thanks for directions! Hope I got it right.

Nice attempt! But I don't think either any isFieldPun (universeBi x) or any isFieldPunUpdate (universeBi x) is the right thing to do.

Should I take the opportunity then and get rid of any isFieldPun (universeBi x) from Wildcard? It doesn't break any tests, but I don't really understand what Wildcard is supposed to check sweat_smile

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO isWildCard should just look for RecordWildCards, which is what hasFieldsDotDot is.

tests/hint.test Outdated
@@ -51,6 +51,29 @@ Perhaps:

1 hint

---------------------------------------------------------------------
RUN tests/use-const.hs
Copy link
Collaborator

Choose a reason for hiding this comment

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

These tests can be written directly in the <TEST> section in hlint.yaml. The .test files are for tests that need custom hint files, which can't be written there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I didn't notice <TEST> section in hlint.yaml 😅

Copy link
Collaborator

@zliu41 zliu41 left a comment

Choose a reason for hiding this comment

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

👍

@@ -174,6 +174,7 @@ instance FreeVars (LocatedA (HsFieldBind (LocatedAn NoEpAnns (FieldOcc GhcPs)) (
freeVars o@(L _ (HsFieldBind _ _ x _)) = freeVars x

instance FreeVars (LocatedA (HsFieldBind (LocatedAn NoEpAnns (AmbiguousFieldOcc GhcPs)) (LocatedA (HsExpr GhcPs)))) where
freeVars (L _ (HsFieldBind _ x _ True)) = Set.singleton $ rdrNameOcc $ rdrNameAmbiguousFieldOcc $ unLoc x -- a pun
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, I didn't know about rdrNameAmbiguousFieldOcc!

@@ -201,7 +201,7 @@ checkSide x bind = maybe True bool x
isType "Compare" x = True -- Just a hint for proof stuff
isType "Atom" x = isAtom x
isType "WHNF" x = isWHNF x
isType "Wildcard" x = any isFieldPun (universeBi x) || any hasFieldsDotDot (universeBi x)
isType "Wildcard" x = any isFieldPun (universeBi x) || any hasFieldsDotDot (universeBi x) || any isFieldPunUpdate (universeBi x)
Copy link
Collaborator

Choose a reason for hiding this comment

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

IMO isWildCard should just look for RecordWildCards, which is what hasFieldsDotDot is.

@zliu41 zliu41 merged commit 1911d44 into ndmitchell:master Jun 12, 2023
@zliu41
Copy link
Collaborator

zliu41 commented Jun 12, 2023

Thanks!

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.

Invalid hint when using NamedFieldPun field updates
2 participants