haskell 有没有办法在do / where / let块中打印出变量的类型?

z4bn682m  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(108)

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

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

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

6l7fqoea

6l7fqoea1#

您可以通过给出一个适当的错误类型注解并检查错误消息来诱导出这些信息。

*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...

字符串

jm2pwxwz

jm2pwxwz2#

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>

相关问题