Question about Haskell AST

Christiaan Baaij christiaan.baaij at
Tue Feb 8 10:46:58 CET 2011

Does the following code help you out? (Do note it's for the GHC 6.12.* API)
It just uses the the default GHC driver, meaning you don't have to separately call the desugerar, simplifier, etc.
It's what I use for my CλaSH tool.

Cheers, Christiaan

-- External Modules
import qualified GHC.Paths

import qualified DynFlags
import qualified GHC
import qualified HscTypes

-- | Loads the given files and returns the Core Bindings
loadBindings ::
  [FilePath] ->                             -- ^ Files to load
  IO [(CoreSyn.CoreBndr, CoreSyn.CoreExpr)] -- ^ Bindings
loadBindings fileNames = do
  GHC.defaultErrorHandler DynFlags.defaultDynFlags $
    GHC.runGhc (Just GHC.Paths.libdir) $ do
      -- Some dynflags trickery.. otherwise they don't get loaded properly
      dflags <- GHC.getSessionDynFlags
      GHC.setSessionDynFlags dflags
      -- Run the default GHC driver
      coreModules <- mapM GHC.compileToCoreSimplified fileNames
      -- Extract and flatten bindings from the modules
      let bindings = concatMap (CoreSyn.flattenBinds . HscTypes.cm_binds) coreModules
      return bindings

On Jan 10, 2011, at 6:21 PM, Jane Ren wrote:

> Hi, 
> I need to be able to take a piece of Haskell source code and get an simplified, typed, intermediate representation of the AST, which means I need to use compiler/coreSyn/CoreSyn.lhs
> So I'm first trying to get the desguaredModule of the source code with
>        ...
>        modSum <- getModSummary $ mkModuleName "..."
>        p <- parseModule modSum
>        t <- typecheckModule p
>        d <- desugarModule t
> Now I'm really stuck on figuring out how to connect the variable d of type desugaredModule to compiler/coreSyn/CoreSyn.lhs to get Expr patterns like App, Let, Case, etc.
> Also, is it correct to get the deguaredModule first?  At least CoreSyn.lhs seems to suggest this.
> Any suggestions would be greatly apprecia
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at

More information about the Glasgow-haskell-users mailing list