3 ms·
> the order of statements in a "do" expression does determine the order of execution Not really, unless you take a very specific definition for "order of execu
by tome 7y ago
> the order of statements in a "do" expression does determine the order of execution
Not really, unless you take a very specific definition for "order of execution". There are plenty of monads where order of statements is unrelated to the order of execution, Identity, for example!
- bad_user 7y agoI don't agree. Bind creates a data dependence that forces an evaluation order. do x <- f y <- g x No matter what Monad you're speaking of, in this expression "x" is going to be evaluated before "y", what we sometimes call a happens before relationship, because evaluating "y" depends on evaluating "x".
- ImprobableTruth 7y agoUnless you're using some non-standard definition of evaluation, this is not true when using a lazy strategy. Consider the term ((const c) x) for g x which reduces to c without ever evaluating x.
- tome 7y agoWhat are you basing your disagreement on? Can you explain to me in which sense "x" is evaluated before "y" in this example? {-# LANGUAGE BangPatterns #-} import Data.Functor.Identity import Debug.Trace f = let !_ = trace "Evaluating f" () in return 1 g x = let !_ = trace "Evaluating g" () in return (x + 1) example :: Int example = runIdentity $ do x <- f y <- g x return y > example Evaluating g Evaluating f 2