1

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

false
  • 10,264
  • 13
  • 101
  • 209
Zimano
  • 1,870
  • 2
  • 23
  • 41

2 Answers2

3

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.

false
  • 10,264
  • 13
  • 101
  • 209
coder
  • 12,832
  • 5
  • 39
  • 53
2

In addition to @coder's answer you could write using library(lambda):

?- setof(X, X+\otherFact(one,X,_,_), List).
false
  • 10,264
  • 13
  • 101
  • 209