Search code examples
prologprolog-toplevel

Predicate Prints Out "Unexpected" false


I am trying to write a predicate, likes/2, in a manner where it runs like the following:

?- likes(A,alan).
A = lindsay ;
A = chloe ;
A = cheyanne ;
A = britney ;

Here is how I am tackling the problem:

% Define your facts:
combo(lindsay,alan).
combo(chloe,alan).
combo(cheyanne,alan).
combo(britney,alan).

% Define your predicate:
likes(A,B) :- combo(A,B); combo(B,A).

Now, the issue that I am facing is that while my program functions as it is supposed to, for the most part, it prints out a false at the end and I don't understand why. Here is the full output:

?- likes(A,alan).
A = lindsay ;
A = chloe ;
A = cheyanne ;
A = britney ;
false.

Solution

  • Short answer. The Prolog top-level interpreter is not always able to detect that there are no more proofs for a query. So, in your case, after giving the solution A = britney it asks you if you want yet another solution.

    In the particular case of the likes(A,alan) query, your single clause for the predicate means that the inference engine tries to prove combo(A,alan); combo(alan,A). The left goal in this disjunction gives you the four solutions that you get. But the right solution may also provide one or more solutions but the engine is only able to sort that out by trying the goal, which fails as none of the clauses for combo/2 have the atom alan as first argument. This failure to prove the right goal gives you the false printing.