Here we demonstrate calling [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:parseModule parseModule], [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:typecheckModule typecheckModule], [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:desugarModule desugarModule], [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:getNamesInScope getNamesInScope], and [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:getModuleGraph getModuleGraph]. This works for haskell-platform, ghc-7.0.3. It also demonstrates how to enable some extensions.

+

Here we demonstrate calling [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:parseModule parseModule], [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:typecheckModule typecheckModule], [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:desugarModule desugarModule], [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:getNamesInScope getNamesInScope], and [http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/GHC.html#v:getModuleGraph getModuleGraph]. This works for haskell-platform, ghc-7.0.3 to ghc-7.6.x. It also demonstrates how to enable some extensions.

<haskell>

<haskell>

Line 57:

Line 57:

--invoke: ghci -package ghc A.hs

--invoke: ghci -package ghc A.hs

+

{-# LANGUAGE CPP #-}

import GHC

import GHC

import Outputable

import Outputable

−

+

import GHC.Paths ( libdir )

import GHC.Paths ( libdir )

--GHC.Paths is available via cabal install ghc-paths

--GHC.Paths is available via cabal install ghc-paths

−

+

import DynFlags

import DynFlags

targetFile = "B.hs"

targetFile = "B.hs"

−

+

main = do

main = do

res <- example

res <- example

+

#if __GLASGOW_HASKELL__ > 704

+

putStrLn $ showSDoc tracingDynFlags ( ppr res )

+

#else

putStrLn $ showSDoc ( ppr res )

putStrLn $ showSDoc ( ppr res )

−

+

#endif

+

example =

example =

+

#if __GLASGOW_HASKELL__ > 704

+

defaultErrorHandler defaultFatalMessager defaultFlushOut $ do

+

#else

defaultErrorHandler defaultLogAction $ do

defaultErrorHandler defaultLogAction $ do

+

#endif

runGhc (Just libdir) $ do

runGhc (Just libdir) $ do

dflags <- getSessionDynFlags

dflags <- getSessionDynFlags

Line 87:

Line 96:

n <- getNamesInScope

n <- getNamesInScope

c <- return $ coreModule d

c <- return $ coreModule d

−

+

g <- getModuleGraph

g <- getModuleGraph

mapM showModule g

mapM showModule g

Revision as of 06:54, 19 July 2013

Contents

1 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 note 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.

2 Getting Started

To use the GHC API you need GHC 6.10.1 or above and import the ghc 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. One good entry point into the docs is GHC.

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.