0

I'm following a Prolog book and currently trying the following example. My facts are;

parent(tom, bob).
parent(pam, bob).
parent(tom, liz).
parent(bob, ann).
parent(bob, pat).
parent(pat, jim).

I queried Prolog as:

?- parent(X, Y).

Results were;

X = tom,
Y = bob ;
X = pam,
Y = bob ;
X = tom,
Y = liz ;
X = bob,
Y = ann ;
X = bob,
Y = pat ;
X = pat,
Y = jim

Why Prolog doesn't show 'false' at the end to show that there aren't anymore possible results?

Moreover, I tried following query. But it gave me different result which was unexpected, gives extra 'false'. Can you tell me what is the reason for that?

?- parent(Y,jim), parent(X,Y).
Y = pat,
X = bob.

?- parent(X,Y), parent(Y,jim).
X = bob,
Y = pat ;
false.

I'm using SWI-Prolog version (threaded, 64 bits, version 8.2.4).

false
  • 10,264
  • 13
  • 101
  • 209
Hareen Laks
  • 1,432
  • 2
  • 17
  • 33
  • 1
    It depends on how backtracking is performed (order of clauses, etc). In the first example, there is one Y and no other possibility. In the second, we backtrack and finally reach fallse – tphilipp May 14 '21 at 18:30

1 Answers1

1

SLD resolution, the Prolog's proof procedure, implicitly defines a search tree of alternative computations.

Prolog searches this tree using a depth-first strategy, one branch at a time, backtracking when it encounters a failure node.

SWI-Prolog prints false only when the last node visited during the search is a failure node.

Considering the program:

parent(tom,bob). % parent.1
parent(pam,bob). % parent.2
parent(tom,liz). % parent.3
parent(bob,ann). % parent.4
parent(bob,pat). % parent.5
parent(pat,jim). % parent.6

The search trees are:

  • First query:

enter image description here

  • Second query:

enter image description here

slago
  • 5,025
  • 2
  • 10
  • 23