有没有什么办法在ghci
中打印出嵌套变量的推断类型?考虑代码,任何方式在do/while/let块中打印出一个变量类型?
let f = g where
g (x :: Int) = x
然后,很高兴查询g
的类型,例如, :t f.g
将打印出Int -> Int
。
有没有什么办法在ghci
中打印出嵌套变量的推断类型?考虑代码,任何方式在do/while/let块中打印出一个变量类型?
let f = g where
g (x :: Int) = x
然后,很高兴查询g
的类型,例如, :t f.g
将打印出Int -> Int
。
您可以通过给出错误的类型注释并检查错误消息来哄骗这些信息。
*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...
这绝对是更方便的解决方案。当你只有“a”时,它似乎工作得很好。然而,我确实碰巧遇到了一个特殊的情况,我在do块中有一个变量,使用范围变量,其中类型注释似乎没有任何显示。 (奇怪的是,也许我应该提交一个bug)。 – gatoatigrado
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>
我发现这比其他答案更容易,因为有时你通过强制类型来得到不同的错误,但这样你总能找出类型。 –
'g'与'f'有没有相同的类型? ':t f'。 –
这是一个玩具的例子 – gatoatigrado
顺便说一下,你是如何得到ghci接受多线声明?它从来没有为我工作。 –