2015-10-15 104 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