对方法的约束取决于作用域中的实例吗?

问题描述 投票:3回答:1

考虑此代码:

{-# 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

惊讶!

为什么会这样?

haskell type-inference typeclass
1个回答
1
投票

此行为在以下博客文章中进行了解释:

简而言之:GHC足够聪明,可以看到您只有C类型类的一个实例,并确定它是唯一可能的实例,因此,每当它看到C m约束时,都会将其替换为Monoid m,因为它们是等效的。

© www.soinside.com 2019 - 2024. All rights reserved.