[Haskell-cafe] monoid pair of monoids?
Christopher Howard
christopher.howard at frigidcode.com
Fri Dec 21 09:27:19 CET 2012
On 12/20/2012 08:54 PM, Daniel Feltey wrote:
> You were only missing the restriction that both types a and b must be
> instances of Monoid in order to make Socket a b into an instance of Monoid.
>
>
>
> Dan Feltey
Thank you for your help. An additional question, if I might: For the
sake of elegance and simplicity, I modified the class and instances to
avoid the "tuple" aspect:
code:
--------
data Socket2 a b = Socket2 a b
deriving (Show)
instance (Monoid a, Monoid b) => Monoid (Socket2 a b) where
mempty = Socket2 mempty mempty
Socket2 a b `mappend` Socket2 w x = Socket2 (a `mappend` w) (b
`mappend` x)
--------
Of course, I thought it would be likely I would want other classes and
instances with additional numbers of types:
code:
--------
data Socket3 a b c = Socket3 a b c
deriving (Show)
instance (Monoid a, Monoid b, Monoid c) => Monoid (Socket3 a b c) where
mempty = Socket3 mempty mempty mempty
Socket3 a b c `mappend` Socket3 w x y =
Socket3 (a `mappend` w) (b `mappend` x) (c `mappend` y)
data Socket4 a b c d = Socket4 a b c d
deriving (Show)
instance (Monoid a, Monoid b, Monoid c, Monoid d) => Monoid (Socket4 a b
c d) where
mempty = Socket4 mempty mempty mempty mempty
Socket4 a b c d `mappend` Socket4 w x y z =
Socket4 (a `mappend` w) (b `mappend` x) (c `mappend` y) (d
`mappend` z)
data Socket 5 a b c d e... et cetera
--------
Seeing as the pattern here is so rigid and obvious, I was wondering: is
it possible to abstract this even more? So I could, for instance, just
specify that I want a Socket with 8 types, and poof, it would be there?
Or is this as meta as we get? (I.e., without going to something like
Template Haskell.)
--
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/d9a498ce/attachment.pgp>
More information about the Haskell-Cafe
mailing list