Findall does not preserve the names of the variables

I’m using: SWI-Prolog version 8.0.3

I have a predicate p(B,A), that returns an atom A containing variables
shared with B.
I need all the solutions of p(B,A) where the name of the variables shared by A and B are meaningful.
So I wrote :

q(B) :-
    findall(A, p(B,A), L).

where the predicate q(B) is the context from I call p(B,A) through the findall.

Using gtrace I have seen that the variable of each A, are renamed (copied ?) in L. But I strongly need to have the same variables.
Moreover, I cannot use varnumbers(A) or varnumbers(B) because B
is used in another context before and after the call of q(B), and I need
to keep the names of the variables of B unchanged.

How can I have all the solutions such that the names of the variables shared by A and B are not rename (copied) so that B never change ?

Thanks a lot

Dominique

I forgot to say
Hello !

(sorry)

I’ve also been bitten by this before (wrote about it here); part of how findall works is that it existentially qualifies all variables that appear in the body dynamically, not lexically, so if there are any variables in A, they also get qualified (i.e. made fresh for each time though).

In this case, bagof/3 instead of findall will probably do what you want.

Thanks a lot for your clear answer !

1 Like