haskellの機能依存関係 wiki:
これらの定義を考えると:
data Vector = Vector Int Int deriving (Eq, Show)
data Matrix = Matrix Vector Vector deriving (Eq, Show)
instance Num Vector where
Vector a1 b1 + Vector a2 b2 = Vector (a1+a2) (b1+b2)
Vector a1 b1 - Vector a2 b2 = Vector (a1-a2) (b1-b2)
{- ... and so on ... -}
instance Num Matrix where
Matrix a1 b1 + Matrix a2 b2 = Matrix (a1+a2) (b1+b2)
Matrix a1 b1 - Matrix a2 b2 = Matrix (a1-a2) (b1-b2)
{- ... and so on ... -}
class Mult a b c where
(*) :: a -> b -> c
instance Mult Matrix Matrix Matrix where
{- ... -}
instance Mult Matrix Vector Vector where
{- ... -}
次のようなあいまいなタイプがある理由がわかりません。
m1, m2, m3 :: Matrix
(m1 * m2) * m3 -- type error; type of (m1*m2) is ambiguous
明らかに、m1とm2がMatrixの場合、可能なリターンのタイプはMatrixのみです。つまり、を適用しinstance Mult Matrix Matrix Matrix
ます。