What are the benefits of applicative parsing over monadic parsing?

后端 未结 5 487
醉话见心
醉话见心 2020-12-12 12:41

There seems to be a consensus that you should use Parsec as an applicative rather than a monad. What are the benefits of applicative parsing over monadic parsing?

5条回答
  •  囚心锁ツ
    2020-12-12 13:12

    Monads are strictly a more featureful abstraction than Applicatives. You could write

    instance (Monad m) => Applicative m where
      pure  = return
      (<*>) = ap
    

    But there is no way to write

    instance (Applicative a) => Monad a where
      return = pure
      (>>=) = ???
    

    So yes, it is essentially a matter of style. I imagine if you use return and ap, then there should be no performance loss over using pure and <*>. Because Applicative is a strictly smaller interface than Monad, this means that <*> can sometimes be more highly optimized than ap. (But with clever GHC rewrite rules, one can often achieve the same optimizations regardless.)

    Is monadic parsing out?

    Since Monads are a subset of Applicatives, I would conclude that applicative parsing is a subset of monadic parsing.

提交回复
热议问题