Search code examples
prologswi-prologclpfd

How to determine the outcome of queries for constraint logic programming


I am doing some revision for constraint logic programming and wanted to know how I can read the following queries in order to predicate their results correctly.

Basically there is a question which asks whether or not the answer provided by the following query is correct or not.

So this is the question

Consider the following queries and answers. Some answers coincide
with what SWI-Prolog would infer whereas others are erroneous.
Indicate which answers are genuine and which ones are fake (no
explanation is required).

(i) ?- [X, Y, Z] ins 0 .. 4, X #= Y + 1.
X in 1..4, Y in 0..3, Z in 0..4.

(ii) ?- [X, Y, Z] ins 0 .. 4, X #= Y + Z.
X in 0..4, Y in 0..2, Z in 0..2.

(iii) ?- [X, Y, Z] ins 0 .. 4, X #= Z - Y.
X in 0..4, Y in 0..4, Z in 0..4.

(iv) ?- [X, Y, Z] ins 0 .. 4, X #= Y * Y, Z #= -Y.
Y = 0, Z = 0.

My question is what is the best way to read the query in order to determine whether the answer is correct or not.


Solution

  • Several steps:

    1. Is the shown answer a syntactically valid Prolog goal? If not, then the answer is definitely fake, because the actual toplevel only emits syntactically valid residual goals.
    2. Moving on: Are there any solutions of the original query that are precluded by the shown residual goals? If yes (= incompleteness), then the answer is fake, because the actual toplevel only emits residual goals that are semantically equivalent to the original query.
    3. Conversely, do the residual goals admit a solution that the initial query does not? If yes (= overly general), then the answer is fake. Exercise: Why?

    In your case, the shown answer is a syntactically valid conjunction, so it is definitely a candidate for a correct solution. However, the answer admits solutions (exercise: which?) that the original query does not, and so the answer is not correct.