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

Add sig lens for where clauses #2966

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

July541
Copy link
Collaborator

@July541 July541 commented Jun 17, 2022

Close #2943.

image

@July541 July541 requested a review from pepeiborra as a code owner June 17, 2022 10:40
ghcide/test/exe/Main.hs Outdated Show resolved Hide resolved
ghcide/src/Development/IDE/Plugin/TypeLenses.hs Outdated Show resolved Hide resolved
-- | Get the type string of a binding id
bindToSig :: Id -> HscEnv -> GlobalRdrEnv -> IOEnv (Env TcGblEnv TcLclEnv) String
bindToSig id hsc rdrEnv = do
env <- tcInitTidyEnv
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this expensive? It seems like we might do it a lot...

ghcide/test/exe/Main.hs Show resolved Hide resolved
ghcide/src/Development/IDE/Plugin/TypeLenses.hs Outdated Show resolved Hide resolved
| WhereBindings{..} <- localBindings
, let sigSpans = getSrcSpan <$> existedSigNames
, WhereBinding{..} <- bindings
, let idsWithoutSig = filter (\x -> getSrcSpan (idName x) `notElem` sigSpans) bindingId
Copy link
Collaborator

Choose a reason for hiding this comment

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

this seems a bit odd to me: we're checking that a binding doesn't have a signature by comparing source spans. Why not just compare the ids directly?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The two names are not exactly equal, one name is for the signature and another is for the binding, but they have the same SrcSpan. I've commented it in the definition of WhereBinidng.

Copy link
Collaborator

Choose a reason for hiding this comment

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

That seems surprising? I would really have expected that the names of the identifiers would be the same? Or maybe there's a different comparison method you're supposed to use there?

ghcide/src/Development/IDE/Plugin/TypeLenses.hs Outdated Show resolved Hide resolved
ghcide/test/exe/Main.hs Outdated Show resolved Hide resolved
ghcide/test/exe/Main.hs Outdated Show resolved Hide resolved
ghcide/test/exe/Main.hs Outdated Show resolved Hide resolved
@July541
Copy link
Collaborator Author

July541 commented Jun 20, 2022

I rewrite the test to make it clear.

I increasingly find that I know little about type information in Haskell, I'm confused by the concept of tidy type, open type, sigma type...

Copy link
Collaborator Author

@July541 July541 left a comment

Choose a reason for hiding this comment

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

Marked two known failed tests which I can't tackle.

f aa bb = (aa, ida bb)
where
ida :: b -> b
ida = id
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It fails as you may guess...

f = undefined
where
g :: Num a => a -> a -> a
g a b = a + b
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Num a lost here, the same method can show constraints for top-level bindings...

Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you saying that the lens cannot deduce Num a => here if the signature is not present?
I find that ok, constraints belong at the top level, not in local type signatures

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry for ignoring this...

Are you saying that the lens cannot deduce Num a => here if the signature is not present?

Yes.

I find that ok, constraints belong at the top level, not in local type signatures

But it's not correct without constraints.

@michaelpj
Copy link
Collaborator

I increasingly find that I know little about type information in Haskell, I'm confused by the concept of tidy type, open type, sigma type...

Yeah, it's very complicated and I don't understand it either. Worth talking to the GHC devs in #ghc, though, they're very helpful.

@July541
Copy link
Collaborator Author

July541 commented Dec 11, 2022

Some notes from what I observed:

  1. the symbol r has a different type of notation between its definition and application.

image

  1. Following 1, I tried to print the ast for the local variable and top variable with the same expression f a b = a <> b, where I got a FunTywrapped with a ForAllTy for top bindings, and for local bindings, there only a ForAllTy, that means we can't get a proper representation for local variables till now.

  2. What I am trying to do:

    1. There has a mystery function I haven't know that can give an explicit forall refer to its corresponding top bindings :(
    2. Extract type variable from top level bindings and then manually construct a forall binding with mkForAllTy, it's not easy and I also doubt its feasibility.
    3. We can accept this shortcoming by displaying these by inlay hints until we find a solution to build the correct type info.

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.

Code lens to display type signature for local bindings in where clauses
3 participants