The programs returns only X = adam
because you inserted the cut !
. The cut is used when you have found the right rules and you don't need to do further evaluations, but it cuts all the other solutions.
In your case
num_parent(adam, X) :- !, X = 0.
num_parent(eve, X) :- !, X = 0.
num_parent(_, 2). %replaced num_parent(X, 2) with num_parent(_, 2) to avoid singleton variable
num_parent(X, 0)
returns only X = adam
.
If you write
num_parent(adam, X) :- X = 0.
num_parent(eve, X) :- !, X = 0.
num_parent(_, 2).
the solution will be X = adam
and X = eve
, and in this case:
num_parent(adam, X) :- X = 0.
num_parent(eve, X) :- X = 0.
num_parent(_, 2).
the solution will be X = adam
, X = eve
and false
because the query num_parent(X, 0)
doesn't unify with num_parent(_, 2)
.
You can better see this behavior using the tracer.