2015-10-15 67 views
0

我希望能够说的是一个A的所有类型它们也是BHaskell的类型类隐含

class A g where 
    f :: g -> Int 

class B g where 
    h :: g -> Int 

instance A g => B g where 
    h = f 

我得到的编译错误:

Illegal instance declaration for `B g' … 
     (All instance types must be of the form (T a1 ... an) 
     where a1 ... an are *distinct type variables*, 
     and each type variable appears at most once in the instance head. 
+5

的可能的复制[你可以做一个类的实例不是一个类型,但在Haskell中全班同学?( http://stackoverflow.com/questions/32769112/can-you-make-an-instance-of-a-class-not-for-a-type-but-for-a-whole-class-in-hask) – dfeuer

+0

特别参见[Mike Benfield的答案](http://stackoverflow.com/a/32769655/1477667)。 – dfeuer

回答

4

You should not do this 。然而,写出类似

class B g => A g where 
    f :: g -> Int 

这是非常合理的,这产生了有用的必要条件。随着DefaultSignatures扩展(我个人不喜欢以各种理由),你甚至可以编写

class B g where 
    h :: g -> Int 
    default h :: A g => g -> Int 
    h = f