[Haskell-cafe] segfault when using ghc api

Daniel Schüssler anotheraddress at gmx.de
Mon Feb 28 13:01:42 CET 2011


Hi,

On 2011-February-27 Sunday 16:20:06 Edward Amsden wrote:
> Secondly,
> 
> I'd like to get to a GHC session that just has, say, Prelude in scope
> so I can use dynCompileExpr with "show" etc, but I cannot figure out
> how to bring it into scope. The closest I got was to get GHC
> complaining that it was a package module.

I don't know if setContext is the proper/best way to do it, but it seems to 
work:

import GHC
import GHC.Paths ( libdir )
import DynFlags ( defaultDynFlags )
import Module(stringToPackageId, mkModuleName)
import Data.Dynamic(fromDynamic)
import System.Environment
 
evalString s = 
    defaultErrorHandler defaultDynFlags $ do
      runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags dflags
        setContext [] [ (mkModule (stringToPackageId "base") (mkModuleName 
"Prelude")
                        ,Nothing) ]

        dyn <- dynCompileExpr s
        return $ fromDynamic dyn

main = do
 (s:_) <- getArgs
 e <- evalString s
 putStrLn $ maybe "oops" id e





More information about the Haskell-Cafe mailing list