【发布时间】:2012-09-06 00:55:38
【问题描述】:
我正在尝试实现康托尔配对函数,作为一个实例 泛型 Pair 类型类,如下所示:
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 接口本身, 但不知道该怎么做。
【问题讨论】: