suggestion: use lazy pattern matching for Monoid instances of tuples

Petr Pudlák petr.mvd at gmail.com
Sat Aug 17 22:31:00 CEST 2013


Dear haskellers,

currently the instances are defined as

|instance  (Monoid  a,Monoid  b) =>Monoid  (a,b)where
         mempty = (mempty, mempty)
         (a1,b1) `mappend` (a2,b2) = (a1 `mappend` a2, b1 `mappend` b2)|

However for some applications this isn't lazy enough, for example

|-- | Build two lists by folding on a pair of `Endo` monoids.
test  = head $ appEndo (fst $ foldMap (f &&& f) [1..]) []
   where
     f =Endo  . (:)|

never terminates because of the unnecessary pattern matching on the 
constructor |(,)| forces evaluation of the whole infinite list.

I suggest to change all Monoid instances for tuples to be like

|  instance  (Monoid  a,Monoid  b) =>Monoid  (a,b)where
          mempty = (mempty, mempty)
          ~(a1,b1) `mappend` ~(a2,b2) = (a1 `mappend` a2, b1 `mappend` b2)
--      ^^^                ^^^|

which fixes the problem.

Best regards,
Petr

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20130817/b79d57bf/attachment.htm>


More information about the Libraries mailing list