考虑此代码:
{-# language FlexibleInstances, UndecidableInstances #-}
module Y where
class C m where
x :: m
instance {-# overlappable #-} Monoid m => C m where
x = mempty
instance C Int where
x = 53
x
是什么类型?
λ :type x
x :: C m => m
到目前为止-很好。现在删除Int
实例。 x
是什么类型?
λ :type x
x :: Monoid m => m
惊讶!
为什么会这样?
此行为在以下博客文章中进行了解释:
简而言之:GHC足够聪明,可以看到您只有C
类型类的一个实例,并确定它是唯一可能的实例,因此,每当它看到C m
约束时,都会将其替换为Monoid m
,因为它们是等效的。