0
votes

i'm new to Prolog, and try to understand why this very simple program is return 2 solutions : true AND false. For me, this should return only true, why return false too ?

predicate1(_,[]).
predicate1(X,[_|T]) :- predicate1(X,T).

?- predicate1(abc,[]).

Thanks for your help.

1
This gets asked a lot. Firstly realize that the predicate is not returning true or false. It is succeeding or failing. When it succeeds and finds a solution, it shows true. If it goes back to a choice point to find an other solution and doesn't find one, it then fails (to find another solution)\ and says false. - lurker

1 Answers

0
votes

Your goal unifies with the first clause (a fact) for the predicate:

?- predicate1(abc,[]) = predicate1(_,[]).
true.

Thus the query returns true as its first result. But a choice-point is created as there's a second clause (a rule) for the predicate. As the unification of the goal and the head of the rule fails, you get false when asking for a second solution:

?- predicate1(abc,[]) = predicate1(X,[_|T]).
false.