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

David Menendez dave at zednenem.com
Sun Jan 20 02:10:22 EST 2008


On Jan 20, 2008 12:56 AM, Thomas Hartman <tphyahoo at gmail.com> wrote:

> 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 <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080120/a31c60e3/attachment.htm


More information about the Haskell-Cafe mailing list