Jenn24
Jenn24

Reputation: 53

Specifying that a type can only exist in one class if it already exists in other classes in Haskell

I am working on a problem involving unification, with a user-defined type Subst a. The problem reads:

"Define a type class Unifiable that specifies that a function unify::a -> a -> Maybe (Subst a) must be defined for any type a inside this class. A type a can only be in the Unifiable class if it is already in the Eq and Substitutable classes, and this should be represented in your definition."

This is how I have defined Substitutable and Unifiable so far:

class Substitutable a where
subst :: Subst a -> a -> a

class Unifiable a where
unify :: a -> a -> Maybe (Subst a)

However, I'm not sure how to go about specifying that type a can be in the Unifiable class only if it is already in Eq and Substitutable classes.

I'm not looking for an answer to this specific problem, but in general, how would I go about specifying a type can only be in one class if it is already in other classes?

Thanks.

Upvotes: 2

Views: 94

Answers (1)

chepner
chepner

Reputation: 530882

The class definition can contain constraints, just like a function.

class (Eq a, Substitutable a) => Unifiable a where
    unify :: a -> a -> Maybe (Subst a)

Upvotes: 6

Related Questions