%/***********************************************************************
% * A solution to SRQ (Self-referential quizzes) in the boolean domain
% * implemented in SICStus 3.5.
% * Antonio J. Fernandez  
% *                April 1997.
% * *********************************************************************/

top:-q.

q:-     statistics(runtime,_), 
        srq(L), statistics(runtime,[_,Y]),
        write(L), nl,
	write('time : '), write(Y), nl.

%/* Use next predicate q to see the time in calculate all the solutions*/
%
%/* q:-     statistics(runtime,_),
%        (srq(L), 
%	 write(L), nl,
%         fail
%          ; 
%	write('no more solutions '),nl),
%        statistics(runtime,[_,Y]),
%        fd_statistics,
%        write('time : '),write(Y), nl.
%*/

srq(L):- L=[A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,
            B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,
            C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,
            D1,D2,D3,D4,D5,D6,D7,D8,D9,D10,
            E1,E2,E3,E4,E5,E6,E7,E8,E9,E10],

        domain(L,0,1),

%/*      exactly(1,[A1,B1,C1,D1,E1],1).*/

        1#=A1+B1+C1+D1+E1,
        1#=A2+B2+C2+D2+E2,
        1#=A3+B3+C3+D3+E3,
        1#=A4+B4+C4+D4+E4,
        1#=A5+B5+C5+D5+E5,
        1#=A6+B6+C6+D6+E6,
        1#=A7+B7+C7+D7+E7,
        1#=A8+B8+C8+D8+E8,
        1#=A9+B9+C9+D9+E9,
        1#=A10+B10+C10+D10+E10,

%%      Redundant constraints 
%%       10#=SmAE+SmBCD,

%%  It allows the reification over variables.

%%  Question 1  
        A1 #<=> A4 #/\ (#\A1) #/\ (#\A2) #/\ (#\A3),
        B1 #<=> A3 #/\ (#\A1) #/\ (#\A2),
        C1 #<=> A2 #/\ (#\A1),
        D1=A1,
        E1 #<=>(#\A1) #/\ (#\A2) #/\ (#\A3) #/\ (#\A4),

%%  Question 2  
        A2 #<=> (A3#=A4) #/\ (B3#=B4) #/\ (C3#=C4) #/\ (D3#=D4) #/\ (E3#=E4),
        B2 #<=> (A4#=A5) #/\ (B4#=B5) #/\ (C4#=C5) #/\ (D4#=D5) #/\ (E4#=E5),
        C2 #<=> (A5#=A6) #/\ (B5#=B6) #/\ (C5#=C6) #/\ (D5#=D6) #/\ (E5#=E6),
        D2 #<=> (A6#=A7) #/\ (B6#=B7) #/\ (C6#=C7) #/\ (D6#=D7) #/\ (E6#=E7),
        E2 #<=> (A7#=A8) #/\ (B7#=B8) #/\ (C7#=C8) #/\ (D7#=D8) #/\ (E7#=E8),
%% Question 3 

        A3 = A4,
        B3 #<=> A5 #/\ (#\A4),
        C3 #<=> A6 #/\ (#\A4) #/\ (#\A5),
        D3 #<=> A7 #/\ (#\A4) #/\ (#\A5) #/\ (#\A6),
        E3 #<=> A8 #/\ (#\A4) #/\ (#\A5) #/\ (#\A6) #/\ (#\A7),

%% Question 4 
        A4=B2,
        B4 #<=> B4 #/\ (#\B2),
        C4 #<=> B6 #/\ (#\B2) #/\ (#\B4),
        D4 #<=> B8 #/\ (#\B2) #/\ (#\B4) #/\ (#\B6),
        E4 #<=> B10 #/\ (#\B2) #/\ (#\B4) #/\ (#\B6) #/\ (#\B8),

%% Question 5 
        A5=C1, B5=C3, C5=C5, D5=C7, E5=C9,

%% Question 6  
        Bef in 0..1,
        Aft in 0..1,
        SmD in 0..10,

        Bef #<=> D1 #\/ D2 #\/ D3 #\/ D4 #\/ D5,
        Aft #<=> D7 #\/ D8 #\/ D9 #\/ D10,

%% exactly(1,[D1,D2,D3,D4,D5,D6,D7,D8,D9,D10],SMD), 

        SmD #= D1+D2+D3+D4+D5+D6+D7+D8+D9+D10,

        A6 #<=> Bef #/\ (#\Aft),
        B6 #<=> (#\Bef) #/\ Aft,
        C6 #<=> Bef #/\ Aft,
        D6 #<=> (SmD#=0),
        E6=D6,

%%  Question 7 

        A7 #<=> E5 #/\ (#\E6) #/\ (#\E7) #/\ (#\E8) #/\ (#\E9) #/\ (#\E10),

        B7 #<=> E6 #/\ (#\E7) #/\ (#\E8) #/\ (#\E9) #/\ (#\E10),
        C7 #<=> E7 #/\ (#\E8) #/\ (#\E9) #/\ (#\E10),
        D7 #<=> E8 #/\ (#\E9) #/\ (#\E10),
        E7 #<=> E9 #/\ (#\E10),

%%  Question 8  
        
        domain([SmB,SmC,SmD,SmBCD],0,10),

        SmB #= B1+B2+B3+B4+B5+B6+B7+B8+B9+B10,
        SmC #= C1+C2+C3+C4+C5+C6+C7+C8+C9+C10,
        SmBCD#=SmB+SmC+SmD,

        A8 #<=> (SmBCD#=7),
        B8 #<=> (SmBCD#=6),
        C8 #<=> (SmBCD#=5),
        D8 #<=> (SmBCD#=4),
        E8 #<=> (SmBCD#=3),

%%  Question 9 

        SmA in 0..10, SmE in 0..10, SmAE in 0..10,

        SmA#=A1+A2+A3+A4+A5+A6+A7+A8+A9+A10,
        SmE#=E1+E2+E3+E4+E5+E6+E7+E8+E9+E10,

        SmAE#=SmA+SmE,

        A9 #<=> (SmAE#=0),
        B9 #<=> (SmAE#=1),
        C9 #<=> (SmAE#=2),
        D9 #<=> (SmAE#=3),
        E9 #<=> (SmAE#=4),       

%% NORMAL LABELING.
        labeling([leftmost],L).

%% FIRST FAIL
%        /*labeling([ff],L).*/

%        /*labeling([ffc],L).*/

%        /* labeling([leftmost],L).*/
        

%/*****************************************************************************
%a constraint that uses reification, consider
%exactly(X,L,N) which is true if X occurs exactly N times in the list L. 
%***************************************************************************/

%%exactly(_, [], 0).

%%exactly(X, [Y|L], N) :- X #= Y #<=> B, N #= M+B, exactly(X, L, M).
