Doing this imports the module GHC from the package ghc, which comes with GHC 6.5 and subsequent. The module GHC exports the "GHC API", which is still in a state of flux. Currently it's not even Haddock-documented. You can see the source code (which is somewhat documented) here
http://darcs.haskell.org/ghc/compiler/main/GHC.hs

Here's an example main program that does it Media:Main.hs (good for GHC 6.6). You need to manually change the value of

myGhcRoot

to point to your GHC directory.

To compile Media:Main.hs, you have to turn on the flag "-package ghc", e.g.

ghc -package ghc Main.hs

2 Using the GHC library from inside GHCi

This works, to some extent. However, beware about loading object code, because there is only a single linker symbol table in the runtime, so GHCi will be sharing the symbol table with the new GHC session.

The RunResult of this is RunOk [n] where n is bound to 4. So if we subsequently enter

runStmt session n

the RunResult is not updated but 4 will be printed

CompileExpr, DynCompileExpr

Get module dependency graph

GHC.getModuleGraph :: Session -> IO ModuleGraph

Get bindings

GHC.getBindings :: Session -> IO [TyThing]

Error messages can be routed through a callback mechanism.

setSessionDynFlags ::

4 Interactive mode example

The file Media:Interactive.hs (also requires Media:MyPrelude.hs) serves as an example for using GHC as a library in interactive mode. It also shows how to replace some of the standard prelude functions with modified versions. See the comments in the code for further information.