
I have ---------- import qualified Data.Map as M type Link a = (a, Int) data MChain a = Map a [Link a] deriving (Show) ------------------- and want to make a Monoid of MChain. So I have ------------------- instance Monoid (MChain a) where mempty = M.empty mappend = undefined ------------------- this won’t compile and I need M.empty to be Map a [Link a] The error is Couldn't match expected type ‘MChain a1’ with actual type ‘M.Map k0 a0’ • In the expression: M.empty :: MChain a In an equation for ‘mempty’: mempty = M.empty :: MChain a In the instance declaration for ‘Monoid (MChain a)’ This part confuses me "Couldn't match expected type ‘MChain a1 with actual type ‘M.Map k0 a0’ " How should I proceed. Many thanks Mike

On Fri, Feb 03, 2017 at 09:47:04PM +0000, mike h wrote:
I have
---------- import qualified Data.Map as M
type Link a = (a, Int) data MChain a = Map a [Link a] deriving (Show)
-------------------
and want to make a Monoid of MChain. So I have
------------------- instance Monoid (MChain a) where mempty = M.empty mappend = undefined -------------------
this won’t compile and I need M.empty to be Map a [Link a]
Hello Mike, I think the error lies in the confusion between `type` and `data` declaration. type Something = Int but data Something = SomeConstructor Int So I bet you wanted to write data MChain a = MChain (M.Map a [Link a]) deriving (Show) `M.empty` returns a Map. λ> :t M.empty M.empty :: M.Map k aj Hence this will work: instance Monoid (MChain a) where mempty = MChain M.empty mappend = undefined Does this help?

Yes, that’s exactly what I meant to write!!! :) Thank you Francesco. Mike
On 3 Feb 2017, at 21:59, Francesco Ariis
wrote: On Fri, Feb 03, 2017 at 09:47:04PM +0000, mike h wrote:
I have
---------- import qualified Data.Map as M
type Link a = (a, Int) data MChain a = Map a [Link a] deriving (Show)
-------------------
and want to make a Monoid of MChain. So I have
------------------- instance Monoid (MChain a) where mempty = M.empty mappend = undefined -------------------
this won’t compile and I need M.empty to be Map a [Link a]
Hello Mike, I think the error lies in the confusion between `type` and `data` declaration.
type Something = Int
but
data Something = SomeConstructor Int
So I bet you wanted to write
data MChain a = MChain (M.Map a [Link a]) deriving (Show)
`M.empty` returns a Map.
λ> :t M.empty M.empty :: M.Map k aj
Hence this will work:
instance Monoid (MChain a) where mempty = MChain M.empty mappend = undefined
Does this help?
_______________________________________________ Beginners mailing list Beginners@haskell.org mailto:Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Francesco Ariis
-
mike h