我希望能够定义一个(多参数)类型类实例,其类方法的实现忽略其参数之一。这可以很容易地完成如下。
instance MyType MyData () where
specific _ a = f a
当我在多个地方使用这种模式时,我尝试通过添加一个专门的类方法和足够的默认实现来概括它。我想出了以下内容。
{-# LANGUAGE MultiParamTypeClasses, AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
class MyType a b where
specific :: b -> a -> a
specific = const dontCare
dontCare :: a -> a
dontCare = specific (undefined :: b)
{-# MINIMAL specific | dontCare #-}
然而,这会产生错误
Could not deduce (MyType a b0) arising from a use of ‘dontCare’
[..] The type variable ‘b0’ is ambiguous
.我不明白为什么后者应该是类型变量 b
的情况。范围从类签名到方法声明。你能帮我理解这里出现的确切问题吗?是否有另一种合理的方式来实现我的意图,即以通用方式允许此类修剪实例?
最佳答案
问题出在 specific
的默认定义中。 .让我们缩小一下,看看你的方法实际上是什么类型,基于你的类型签名。
specific :: forall a b. MyType a b => b -> a -> a
dontCare :: forall a b. MyType a b => a -> a
在
specific
的默认定义中, 你使用 dontCare
在类型 a -> a
.所以 GHC 推断 dontCare
的第一个类型参数是 a
.但是没有什么限制它的第二个类型参数,所以 GHC 无法选择正确的实例字典来使用它。这就是为什么你最终需要 AllowAmbiguousTypes
让 GHC 接受 dontCare
的类型签名.这些“模糊”类型在现代 GHC 中有用的原因是我们有 TypeApplications
让我们修复它们。这个定义很好用:class MyType a b where
specific :: b -> a -> a
specific = const (dontCare @_ @b)
dontCare :: a -> a
dontCare = specific (undefined :: b)
{-# MINIMAL specific | dontCare #-}
类型应用程序指定第二个参数是
b
.您可以填写a
对于第一个论点,但 GHC 实际上可以很好地解决这个问题。
关于haskell - 使用 typeclass 方法的默认实现来省略参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62271528/