After I have seen these question, I have tried to solve below problem. But I could not solve it. Can anyone help me ?
predefined :
foo( X, Y ) bar( Y, Z )
^ ^ ^ ^
all of them is atomic, that is they only return or take one value at a time
sample example:
foo(john, brad).
bar(john, marry).
foo( Y, brad)
Y = john % after pressing a, all possibilities will be seen at terminal
bar(Y, Z )
Z = marry % after pressing a, all possibilities will be seen at terminal
%..................
For these case how can I run bar with all possibilities of Y, resulted after eash run of foo ?
If
bar/2is a predicate that results in certain side effects, such as input-output, being produced, then that is what backtracking is for.You just say:
And get all the possibilities, pressing
aor;depending on your implementation.Alternatively, you can write
On the other hand, if you need to check if there is a
bar(Y,Z)pair for everyYthat satisfiesfoo(X,Y), than you can use Prologforall/2facility. For instance, the following goal will be true only if for everyYthat results from foo(X,Y), there exists aZthat satisfiesbar(Y,Z).Examples
Given the following facts
With the first goal you get
With the second goal:
Since the goal
bar(b,Z).fails.