Bit confused...

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

Bit confused...

Mike Houghton
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




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Bit confused...

Francesco Ariis
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Bit confused...

Mike Houghton
Yes, that’s exactly what I meant to write!!! :)
Thank you Francesco.

Mike

On 3 Feb 2017, at 21:59, Francesco Ariis <[hidden email]> 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
[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