%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Hanoi problem with N blocks 
%%% modified JUNE 7th 2009
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% The number of blocks N is n(N).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 2^N - 1 moves are needed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

n(4).

blk(X) :-  n(N),
   interval(X,1,N).  

blkorfloor(X) :- blk(X).
blkorfloor(0). %%% floor = 0

piolo(a). piolo(b). piolo(c).
   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

fluent(inpiolo(X,P)) :- blk(X),piolo(P).

fluent(top(P,Y)) :- piolo(P), blkorfloor(Y).

fluent(on(X,Y)):- blk(X), blk(Y), X < Y.
fluent(on(X,0)):- blk(X).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

action(move(X,A,B)):- blk(X), piolo(A), piolo(B), neq(A,B).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

executable(move(X,A,B), [top(A,X),top(B,Z)]) :-  
      action(move(X,A,B)),
      blk(X),blk(Z), X < Z.

executable(move(X,A,B), [top(A,X),top(B,0)]) :-  
       action(move(X,A,B)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

causes(move(X,A,B),inpiolo(X,B),[]):- 
       action(move(X,A,B)).
causes(move(X,A,B),on(X,Z),[top(B,Z)]):- 
       blkorfloor(Z),
       action(move(X,A,B)).
causes(move(X,A,B), top(B,X),[]):- 
       action(move(X,A,B)).
causes(move(X,A,B), top(A,Y),[on(X,Y)]):- 
       fluent(on(X,Y)),
       action(move(X,A,B)).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

caused([on(X,Y)],neg(on(X,Z))) :- 
      fluent(on(X,Y)),
      fluent(on(X,Z)), 
      diff(X,Y,Z).
caused([on(X,Y)],neg(on(Z,Y))) :- 
      fluent(on(X,Y)),
      fluent(on(Z,Y)), 
      blk(Y), %%% Per 0 potrebbe andar bene
      diff(X,Y,Z).
      
caused([inpiolo(X,P)],neg(inpiolo(X,Q))) :-
   blk(X),piolo(P),piolo(Q),neq(P,Q).

caused([top(X,A)],neg(top(X,B))) :- 
      fluent(top(X,A)),
      fluent(top(X,B)),
      neq(A,B).
caused([top(X,A)],neg(top(Y,A))) :- 
      fluent(top(X,A)),
      fluent(top(Y,A)),
      neq(X,Y),
      blk(A). %%% A=0 potrebbe andar bene

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

initially(inpiolo(A,a)):- blk(A).
initially(top(a,1)).
initially(top(b,0)).
initially(top(c,0)).

%%%% Superfluo: lo capisce da solo
%initially(on(N,0)):-  n(N).
%initially(on(X,Y)):-  blk(X),blkorfloor(Y),n(N), X < N, Y is X + 1.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

goal(inpiolo(A,b)):- blk(A).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




