amindfv
amindfv

Reputation: 8458

Find inferred type for local function

Is there a way in ghci (or ghc) to find what the inferred type of a local function is?

E.g. if I have a function

f l = map f' l
   where f' = (+1)

is there a :t-like way in ghci to see what the inferred type for f' is?

Upvotes: 14

Views: 1231

Answers (3)

lethalman
lethalman

Reputation: 2019

Try hdevtools, it's quite fast and easy to use, though there's only integration for Vim.

Upvotes: 0

jberryman
jberryman

Reputation: 16635

Indeed there is, which I learned about thanks to hammar's awesome answere here. Here's the short version:

Prelude> :l /tmp/foo.hs
[1 of 1] Compiling Main             ( /tmp/foo.hs, interpreted )
Ok, modules loaded: Main.
*Main> :break f
Breakpoint 0 activated at /tmp/foo.hs:(1,1)-(2,18)
*Main> f [1..10]
Stopped at /tmp/foo.hs:(1,1)-(2,18)
_result :: [b] = _
[/tmp/foo.hs:(1,1)-(2,18)] *Main> :step
Stopped at /tmp/foo.hs:1:7-14
_result :: [b] = _
f' :: b -> b = _
l :: [b] = _
[/tmp/foo.hs:1:7-14] *Main> :t f'
f' :: b -> b

Upvotes: 8

Tikhon Jelvis
Tikhon Jelvis

Reputation: 68152

I don't know of any way of doing it from GHCi.

However, if you're using an editor like Emacs or Vim, you can try ghc-mod. This is an external tool that plugs into an editor and gives you some IDE-like functionality for Haskell programs, including the ability to get the type of an arbitrary expression, including a local definition.

In Emacs, you would use C-c C-t to find the type of an expression.

If you're not using Emacs or Vim, you could probably wrap ghc-mod as a GHCi extension or something, but I think that would be somewhat awkward. I can't imagine a good way to do it without having an editor-like UI. However, ghc-mod itself is just a standalone command-line tool, so it's easy to work with. If you can think of a good user interface for it that's independent of an existing text editor, go for it!

Of course, if you're not using Emacs or Vim, you probably should :P.

Upvotes: 5

Related Questions