amcphail / hmatrix-gsl-stats

GSL Statistics functions for Haskell hmatrix
BSD 3-Clause "New" or "Revised" License
6 stars 3 forks source link

Allow the user to handle errors on the Haskell side #1

Closed joachifm closed 12 years ago

joachifm commented 12 years ago

Currently, the default GSL error handler is invoked whenever an error occurs (wrong input, etc), causing the program to crash. Instead, the API should allow the user to handle errors on the Haskell side.

A straightforward approach is to wrap calls to C in a function that explicitly disables the error handler (gsl_set_error_handler_off) and checks the return code, wrapping the result in Either.

amcphail commented 12 years ago

hmatrix has a facility for errors to be handled by Haskell (Control.Exception) so that ghci does not abort:

Numeric.GSL.setErrorHandlerOff

-- | This action removes the GSL default error handler (which aborts the program), so that -- GSL errors can be handled by Haskell (using Control.Exception) and ghci doesn't abort. foreign import ccall unsafe "GSL/gsl-aux.h no_abort_on_error" setErrorHandlerOff :: IO ()

I prefer catching exceptions to changing the return type of all functions to Either.

joachifm commented 12 years ago

Ok, that works for me. I still think it's surprising that ostensibly pure code can crash the program, but I see your point about Either. Thanks.