3

With functional dependencies, I can declare the Foo class:

class Foo a b c | a -> b where
    foo1 :: a -> b -> c
    foo2 :: a -> c

and when I call foo2, everything works fine. The compiler knows which instance to use because of the dependency.

But if I remove the dependency to create Foo':

class Foo' a b c where
    foo1' :: a -> b -> c
    foo2' :: a -> c

everything still compiles fine, but now whenever I try to call foo2' GHC throws an error about not being able to resolve which instance to use because b is ambiguous.

Is it ever possible to call foo2' without error? If so, how? If not, why doesn't it generate a compilation error?

4

1 回答 1

3

在这种情况下调用是不可能的foo2',因为正如 Daniel Fischer 所说,没有办法确定使用哪个实例。例如,如果您有:

instance Foo' Int Int Int where
    foo2' x = x

instance Foo' Int Bool Int where
    foo2' x = x + 1

这两个foo2's 具有相同的类型签名,因此无法确定调用哪一个。

解决此问题的常用方法是使用代理:

data Proxy a = Proxy

class Foo'' a b c = where
    foo2'' :: Proxy b -> a -> c

您使用它来选择哪个实例:

foo'' (Proxy :: Proxy Bool) 42
于 2013-01-05T01:52:21.313 回答