Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 933
mentics Avatar asked Mar 29 '11 17:03

mentics


2 Answers

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.

like image 118
geekosaur Avatar answered Nov 15 '22 08:11

geekosaur


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.

like image 30
Chris Kuklewicz Avatar answered Nov 15 '22 09:11

Chris Kuklewicz