In Monoid and Semigroup instances of Alternative Alt used.
Why we can't write instance without it?
{-# LANGUAGE FlexibleInstances #-}
instance Alternative f => Semigroup (f a) where
(<>) = <|>
instance Alternative f => Monoid (f a) where
mempty = empty
And if we can write that, can we then replace Alternative with (Monoid (f a), Applicative f) in functions?
You use it for deriving
Monoid
for anyAlternative
Alt
is a newtype for good reason as there are many ways to describeMonoid
behaviour for an applied typef a
. For exampleApplicative
lifting:Ap
.The instances you give are maximally overlapping, the
Monoid
instance of any applied type is now forced to be theAlternative
instance, completely ignoring thea
parameter.There are many instances where this would not be correct, for example
Semigroup a => Semigroup (Maybe a)
is not the same as theSemigroup
you would get fromAlternative Maybe
.It is possible using a rather new feature
QuantifiedConstraints
to quantify over the argument of a type constructorforall x. Monoid (f x)
. This is not the same asAlternative
but similar