Relax ordering constraints in monadic computation

前端 未结 3 435
伪装坚强ぢ
伪装坚强ぢ 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:54

    The semantics of C don't actually specify what order these two function calls will be evaluated, so the compiler is free to move things around as it pleases.

    But what if doSomething() causes a side-effect that will change the behavior of doSomethingElse()? Do you really want the compiler to mess with the order? (Hint: no) The fact that you are in a monad at all suggests that such may be the case. Your note that "you lose sharing on the results" also suggests this.

    However, note that monadic does not always mean sequenced. It's not exactly what you describe, but you may be interested in the Par monad, which allows you to run your actions in parallel.

    You are interested in leaving the order undefined so that the compiler can magically optimize it for you. Perhaps instead you should use something like the Par monad to indicate the dependencies (some things inevitably have to happen before others) and then let the rest run in parallel.

    Side note: don't confuse Haskell's return to be anything like C's return

提交回复
热议问题