Must mplus always be associative? Haskell wiki vs. Oleg Kiselyov

前端 未结 3 1982
攒了一身酷
攒了一身酷 2020-12-29 05:12

The Haskell wikibook asserts that

Instances of MonadPlus are required to fulfill several rules, just as instances of Monad are required to fulfill the

3条回答
  •  佛祖请我去吃肉
    2020-12-29 05:45

    The two laws that everybody agrees that MonadPlus should obey are the identity and associativity laws (a.k.a. the monoid laws):

    mplus mempty a = a
    
    mplus a mempty = a
    
    mplus (mplus a b) c = mplus a (mplus b c)
    

    I always assume they hold in all MonadPlus instances that I use and consider instances that violate those laws to be "broken", whether or not they were written by Oleg.

    Oleg is right that associativity does not play nicely with breadth-first search, but that just means that MonadPlus is not the abstraction he is looking for.

    To answer the point you made in a comment, I would always consider that rewrite rule of yours sound.

提交回复
热议问题