[Haskell-cafe] Re: Cleaning up threads

Ertugrul Soeylemez es at ertes.de
Tue Sep 14 23:15:21 EDT 2010

Mitar <mmitar at gmail.com> wrote:

> On Wed, Sep 15, 2010 at 2:16 AM, Ertugrul Soeylemez <es at ertes.de> wrote:
> > The point is that killThread throws an exception.  An exception is
> > usually an error condition.
> This is reasoning based on nomenclature. If exceptions were named
> "Signal" or "Interrupt"?
> > My approach strictly separates an unexpected crash from an intended
> >quit.
> For this you can have multiple types of exceptions, some which signify
> error condition and some which signify that user has "interrupted" a
> process and that process should gracefully (exceptionally) quit.
> I like exceptions because you can split main logic from "exceptional"
> logic (like user wants to prematurely stop the program). But you still
> want to clean up properly everything. Once you have this "exceptional"
> logic in place (and you should always have it as some exceptional
> things can always happen) why do not use it also for less exceptional
> things (because you have cleaner code then).

The problem with exceptions is that Haskell's type system doesn't really
capture them.  A function raising an exception is semantically
equivalent to a function, which recurses forever.  On the other hand a
well-typed abortion using the ContT monad transformer /is/ captured by
the type system and hence can be stated and type-checked explicitly.

Exceptions are side effects in Haskell.  That's why an exception is
semantically equivalent to a crash, hence my wording.

> > Also using the Quit command from my example you can actually wait
> > for the thread to finish cleanup work.  You can't do this with an
> > exception.
> You can. If you would have a proper way to mask them:
> http://hackage.haskell.org/trac/ghc/ticket/1036

Even if you could mask them exception throwing and catching is outside
of Haskell's type system.  They're still an IO side effect.

And also there is nothing wrong with using ContT.  It doesn't make the
code any more complicated and very likely even less.  See my example.


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Haskell-Cafe mailing list