class (Eq a) => My a where
f:: (a,a) -> (a,a)
instance My Int where
f (a,b) = (a,b)
instance My Char where
f (a,b) = (a,b)
「ペアの特殊化」。それはコンパイルエラーを引き起こし、私は理由を知らない。それを修復し、それがなぜ誤りであるか私に説明してください。ハスケルでは減算タイプが失敗します。
instance (My a, My b) => My (a,b) where
f ((a,b), (c,d)) = ((f (a,b)), (f (c,d)))
エラー:
test.hs:11:31:
Could not deduce (a ~ b)
from the context (Eq (a, b), My a, My b)
bound by the instance declaration at test.hs:10:10-33
`a' is a rigid type variable bound by
the instance declaration at test.hs:10:10
`b' is a rigid type variable bound by
the instance declaration at test.hs:10:10
Expected type: (a, b)
Actual type: (a, a)
In the return type of a call of `f'
In the expression: (f (a, b))
In the expression: ((f (a, b)), (f (c, d)))
Failed, modules loaded: none.
問題は、GHCが平等を持っていない限り、あなたが与えた最初のインスタンスにコミットしないということです。私はそれがタイプエラーにつながるのを見たことが分かっていますが、詳細を覚えていません。 – dfeuer