ridgeworks / clpBNR

CLP(BNR) module for SWI-Prolog
MIT License
38 stars 5 forks source link

bug with graphical debugger #21

Closed kwon-young closed 9 months ago

kwon-young commented 9 months ago

I am using swi-prolog 9.1.18 built from source with clpBNR 0.11.2 and have found a bug in the swi graphical debugger when attributed variables from clpBNR are shown. The bug is triggered when trying to change the program current frame by clicking on select parent frame or select child frame or double clicking on a variable. When trying to change the frame, the debugger will simply fail and when trying to portray a variable, the new window will be empty.

Here is a minimal reproducer:

Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.18)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.

For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).

?- use_module(library(clpBNR)).
% *** clpBNR v0.11.2 ***.
%   Arithmetic global flags set to prefer rationals and IEEE continuation values.
true.

?- gtrace, X::real(0, 1), {Y == X * 2}.
% The graphical front-end will be used for subsequent tracing
ERROR: [Thread pce] Unhandled exception: nb_getval/2: variable `'clpBNR:bindings'' does not exist
X::real(0, 1),
Y::real(0, 2).

After the graphical debugger has launched, trace the program to go down 1 frame while having a clpBNR attributed variable shown and then try to go back up a frame. You should see the exception appear.

ridgeworks commented 9 months ago

Thanks for reporting (I don't use the graphical debugger much).

The problem arises because the graphical debugger runs in a separate (pce) thread which is started before clpBNR is initialized. As a result, the required global variable to support the attribute_goals hook isn't available to support displaying the constrained variable in the graphical debugger window. (This global was introduced in clpBNR 0.11.2 to fix some deficiencies in the top level display with nested constraint values.)

The fix is fairly simple (1 line) but there's no obvious workaround, e.g., "manually" defining the global in the pce thread. Since I just released 0.11.2, I'd prefer not to go through the overhead of a new release just for this bugfix. So short term options for you to consider:

kwon-young commented 9 months ago

I can provide patch instructions for 0.11.2 so you can fix your copy. Since you do use the graphical debugger, this would would help me with additional testing of that component with clpBNR.

If you can provide me the patch instructions, I can follow them and test them for you.

ridgeworks commented 9 months ago

Sure. If you look at the code structure here on GitHub, you'll find the file prolog/clpBNR/ia_utilities.pl. The predicate to fix is attribute_hooks at line 79 which currently reads:

attribute_goals(X) -->                     % constructs goals to build X
    {current_prolog_flag(clpBNR_verbose,Verbose),  % details depend on flag clpBNR_verbose
     g_read('clpBNR:bindings',Bindings),
     domain_goals_(Verbose,Bindings,X,Goals)
    },
    Goals.

The call to g-read which reads the global var clpBNR:bindings should be replaced with:

     catch(g_read('clpBNR:bindings',Bindings),error(existence_error(variable, _), _Ctxt),Bindings=any),

You'll have to find where on your installation the clpBNR pack has been installed; FYI, use ?- file_search_path(library,Y). if you can't find it.

The clpBNR version number is at file prolog/clpBNR.pl, line 105 if you want to modify that to minimize any future confusion with the version you're using.

Thanks for your assistance.

kwon-young commented 9 months ago

I have applied your patch and everything seems to work now :) As always, thank you very much for your quick help!

ridgeworks commented 9 months ago

Good news; thanks again for reporting this.

ridgeworks commented 7 months ago

Fixed in 0.11.3.