Can GHCi tell me the type of a local Haskell function?

旧巷老猫 提交于 2019-12-19 05:23:52

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/2128542/can-ghci-tell-me-the-type-of-a-local-haskell-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!