首页 > 解决方案 > 如何分析使用 Cabal 构建的 TemplateHaskell?

问题描述

完整项目位于https://github.com/ysangkok/cabal-profiling-issue

该项目包含由cabal init. 我现在将粘贴最有趣的源代码片段。

Main.hs我有:

newtype Wrapper = Wrapper Int

deriveConvertible ''Wrapper ''Int

TH.hs我有:

import            Data.Convertible

deriveConvertible :: TH.Name -> TH.Name -> TH.Q [TH.Dec]
deriveConvertible newType otherType = do
  Just newCon <- TH.lookupValueName (TH.nameBase newType)
  v <- TH.newName "v"

  [d|
    instance Convertible $(TH.conT newType) $(TH.conT otherType) where
      safeConvert $(TH.conP newCon [TH.varP v]) = Right $(TH.varE v)

    instance Convertible $(TH.conT otherType) $(TH.conT newType) where
      safeConvert source = Right ($(TH.conE newCon) source)

    |]

但是,如果我安装profiling: truecabal.project运行cabal buildGHC 8.6.5 和 Cabal 3.4.0.0(使用 ghcup 安装),我会得到:

    Failed to load interface for 'Data.Convertible.Base'
    Perhaps you haven't installed the profiling libraries for package 'convertible-1.1.1.0'?

代码有什么问题,为什么它在没有分析的情况下编译,但在启用时失败?

标签: haskellghcheap-profilingcabal-new

解决方案


编译包含 TH 代码进行分析的多模块程序是一个已知问题,请参阅文档中的相关部分:

如果您有一个包含 Template Haskell 代码的多模块程序并且需要编译它以进行分析,这会导致困难,因为 GHC 无法加载分析的目标代码并在执行拼接时使用它。

作为一种解决方法,只需放入TemplateHaskellother-modules的 test.cabal 中,

     other-extensions: TemplateHaskell

然后使用 profiling(即使用cabal build --enable-library-profiling)进行构建,一切都会好起来的。

有关我们为什么需要部分的更多详细信息TemplateHaskellother-modules请参阅https://github.com/haskell/cabal/issues/5961


推荐阅读