Keynan
Keynan

Reputation: 1346

Haskell Type Class Implied

I want to be able to say that for all Types that are an A they are also a B

class A g where
  f :: g -> Int

class B g where
  h :: g -> Int

instance A g => B g where
    h = f

I'm getting the compile error:

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.

Upvotes: 0

Views: 92

Answers (1)

dfeuer
dfeuer

Reputation: 48591

You should not do this. However, it's perfectly reasonable to write something like

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

This produces a useful entailment. With the DefaultSignatures extension (which I personally dislike for various reasons), you can even write

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

Upvotes: 4

Related Questions