これは非常に便利なクラスです:
class Foo f a where
foo :: f a
多くのタイプのデフォルト値を作成させてください。実際、何が何でaあるかを知る必要さえないかもしれません。
instance Foo Maybe a where
foo = Nothing
これでMaybe afor allaができたので、後で特殊化できます。
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です。または、より一般的には、型クラスの制約を普遍的に一般化するにはどうすればよいですか?