[Haskell-cafe] Re: Takusen and strictness, and perils of getContents

oleg at pobox.com oleg at pobox.com
Tue Mar 6 22:38:41 EST 2007

Simon Marlow wrote:
> Anyway, I just wanted to point out that nowadays we have the option of
> using imprecise exceptions to report errors in lazy I/O.

Is this really a solution? Currently, getContents reports no errors
but does perfect error recovery: the result of the computation prior to
the error is preserved and reported to the caller. Imprecise
exceptions give us error reporting -- but no error recovery. All
previously computed results are lost. Here's a typical scenario:
	do l <- getContents
	   return (map process l)
If an error occurs reading lazy input, we'd like to log the error and
assume the input is terminated with EOF. If getContents raises an
imprecise exception, what do we do? 
	return (map process (catch l (\e -> syslog e >> return [])))
That of course won't work: l is a value rather than an effectful
computation; besides; catch can't occur in the pure code to start
with. What we'd like are _resumable_ exceptions. The exception handler
receives not only the exception indicator but also the continuation
where the exception has occurred. Invoking this continuation means
error recovery. Resumable exceptions are used extensively in CL; they
are also available in OCaml. So, hypothetically we could write
	do l <- getContents
	   resume_catch (return (map process l))
		(\e k -> syslog e >> k [])

Besides the obvious typing problem, this won't work for the reason
that exceptions raised in the pure code are _imprecise_ -- that is, no
precise continuation is available, even in principle.

More information about the Haskell-Cafe mailing list