% I assume that the locations on the board are numbered % (1,1), (1,2), ...., (9,9) % the topleft corner is (1,1) and the rightbottom corner % is (9,9) % row is horizontal % column is vertical % cell(i,j,k) means that the number k is placed at the % location (i,j) % to run the program use the command % lparse sudoku.lp instance | smodels % where instance is the name of the file describing the % problem (the website has three examples, download them % and save them into exp1, exp2, exp3 for example; then % lparse sudoku.lp exp1 | smodels solves the first instance) % define that 1,..,9 are number % (i.e., number(1),...,number(9) are facts of the program % number(1..9). % this is to define that I,... are variables of the type % number #domain number(I;J;K1;K2;N). % defining the small squares % e.g. the locations (I,J) for in1(I) and in1(I) are true % represents the first square in1(N) :- N >= 1, N <= 3. in2(N) :- N >= 4, N <= 6. in3(N) :- N >= 7, N <= 9. % for each number k (k=1,...,9) % there are exactly 9 atoms of the from % cell(I,J,k) where I is a number (between 1 and 9) % J is a number (between 1 and 9) % in the stable model (if one exists!) 9 {cell(I1,J1,K) : number(I1): number(J1)} 9 :- number(K). % for each K, the above rule is equvalent to the following rule: % 9 {cell(1,1,1), cell(1,2,1),...,cell(9,9,1)} 9 :- number(1). % between the brackets are all the 81 locations of the board. % one cell cannot have two numbers % neq(K1,K2) means that K1 is different than K2 :- cell(I, J, K1), cell(I, J, K2), neq(K1, K2). % for each small square, each number (K) can % appear at least one and at most one time 1 {cell(I1,J1,K) : in1(I1) : in1(J1)} 1 :- number(K). % for example, for K=1, the above rule is equivalent to % the rule % 1 {cell(1,1,1),cell(1,2,1),cell(1,3,1), % cell(2,1,1),cell(2,2,1),cell(2,3,1), % cell(3,1,1),cell(3,2,1),cell(3,3,1)} 1 :- number(1). 1 {cell(I1,J1,K) : in2(I1) : in2(J1)} 1 :- number(K). 1 {cell(I1,J1,K) : in3(I1) : in3(J1)} 1 :- number(K). 1 {cell(I1,J1,K) : in1(I1) : in2(J1)} 1 :- number(K). 1 {cell(I1,J1,K) : in1(I1) : in3(J1)} 1 :- number(K). 1 {cell(I1,J1,K) : in2(I1) : in3(J1)} 1 :- number(K). 1 {cell(I1,J1,K) : in2(I1) : in1(J1)} 1 :- number(K). 1 {cell(I1,J1,K) : in3(I1) : in2(J1)} 1 :- number(K). 1 {cell(I1,J1,K) : in3(I1) : in1(J1)} 1 :- number(K). % each number k, appears one and only one on each row 1 {cell(1,J1,K) : number(J1)} 1 :- number(K). 1 {cell(2,J1,K) : number(J1)} 1 :- number(K). 1 {cell(3,J1,K) : number(J1)} 1 :- number(K). 1 {cell(4,J1,K) : number(J1)} 1 :- number(K). 1 {cell(5,J1,K) : number(J1)} 1 :- number(K). 1 {cell(6,J1,K) : number(J1)} 1 :- number(K). 1 {cell(7,J1,K) : number(J1)} 1 :- number(K). 1 {cell(8,J1,K) : number(J1)} 1 :- number(K). 1 {cell(9,J1,K) : number(J1)} 1 :- number(K). % each number k, appears one and only one on each column 1 {cell(J1,1,K) : number(J1)} 1 :- number(K). 1 {cell(J1,2,K) : number(J1)} 1 :- number(K). 1 {cell(J1,3,K) : number(J1)} 1 :- number(K). 1 {cell(J1,4,K) : number(J1)} 1 :- number(K). 1 {cell(J1,5,K) : number(J1)} 1 :- number(K). 1 {cell(J1,6,K) : number(J1)} 1 :- number(K). 1 {cell(J1,7,K) : number(J1)} 1 :- number(K). 1 {cell(J1,8,K) : number(J1)} 1 :- number(K). 1 {cell(J1,9,K) : number(J1)} 1 :- number(K).