Wizard

COMP 524: Programming Language Concepts

Spring, 2008
Jeff Terrell
jsterrel AT cs.unc.edu
(919) 962-1791 (office: Sitterson 138)

COMP 524 Exercise 4 (Prolog)

General Instructions

First, review the assignment submission policy in the syllabus. Note that there is no collaboration allowed.

This assignment is due at 11:59pm on Thursday, April 24. Submit assignments to me via email. All of your code should be included in a file called 'exercise4.pl', and 'exercise4.pl' should include everything that you turn in. When I am finished grading your assignment, I will email you your grade and any comments that I have (assuming I have your permission for this). There are a total of 100 points and 10 bonus points.

Remember: start early! I guarantee my availability during office hours, but not at 11pm the night it is due.

My solution is available.


Background: Sudoku

You will be writing a sudoku solver in Prolog. First, let's discuss the game of Sudoku. Note that an actual sudoku game involves a 9x9 board, but here we will only use a 4x4 board.

The board is has 4 rows and 4 columns, for a total of 16 cells. If you divide the board in half, horizontally and vertically, then you have defined the 4 blocks as well: an upper-left block of 4 cells, an upper-right block of 4 cells, an lower-left block, and a lower-right block. (See Figure 1.) At the beginning, each cell is either empty, or it contains a 1, 2, 3, or 4.
blocks in sudoku

The object is to put a number in each of the non-blank cells such that the following rules hold:

Said another way, a completed board will obey the following conditions:

Here is one example. Starting with this board:
given board 1
the solution is:
solution of board 1

Notice how the rows, columns, and blocks each have 1, 2, 3, and 4 exactly once.

Here is another example. Starting with this board:
given board 2
the solution is:
solution of board 2


Sudoku solver

Your task is to write a sudoku solver in Prolog. Given an initial board, you must print the final solution to standard output. Start with these facts:

a1(1). a1(2). a1(3). a1(4). a2(1). a2(2). a2(3). a2(4).
a3(1). a3(2). a3(3). a3(4). a4(1). a4(2). a4(3). a4(4).
b1(1). b1(2). b1(3). b1(4). b2(1). b2(2). b2(3). b2(4).
b3(1). b3(2). b3(3). b3(4). b4(1). b4(2). b4(3). b4(4).
c1(1). c1(2). c1(3). c1(4). c2(1). c2(2). c2(3). c2(4).
c3(1). c3(2). c3(3). c3(4). c4(1). c4(2). c4(3). c4(4).
d1(1). d1(2). d1(3). d1(4). d2(1). d2(2). d2(3). d2(4).
d3(1). d3(2). d3(3). d3(4). d4(1). d4(2). d4(3). d4(4).

These rules state that a1 (row A, column 1) can either be a 1, or a 2, or a 3, or a 4--and so on for every cell. Note that row A is the top row, and column 1 is the left-most column.

Use the following code to print the solution:

ps(1) :- write(' 1 ').
ps(2) :- write(' 2 ').
ps(3) :- write(' 3 ').
ps(4) :- write(' 4 ').

print(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P) :-
  ps(A), ps(B), write(' | '), ps(C), ps(D), nl,
  ps(E), ps(F), write(' | '), ps(G), ps(H), nl,
  write('---+---|---+---'), nl,
  ps(I), ps(J), write(' | '), ps(K), ps(L), nl,
  ps(M), ps(N), write(' | '), ps(O), ps(P), nl.

Also, don't forget to declare your [abcd][1234] functions as dynamic, e.g. :- dynamic(a1/1).

Rules will be retracted and asserted to your knowledge base to give it the initial configuration. For example, to run the first board above, the following queries would be given at the interactive prompt:

retract(a3(X)), fail.
retract(a4(X)), fail.
retract(c1(X)), fail.
retract(c3(X)), fail.
assert(a3(1)).
assert(a4(3)).
assert(c1(4)).
assert(c3(2)).
main.

One last hint: you can test the sum of several numbers with the is predicate. For example, 10 is 1+2+4+3 yields Yes, whereas 10 == 1+2+4+3 yields No. Note that the order matters: 10 must come first.

Your task is to add to the above by writing a main rule that generates a solution to the given board. This is worth 100 points.

Bonus (+10 points): order the right-hand-side of main so that the solution is always generated nearly instantly. (A naive approach takes around 10 seconds to solve and 10 more seconds to search for any other solutions. A more clever approach takes negligible time.)

exercise4.php: Last Modified: 04/30/08@15:12:48 | Size: 5556 bytes | View Source Valid XHTML 1.1 Valid CSS