haskell - GHC-7.6 中的数据构造函数升级

标签 haskell constructor ghc

我有这个代码:

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.

我的问题是:

  1. 不推广此类构造函数的理由是什么?
  2. 正确的做法是什么?

最佳答案

您没有说明您使用的是哪个版本的 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/

相关文章:

haskell - HXT 获取第一个元素 : refactor weird arrow

c++ - 多线程深度复制

c++ - 继承:没有合适的默认构造函数可用

java - 具有公共(public)构造函数而不是 protected 构造函数的抽象类有什么意义吗?

templates - 如何在 Yesod 中导入莎士比亚模板?

haskell - "User error"使用 getArgs 时模式匹配失败

Haskell初学者: "No instance for...arising from..." error

Haskell 对可变数量字符串的列表理解

haskell - 理解单态与多态 Core 表达式

haskell - 在 haskell 中定义一个新的 monad 不会引发 Applicative 的实例