setof(+Template, +Goal, -Set)binds
Setto the list of all instances of
Templatesatisfying the goal
For example, given the facts and rule:
happy(fido). happy(harry). happy(X) :- rich(X). rich(harry).
it follows that
?- setof(Y, happy(Y), Set). Y = _G180 Set = [fido, harry] ; false.
harryonly appears once in the binding for
Setis in sorted order.
?- bagof(Y, happy(Y), Bag). Y = _G180 Bag = [fido, harry, harry] ; false.and
?- findall(Y, happy(Y), Bag). Y = _G180 Bag = [fido, harry, harry] ; false.
The differences between
on the one hand,
setof on the other hand, include
the fact that with
setof, you get
sorted order and no repetitions. (For differences
setof will fail (and so not bind
if the there are no instances of
Template for which
Goal succeeds - i.e. effectively it fails if
Set would be empty.
bagof also fails in these
findall does not (it binds the
variable that is its third argument to the empty list, or, if the
third argument is instantiated, it succeeds if the third argument
is the empty list).