0
votes

I not shure how in my exam answer to this qestion:

Let's consider this prolog program:

p(a,b).
p(b,c).
p(c,a).
q(X,X).
q(X,Y):-p(X,Z),q(Z,Y).

and question:

?- q(X,X).

And valid? answer is:

Above aim is fulfilled in infinitely many ways, but none of them is substitution X = b.

And I don't get it, really. IMO this predicate can be fullfilled in 3 ways:

 q(a,a) or  q(b,b) or  q(c,c).

What I don't understand here? Or maybe the answer is wrong and I just waste time and my answer is good?

1
The query is the same as one of the rules, so I think "infinitely many way" is correct. But there is nothing that prevents the substitution X=b. - nhahtdh
But rules contain constants "a","b" and "c" and yeah rules are same as query so there should be only 3 answers IMO. Infinitely answers would be if p(A,B) and p(B,C) and p(C,A) then q(X,X) would have infinitely many answers... but thats just my logic. Maybe it's different. - Machiaweliczny
There is nothing preventing q(d,d) or q(e,e) to be evaluated to true. As long as the 2 arguments are the same, the query will satisfy the rule q(X,X). - nhahtdh
Right, so proper answer should be: "query have infinitely many results" - Machiaweliczny
the question is, what happens when you issue the query ?- q(X,X).. No q(b,b) will be ever produced. Were you to issue a query ?- q(b,b). or ?- q(42,42). the two would indeed succeed; but that is not what is asked. - Will Ness

1 Answers

1
votes

The q(X,X). rule matches any input where the arguments to q are equal. You can easily try this out for yourself:

?- [user].
|: q(X, X).
|: q(X, Y) :- p(X, Z), q(Z, Y).
|: % user://1 compiled 0.00 sec, 1,808 bytes
true.

?- q(b, b).
true.

?- q(X, X), X=b.
X = b.

Instead of b, you can fill in any Prolog term in the final query. (Note that I haven't even entered the definition of p here; it's irrelevant.)