The Proper Definition of (evaluate :: a -> IO a)
isaacdupree at charter.net
Sat May 5 08:03:57 EDT 2007
-----BEGIN PGP SIGNED MESSAGE-----
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.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----
More information about the Libraries