[Haskell-cafe] Effects of Monads in Parallelisation

David Barbour dmbarbour at gmail.com
Tue Jul 26 17:30:23 CEST 2011


2011/7/26 Burak Ekici <ekcburak at hotmail.com>

> what has been changed after the encapsulation
> of 'par' function by Eval monad?
>
> If you asked to compare the parallelisation via monads
> with non-monadic manner of it, what could you say?
>

'Eval' provides some useful discipline and structure. It allows you to more
clearly start sparks or sequence certain evaluations before you progress
past the 'using' or 'withStrategy' directive, even if you don't need the
output right away.

 rpar x = x `par` return x
 rseq x = x `pseq` return x

 data Eval a = Done a
 runEval :: Eval a -> a
 runEval (Done x) = x

 instance Monad Eval where
   return x = Done x
   Done x >>= k = k x

You will eventually 'runEval' to extract the evaluated result. This is
strict on the monad itself - i.e. you pattern-match against 'Done' in both
'runEval' and in the binding operator (>>=). Therefore, you know that ALL
your 'rseq' and 'rpar' statements have finished executing before you get
past 'runEval'.

Without use of a monad, this is a bit more difficult - i.e. a use of 'par'
might be buried deep in some lazy evaluation, unless you're very careful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110726/1a44799b/attachment.htm>


More information about the Haskell-Cafe mailing list