
Is "Socket2 a b" any different from the pair (a,b)? Assuming Socket2 looks roughly like the following:
import Data.Monoid data Socket2 a b = Socket2 (a,b)
Then if both a and b are instances of Monoid we can make Socket2 a b into an instance of Monoid the same way we make (a,b) into a Monoid.
instance (Monoid a, Monoid b) => Monoid (Socket a b) where mempty = Socket2 (mempty, mempty) Socket2 (a, b) `mappend` Socket2 (w, x) = Socket2 (a `mappend` w, b `mappend` x)
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 On Thu, Dec 20, 2012 at 8:40 PM, Christopher Howard < christopher.howard@frigidcode.com> wrote:
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe