我有这个代码:
class SymbolSet tpe where
data Symbol tpe :: *
data SSet tpe where
Identity :: tpe -> SSet tpe
And :: SSet tpe -> Symbol tpe -> SSet tpe
class HasElem a b where
instance (SymbolSet tpe) => HasElem (And (Identity tpe) s) s
instance (HasElem sset s) => HasElem (And sset s) s
正在 GHC-7.4 中编译。然而,在迁移到 GHC-7.6 时,它开始出现编译错误:
'And' of tpe `forall tpe. tpe -> Symbol * tpe -> SSet tpe' is not promotable
在深入研究文档时,我发现 GHC-7.6 中的“数据类型提升”页面添加了一个新条款。与 GHC-7.4
We do not promote datatypes whose constructors are kind polymorphic, involve constraints, or use existential quantification.
我的问题是:
- 不推广此类构造函数的理由是什么?
- 正确的做法是什么?
最佳答案
您没有说明您使用的是哪个版本的 GHC 7.6,也没有包括您使用的扩展,所以我猜测了一下。
This ticket似乎回答了你的问题1,尽管我自己并不完全理解这个问题。在您的特定示例中,我认为 SSet
不可提升,因为它的参数之一 (Symbol tpe
) 是一个关联类型,它带来了 SymbolSet
约束。
如果我将 Symbol
移出类,我们会得到类型提升,但是现在我们会遇到类型不匹配错误:
{-# LANGUAGE DataKinds , TypeFamilies , GADTs , MultiParamTypeClasses #-}
class SymbolSet tpe where
-- data Symbol tpe :: *
data Symbol tpe :: *
-- ...
我可以通过向 HasElem
添加类型签名来编译整个 shebang:
{-# LANGUAGE DataKinds , TypeFamilies , GADTs , MultiParamTypeClasses, FlexibleInstances #-}
class SymbolSet tpe where
-- MOVED OUT OF CLASS:
data Symbol tpe :: *
data SSet tpe where
Identity :: tpe -> SSet tpe
And :: SSet tpe -> Symbol tpe -> SSet tpe
-- ADDED KIND SIGNATURES:
class HasElem (a :: SSet *) (b :: Symbol *) where
instance (SymbolSet tpe) => HasElem (And (Identity tpe) s) s
instance (HasElem sset s) => HasElem (And sset s) s
我不太理解你的代码,所以这可能不适合你。
关于haskell - GHC-7.6 中的数据构造函数升级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19875636/