
%  Consult the Golog interpreter
:- [golog_swi].

%  Direttive per SWI-prolog 
:- discontiguous clear/2, on/3, onTable/2.

%  Action Precondition Axioms.
poss(putOn(X,Y),S) :-  clear(X,S), clear(Y,S),\+ on(X,Y,S),  \+ X=Y.
poss(putOnTable(X),S) :- clear(X,S), \+ onTable(X,S).

%  Successor State Axioms.
on(X,Y,do(A,S)) :- A = putOn(X,Y) ;
                   on(X,Y,S), \+ (A = putOnTable(X); A = putOn(X,_)).
onTable(X,do(A,S)) :- A = putOnTable(X) ;
                      onTable(X,S), \+ A= putOn(X,_).
clear(X,do(A,S)) :- on(Y,X,S), (A = putOn(Y,_) ; A = putOnTable(Y)) ;
                    clear(X,S), \+ A = putOn(_,X).

%  Primitive Action Declarations.
primitive_action(putOn(_,_)).
primitive_action(putOnTable(_)).

%  Restore suppressed situation arguments
restoreSitArg(onTable(X),S,onTable(X,S)).
restoreSitArg(on(X,Y),S,on(X,Y,S)).
restoreSitArg(clear(X),S,clear(X,S)).

%  Predicato statico block
block(X) :- member(X,[a,b,c,d,e]).

%  Initial situation
onTable(a,s0).   on(b,a,s0).   on(c,b,s0).   clear(c,s0).
onTable(d,s0).   on(e,d,s0).   clear(e,s0). 

% esempi di procedure

proc(smontaTutti,
     while(some(x, block(x) & -onTable(x)),
	   pi(x,putOnTable(x)))).

proc(smonta(X),
     ?(onTable(X))#
    pi(y,?(on(X,y)):
      putOnTable(X): smonta(y))).

    