型クラスをいじって、一見無害に見えるものを思いついた
class Pair p a | p -> a where
one :: p -> a
two :: p -> a
これはうまくいくようです、例えば
instance Pair [a] a where
one [x,_] = x
two [_,y] = y
しかし、私はタプルに困っています。次の定義はコンパイルされますが...
instance Pair (a,a) a where
one p = fst p
two p = snd p
...期待どおりに使用できません:
main = print $ two (3, 4)
No instance for (Pair (t, t1) a)
arising from a use of `two' at src\Main.hs:593:15-23
Possible fix: add an instance declaration for (Pair (t, t1) a)
In the second argument of `($)', namely `two (3, 4)'
In the expression: print $ two (3, 4)
In the definition of `main': main = print $ two (3, 4)
インスタンスを正しく定義する方法はありますか? newtype
それとも、ラッパーに頼る必要がありますか?