Prolog findall/3

1.6k views Asked by At

Say I have a predicate pred containing several facts.

pred(a, b, c).
pred(a, d, f).
pred(x, y, z).

Can I use findall/3 to get a list of all facts which can be pattern matched?

for example, if I have

pred(a, _, _) I would like to obtain

[pred(a, b, c), pred(a, d, f)]

1

There are 1 answers

0
Patrick J. S. On

Just summing up what @mbratch said in the comment section:

Yes, but you have to make sure that you either use named variables or construct a simple helper predicate that does that for you:

Named variables:

findall(pred(a,X,Y),pred(a,X,Y),List).

Helper predicate:

special_findall(X,List):-findall(X,X,List).

?-special_findall(pred(a,_,_),List).
List = [pred(a, b, c), pred(a, d, f)].

Note that this doesn't work:

findall(pred(a,_,_),pred(a,_,_),List).

Because it is equivalent to

 findall(pred(a,A,B),pred(a,C,D),List).

And thus doesn't unify the Variables of Template with those of Goal.