【发布时间】:2020-01-18 02:13:03
【问题描述】:
我正在写一个函数deriveMyTypeClass ::Q [Dec]
在哪里,给定类型的名称,我正在遍历它的构造函数并实例化我根据结构编写的类型类。
我的类型类看起来像:
class MyTypeclass a where
type Foo a
f :: a -> a -> Foo a
g :: Foo a -> a -> a
在我的函数deriveMyTypeclass 中,我应该将什么声明传递给InstanceD 以满足类型族声明。会是TySynInstD吗?
现在我有一些类似的东西:
deriveMyTypeclass :: Name -> Q [Dec]
deriveMyTypeclass tyName = do
... blah blah blah reify tyName ...
return $ [
InstanceD Nothing []
(AppT (ConT ''MyTypeclass) (ConT tyName)
[ -- declarations go here
, TySynInstD $ ?????
, FunD 'f ...
, FunD 'g ...
]
]
任何帮助表示赞赏:)
附:我正在使用版本 template-haskell-2.14.0.0 但如果您的解决方案需要 2.15,那么我会考虑升级。
【问题讨论】:
-
您是否尝试过手写一个实例并将其粘贴到
[d|以查看它如何转换为 TH? -
我没有,但这是个好主意。谢谢!
标签: haskell metaprogramming template-haskell type-families