需要一个使用 GHC 解析和类型检查 Haskell 的教程

发布于 2024-08-20 21:02:17 字数 306 浏览 4 评论 0原文

我正在开发一个分析 Haskell 代码的项目。我决定使用 GHC 来解析源代码并推断类型,而不是编写自己的代码来执行此操作。现在,我正在努力阅读 Haddock 文档,但进展缓慢。有谁知道一个好的教程吗?

编辑:澄清一下,我不是在寻找像 hlint 这样的东西。我正在编写自己的工具来分析 Haskell 代码的运行时特征,所以就像我正在编写不同的 hlint。我正在寻找的基本上是 wiki 页面的扩展 GHC 作为库

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱她像谁 2024-08-27 21:02:17

啊!在以下位置找到了更好的文档入口点:
http://www.haskell.org /ghc/docs/latest/html/libraries/ghc-6.12.1/GHC.html

我更新了 wikipage 与此示例:

这里我们演示调用 parseModule、typecheckModule、desugarModule、getNamesInScope 和 getModuleGraph。这适用于 haskell 平台、ghc-6.12.1。

bug:libdir 是硬编码的。请参阅上面的 ghc 路径。

--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!"

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!"
情归归情 2024-08-27 21:02:17

亚当,这是相当艰难的雪橇。自 2006 年推出以来,GHC API 的记录一直有些不足。我建议尝试找到一些使用 GHC API 编写的小应用程序。正确的提问地点可能是 GHC 用户邮件列表

ghctags 就是这样一个程序,它随 GHC 源代码树一起提供。我写了最初的版本,但我不能推荐它——代码上有太多的足迹,我无法再遵循它。我能说的最多的是,虽然它很难理解,但它至少并且难以理解——比所有 GHC 简单得多。

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.

不知在何时 2024-08-27 21:02:17

如果解析是最重要的事情,我推荐 haskell-src-exts。它解析所有的Haskell98,一大堆扩展,并且非常容易使用。

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.

围归者 2024-08-27 21:02:17

Haskell wikiGHC 文档可能包含您要查找的内容。您可能感兴趣的另一个工具是 hlint,用于检查语法和其他内容关于你的源代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文