On Dec 16, 2011, at 3:59 AM, Carl Howells wrote:
> 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 (<|>).
I understand that one needs to worry about kinds in general, but in this
particular case such a subtlety is non-issue because you would always be
defining Monad for a particular type. That is to say, given an alternative f,
the instance of Monoid would be
instance Monoid (f a) where { ... }
where in the above a is an arbitrary type variable.
To give you a more concrete example, the following code compiles and runs,
producing the output [1,2,3,4,5,6]
================================================
import Data.Monoid
newtype L a = L [a] deriving (Show,Eq)
instance Monoid (L a) where
mempty = L []
mappend (L x) (L y) = L (x ++ y)
main = putStrLn . show $ (L [1,2,3]) `mappend` (L [4,5,6])
================================================
Cheers,
Greg
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe