gpt4 book ai didi

haskell - 使用 GHC API 时查找 cabal 包

转载 作者:行者123 更新时间:2023-12-04 12:23:11 27 4
gpt4 key购买 nike

我正在尝试制作一个使用 GHC API 为我检查 haskell 文件的程序。我已经获得了适用于本地文件的类型检查,但我有一个特定的 cabal 包,我也需要它可用(这个可执行文件将属于同一个包)。如何添加此导入依赖项?

我还尝试使用 ghc 命令行编译来解决这个问题,使用 ghc -package PKG-VER --make Test.hs -v但它似乎只在本地目录中查找导入。

我当前的代码如下所示:

import           Control.Exception
import Control.Monad
import Control.Monad.State
import DynFlags
import Exception
import GHC
import GHC.Paths (libdir)
typecheck :: MonadIO m => [FilePath] -> FilePath -> m ()
typecheck otherincludes fp =
liftIO . defaultErrorHandler defaultLogAction . runGhc (Just libdir) $ do
dynflags <- getSessionDynFlags
void $ setSessionDynFlags dynflags { includePaths = otherIncludes ++ includePaths dynflags }
target <- guessTarget fp Nothing
setTargets [target]
void $ load LoadAllTargets
deps <- depanal [] False
forM_ deps $ \ms -> parseModule ms >>= typecheckModule

最佳答案

我设法让您的代码读取并自行检查如下:

package Test where
import Control.Exception
import Control.Monad
import Control.Monad.State
import DynFlags
import Exception
import GHC
import GHC.Paths (libdir)
typecheck :: MonadIO m => [FilePath] -> FilePath -> m ()
typecheck otherincludes fp =
liftIO . defaultErrorHandler defaultLogAction . runGhc (Just libdir) $ do
dynflags <- getSessionDynFlags
void $ setSessionDynFlags dynflags {
includePaths = otherincludes ++ includePaths dynflags,
packageFlags = [ExposePackage "ghc"]} }
target <- guessTarget fp Nothing
setTargets [target]
void $ load LoadAllTargets
deps <- depanal [] False
forM_ deps $ \ms -> parseModule ms >>= typecheckModule

这是它在 ghci 中的运行方式:
$ ghci Test.hs -package ghc
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package array-0.4.0.0 ... linking ... done.
Loading package deepseq-1.3.0.0 ... linking ... done.
Loading package containers-0.4.2.1 ... linking ... done.
Loading package filepath-1.3.0.0 ... linking ... done.
Loading package old-locale-1.0.0.4 ... linking ... done.
Loading package old-time-1.1.0.0 ... linking ... done.
Loading package bytestring-0.9.2.1 ... linking ... done.
Loading package unix-2.5.1.0 ... linking ... done.
Loading package directory-1.1.0.2 ... linking ... done.
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package process-1.1.0.1 ... linking ... done.
Loading package Cabal-1.14.0 ... linking ... done.
Loading package binary-0.5.1.0 ... linking ... done.
Loading package bin-package-db-0.0.0.0 ... linking ... done.
Loading package hoopl-3.8.7.3 ... linking ... done.
Loading package hpc-0.5.1.1 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package ghc-7.4.1 ... linking ... done.
Ok, modules loaded: Test.
Prelude Test> typecheck [] "Test.hs"
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package mtl-2.1.1 ... linking ... done.
Prelude Test>

所以诀窍似乎是通过 dynflags 中的暴露包。 setSessionDynFlags 的参数.见 DynFlags一些文档的模块。

关于haskell - 使用 GHC API 时查找 cabal 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246504/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com