我试图实现康托尔配对功能,为 通用对类型类的实例,像这样:添加类约束类型类实例
module Pair (Pair, CantorPair) where
-- Pair interface
class Pair p where
pi :: a -> a -> p a
k :: p a -> a
l :: p a -> a
-- Wrapper for typing
newtype CantorPair a = P { unP :: a }
-- Assume two functions with signatures:
cantorPair :: Integral a => a -> a -> CantorPair a
cantorUnpair :: Integral a => CantorPair a -> (a, a)
-- I need to somehow add an Integral a constraint to this instance,
-- but I can't work out how to do it.
instance Pair CantorPair where
pi = cantorPair
k = fst . cantorUnpair
l = snd . cantorUnpair
我怎么能适当积分约束添加到实例? 我有一种模糊的感觉,我可能需要修改Pair接口本身,但不知道如何去解决这个问题。
嗯,在这个阶段,我想我会一直使用积分,但是我认为将这个约束添加到Pair是没有意义的。 例如,另一实例可以IdentityPair一个可以添加诸如 数据= I AA 实例对IdentityPair其中 PI XY = I XY K(1×_)= X 升(I _ Y)= Y – guhou
啊,谢谢!我认为我需要MultiParamTypeClasses,但无法让我的小提琴工作。一定是因为我没有使用FlexibleInstances。 – guhou