Why should Applicative be a superclass of Monad?

前端 未结 4 1822
礼貌的吻别
礼貌的吻别 2020-12-04 15:32

Given:

Applicative m, Monad m => mf :: m (a -> b), ma :: m a

it seems to be considered a law that:

mf <*> ma ==         


        
4条回答
  •  醉酒成梦
    2020-12-04 16:12

    Among other things, you ask why is the Functor-Applicative-Monad proposal a good thing. One reason is because the lack of unity means there is a lot of duplication of API. Consider the standard Control.Monad module. The following are the functions in that module that essentially use the Monad (there are none for MonadPlus) constraint:

    (>>=) fail (=<<) (>=>) (<=<) join foldM foldM_
    

    The following are the functions in that module where a Monad/MonadPlus constraint could as far as I can tell easily be relaxed to Applicative/Alternative:

    (>>) return mzero mplus mapM mapM_ forM forM_ sequence sequence_ forever
    msum filterM mapAndUnzipM zipWithM zipWithM_ replicateM replicateM_ guard
    when unless liftM liftM2 liftM3 liftM4 liftM5 ap
    

    Many of the latter group do have Applicative or Alternative versions, in either Control.Applicative, Data.Foldable or Data.Traversable – but why need to learn all that duplication in the first place?

提交回复
热议问题