この関数の依存関係で表される制約は、コレクションタイプがc
の場合、そのアイテムのタイプはi
です。たとえば、c ~ [a]
、つまりコレクションがa
のリストである場合は、i ~ a
と判断できるはずです。
このような関数の依存関係がなければ、2つのインスタンスを持つことができます。 Unfoldable [a] a
(明らか/意図されたインスタンス)およびUnfoldable [a] [a]
(insert = concat
,singleton = id
のようなものを持つ)
{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-}
class Unfoldable c i where
empty :: c
instance Unfoldable [a] a where
empty = []
instance Unfoldable [a] [a] where
empty = []
xs :: [a]
xs = empty
をこれがその結果:
No instance for (Unfoldable [a] i0) arising from a use of `empty'
The type variable `i0' is ambiguous
Relevant bindings include
xs :: [a]
Note: there are several potential instances:
instance Unfoldable [a] a
instance Unfoldable [a] [a]
In the expression: empty
In an equation for `xs': xs = empty
型チェッカーは、その後
empty :: [a]
のようなものを見ている場合、それはインスタンスが使用することを選択する方法はありません