haskell - 如何分析使用 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: true
并cabal.project
运行cabal build
GHC 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'?
代码有什么问题,为什么它在没有分析的情况下编译,但在启用时失败?
解决方案
编译包含 TH 代码进行分析的多模块程序是一个已知问题,请参阅文档中的相关部分:
如果您有一个包含 Template Haskell 代码的多模块程序并且需要编译它以进行分析,这会导致困难,因为 GHC 无法加载分析的目标代码并在执行拼接时使用它。
作为一种解决方法,只需放入TemplateHaskell
您other-modules
的 test.cabal 中,
other-extensions: TemplateHaskell
然后使用 profiling(即使用cabal build --enable-library-profiling
)进行构建,一切都会好起来的。
有关我们为什么需要部分的更多详细信息TemplateHaskell
,other-modules
请参阅https://github.com/haskell/cabal/issues/5961。
推荐阅读
- heroku - Heroku H10, path="/", "/favicon.ico" status=503 Plotly 仪表板
- postgresql - 如何计算每 15 分钟的平均值?
- angularjs - Angular刷新缓存而不刷新浏览器
- sql - 在 SQL(MS Access) 中获取 1 到 MAX(id)+1 的简单方法
- python - 如何在 kubernetes 中使用 PySpark 读取 yml (yaml) 类型的文件
- javascript - Angular 中的条件下拉功能组件渲染
- reinforcement-learning - 如何处理强化学习中的大型和尺寸变化的观察?
- python - [IBM][CLI Driver] SQL10013N 无法加载指定的库“GSKit 错误:207”。SQLSTATE=42724 SQLCODE=-10013
- mysql - 不返回 MySQL 中的年份
- ios - 如何将 Unity v2021 中的屏幕截图捕获到 iOS 设备?