Ticket #10 (new enhancement)

Opened 5 years ago

Last modified 5 years ago

Show types for subexpressions

Reported by: nomeata Owned by: claus
Priority: minor Milestone:
Component: compiler/ghc.vim Version:
Keywords: Cc:

Description

Hi,

not sure if this is possible, but it would be nice to use _t even on locally bound identifiers. So if I have

readTheFile filename = readFile filename

I could hover the filename (either in the pattern or in the body), and it would show "String" or "FileName?".

This could be achieved by temporarily annotating the value with a type signature, e.g.

data Dummy = Dummy
readTheFile filename = readFile (filename :: Dummy)

(where Dummy might be replaced by some random string) and then parsing the error message

tmp.hs:2:33:
    Couldn't match expected type `FilePath'
           against inferred type `Dummy'
    In the first argument of `readFile', namely `(filename :: Dummy)'
    In the expression: readFile (filename :: Dummy)
    In the definition of `readTheFile':
        readTheFile filename = readFile (filename :: Dummy)

extracing the type from the "expected type" information.

Additional bonus if it works for subexpression in visual mode. E.g., I can select a range in the code and _t gives me the type of the range (if putting that range in brackets and adding a type signature is still valid code).

Thanks, Joachim

Change History

Changed 5 years ago by claus

Yes, that feature has been on my wishlist for quite some time, so it'll be there, eventually;-) Though I'll probably prefer either adding it to GHCi, or at least using a dedicated GHC Api client (perhaps scion, perhaps my own), see #2.

The implementation itself isn't too difficult, but the interface isn't entirely clear: for GHCi, even the input format isn't straightforward (cursor position?), but even with that out of the way, the output format needs thinking about, as local types will often refer to other local types (which may not have a source representation). Have a look at `-ddump-tc`, for instance.

The error message hack isn't bad, but needs quite a bit of interpretation in practice (if there is no error, the type was polymorphic, but what would be a free type variable? if the error just says "missing instance", that tells us something else; sometimes, the other type is mentioned first; etc.). So it would only be a temporary workaround, with too much balast to get it to work properly, I'm afraid.

If you'd like to try the basic hack anyway, something like this should get you started:

:vmap ,t c()<esc>Pa::()<esc>:w<cr>:make<cr>:copen<cr>

It maps the keys ,t in Visual mode (when you highlight some text using v), so that the highlighted text gets wrapped in (_::()), the file written, :make gets invoked and the quickfix window gets opened, so you can see the error message presumably including the type you're interested in. No guarantees!-)

Now, what is my bonus, not to mention "additional bonus"?-)

Joking aside, I'll leave this ticket open, as it is more specific than #2.

Note: See TracTickets for help on using tickets.