0

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.

false
  • 10,264
  • 13
  • 101
  • 209
Cristof
  • 26
  • 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 Dec 22 '19 at 23:32

1 Answers1

0

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.
Paulo Moura
  • 18,373
  • 3
  • 23
  • 33