这是一个非常有用的类:
class Foo f a where
foo :: f a
它让我为许多类型设置默认值。事实上,我什至可能不需要知道是什么a
。
instance Foo Maybe a where
foo = Nothing
现在我有一个Maybe a
for all a
,以后可以专攻它。
specialize :: (forall a. f a) -> f Int
specialize x = x
fooMaybe :: Maybe Int
fooMaybe = specialize foo
嗯....这fooMaybe
确实看起来很具体。让我们看看我是否可以使用上下文来概括它:
fooAll :: (Foo f a) => f Int
fooAll = specialize foo
哎呀!可能不会。
Foo.hs:18:21:
Could not deduce (Foo * f a1) arising from a use of `foo'
from the context (Foo * f a)
bound by the type signature for fooAll :: Foo * f a => f Int
at Foo.hs:17:11-28
所以,我的问题是,我该如何编写fooAll
的通用版本fooMaybe
?或者,更一般地说,我怎样才能普遍概括一个类型类约束?