[Haskell-cafe] Function Precedence

Hans Aberg haberg at math.su.se
Tue Apr 8 15:23:33 EDT 2008

On 2 Apr 2008, at 16:20, Loup Vaillant wrote:
>> class AdditiveSemiMonoid a where
>>   (+) :: a -> a -> a
> Err, why *semi* monoid? Plain "monoid" would not be accurate?

I found an example where it is crucial that the monoid has a unit:

When given a monoid m, then one can also define an m-algebra, by  
giving a structure map (works in 'hugs -98'):
   class Monad m => MAlgebra m a where
     smap :: m a -> a

Now, the set of lists on a set A is just the free monoid with base A;  
the list monad identifies the free monoids, i.e., the lists. So this  
then gives Haskell interpretation
   class Monoid a where
   unit  :: a
   (***) :: a -> a -> a

instance Monoid a => MAlgebra [] a where
   smap [] = unit
   smap (x:xs) = x *** smap xs
Here, I use (***) to not clash with the Prelude (*).

But the function "product" here shows up as the structure map of a  
multiplicative monoid. Similarly, "sum" is the structure map of the  
additive monoids. And (++) is just the monoid multiplication of the  
free monoids.


More information about the Haskell-Cafe mailing list