prologclpfdreification

SWI Prolog Clpfd Library - Reification


I have an upcoming Logic exam and have been studying some past papers from my course. I've come across a question regarding reification and have posted it below;

Illustrate reification by using it to express the property that a variable B can either take the value of 1 or 8.

After reading some resources and looking at the SWI Prolog manual, I still find the concept of reification quite confusing (primarily studying Java so the switch to learning Prolog has been difficult). It's quite confusing having to use boolean logic within the prolog query.

Without reification, I would have to write the following code (which I know is far too long to be the correct answer);

 B in 1..8, B #\= 2,B #\= 3,B #\= 4,B #\= 5,B #\= 6,B #\= 7. 

Would really appreciate if someone could show me the above query, but using reification.


Solution

  • From the documentation:

    The constraints in/2, #=/2, #\=/2, #/2, #==/2 can be reified, which means reflecting their truth values into Boolean values represented by the integers 0 and 1. Let P and Q denote reifiable constraints or Boolean variables, then:

    ...
    P #\/ Q True iff either P or Q
    ...
    

    For you, it seems P is B #= 1 and Q is B #= 8, so you end up with:

    ?- B #= 1 #\/ B #= 8.
    B in 1\/8.
    

    As you see, you are not really using the reified values. You are just using reification as a round-about way of declaring the domain of your variable. The answer to your query, B in 1 \/ 8, is what you would probably use directly if you wanted to say that "B is either 1 or 8". If you look carefully at the documentation of in/2, you should see that the domain can be either an integer, a range Lower .. Upper, or the union of Domain1 \/ Domain2. In your case both domains are a single integer, 1 and 8.

    PS: Once you go down that road, why not:

    ?- B in 1..8 #/\ #\ B in 2..7.
    B in 1\/8.
    

    B is in [1,8] AND B is not in [2,7].

    The possibilities are endless :)