Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 475
amindfv Avatar asked Feb 22 '13 22:02

amindfv


2 Answers

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
like image 163
jberryman Avatar answered Nov 09 '22 09:11

jberryman


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.

like image 5
Tikhon Jelvis Avatar answered Nov 09 '22 08:11

Tikhon Jelvis