Using the GHC API: pretty printing with qualified names

Claus Reinke claus.reinke at
Wed Nov 17 17:25:29 EST 2010

> Hello, I'm the maintainer for EclipseFP, which involves using the scion
> library and the GHC API to provide IDE functionality. I have a little 
> issue
> that no doubt stems from me not understanding the GHC API well, and I 
> would
> be grateful for any light on the matter.

A meta-comment: the GHC API is much easier to grasp if
you have a client at hand. In the case of IDE-like tools, my
recommendation would be to look at the GHCi source -
there is a wide range of overlap between what makes
sense for GHCi and what makes sense for an IDE (think
of GHCi as a commandline IDE;-).

For your particular question: GHCi provides :info, which
does manage to get (rough) source info for plain unqualified
names, within a session context. So, starting from the source
of the :info implementation might give you some hints (the
command itself also helps you to find the GHC API source
in question).

Prelude> :set -package ghc
Prelude> :info GHC.getName
class Name.NamedThing a where
  Name.getName :: a -> Name.Name
        -- Defined in Name

Prelude> :info Monad
class Monad m where
  (>>=) :: m a -> (a -> m b) -> m b
  (>>) :: m a -> m b -> m b
  return :: a -> m a
  fail :: String -> m a
        -- Defined in GHC.Base
instance Monad Maybe -- Defined in Data.Maybe
instance Monad [] -- Defined in GHC.Base
instance Monad IO -- Defined in GHC.Base

Prelude> :info mapM
mapM :: Monad m => (a -> m b) -> [a] -> m [b]
        -- Defined in Control.Monad

Of course, those sources and GHCi commands aren't
perfect (some of the code might still pre-date the API),
and batch-processing an AST is different from an
interactive read-eval-print loop, but some of the API
was abstracted from GHCi and GHCi always needs to
build with the current API version.

Another side note: as you can see from the examples,
you may not always have any source code to point to,
in a typical GHC installation. That is why haskellmode
for Vim links to the haddocks for those ids (somewhat
oddly, the haddocks might come with HTML-ified sources,
even when the original source is not installed..).

Hope this helps,

More information about the Glasgow-haskell-users mailing list