The Proper Definition of (evaluate :: a -> IO a)

Isaac Dupree isaacdupree at charter.net
Thu May 10 16:51:00 EDT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stefan O'Rear wrote:
> On Tue, May 08, 2007 at 05:47:27PM -0700, Ashley Yakeley wrote:
>> If Isaac's definition is accurate, we might as well generalise it to any 
>> Monad.
> 
> Isaac's definition is equivalent to the standard evaluate as I proved.
> However, my proof could be using misfeatures of IO.

Monads like Maybe and [] have no definition of evaluate consistent with
all the laws, they only have (return) and (return $!) as distinct
possibilities. In which case my definition makes evaluate in those
monads be equivalent to (return $!).  Which might not be that bad.  For
monads such as Lazy ST, it is clear that (>>=) cannot be strict in its
first argument in order for the laziness to work, so evaluate can be
defined with all the laws.  In other function-based monads - including
IO - it is not so documented or necessary.

What is evaluate used for? Based on Google codesearch and my own
experiences:

In a forkIO for the sole purpose of parallel evaluation.  Could be
summarized by:
  forkEvaluate :: a -> IO a
  forkEvaluate a = forkIO (evaluate a >> return ())
Aren't there supposed to be more functional ways like `par` to achieve
this? Anyway, looks like that definition only works in the IO monad.

To immediately catch exceptions from it (Control.Exception.catch or
try).  This also only works in the IO monad (or derived monads that can
define catch... there is always (liftIO . evaluate) I guess).

To simply make things be evaluated sooner.  This is sometimes necessary,
to counteract the effects of file-reading using unsafeInterleaveIO, for
example.  I haven't seen this use in non-IO monads (which might violate
separation of concerns) but it could be.

In practice, even in IO code, (return $!) ends up being used. After all
it is not hidden away in a Control.Exception module (that is officially
non-portable perhaps).

No opinion :)


Isaac
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGQ4W0HgcxvIWYTTURAiXIAKDWeixSdezmSz2bGCfxZ1Ox3MYMHQCghVKh
xe9UYKbp/IVkx3t6thYj/xM=
=ZmRd
-----END PGP SIGNATURE-----


More information about the Libraries mailing list