[Haskell-cafe] monoid pair of monoids?

Christopher Howard christopher.howard at frigidcode.com
Fri Dec 21 06:40:13 CET 2012


In my current pondering of the compose-able objects them, I was thinking
it would be useful to have the follow abstractions: Monoids, which were
themselves tuples of Monoids. The idea was something like so:

code:
--------
import Data.Monoid

instance Monoid (Socket2 a b) where

  mempty = Socket2 (mempty, mempty)

  Socket2 (a, b) `mappend` Socket2 (w, x) = Socket2 (a `mappend` w, b
`mappend` x)

data Socket2 a b = Socket2 (a, b)
--------

However, this does not compile because of errors like so:

code:
--------
Sockets.hs:9:21:
    No instance for (Monoid a)
      arising from a use of `mempty'
    In the expression: mempty
    In the first argument of `Socket2', namely `(mempty, mempty)'
    In the expression: Socket2 (mempty, mempty)
--------

This makes sense, but I haven't figured out a way to rewrite this to
make it work. One approach I tried was to encode Monoid constraints into
the data declaration (which I heard was a bad idea) but this didn't
work, even using forall. Also I tried to encode it into the instance
declaration, but the compiler kept complaining about errant or illegal
syntax.

-- 
frigidcode.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 553 bytes
Desc: OpenPGP digital signature
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121220/60fac489/attachment.pgp>


More information about the Haskell-Cafe mailing list