trealla-prolog / trealla

A compact, efficient Prolog interpreter written in plain-old C.
MIT License
274 stars 13 forks source link

GSL: Error handling #561

Closed flexoron closed 4 months ago

flexoron commented 4 months ago
$ tpl matrix.pl 
?- main.
gsl: lu.c:324: ERROR: matrix is singular
Default GSL error handler invoked.
Aborted (core dumped)

A first simple step to catch this:

$ vi tpl.c
: 
#include <gsl/gsl_errno.h>
void tpl_gsl_error_handler(const char *reason, const char *file, int line, int err)
{
  if (1)
    printf ("(caught [%s:%d: %s (%d)])\n", file, line, reason, err);
}
: 
int main(int ac, char *av[])
{
: 
/* somewhere in main: */
gsl_set_error_handler(&tpl_gsl__error_handler);
: 
}

$ vi Makefile
LDFLAGS = -L/usr/local/lib -lm -lgsl -lgslcblas
$ tpl matrix.pl 
?- repeat, main, fail.
1.0
4.0
(caught [lu.c:324: matrix is singular (1)]) % Input #04,2x2,0.0  0,0 0,0
0.0
16.0
256.0
(caught [lu.c:324: matrix is singular (1)]) % Again #04,2x2,0.0  0,0 0,0
0.0
65536.0
infradig commented 4 months ago

Well i'm not planning to make GSL a compilation dependency. But gsl_set_error_handler could be called via FFI and used to set a user-defined handler or throw an exception. Something along those lines.

flexoron commented 4 months ago

Yes,ok. As a first step I tried this and it works, too. gsl_set_error_handleroff()

infradig commented 4 months ago

Yes,ok. As a first step I tried this and it works, too. gsl_set_error_handleroff()

I added the FFI defn for gsl_set_error_handler_off.

flexoron commented 4 months ago

It seems GSL is a very likeable tool.