prologswi-prologconstraint-handling-rules

CHR solution output in Prolog


I'm running a textbook CHR program in SWI-Prolog.

:- use_module(library(chr)).
:- chr_constraint fib/2.

f0 @ fib(0,M) ==> M=1.
f1 @ fib(1,M) ==> M=1.
fn @ fib(N,M) ==> N>=2 | N1 is N-1, fib(N1,M1), N2 is N-2, fib(N2,M2), M is M1+M2.

All goes fine, but I don't get why the output is so long

?- fib(3,A).
A = 3,
fib(1, 1),
fib(0, 1),
fib(1, 1),
fib(2, 2),
fib(3, 3).

Who not only A = 3? Can I disable the rest? It's a real inconvenience for bigger values...


Solution

  • Had to hunt in the source code for a solution to this, but you can turn it off by setting a flag to false:

    ?- set_prolog_flag(chr_toplevel_show_store, false).
    true.
    ?- fib(3, A).
    A = 3.
    

    Setting the flag in .swiplrc doesn't work, it needs to be done after the module is imported, so you could include it in your source code instead:

    :- use_module(library(chr)).
    :- set_prolog_flag(chr_toplevel_show_store, false).
    
    :- chr_constraint fib/2.
    
    f0 @ fib(0,M) ==> M=1.
    f1 @ fib(1,M) ==> M=1.
    fn @ fib(N,M) ==> N>=2 | N1 is N-1, fib(N1,M1), N2 is N-2, fib(N2,M2), M is M1+M2.