This优秀教程,定义了YesNo
类型类和YesNo Int
个实例,如下所示:
class YesNo a where
yesno :: a -> Bool
instance YesNo Int where
yesno 0 = False
yesno _ = True
这适用于明确的类型声明,如:
*Main> yesno (0::Int)
False
我想避免使用显式类型声明,并使其适用于Num a
类约束:
instance (Num a) => YesNo a where
yesno 0 = False
yesno _ = True
但是这个实例定义没有编译错误:
Constraint is no smaller than the instance head
我理解Num
的构造函数数量大于YesNo
类,因此错误。但是,如何在不设置UndecidableInstances
编译器标志的情况下解决此问题?