2

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.
The Hound
  • 81
  • 8

1 Answers1

1

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.

Paulo Moura
  • 18,373
  • 3
  • 23
  • 33
  • I'm confused as to what to do to stop the false from appearing. I can't get rid of the right side of the equation because the names can be given in any order. What should I do? Or is this something that is supposed to happen? – The Hound Jan 28 '15 at 01:27
  • 1
    @TheHound why are you worried about the `false` response? It's perfectly normal, and expected from Prolog. It's saying it failed to find more solutions after the ones already displayed. – lurker Jan 28 '15 at 01:31
  • @lurker Ah, I didn't know that. I'm new to Prolog so I'm still getting used to everything. – The Hound Jan 28 '15 at 01:37
  • 1
    @TheHound It's a common misunderstanding for those just getting started with Prolog. There are times when Prolog knows there aren't any more solutions so won't backtracking and won't show `false`. Paulo's answer gives details for your particular case why there's a *choice point* which Prolog needs to go back to. Not all predicates will leave such a choice point. But if they do, it's not necessarily a problem. – lurker Jan 28 '15 at 01:42