Findall / 3 creates new, unrelated variables in its result list

?- permutation([A,B,C],Z).
Z = [A, B, C] ;
Z = [A, C, B] ;
Z = [B, A, C] ;
Z = [B, C, A] ;
Z = [C, A, B] ;
Z = [C, B, A] ;
false.

Has the meaning. I can work with permutation [A,B,C]and that permutation contains the same elements as in [A,B,C], so everything that I do with these elements will apply to my original list.

Now:

?- findall(X, permutation([A,B,C], X), Z).
Z = [[_G1577, _G1580, _G1583], [_G1565, _G1568, _G1571], [_G1553, _G1556, _G1559], [_G1541, _G1544, _G1547], [_G1529, _G1532, _G1535], [_G1517, _G1520, _G1523]].

Why?? Why findall/3gives me lists containing completely unrelated variables instead A,B,C? The lists in Zare not even related to each other, so in fact the result is I get only 6 random lists of length 3, which is not what I requested.

With this behavior, we get such funny results:

?- findall(X, permutation([A,B,C],X), Z), A = 1.
A = 1,
Z = [[_G1669, _G1672, _G1675], [_G1657, _G1660, _G1663], [_G1645, _G1648, _G1651], [_G1633, _G1636, _G1639], [_G1621, _G1624, _G1627], [_G1609, _G1612, _G1615]].

This makes no sense from a logical point of view.

, findall/3 , , , , .

:

  • ?

  • , , ?

  • findall/3 , ?

+6
3

?

findall/3 - , . , , .

DEC10 Prolog findall/3. 1978 , 1984 . 1984 setof/3, , . ISO-Prolog ( findall/3) , . .

, , ?

Findall , , setof/3 bagof/3 . . , , , , , findall.

. , , , clpfd- . :

?- findall(A, (A in 1..3 ; A in 5..7), As).

SWI , SICStus , .

findall/3 , ?

setof/3 bagof/3 (). , - ...

+8

.

?- setof(X,permutation([A,B,C],X),Z).
Z = [[A, B, C], [A, C, B], [B, A, C], [B, C, A], [C, A, B], [C, B, A]].

findall at sicstus,

findall (? Template,: Goal,? Bag) ISO      - , Prolog. . , . , findall/3 Goal . .

, , findall.

+2
?- findall(X, permutation([A,B,C],X), Z), A = 1.

Prolog [A, B, C], Prolog A, B, C, , , :

Z = [[_G1669, _G1672, _G1675], [_G1657, _G1660, _G1663], [_G1645, _G1648, _G1651], [_G1633, _G1636, _G1639], [_G1621, _G1624, _G1627], [_G1609, _G1612, _G1615]].

, A, B C, :

?- A=1, B=2, C=3, findall(X, permutation([A,B,C],X), Z).
A = 1,
B = 2,
C = 3,
Z = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

This did not happen earlier in your request findall(X, permutation([A,B,C],X), Z), A = 1., because Prolog will first try to resolve the condition findall(X, permutation([A,B,C],X), Z), and thenA = 1

+1
source

All Articles