查找本地函数的推断类型

时间:2013-02-22 22:38:03

标签: haskell type-inference ghci

ghci(或ghc)中是否有办法找到本地函数的推断类型?

E.g。如果我有一个功能

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

在ghci中是否有类似:t的方式来查看f'的推断类型是什么?

3 个答案:

答案 0 :(得分:8)

确实有,我学到了感谢hammar的精彩回答here。这是简短版本:

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

答案 1 :(得分:5)

我不知道GHCi是怎么做的。

但是,如果您使用的是Emacs或Vim等编辑器,则可以尝试ghc-mod。这是一个外部工具,可插入编辑器,为Haskell程序提供类似IDE的功能,包括获取任意表达式类型的能力,包括本地定义。

在Emacs中,您可以使用C-c C-t来查找表达式的类型。

如果你没有使用Emacs或Vim,你可以将ghc-mod包装为GHCi扩展或其他东西,但我认为这有点尴尬。没有类似编辑器的用户界面,我无法想象一个好方法。但是,ghc-mod本身只是一个独立的命令行工具,因此很容易使用。如果您能想到一个独立于现有文本编辑器的良好用户界面,那就去吧!

当然,如果您不使用Emacs或Vim,您可能应该:P。

答案 2 :(得分:0)

尝试hdevtools,它非常快速且易于使用,但只有Vim集成。

相关问题