Relax ordering constraints in monadic computation

前端 未结 3 428
伪装坚强ぢ
伪装坚强ぢ 2020-12-28 15:03

here is some food for thought.

When I write monadic code, the monad imposes ordering on the operations done. For example, If I write in the IO monad:



        
3条回答
  •  春和景丽
    2020-12-28 15:35

    This problem of over-sequentializing monad code is known as the "commutative monads problem".

    Commutative monads are monads for which the order of actions makes no difference (they commute), that is when following code:

    do a <- f x
       b <- g y
       m a b
    

    is the same as:

    do b <- g y
       a <- f x
       m a b
    

    there are many monads that commute (e.g. Maybe, Random). If the monad is commutative, then the operations captured within it can be computed in parallel, for example. They are very useful!

    However, we don't have a good syntax for monads that commute, though a lot of people have asked for such a thing -- it is still an open research problem.

    As an aside, applicative functors do give us such freedom to reorder computations, however, you have to give up on the notion of bind (as suggestions for e.g. liftM2 show).

提交回复
热议问题