I came across this post on Math Exchange, asking for the solution to the following logic puzzle:
Which answer in this list is the correct answer to this question?
- All of the below.
- None of the below.
- All of the above.
- One of the above.
- None of the above.
- None of the above.
I know that the answer is 5, but because I am trying to learn Prolog I'd like to find a way for Prolog to give me the answer. I tried to use the following knowledge base to encode the information given in the puzzle:
answer(1) :- answer(2),answer(3),answer(4),answer(5),answer(6).
answer(2) :- \+answer(3), \+answer(4), \+answer(5), \+answer(6).
answer(3) :- answer(1), answer(2).
answer(4) :- answer(1); answer(2); answer(3).
answer(5) :- \+answer(1), \+answer(2), \+answer(3), \+answer(4).
answer(6) :- \+answer(1), \+answer(2), \+answer(3), \+answer(4), \+answer(5).
However, when trying to evaluate
?- answer(X).
I run into a infinite recursion (which is to be expected when naively substituting the predicates).
- Is there a way to solve this specific puzzle with Prolog? 1
- More generally, how can I know ahead of time when a logic puzzle is well suited for Prolog?
1: Yes, I know Prolog is turing complete, so encoding the problem into a turing machine and iterating all the possible solutions (like is done in the first answer to the Math exchange post) is of course a possibility but I'm looking for the "canonical" way of solving this.