existential qualifier in prolog, using setof / bagof

家住魔仙堡 提交于 2019-11-29 11:07:13

Ok, I'm not sure I can explain it perfectly, but let me try.

It has to do with the fact that you are querying over a 2-ary relation, mother/2. In that case using X-S as the template has a similar effect on the result set C as using S^ in front of the goal. In X-S you are using both variables in the template, and therefore each possible binding of X and S is included in C. You get the same effect using S^ in front of the goal, as this is saying "ignore bindings of S when constructing the result".

But the difference between the two becomes clearer when you query over a 3-ary relation. The SWI manual has this example:

foo(a, b, c).
foo(a, b, d).
foo(b, c, e).
foo(b, c, f).
foo(c, c, g).

Now do similar queries as in your example

setof(X-Z, foo(X,Y,Z), C).

and

setof(Z, X^foo(X,Y,Z), C).

and you get different results.

It's not just checking unification, X-Z effectively changes your result set.

Hope that helps.

Edit: Maybe it clarifies things when I include the results of the two queries above. The first one goes like this:

?- setof(X-Z, foo(X,Y,Z), C).   
Y = b
C = [a-c, a-d] ;
Y = c
C = [b-e, b-f, c-g] ;
No

The second one yields:

?- setof(Z, X^foo(X,Y,Z), C).
Y = b
C = [c, d] ;
Y = c
C = [e, f, g] ;
No
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!