Unexpected behavior of findnsols (&findall)

Hello! I’m new to prolog and found this unexpected behavior when using ‘findnsols’ (which calls '‘findall’). Not all solutions returned satisfy the goal, which really confuses me. Do I misunderstand something?

This is the program:

goal(V_1) :-
    forall(member(O, [block1,block2]), predneg_on_obj(O,V_1)), 

And this is the query:

findnsols(99999, V, goal(V), L)

The result is:

L=[block1, block2]

It’s obvious that block2 is not part of the solution, and we can verify it by calling the command below, which returns False.


So we can see while ‘block2’ appeared in the solution, it does not satisfy the goal.
Any help is greatly appreciated!

?- goal(V), V = block2.
V = block2.

To figure out why, check the implementation/definition of forall/2. Success!

1 Like

I see. Thank you so much for your quick reply!

After reading the definition of forall many times, I more or less understand what’s going on. If I understand correctly, Cond in forall will not bind variables, that’s why you give V = block2. To acheive what I expect, probably I can try foreach instead.

I modified this post as I figured the previous question out.

Thanks again for your help!