Difference between revisions of "GHC/As a library"

From HaskellWiki
< GHC
Jump to navigation Jump to search
m
(more docs)
Line 12: Line 12:
 
== Getting Started ==
 
== Getting Started ==
   
To use the GHC API you of course need GHC 6.10.1 or above and import the <tt>ghc</tt> package.
+
To use the GHC API you need GHC 6.10.1 or above and import the <tt>ghc</tt> package.
 
<pre>
 
<pre>
 
ghc -package ghc my_program.hs
 
ghc -package ghc my_program.hs
 
</pre>
 
</pre>
  +
In most cases you probably also want to use the [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/ghc-paths ghc-paths package].
   
 
Most of the common functionality is provided by the <tt>GHC</tt> module, but occasionally you may have to import other modules. See the GHC's haddock documentation for a list of these modules.
 
Most of the common functionality is provided by the <tt>GHC</tt> module, but occasionally you may have to import other modules. See the GHC's haddock documentation for a list of these modules.
Line 22: Line 23:
   
 
== A Simple Example ==
 
== A Simple Example ==
  +
  +
The following little program essentially does what <tt>ghc --make</tt> does.
   
 
<haskell>
 
<haskell>
 
import GHC
 
import GHC
  +
import GHC.Paths ( libdir )
 
import DynFlags ( defaultDynFlags )
 
import DynFlags ( defaultDynFlags )
   
 
main =
 
main =
 
defaultErrorHandler defaultDynFlags $ do
 
defaultErrorHandler defaultDynFlags $ do
runGhc (Just top_dir) $ do
+
runGhc (Just libdir) $ do
 
dflags <- getSessionDynFlags
 
dflags <- getSessionDynFlags
 
setSessionDynFlags dflags
 
setSessionDynFlags dflags
Line 36: Line 40:
 
load LoadAllTargets
 
load LoadAllTargets
 
</haskell>
 
</haskell>
  +
  +
The outermost function, <haskell>defaultErrorHandler</haskell>, sets up proper exception handlers and prints an error message and exits with exit code 1 if it encounters one of these exceptions.
  +
  +
Most of GHC's high-level API requires access to a current session. Therefore, these functions require to be called inside a monad that is an instance of the <haskell>GhcMonad</haskell> typeclass. Two default implementations of this typeclass are <haskell>Ghc</haskell> and <haskell>GhcT</haskell>. In the above example we used the <haskell>Ghc</haskell> monad since we don't need to track any extra state.
  +
  +
The argument to <haskell>runGhc</haskell> is a bit tricky. GHC needs this to find its libraries, so the argument must refer to the directory that is printed by <tt>ghc --print-libdir</tt> for the ''same'' version of GHC that the program is being compiled with. Above we therefore use the <tt>ghc-paths</tt> package which provides this for us.

Revision as of 09:14, 9 October 2008

For instructions on the GHC API with GHC 6.8 or older please refer to GHC/As a library (up to 6.8)

Introduction

GHC's functionality can be useful for more things than just compiling Haskell programs. Important use cases are programs that analyse (and perhaps transform) Haskell code. Others include loading Haskell code dynamically in a GHCi-like manner. For this reason, a lot of GHC's features can be accessed by programs which import the ghc package.

The instructions on this page concern the API of GHC 6.10.1 and above. Please not that the GHC API is still in flux and may change quite significantly between major releases while we (the GHC team) provide new features or simplify certain aspects.


Getting Started

To use the GHC API you need GHC 6.10.1 or above and import the ghc package.

ghc -package ghc my_program.hs

In most cases you probably also want to use the ghc-paths package.

Most of the common functionality is provided by the GHC module, but occasionally you may have to import other modules. See the GHC's haddock documentation for a list of these modules.

FIXME: link to haddock docs

A Simple Example

The following little program essentially does what ghc --make does.

import GHC
import GHC.Paths ( libdir )
import DynFlags ( defaultDynFlags )

main = 
    defaultErrorHandler defaultDynFlags $ do
      runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags dflags
        target <- guessTarget "test_main.hs" Nothing
        setTarget [target]
        load LoadAllTargets
The outermost function,
defaultErrorHandler
, sets up proper exception handlers and prints an error message and exits with exit code 1 if it encounters one of these exceptions. Most of GHC's high-level API requires access to a current session. Therefore, these functions require to be called inside a monad that is an instance of the
GhcMonad
typeclass. Two default implementations of this typeclass are
Ghc
and
GhcT
. In the above example we used the
Ghc
monad since we don't need to track any extra state. The argument to
runGhc
is a bit tricky. GHC needs this to find its libraries, so the argument must refer to the directory that is printed by ghc --print-libdir for the same version of GHC that the program is being compiled with. Above we therefore use the ghc-paths package which provides this for us.