How to use the Data.Map inbuild Monoid

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use the Data.Map inbuild Monoid

Leonhard Applis
Hello Francesco,

> `Map k v` is already an instance of `Monoid` (when `v` is an instance of `Ord`), are you sure you need to write another one?

Thanks for your answer!
My "Values" is a Monoid, however it seems that the Graph cannot properly be seen correctly as a Monoid

using mconcat does not (properly) work, meaning that <> is not correctly applied to the someMonoids
so instead of

type G = Map Text someMonoid
mconcat [gs]

I have to write:

foldr (Map.unionWith (<>)) Map.empty [gs]

This passes the tests.
Same Problem with
g1 <> g2  --(Does not work properly)
and
Map.unionWith (<>) g1 g2 --(Does work)

I have declared someMonoid myself, do I need to declare something special about it?
I feel like my G is messing up with <> being about him, or about someMonoid

A broken down piece of code is:

import Data.Map as Map

data Sum = Sum Int deriving (Eq,Show)

instance Semigroup Sum where 
    (<>) (Sum a) (Sum b)= Sum(a + b) 
instance Monoid Sum where 
    mempty = Sum 0 

type G = Map.Map String Sum

And to verify my problem:
GHCI > v = Map.singleton "A" (Sum 1)
GHCI > u = Map.singleton "A" (Sum 1)
GHCI> c= v <> u
GHCI> Map.lookup "A" c
Just (Sum 1)

but I want
Just (Sum 2)

thanks
Leonhard

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

publickey - Leonhard.Applis@protonmail.com - 0x807FDDF3.asc (2K) Download Attachment
signature.asc (490 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How to use the Data.Map inbuild Monoid

Simon Jakobi
Hi Leonhard,

the monoidal-containers package should be interesting to you:

http://hackage.haskell.org/package/monoidal-containers

Cheers,
Simon

Am Mo., 18. Nov. 2019 um 23:22 Uhr schrieb Leonhard Applis
<[hidden email]>:

>
> Hello Francesco,
>
> > `Map k v` is already an instance of `Monoid` (when `v` is an instance of `Ord`), are you sure you need to write another one?
>
> Thanks for your answer!
> My "Values" is a Monoid, however it seems that the Graph cannot properly be seen correctly as a Monoid
>
> using mconcat does not (properly) work, meaning that <> is not correctly applied to the someMonoids
> so instead of
>
> type G = Map Text someMonoid
> mconcat [gs]
>
> I have to write:
>
> foldr (Map.unionWith (<>)) Map.empty [gs]
>
> This passes the tests.
> Same Problem with
> g1 <> g2  --(Does not work properly)
> and
> Map.unionWith (<>) g1 g2 --(Does work)
>
> I have declared someMonoid myself, do I need to declare something special about it?
> I feel like my G is messing up with <> being about him, or about someMonoid
>
> A broken down piece of code is:
>
> import Data.Map as Map
>
> data Sum = Sum Int deriving (Eq,Show)
>
> instance Semigroup Sum where
>     (<>) (Sum a) (Sum b)= Sum(a + b)
> instance Monoid Sum where
>     mempty = Sum 0
>
> type G = Map.Map String Sum
>
> And to verify my problem:
> GHCI > v = Map.singleton "A" (Sum 1)
> GHCI > u = Map.singleton "A" (Sum 1)
> GHCI> c= v <> u
> GHCI> Map.lookup "A" c
> Just (Sum 1)
>
> but I want
> Just (Sum 2)
>
> thanks
> Leonhard
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners