GHC as a library - getting output from GHCI

Pepe Iborra mnislaih at
Fri May 4 14:12:55 EDT 2007


On 04/05/2007, at 19:19, Mads Lindstrøm wrote:

> Hi Pepe
> I would have liked something cross-platform.

Take a look at the unix-compat[1] package by Bjorn Bringert, although  
it looks like it won't help you. Maybe it can be extended.

> Also, if stmt contains an error, wrapStmt will not be evaluated,  
> and the
> resulting error is written to standard output or maybe to standard
> error. Whichever way, I am back to the same problem.

What else should be the behaviour in the case stmt contains an error?
You can capture errors and stop them being written out very easily.
Take a look at this snippet (not mine, it's beschmi's code) from Shim 

load' :: FilePath -> Maybe String -> SHM (SuccessFlag, 
load' sourcefile source = do
   source' <- addTime source
   ses <- getSessionFor sourcefile
   dflags0 <- io $ GHC.getSessionDynFlags ses
   ref <- io $ MVar.newMVar []
   let dflags1 = dflags0{ log_action = logMsg ref }
   io $ GHC.setSessionDynFlags ses dflags1
   io $ GHC.setTargets ses [Target (TargetFile sourcefile Nothing)  
   loadResult <- io $ GHC.load ses LoadAllTargets
   cnotes <- io $ reverse `liftM` MVar.readMVar ref
   case loadResult of
        Succeeded -> do -- GHC takes care of setting the right context
          modq <- io $ findModuleInFile ses sourcefile
          io $ GHC.setContext ses [modq] []
          return (Succeeded,cnotes,ses)
        Failed    -> do   -- We take care of getting at least the  
          io(GHC.setContext ses [] =<< atomM (getPrelude ses))
          return (Failed,cnotes,ses)
   where atomM = liftM (:[])
         logMsg ref severity' srcSpan' style' msg' = do
           dir <- getCurrentDirectory
           logS ('\n':show ((mkLocMessage srcSpan' msg') style'))
           MVar.modifyMVar_ ref
             (\l -> return $ (CompileNote severity' srcSpan'
                                          style' msg' dir):l)

Here, DynFlags.logAction is being filled with a handler that writes  
to a MVar instead of the default handler that writes to stdout.


[1] - 
[2] -
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Glasgow-haskell-users mailing list