我试图了解如何使用数据类型系列来隐藏构造函数。给出的一个简单的例子是一对操作要从和转换为普通对等的操作)。二进制函数的实例不编译;错误消息是
src/TypeFamilyTest.hs:66:21:Bifunctor的第一个论点应该是* -> * -> *,但是Pairs a b有善良的ghc-prim-0.4.0.0:GHC.Prim.Constraint在Bifunctor (Pairs a b)的实例声明中
尝试Bifunctor Pair where ...,我得到了另一条错误消息,列出了相同的GHC.Prim.Constraint。实例的正确参数是什么,以及如何传递上下文?
class Pairs a b where
data Vec2 a b
mkPair :: (a,b) -> Vec2 a b -- (FirstF a, SecondF a) -> a
splitPair :: Vec2 a b -> (a,b)
fstP :: Vec2 a b -> a
sndP :: Vec2 a b -> b
fstP = fst . splitPair
sndP = snd . splitPair
instance ( ) => Bifunctor (Pairs a b) where
bimap opv oph vh = mkPair (opv . fstP $ vh, oph . sndP $ vh)发布于 2016-04-15 08:33:39
类型错误告诉您一切。Pair不是数据类型的名称。它是一个类的名称。Pair a b :: Constraint so Pair :: * -> * -> Constraint.Bifunctor只能由* -> * -> *类型的数据类型实例化。
我猜你的意思可能是这样的:
newtype Pair a b = Vec2 { splitPair :: (a, b) }
fstP :: Pair a b -> a
fstP = fst . splitPair
sndP :: Pair a b -> b
sndP = snd . splitPair
instance Bifunctor Pair where
bimap f g (Vec2 (x, y)) = Vec2 (f x, g y)我不明白为什么您最初将Vec2作为一个关联类型。我怀疑这可能是一个XY problem -你到底想要实现什么?
https://stackoverflow.com/questions/36641840
复制相似问题