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

gat*_*ado 15 haskell ghci

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

let f = g where
    g (x :: Int) = x
Run Code Online (Sandbox Code Playgroud)

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

Ed'*_*'ka 10

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

{-# LANGUAGE ScopedTypeVariables #-} 

f a = g a where
    g (x :: Int) = x
Run Code Online (Sandbox Code Playgroud)

然后在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>
Run Code Online (Sandbox Code Playgroud)


Ant*_*ony 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...
Run Code Online (Sandbox Code Playgroud)