有什么办法在do / while / let块中打印出一个变量类型?

时间:2011-07-20 03:34:32

标签: haskell ghci

有没有办法在ghci中打印出嵌套变量的推断类型?考虑一下代码,

let f = g where
    g (x :: Int) = x

然后,查询g的类型会很好,例如:t f.g会打印Int -> Int

2 个答案:

答案 0 :(得分:10)

ghci调试器可以使用正确放置的断点为您打印(但您需要在模块中加载定义):

{-# LANGUAGE ScopedTypeVariables #-} 

f a = g a where
    g (x :: Int) = x

然后在ghci:

Prelude> :l tmp2.hs
[1 of 1] Compiling Main             ( tmp2.hs, interpreted )
Ok, modules loaded: Main.
*Main> :b 3 9
Breakpoint 0 activated at tmp2.hs:3:7-9
*Main> f undefined
Stopped at tmp2.hs:3:7-9
_result :: Int = _
a :: Int = _
g :: Int -> Int = _
[tmp2.hs:3:7-9] *Main>

答案 1 :(得分:9)

您可以通过提供适当错误的类型注释并检查错误消息来哄骗此信息。

*Main> let f = g where g::a; g (x::Int) = x

<interactive>:1:23:
    Couldn't match type `a1' with `Int -> Int'
      `a1' is a rigid type variable bound by...