简体   繁体   中英

Why is there no type class for monoids on functors in Haskell?

I admit that this question is a bit unspecific, but I was wondering why I never stumbled upon a type class for monoids on functors in Haskell. Did I just miss it, is there good reason for this absence or is it entirely due to historic causes? IMHO, the following inheritance chart looks a bit odd without a top right corner:

  Functor
     |
     V
Applicative ––> Alternative
     |               |
     V               V
   Monad    ––>  MonadPlus

One key factor to think about here is, "What does the arrow from Functor really mean?" If there are no axioms that unify Functor with FunctorPlus then you might as well just define instance Monoid (F t) where ... and be done with it. So what axioms are you looking for -- just fmap f fempty = fempty or also fmap fx <|> fmap fy == fmap f (x <|> y) ...?

Another key factor will be the dearth of interesting structures which are functors but not applicatives. There's probably an argument having to do with generic-programming (metaprogramming the deriving keyword) where everything is a sum of products and therefore we can derive Applicative for anything of kind * -> * , but I don't know the details. The only value FunctorPlus could possibly have is "doing the same thing that Alternative does for Functors which are not Applicative," and so if that set is really small then there's obviously not much value added.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM