[Haskell-cafe] Newbie: Is it possible to catch _|_ ?

Iavor S. Diatchki diatchki at cse.ogi.edu
Mon Apr 5 13:34:42 EDT 2004

this is an attempt to give an answer to someone who is new to Haskell.

the answer to your question is: no, there is no way to "catch" _|_,
as that would mean that we can solve the halting problem.
a piece of advice, especially while you are new to haskell ---
don't worry too much about _|_. occasonally you will run into
an infinite loop, but that probably means you have a bug in your program,
so fix it, and don't try to "catch" it.

as an unrelated comment, Haskell does have some exception handling features,
but they often get pretty advanced.  mostly these are unrelated to _|_.

what the previous answers were saying, is that some implementations 
(notably GHC)
can sometimes detect that a program will loop forever, and instead of 
code that will simply loop, they generate code that produces a helpful 
so that you can fix your program.

the way this particular feture happens to be implemented in GHC is that
it generates code that raises an exception.  it is hard to see why one 
might want
to handle this exception, especially since there are no guarantees 
whatsoever that
it will ever occur.  furthermore this is highly GHC specific behaviour.

hope this is helpful

Jon Cast wrote:

>Russ Lewis <spamhole-2001-07-16 at deming-os.org> wrote:
>>Another newbie question: Is it possible to catch _|_ - that is, to 
>>encounter it, handle it and continue?  I'm assuming that you all are 
>>going to say no, since I can't figure out any way to do this and retain 
>>the functional nature of Haskell.
>This isn't possible in a deterministic language, for several reasons.
>The IO monad, however, is non-deterministic, and its `catch' function
>can be used to catch any _|_ value that can be caught, specifically
>those arising from calls to throw, error, and certain infinite loops.
>It is non-deterministic, though, so it won't catch all _|_s, nor will it
>give any guarantee as to which _|_ it will catch (for example, in error
>3 + throw (DynException (toDynamic False)) it's indeterminate whether
>error's return value or throw's return value will ultimately be caught.
>Jon Cast
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org

More information about the Haskell-Cafe mailing list