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

Isaac Dupree isaacdupree at charter.net
Sat May 5 08:03:57 EDT 2007


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

The obvious "evaluate x = x `seq` return x" fails one of the following
laws for evaluate:

"
evaluate x `seq` y    ==>  y
evaluate x `catch` f  ==>  (return $! x) `catch` f
evaluate x >>= f      ==>  (return $! x) >>= f

Note: the first equation implies that (evaluate x) is not the same as
(return $! x).
"

However, strictness does not obey the monad laws. A correct definition
for IO would be "evaluate x = (x `seq` return x) >>= return", or
equally, "(return $! x) >>= return", or even more horrible-looking,
"fmap id (return $! x)"!  This works because (at least in present
Haskell implementations) IO's (>>=) is not strict in its first argument.

Noticing that this function has been implemented (slightly wrongly) for
NHC in base, so I mention this odd suggestion.


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

iD8DBQFGPHKtHgcxvIWYTTURAmT2AKC+pipeHff1jVOCqxjOOz2p+6/JqQCeNqAN
bjkaLwbn+BRVCya2e49diF4=
=J9iV
-----END PGP SIGNATURE-----


More information about the Libraries mailing list