指定类型只能存在于一个类中,如果它已存在于Haskell中的其他类中

Jen*_*n24 2 haskell instance unification typeclass

我正在研究涉及统一的问题,使用用户定义的类型Subst a.问题是:

"定义一个类型类Unifiable,它指定一个函数unify :: a - > a - > Maybe(Subst a)必须为这个类中的任何一个类型定义.一个类型a只能在Unifiable类中,如果它已经是在Eq和Substitutable类中,这应该在你的定义中表示."

到目前为止,我已经定义了Substitutable和Unifiable:

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

class Unifiable a where
unify :: a -> a -> Maybe (Subst a)
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何将类型a指定为Unifiable类,只有它已经在Eq和Substitutable类中.

我不是在寻找这个特定问题的答案,但总的来说,如果指定一个类型只能在一个类中,如果它已经存在于其他类中,我该怎么办?

谢谢.

che*_*ner 6

类定义可以包含约束,就像函数一样.

class (Eq a, Substitutable a) => Unifiable a where
    unify :: a -> a -> Maybe (Subst a)
Run Code Online (Sandbox Code Playgroud)