/****************************************************************
LLP Example
Example programs
by Naoyuki Tamura (tamura@kobe-u.ac.jp)
Dept. CS, Kobe University
****************************************************************/
main :-
examples(Goal),
statistics(runtime, _),
once(Goal),
statistics(runtime, [_,T]),
write(Goal), nl,
write('CPU time = '), write(T), write(' msec'), nl, nl,
fail.
main.
examples(reverse([a,b,c,d,e], R)).
examples(sum([1,2,3,4], Sum)).
examples(knight5(Tour)).
examples(kirkman(Groups)).
examples(crypt(Solution)).
examples(queen_solve(8)).
examples(queen_solve(10)).
examples(queen_solve(12)).
examples(queen(12, Queens)).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
reverse(Xs, Zs) :- result(Zs) -<> rev(Xs, []).
rev([], Zs) :- result(Zs).
rev([X|Xs], Zs) :- rev(Xs, [X|Zs]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sum(List, Sum) :- result(Sum) -<> s(List, 0).
s([], S) :- result(S).
s([X|Xs], S0) :- S is X+S0, s(Xs, S).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
knight5(Tour) :-
(k(1,1), k(1,2), k(1,3), k(1,4), k(1,5),
k(2,1), k(2,2), k(2,3), k(2,4), k(2,5),
k(3,1), k(3,2), k(3,3), k(3,4), k(3,5),
k(4,1), k(4,2), k(4,3), k(4,4), k(4,5),
k(5,1), k(5,2), k(5,3), k(5,4), k(5,5)) -<>
tour(1, 1, Tour).
tour(I, J, [(I,J)|Tour]) :-
k(I, J),
next(I, J, I1, J1),
tour(I1, J1, Tour).
tour(I, J, [(I,J)]) :-
k(I, J).
next(I, J, I1, J1) :- I1 is I-2, J1 is J-1.
next(I, J, I1, J1) :- I1 is I-2, J1 is J+1.
next(I, J, I1, J1) :- I1 is I-1, J1 is J-2.
next(I, J, I1, J1) :- I1 is I-1, J1 is J+2.
next(I, J, I1, J1) :- I1 is I+1, J1 is J-2.
next(I, J, I1, J1) :- I1 is I+1, J1 is J+2.
next(I, J, I1, J1) :- I1 is I+2, J1 is J-1.
next(I, J, I1, J1) :- I1 is I+2, J1 is J+1.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
kirkman(Groups) :-
(arrange(35, Groups) -<> cont) -<>
gen_res(15).
gen_res(0) :-
cont.
gen_res(N) :-
N > 0,
(g(N),g(N),g(N),g(N),g(N),g(N),g(N)) -<>
gen_res(1, N).
gen_res(N, N) :-
N1 is N-1,
gen_res(N1).
gen_res(I, N) :-
I < N,
I1 is I+1,
meet(I, N) -<> gen_res(I1, N).
arrange(0, []).
arrange(I, [[G1,G2,G3]|Groups]) :-
I > 0,
g(G1), g(G2), g(G3),
meet(G1, G2), meet(G1, G3), meet(G2, G3),
I1 is I-1,
arrange(I1, Groups).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
crypt([S,E,N,D]+[M,O,R,E]=[M,O,N,E,Y]) :-
(d(0), d(1), d(2), d(3), d(4),
d(5), d(6), d(7), d(8), d(9))
-<>
(add( 0, D, E, Y, C1),
add(C1, N, R, E, C2),
add(C2, E, O, N, C3),
add(C3, S, M, O, C4),
add(C4, 0, 0, M, 0),
S \== 0, M \== 0,
top).
add(C0, X, Y, Z, C1) :-
digit(X), digit(Y), digit(Z),
Sum is C0+X+Y,
Z is Sum mod 10,
C1 is Sum//10.
digit(X) :- var(X), d(X).
digit(X) :- nonvar(X).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
queen_solve(N) :- queen(N, _), fail.
queen_solve(_).
queen(N, Q) :-
(n(N), result(Q)) -<> place(N).
place(1) :-
(c(1),u(2),d(0)) -<> (n(N), solve(N, [])).
place(I) :-
I > 1, I1 is I-1,
U1 is 2*I, U2 is 2*I-1, D1 is I-1, D2 is 1-I,
(c(I),u(U1),u(U2),d(D1),d(D2)) -<> place(I1).
solve(0, Q) :-
result(Q), top.
solve(I, Q) :-
I > 0, c(J), U is I+J, u(U), D is I-J, d(D),
I1 is I-1, solve(I1, [J|Q]).