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
Monoidfor anyAlternativeAltis a newtype for good reason as there are many ways to describeMonoidbehaviour for an applied typef a. For exampleApplicativelifting:Ap.The instances you give are maximally overlapping, the
Monoidinstance of any applied type is now forced to be theAlternativeinstance, completely ignoring theaparameter.There are many instances where this would not be correct, for example
Semigroup a => Semigroup (Maybe a)is not the same as theSemigroupyou would get fromAlternative Maybe.It is possible using a rather new feature
QuantifiedConstraintsto quantify over the argument of a type constructorforall x. Monoid (f x). This is not the same asAlternativebut similar