【问题标题】:Data.Vector.Binary prevents encoding of lists. Is this a GHC bug?Data.Vector.Binary 防止对列表进行编码。这是 GHC 错误吗?
【发布时间】:2012-07-24 12:04:59
【问题描述】:

在 GHCI 中,如果你运行:

import Data.Binary
encode [1]

一切顺利。但是,如果您随后运行:

import Data.Vector.Binary
encode [1]

您收到一条错误消息:

二进制 [t0] 的重叠实例 由使用`encode'引起的

匹配实例:

 instance (Data.Vector.Generic.Base.Vector v a, Binary a) =>
          Binary (v a)
   -- Defined in `Data.Vector.Binary'

 instance Binary a => Binary [a] -- Defined in `Data.Binary'

在表达式中:编码[1]

在 `it' 的等式中:it = encode [1]

GHC 似乎认为我的列表是向量类型而不是列表。这是为什么呢?

【问题讨论】:

    标签: haskell


    【解决方案1】:

    如果头部匹配,类型类匹配不会回溯。实例头部

     Binary (v a)
    

    说“对于任何类型 v: * -> * 和类型 a:* 匹配 Binary (v a)。”因此[] a 匹配。 Data.Vector 中的实例可能比它应该的更通用。 OverlappingInstances 扩展允许您通过选择“最具体的实例”来解决这种情况,在这种情况下显然是[a]

    IMO,Haskell 应该提供一种控制实例范围的机制,以帮助防止此类问题,但它没有。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      • 2013-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      • 1970-01-01
      相关资源
      最近更新 更多