overlapping instance error -- need help using instance Monoid (Sum a) where a is a map of "money" values

The code below compiles as given, however if I uncomment the tSM function I get the overlapping instance error mentioned in the subject line. Can someone give me advice on how to do what I want to do? basically I want to add, for example, (USD,1) and (USD,2) and (Euro,3) and get result fromList [(USD,3), (Euro,3)] the datatypes below are more verbose but the above is the basic idea thanks! thomas. ***** module TransactionRows {- ( mkTransactionRows,TransactionRows ) -} where import Data.Monoid import Data.List import qualified Data.Map as M data Currency a = Currency a deriving ( Show, Eq, Ord ) data Money c a = Money ( M.Map c a ) deriving Show instance (Num a, Ord c)=> Monoid ( Sum (Money c a) ) where mempty = Sum ( Money M.empty ) Sum ( Money m1 ) `mappend` Sum ( Money m2 ) = Sum (Money m1 `plusmoney` Money m2) plusmoney (Money m1s) (Money m2s) = Money msum where msum = foldl' f m1s (M.toList m2s) f m (k,v) = M.insertWith (+) k v m mkMoney1 :: Currency String -> Float -> Money (Currency String) Float mkMoney1 c a = Money $ M.singleton c a --sumMoney :: [Money (Currency String) Float] -> Money (Currency String) Float sumMoney ms = getSum $ mconcat $ map Sum ms -- if I uncomment this, get Overlapping instances for Monoid error --tSM = sumMoney [mkMoney1 (Currency "usd") 1 :: Money (Currency String) Float]

On Jan 20, 2008 12:56 AM, Thomas Hartman
The code below compiles as given, however if I uncomment the tSM function I get the overlapping instance error mentioned in the subject line.
Can someone give me advice on how to do what I want to do?
basically I want to add, for example, (USD,1) and (USD,2) and (Euro,3) and get result
fromList [(USD,3), (Euro,3)]
the datatypes below are more verbose but the above is the basic idea
thanks!
thomas.
*****
module TransactionRows {- ( mkTransactionRows,TransactionRows ) -} where import Data.Monoid import Data.List import qualified Data.Map as M
data Currency a = Currency a deriving ( Show, Eq, Ord ) data Money c a = Money ( M.Map c a ) deriving Show instance (Num a, Ord c)=> Monoid ( Sum (Money c a) ) where mempty = Sum ( Money M.empty ) Sum ( Money m1 ) `mappend` Sum ( Money m2 ) = Sum (Money m1 `plusmoney` Money m2)
This is the overlap. Data.Monoid already defines an instance for Monoid (Sum
a), so it's going to overlap with your Monoid (Sum (Money c a)).
Unless you're planning to define more than one Monoid for Money, I suggest
skipping Sum entirely.
--
Dave Menendez
participants (2)
-
David Menendez
-
Thomas Hartman