如何使用Haskell Ghc API动态加载Haskell源代码?当导入一些第三方依赖时

时间:2019-01-10 16:53:02

标签: haskell ghc

我有一个小项目,该项目生成一些依赖于命令行参数的源代码,然后执行生成的haskell代码。

我尝试使用GHC API runGhc加载并执行源文件。我发现了一些示例,所有源代码文件都不导入第三方程序包。

但是我的源代码导入了一些程序包,并引发了Could not find module异常。

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP               #-}
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE LambdaCase        #-}
{-# LANGUAGE MultiWayIf        #-}
{-# LANGUAGE NamedFieldPuns    #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE RecordWildCards   #-}
{-# LANGUAGE TupleSections     #-}
{-# LANGUAGE ViewPatterns      #-}



import GHC
import GHC.Paths ( libdir )
import DynFlags

main =
    defaultErrorHandler defaultFatalMessager defaultFlushOut $ do
      runGhc (Just libdir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags $ dflags{
          hscTarget = HscInterpreted
          , ghcLink = LinkInMemory

                                   }
        target <- guessTarget "Demo.hs" Nothing
        setTargets [target]
        load LoadAllTargets

我的Demo.hs就像

{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE GADTs             #-}
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}


module Demo where
import qualified Data.ByteString.Lazy.Internal as BSL
import qualified Data.Aeson as DA

buildRequestFromJson ::
                       BSL.ByteString -> BSL.ByteString -> Maybe EchoRequest
buildRequestFromJson payload = (DA.decode payload)

错误是

    Could not find module ‘Data.Aeson’
    Perhaps you meant Data.Version (from base-4.12.0.0)
    Use -v to see a list of the files searched for.
    |
 13 | import qualified Data.Aeson as DA
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我正在寻找如何执行源代码。我无法使用hintplugins软件包。而且我想知道如何处理。

0 个答案:

没有答案