Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Encoding and solving logic puzzles


1. Encoding and solving logic puzzles

2. Prolog and Datalog
Prolog Clocksin, W. F., & Mellish, C. S. (1984). Programming in Prolog, 2nd ed. Berlin: Springer-Verlag.. , for programming in logic, is a logic language that can be used to concisely specify the logic of a solution. Datalog is a subset of Prolog.

Technically, the main differences are that Datalog does not support terms (i.e., arbitrary list structures) and user-defined operator precedence for dynamic parsing, etc.

Datalog is so named because of the close correspondence to relational databases, especially in the declarative nature of queries that can be posed to the database. In fact, a Datalog program is often referred to as a database.

3. Combinations
Here is the Prolog code.

Here is the output of the Prolog code.


4. Prolog
Note:

5. Permutations
Here is the Prolog code.

Here is the output of the Prolog code.


6. Inequality
Note that the variables B1 and B2 must be instantiated to values before the inequality check
   B1 \== B2

can be used to insure that they do not have the same value.

7. Logic puzzle
Adapted from Martin Gardner (1978, p. 92-93). For puzzle purposes, it is assumed that boys dance only with girls (and girls with boys).

8. Observation
As Gardner states,

9. Matrix
Gardner's approach, which is the only reasonable one when doing the problem manually, is to create a matrix (or matrices) that represent all possible cases for the problem and to eliminate cases that do not meet the requirements specified by the rules.

10. Datalog
The approach here is to encode the rules specified by the problem into Datalog logic rules and let Datalog determine the answers. The development of a logic specification and solution to this problem is also, in microcosm, a good example of the entire programming process.

11. Different values
The dif predicate will be used for two values (atomic, number, etc.) that succeeds if the values are different.

Otherwise, the predicate fails and backtracking to the next possible solution is initiated.
dif(X, Y):- X \== Y.


12. Rule
One way to solve the puzzle using Datalog is as follows. The condition is handled by numbering the boys 1, 2, and 3, and the girls 1, 2, and 3. The goal is to find out the color of the attire worn by each.

13. Attire
Since we are interested in the colors of the attire worn by the boys and girls, the following logical variables are introduced.

14. Rule
The condition for the colors for can be expressed as the following facts.
girl("Red"). girl("Green"). girl("Blue").


15. Permutations of girls
A unique permutation can be insured with the following conditions using the girl predicate.
   % all girls have a different color    girl(G1),    girl(G2), dif(G2, G1),    girl(G3), dif(G3, G1), dif(G3, G2),


16. Rule
The condition for the colors for can be expressed as the following facts.
boy("Red"). boy("Green"). boy("Blue").


17. Permutations of boys
A unique permutation can be insured with the following conditions using the boy predicate.
   % all boys have a different color    boy(B1),    boy(B2), B2 \== B1,    boy(B3), B3 \== B2, B3 \== B1


18. Rule
The condition can be expressed as follows, picking the second pair.
   B2 is "Red",    G2 is "Green",


19. Rule
The condition can be expressed as follows.
   % no boy danced with the girl of the same color    dif(B1, G1),    dif(B2, G2),    dif(B3, G3),


20. Question
The question can be expressed as a Datalog query that uses all of the previous conditions and outputs any solutions found that meet the conditions. This might be done as follows.

21. Program
Here is the final program. Note that within reason there as a great deal of flexibility as to the order of the predicates since we are dealing with finite domains. Machine efficiency can be impacted, however, by the order of the constraints. Here is the Prolog code.

Here is the output of the Prolog code.


22. Discussion
The primary global perspective is that, for example, since boy(B2) is often used just to insure that logical variable B2 does, indeed, have a value, once that is (globally) established, that check is no longer needed.

Another aspect of machine efficiency is that, sometimes, moving conditions earlier (or later) can effectively prune more of the search space, thus resulting in better performance. In the extreme cases, such ordering can make the difference between finding or not finding answers (due to computational time and/or space limits). However, the increased machine efficiency must be traded off against the decreased human efficiency in both creating and understanding the program.

23. End of page