15

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

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

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

4

2 回答 2

11

您可以通过给出适当的错误类型注释并检查错误消息来哄骗这些信息。

*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...
于 2011-07-20T07:38:56.860 回答
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>
于 2011-07-20T15:37:13.103 回答