Пакет constructive-алгебра позволяет вам определять экземпляры алгебраических модулей (таких как векторные пространства, но с использованием кольца, где поле было обязательным)
Это моя попытка определить модуль:
{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances #-}
module A where
import Algebra.Structures.Module
import Algebra.Structures.CommutativeRing
import Algebra.Structures.Group
newtype A = A [(Integer,String)]
instance Group A where
(A a) <+> (A b) = A $ a ++ b
zero = A []
neg (A a) = A $ [((-k),c) | (k,c) <- a]
instance Module Integer A where
r *> (A as) = A [(r <*> k,c) | (k,c) <- as]
Это терпит неудачу:
A.hs:15:10:
Overlapping instances for Group A
arising from the superclasses of an instance declaration
Matching instances:
instance Ring a => Group a -- Defined in Algebra.Structures.Group
instance Group A -- Defined at A.hs:9:10-16
In the instance declaration for `Module Integer A'
A.hs:15:10:
No instance for (Ring A)
arising from the superclasses of an instance declaration
Possible fix: add an instance declaration for (Ring A)
In the instance declaration for `Module Integer A'
Failed, modules loaded: none.
Если я закомментирую экземпляр Group
, то:
A.hs:16:10:
No instance for (Ring A)
arising from the superclasses of an instance declaration
Possible fix: add an instance declaration for (Ring A)
In the instance declaration for `Module Integer A'
Failed, modules loaded: none.
Я прочитал это как требующее, чтобы экземпляр Ring A
имел Module Integer A
, что не имеет смысла и не требуется в определении класса:
class (CommutativeRing r, AbelianGroup m) => Module r m where
-- | Scalar multiplication.
(*>) :: r -> m -> m
Не могли бы вы объяснить это?
Group
дляA
на самом деле не определяет группу? Например,let a = [(1,"foo")]
, затемa <+> neg a = [(1,"foo"),(-1,"foo")]
, что не совпадает сzero
. - person Chris Taylor   schedule 10.05.2012a ++ b
путем группировки по идентичным строкам. Я пропустил приведение к нормальной форме, чтобы упростить пример. - person user21338   schedule 10.05.2012