Difference between revisions of "GHC/As a library"

From HaskellWiki
< GHC
Jump to navigation Jump to search
(use correct dflags)
Line 71: Line 71:
 
let dflags' = foldl xopt_set dflags
 
let dflags' = foldl xopt_set dflags
 
[Opt_Cpp, Opt_ImplicitPrelude, Opt_MagicHash]
 
[Opt_Cpp, Opt_ImplicitPrelude, Opt_MagicHash]
setSessionDynFlags dflags
+
setSessionDynFlags dflags'
 
target <- guessTarget targetFile Nothing
 
target <- guessTarget targetFile Nothing
 
setTargets [target]
 
setTargets [target]

Revision as of 17:46, 4 October 2011

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


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. One good entry point into the docs is GHC.

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
        setTargets [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.

Another example

Here we demonstrate calling parseModule, typecheckModule, desugarModule, getNamesInScope, and getModuleGraph. This works for haskell-platform, ghc-7.0.3. It also demonstrates how to enable some extensions.

--A.hs
--invoke: ghci -package ghc A.hs

import GHC
import Outputable

import GHC.Paths ( libdir )
--GHC.Paths is available via cabal install ghc-paths

import DynFlags
targetFile = "B.hs"

main = do
   res <- example
   putStrLn $ showSDoc ( ppr res )

example = 
    defaultErrorHandler defaultDynFlags $ do
      runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        let dflags' = foldl xopt_set dflags
                            [Opt_Cpp, Opt_ImplicitPrelude, Opt_MagicHash]
        setSessionDynFlags dflags'
        target <- guessTarget targetFile Nothing
        setTargets [target]
        load LoadAllTargets
        modSum <- getModSummary $ mkModuleName "B"
        p <- parseModule modSum
        t <- typecheckModule p
        d <- desugarModule t
        l <- loadModule d
        n <- getNamesInScope
        c <- return $ coreModule d
        
        g <- getModuleGraph
        mapM showModule g     
        return $ (parsedSource d,"/n-----/n",  typecheckedSource d)
--B.hs
module B where

main = print "Hello, World!"

Links