次のように、一般的な Pair 型クラスのインスタンスとして、Cantor Pairing Function を実装しようとしています。
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
インスタンスに適切な Integral 制約を追加するにはどうすればよいですか? Pair インターフェイス自体を変更する必要があるかもしれないという漠然とした気持ちがありますが、これをどうやって行うのかわかりません。