无法使新数据类型Hashable

时间:2014-05-28 12:18:46

标签: haskell

尝试在OS X 10.9.3

上使用ghc 7.8.2编译以下内容
import Data.Hashable (Hashable, hash)

data Edge v = Edge v v deriving (Show)

instance (Eq v) => Eq (Edge v) where
  Edge x1 x2 == Edge y1 y2 =
    x1 == y1 && x2 == y2 || x1 == y2 && x2 == y1

instance (Hashable v) => Hashable (Edge v) where
  hash (Edge x1 x2) = (hash x1) + (hash x2)

失败
Could not deduce (hashable-1.2.1.0:Data.Hashable.Class.GHashable
                    (GHC.Generics.Rep (Edge v)))
  arising from a use of ‘hashable-1.2.1.0:Data.Hashable.Class.$gdmhashWithSalt’
from the context (Hashable v)
  bound by the instance declaration at src/MinCut.hs:12:10-42
In the expression:
  hashable-1.2.1.0:Data.Hashable.Class.$gdmhashWithSalt
In an equation for ‘hashWithSalt’:
    hashWithSalt
      = hashable-1.2.1.0:Data.Hashable.Class.$gdmhashWithSalt
In the instance declaration for ‘Hashable (Edge v)’

有什么问题?

1 个答案:

答案 0 :(得分:8)

Data.Hashable的hackage文档声明Hashable的最小实现是函数hashWithSalt - 查看类型类声明(class Hashable a where)下的文档。

因此,如果您将功能更改为hashWithSalt,则一切都应该有效:

instance (Hashable v) => Hashable (Edge v) where
  hashWithSalt s (Edge x1 x2) = s + (hash x1) + (hash x2)
相关问题