In Prolog, given a knowledge base of facts:
someFact(one).
someFact(two).
otherFact(one, two, 123, 456).
otherFact(one, four, 789, 123).
The query setof(X, someFact(X), List).
produces this result:
List = [one, two]
However, the query setof(X, otherFact(one, X,_,_), List
produces this:
List = [two]
While I expected it to produce [two,four]
. Using this source I discovered that typing ;
when the first list is returned will show all the other options:
List = [two] ;
List = [four] .
Why does it do this? Is it because of the underscores? How do I produce a set where both two
and four
is given, without pressing ;
?
I didn't know how to find an answer to this as I have trouble phrasing this problem into a question that produces search results
You could write:
setof(X, B^A^otherFact(one, X,A,B), List)
.
Querying:
?- setof(X, B^A^otherFact(one, X,A,B), List).
List = [four,two].
That's because even if you place _
in setof(X, B^A^otherFact(one, X,_,_)
, setof/3 is designed to search all free variables. By placing B^A^
in front you choose not to bind either A or B and search only other variables.