[Haskell-cafe] Alternative versus Monoid

Carl Howells chowells79 at gmail.com
Thu Dec 15 18:59:01 CET 2011


Monoid and Alternative are not the same.  There is a very important
difference between them:

class Alternative f where
    (<|>) :: f a -> f a -> f a
    ...

class Monoid a where
    mappend :: a -> a -> a
    ...

The equivalent to Alternative is MonadPlus, not Monoid.  The kinds
matter.  In Alternative, you are guaranteed that the type that f is
applied to cannot affect the semantics of (<|>).  As has been already
demonstrated aptly, the type a in the instance Monoid a => Monoid
(Maybe a) matters quite a lot.

Carl

On Thu, Dec 15, 2011 at 8:04 AM, Yves Parès <limestrael at gmail.com> wrote:
> So why don't we use First and Last with the Alternative interface too?
>
> It's indeed weird the Maybe doesn't react the same way with Alternative and
> Monoid.
>
>
> 2011/12/15 Anthony Cowley <acowley at gmail.com>
>>
>> On Dec 15, 2011, at 10:19 AM, Brent Yorgey wrote:
>>
>> > On Thu, Dec 15, 2011 at 06:49:13PM +1000, Gregory Crosswhite wrote:
>> >>
>> >> So at the end of the day... what is the point of even making Maybe and
>> >> [] instances of Alternative?
>> >
>> > The Alternative and Monoid instances for [] are equivalent.  However,
>> > the Alternative and Monoid instances for Maybe are not. To wit:
>> >
>> >> (Just (Sum  4)) <|> (Just (Sum 3))
>> >  Just (Sum {getSum = 4})
>> >
>> >> (Just (Sum 4)) `mappend` (Just (Sum 3))
>> >  Just (Sum {getSum = 7})
>>
>> We already have,
>>
>> > First (Just (Sum 4)) `mappend` First (Just (Sum 3))
>> First {getFirst = Just (Sum {getSum = 4})}
>>
>> So the overlap of apparent Alternative and Monoid functionality remains.
>> This just represents an opportunity for the caller to select the monoid they
>> want.
>>
>> Anthony
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



More information about the Haskell-Cafe mailing list