Need a tutorial for using GHC to parse and typecheck Haskell
Asked Answered
C

4

25

I'm working on a project for analyzing Haskell code. I decided to use GHC to parse the source and infer types rather than write my own code to do that. Right now, I'm slogging through the Haddock docs, but it's slow going. Does anyone know of a good tutorial?

EDIT: To clarify, I'm not looking for something like hlint. I'm writing my own tool to analyze the runtime characteristics of Haskell code, so it's like I'm writing a different hlint. What I'm looking for is basically an expansion of the wiki page GHC As a library.

Carce answered 11/2, 2010 at 7:52 Comment(3)
This is a really important need!Jere
@Adam Have you found the answer yet. I am also working on a similar application.Turning
Has there been any progress on this? It's a 3 year old question but a tutorial like this is exactly what I'm looking for.Arrear
J
9

Adam, this is pretty tough sledding. Ever since its launch in 2006, the GHC API has been somewhat underdocumented. What I would recommend is to try to find some small applications that have been written using the GHC API. The right place to ask is probably the GHC users' mailing list.

One such program is ghctags, which ships with the GHC source tree. I wrote the original version, but I can't recommend it—there are so many footprints on the code that I can no longer follow it. The best I can say is that although it's hard to follow, it's at least small and hard to follow—much simpler than all of GHC.

Jere answered 12/2, 2010 at 2:5 Comment(0)
H
10

Ah! found a much better entry point into the docs at: http://www.haskell.org/ghc/docs/latest/html/libraries/ghc-6.12.1/GHC.html

I updated the wikipage with this example:

Here we demonstrate calling parseModule, typecheckModule, desugarModule, getNamesInScope, and getModuleGraph. This works for haskell-platform, ghc-6.12.1.

bugs: libdir is hardcoded. See ghc-paths above.

--A.hs
--invoke: ghci -package ghc A.hs
import GHC
import Outputable

--import GHC.Paths ( libdir )
import DynFlags ( defaultDynFlags )
libdir = "/usr/local/lib/ghc-6.12.1"
targetFile = "B.hs"

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

example = 
    defaultErrorHandler defaultDynFlags $ do
      runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        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!"
Heterozygote answered 20/2, 2010 at 2:52 Comment(1)
Do you have any example how to proceed further? I mean how to compile the CORE into binaries?Wreckfish
J
9

Adam, this is pretty tough sledding. Ever since its launch in 2006, the GHC API has been somewhat underdocumented. What I would recommend is to try to find some small applications that have been written using the GHC API. The right place to ask is probably the GHC users' mailing list.

One such program is ghctags, which ships with the GHC source tree. I wrote the original version, but I can't recommend it—there are so many footprints on the code that I can no longer follow it. The best I can say is that although it's hard to follow, it's at least small and hard to follow—much simpler than all of GHC.

Jere answered 12/2, 2010 at 2:5 Comment(0)
P
2

If parsing is the most important thing, I recommend haskell-src-exts. It parses all of Haskell98, a whole pile of extensions and is very easy to use.

Peursem answered 20/2, 2010 at 4:59 Comment(0)
I
-1

The Haskell wiki and GHC documentation probably has what you are looking for if you search for the articles. Also a tool you might be interested in is hlint, for checking the syntax and other things about your source code.

Inhabiter answered 11/2, 2010 at 8:52 Comment(1)
I've already looked there. "GHC as a library" (haskell.org/haskellwiki/GHC/As_a_library) is exactly the topic I need information on, but the page is hopelessly sparse. hlint does not do what I want.Carce

© 2022 - 2024 — McMap. All rights reserved.