[Haskell-beginners] Bit confused...

Francesco Ariis fa-ml at ariis.it
Fri Feb 3 21:59:48 UTC 2017


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?





More information about the Beginners mailing list