Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog backtracking policy

SWI-Prolog, version 6.6.6.

Consider the following facts:

p(a, a).
p(a, b).

It results in the following answer:

?- p(a, a).
true ;
false.

But if I slightly change the data:

p(a, a).
p(b, a).

I get a slightly different answer...

?- p(a, a).
true.

It seems that backtracking in the second case does not occur because the first parameter of the predicate in the question does not unify with the other clauses.
Nevertheless, one would expect to have the answer true ; false. for each case: the engine would try the first predicate clause (resulting in true), then backtrack and look for other clauses for the same predicate (resulting in false). Is it a kind of a shortcut in the second case?

Is this a (somewhat) standard behavior - i.e. should be considered when writing prolog rules -, or is it purely implementation-specific?

like image 865
lledr Avatar asked Jun 09 '26 23:06

lledr


1 Answers

Newer versions of SWI-Prolog feature just-in-time multi-argument indexing. This means that the runtime decide just-in-time about adding multi-argument indexes and you don't need to declare them manually.

In the present case, a multi-argument index on the first and second argument will eliminate a choice point. Since only one clause matches arg1=a and arg2=a index wise. See also:

SWI-Prolog provides `just-in-time' indexing over multiple arguments
https://www.swi-prolog.org/pldoc/man?section=jitindex

SWI-Prolog is not the only Prolog system that can do that. For example Jekejeke Prolog can also do MA-JIT. But SWI-Prolog can do something more, namely deep multi-argument indexing.

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

q(f(a,a)).
q(f(a,b)).

?- q(f(a,a)).
true.

DMA-JIT is currently not available in Jekejeke Prolog.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!