开发者

How does :t in ghci access all that introspective information?

It appears to be impossible to introspect type class constraints on functions and data types and such. However, ghci appears to do it.

开发者_JAVA技巧
Prelude> :t show
show :: (Show a) => a -> String

So... somehow it knows the type class constraint since it's printing it out. How is it doing that?


The information is kept in interface files (module.hi). To get at it from in a running program you would need to find and read the .hi files (the Hint package on Hackage does this, I believe); since ghci reads the .hi files in the course of compiling to bytecode, it has that information conveniently available.

You can see what's in a .hi file with ghc --show-iface module.hi.


The separately compiled "binaries" are the ".hi" files. These contain all the type information so that you can write code that uses them, and they contain all the type class definitions and all the type class instances so that your code can use or extend them.

Thus ghci compile source to ".hi" and loads all the dependent ".hi" files. This gives it perfect knowledge of all the types. What ghci does not need to do is go back to the source of all the imported modules, it only needs the ".hi" files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜