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

Inferred type of print using function is surprising #806

Open
ocharles opened this issue Dec 10, 2019 · 10 comments
Open

Inferred type of print using function is surprising #806

ocharles opened this issue Dec 10, 2019 · 10 comments

Comments

@ocharles
Copy link
Contributor

I wanted to play around with the new functionality to insert type signatures. The following:

hello = print

surprises me, as when I add a type signature with a code action, I get:

hello :: GHC.Types.Any -> IO ()
hello = print

which is mostly certainly not what I'd expect!

@ndmitchell
Copy link
Collaborator

If you add an argument a to both sides, does it do better? I wonder if the monomorphism restriction is involved? I know GHC replaces completely polymorphic variables with Any at some step in the process, I'm surprised its happened by the time we get to it.

@ocharles
Copy link
Contributor Author

Ah yea, good idea - it does indeed work if I write

hello a = print a

@ndmitchell
Copy link
Collaborator

Note that I don't get this bug. If I write:

hello = print

Then I get the warning:

    • Ambiguous type variable ‘a0’ arising from a use of ‘print’
      prevents the constraint ‘(Show a0)’ from being solved.
      Relevant bindings include
        hello :: a0 -> IO () (bound at C:\Neil\temp\IDE.hs:3:1)
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
        ...plus 22 others
        ...plus 12 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the expression: print
      In an equation for ‘hello’: hello = print

And that warning prevents it from detecting an incorrect type signature. If I hover over the print it suggests Any -> IO () as the type, but that's not unreasonable as the thing has failed to type check, so the fact it displays anything is moderately surprising.

What did you do to manage to persuade it to get as far as it did? Were there any errors present?

@ocharles
Copy link
Contributor Author

ocharles commented Dec 11, 2019 via email

@ndmitchell
Copy link
Collaborator

I was on GHC 8.6.5, what were you on? My understanding from the diagnostics I saw was that GHC wasn't giving the top-signature warning.

@ocharles
Copy link
Contributor Author

ocharles commented Dec 11, 2019 via email

@ndmitchell
Copy link
Collaborator

Most likely GHC 8.8 manages to figure out a top-level signature warning even in the presence of warnings. I wonder if we should explicitly omit suggesting signatures with Any in them, as its almost never what anyone wants.

@pepeiborra pepeiborra transferred this issue from haskell/ghcide Jan 1, 2021
@jneira jneira added component: ghcide type: enhancement New feature or request labels Jan 3, 2021
@jneira
Copy link
Member

jneira commented Nov 26, 2021

Reproduced with hls-1.5.0 and ghc-8.10.7

@hasufell
Copy link
Member

I wonder if we should explicitly omit suggesting signatures with Any in them, as its almost never what anyone wants.

I second this.

@develop7
Copy link
Contributor

develop7 commented Jun 8, 2024

Reproduced with GHC 9.8.2 & HLS 2.8.0.0

develop7 added a commit to develop7/haskell-language-server that referenced this issue Jun 8, 2024
soulomoon added a commit that referenced this issue Jun 9, 2024
Co-authored-by: soulomoon <fwy996602672@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants