是否可以指定一个类型族的所有实例都必须满足的类型类约束?
例如,给定以下声明,我如何确保所有实例也是 Eq
的实例? :data family Channel c :: *
非常感谢,
迈克尔
最佳答案
这是你想要的?
{-# LANGUAGE FlexibleContexts, TypeFamilies, FlexibleInstances #-}
-- Data family inside a class so that we can add an extra Eq constraint
class Eq (Channel c) => MyClass c where
data Channel c :: *
-- A simple toy instance
instance MyClass Int where
data Channel Int = CI Int deriving Eq
-- A more complex instance with separate Eq instance
instance MyClass Char where
data Channel Char = CC Char
instance Eq (Channel Char) where
(CC x) == (CC y) = x == y
关于haskell - 类型族实例的类型类约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25219162/