Search code examples
haskellapplicativemonoidsalternative-functor

Why does the Alternative typeclass need to be a sub-class of Control.Applicative


Haskell provides a standard typeclass 'Alternative' that effectively provides the <|> operator for any type that is also an Applicative.

As I understand it Alternative is considered a Monoid on Applicative's, however the <|> operator seems to make complete sense in a lot of types that aren't Applicative Functors as well, and there doesn't need to be any specific dependency on the Applicative typeclass for it to work properly.

Is there a reason why Alternative needs to be a subclass of Applicative, and if so is there a standard typeclass to define similar functionality on non-applicative types?


Solution

  • I think Alt from the semigroupoids package comes closest to being a 'standard' typeclass. https://hackage.haskell.org/package/semigroupoids-5.0.0.1/docs/Data-Functor-Alt.html#t:Alt