[Haskell-cafe] how to catch keyboard interrupts?

Uwe Hollerbach uhollerbach at gmail.com
Sat Feb 23 19:37:07 EST 2008


On 2/23/08, Bulat Ziganshin <bulat.ziganshin at gmail.com> wrote:

[about my question about keyboard interrupts]

>  you should store thread id of thread running interpreter and send
>  async exception to it. control.concurrent is probably contains all
>  required functions

Most splendid! Here's what I did

> data MyInterrupt = MyInt Int
> instance Typeable MyInterrupt where
>   typeOf x = typeOf (0 :: Int)
>
> catcher :: MyInterrupt -> IO ()
> catcher e = hPutStrLn stderr "interrupt!"

then later, in the REPL

> catchDyn (evalAndPrint env True line)  (\e -> catcher e)

and in the initialization

> mysighandler tid = Catch (throwDynTo tid (MyInt 0))

and

> myTID <- myThreadId
> installHandler sigINT (mysighandler myTID) Nothing
> installHandler sigQUIT (mysighandler myTID) Nothing
> doREPL env

I had to add the "MyInterrupt" stuff because GHC was complaining about
ambiguous types, initially I had just "(\e -> hPutStrLn stderr (show
e))" as the second arg of the catchDyn.

And try it in the self-test...

        Now test number/string conversions... this'll take a bit longer

        interrupt!
        lisp>

It works! Cool! This is worth bumping the version number :-)

Many thanks!

Uwe


More information about the Haskell-Cafe mailing list