Annotating an AST with type checking / source line number info

Ranjit Jhala jhala at cs.ucsd.edu
Wed Mar 13 21:43:42 CET 2013


Hi Craig -- 

you might look at: 

  http://goto.ucsd.edu/~rjhala/llvm-haskell/doc/html/liquidtypes/Language-Haskell-Liquid-GhcInterface.html#v:getGhcInfo
  http://goto.ucsd.edu/~rjhala/llvm-haskell/doc/html/liquidtypes/src/Language-Haskell-Liquid-GhcInterface.html#getGhcModGuts1

for an example of how how to turn a `FilePath` into a GHC `[CoreBind]`.

  http://www.haskell.org/ghc/docs/latest/html/libraries/ghc/CoreSyn.html#t:CoreBind

The latter (i.e. `CoreBind` and related `Expr` and friends) are GHC's 
"core" representation of programs AFTER some amount of simplification. 
(This may be too "late" for your purposes, i.e. you may want something prior
to simplification, but look at this chain

       mod_guts   <- coreModule `fmap` (desugarModule =<< typecheckModule =<< parseModule modSummary)

to get a sense of the different steps...

Hope this helps!


Ranjit.

On Mar 13, 2013, at 1:32 PM, Craig Innes wrote:

> Hi there,
> 
> I am not hugely familiar with compilers or the particulars of GHC, but am interested in creating a few programs which manipulate Haskell source code in particular ways. Two things I would like to be able to do are:
> 
> - Swap every occurrence of a particular type for a different / dummy value of that type.
> 
> - Find the line / column number of every place a value of a particular type is used
> 
> From compiling Haskell programs, it seems clear that GHC performs type checking, and when I get a compile error it is able extract line and column information about where I had the error, so it appears as if GHC is annotating the source with this information as it compiles.
> 
> I am struggling to find a lot in the way of learning materials for GHC, but from trawling through the API documentation (I am using GHC version 7.4.2), it seems like I can generate an abstract syntax tree via the method:
> 
> parseModule :: GhcMonad m => ModSummary -> m ParsedModule
> 
> My question is this: what combination of functions do I have to use to get not only an AST for my source, but an AST annotated with typing information and line / column number annotations for values within it?
> 
> Also, as I am a bit of a newbie to this whole GHC API thing, any pointers to resources to learn more about it would be enormously appreciated.
> 
> Thanks,
> 
> Craig 
> 
> 
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users




More information about the Glasgow-haskell-users mailing list