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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With