Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why sequence requires monad if applicative would suffice?

The signature of sequence is

sequence :: Monad m => t (m a) -> m (t a)

But we can implement it as

sequence = traverse id

requiring m to be just Applicative. If monads are applicatives then why bother having this constraint on type level?

like image 283
bottaio Avatar asked Apr 03 '19 03:04

bottaio


People also ask

Why do we need monads?

monads are used to address the more general problem of computations (involving state, input/output, backtracking, ...) returning values: they do not solve any input/output-problems directly but rather provide an elegant and flexible abstraction of many solutions to related problems.

Is every monad an applicative?

Monads are not a replacement for applicative functors Instead, every monad is an applicative functor (as well as a functor).

Are all monads applicative functors?

Every Monad is an Applicative Just as IO , every monad can be made into an applicative functor.

Is every functor a monad?

As I understand, every monad is a functor but not every functor is a monad. A functor takes a pure function (and a functorial value) whereas a monad takes a Kleisli arrow, i.e. a function that returns a monad (and a monadic value).


1 Answers

There are many functions in Haskell that are equivalent but distinct because Applicative (resp. Functor) didn’t use to be a superclass of Monad. For example:

  • return vs. pure

  • ap vs. <*>

  • liftM vs. liftA vs. fmap

  • liftM2, liftM3, &c. vs. liftA2, liftA3, &c.

  • mapM/forM vs. traverse/for

  • mapM_/forM_ vs. traverse_/for_

  • sequence vs. sequenceA

  • mzero & mplus (from MonadPlus) vs. empty & <|> (from Alternative)

The old functions with their original Monad signatures are still present, but in new code, since the Applicative–Monad Proposal (AMP) was implemented, you can always use the Applicative versions because they’re slightly more general—that is, you can always replace return with pure, but not vice versa.

like image 177
Jon Purdy Avatar answered Nov 15 '22 09:11

Jon Purdy