Mam w Haskellu zdefiniowaną następującą klasę (półgrup z jedynką):
class Monoid a where
(***) :: a -> a -> a
e :: a
infixl 6 ***
Jeśli chciałbym aby Integer był instancją powyższej klasy to nie ma z tym problemu. Mamy np.
instance Monoid Integer where
e = 1
(***) x y = mod (x*y) (10)
reprezentuje Z_9 z działaniem mnożenia modulo.
Oprócz tego mam zdefiniowany własny typ danych, który reprezentuje macierze 2x2:
data Mtx2x2 a = Mtx2x2 a a a a deriving(Show)
Chciałbym, aby takie macierze również były instancją klasy Monoid, w tym celu robię coś takiego:
instance Monoid (Mtx2x2 Int) where
...
Niestety, powyższa konstrukcja zwraca mi błąd:
Lista10.hs10:
Illegal instance declaration forMonoid (Mtx2x2 Int)' (All instance types must be of the form (T a1 ... an) where a1 ... an are *distinct type variables*, and each type variable appears at most once in the instance head. Use -XFlexibleInstances if you want to disable this.) In the instance declaration for
Monoid (Mtx2x2 Int)'
Kombinowałem na kilka innych sposobów, ale kod się nie kompiluje. Chciałbym to zrozumieć, więc byłbym wdzięczny za wytłumaczenie lub odesłanie do odpowiednich materiałów.