3

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?

false
  • 10,264
  • 13
  • 101
  • 209
lledr
  • 537
  • 1
  • 3
  • 12
  • 2
    `; false` means: Prolog was not sure whether or not further answers exist. So this is a tiny hint about the efficiency: If there is a `; false`, then Prolog needs some space upon success. – false Oct 26 '15 at 19:18
  • 2
    It is [standard behavior](http://www.dfki.uni-kl.de/~sintek/pa/section1_2_0_4.html) but there are [implementation specific details](http://stackoverflow.com/questions/29605132/first-argument-indexing) – vmg Oct 26 '15 at 19:41
  • 1
    @vmg: Not sure what you or the referred document means by "standard". To say the least, there is no standard for any resource related issues in Prolog, and indexing varies in some implementations even from run-to-run. – false Oct 29 '15 at 14:56
  • Then you're right and the word 'standard' was misapplied. It seems it's a 'common implementation', but not really a standard. I can't seem to be able to edit the comment with your correction, though – vmg Oct 29 '15 at 15:46
  • `true ; false` and `true` are equivalent logically, and only differ operationally. – Will Ness Dec 06 '19 at 18:12

1 Answers1

0

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.