Following is my knowledge base:
parent(claudia, taiane).
parent(wl, cris).
parent(cris, jim).
parent(cris, pedro).
man(jim).
man(pedro).
man(w).
man(wl).
woman(taiane).
woman(claudia).
woman(cris).
When my main has just one print:
main:-
man(M),
print(M), nl,
fail.
main:- halt.
when I execute it with swipl -q -s exercise-family-tree.pl -g main I get nonduplicated results (all good).
On the other hand, when I query more in my main and have print two times with two different variables as its arguments in my main:
main:-
%which women have both a father and a son in the database?
woman(X), parent(X, S), parent(F, X), man(F), man(S), print(X), nl,
man(M),
print(M), nl,
fail.
main:- halt.
than the results get duplicated. Why does my code duplicate all the answers in the second case?
The issue is different from the one I raised earlier. Please let any interactive solutions that involve REPL be outside of the scope of this question.
I don't think your problem is anything to do with the top level or way you're running it from the command line, it's that you don't really understand Prolog search, choice points and backtracking and have written a nested loop which prints the same results twice.
The first code acts like (pseudocode):
The second code acts like a nested loop:
In more detail, the first code with
man(M), print(M), nl, failruns like this:The second case, this code:
runs like this:
So you make two different choices for
Sfrom[jim,pedro]byX=chris, parent(X, S)but do not report them, and only report the other choices, forXandM, which are the same in both cases, so it looks like duplication.