Can GHCi tell me the type of a local Haskell funct

2019-02-16 09:56发布

Is it possible to query the ghci for the type it inferred for a function inside another function?

3条回答
在下西门庆
2楼-- · 2019-02-16 10:31

This is a quick and ugly hack, but what I usually do is just use the function in the wrong way and read the error message:

inc x = x + 1
  where
    f (y, z) = y + z
    g = f :: Char

GHCi output:

Couldn't match expected type `Char'
       against inferred type `(t, t) -> t'
In the expression: f :: Char

Although this leaves out the context Num t =>, this usually does provide me with enough information to continue.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-16 10:42

You might try doing it by setting a breakpoint on it, so the function is in scope from the debugger.

Also I think that EclipseFP can tell you the types of things when you mouse over them, at least some of the time.

查看更多
该账号已被封号
4楼-- · 2019-02-16 10:46

With GHC 7.8+, just append `asTypeOf` _ to the expression.

See Find out the type of an expression/function with typed holes for a detailed explanation.

查看更多
登录 后发表回答