How: t in ghci to access all this introspective information?

It seems impossible to introspect class type restrictions on functions and data types, etc. However, ghci seems to be doing this.

Prelude> :t show show :: (Show a) => a -> String 

So ... somehow he knows the class type constraint, since it prints it. How it's done?

+6
haskell introspection typeclass ghci
source share
2 answers

Information is stored in interface files ( module.hi ). To get it from a running program, you will need to find and read .hi files (I believe that the Hint package on Hackage does this; since ghci reads .hi files during compilation into bytecode, it has information available.

You can see that in the .hi file with ghc --show-iface module.hi .

+10
source share

Separately compiled β€œbinaries” are β€œ.hi” files. They contain all the type information so that you can write code that uses them, and they contain all the definitions of type classes and all instances of the type class so that your code can use or extend them.

Thus, ghci compiles the source code into ".hi" and downloads all the dependent .hi files. This gives him complete knowledge of all types. What ghci does not need to do is return to the source of all imported modules, it only needs the ".hi" files.

+3
source share

All Articles